Skip to content

Commit cdfaa6c

Browse files
committed
restructure and rename
1 parent 9d90a79 commit cdfaa6c

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

mesa/experimental/cell_space/cell.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from mesa.experimental.cell_space.cell_collection import CellCollection
88

99
if TYPE_CHECKING:
10-
from mesa.experimental.cell_space.cell_agent import CellHolder
10+
from mesa.experimental.cell_space.cell_agent import DiscreteSpaceAgent
1111

1212

1313
class Cell:
@@ -80,7 +80,7 @@ def disconnect(self, other: Cell) -> None:
8080
"""
8181
self._connections.remove(other)
8282

83-
def add_agent(self, agent: CellHolder) -> None:
83+
def add_agent(self, agent: DiscreteSpaceAgent[Cell]) -> None:
8484
"""Adds an agent to the cell.
8585
8686
Args:
@@ -96,7 +96,7 @@ def add_agent(self, agent: CellHolder) -> None:
9696

9797
self.agents.append(agent)
9898

99-
def remove_agent(self, agent: CellHolder) -> None:
99+
def remove_agent(self, agent: DiscreteSpaceAgent[Cell]) -> None:
100100
"""Removes an agent from the cell.
101101
102102
Args:

mesa/experimental/cell_space/cell_agent.py

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,53 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING, Protocol, runtime_checkable
3+
from typing import TYPE_CHECKING, Any, Protocol, TypeVar
44

5+
from mesa.agent import Agent
56
from mesa.experimental.cell_space.discrete_space import DiscreteSpace
67

78
if TYPE_CHECKING:
8-
from mesa.experimental.cell_space import Cell, Grid
9+
from mesa.experimental.cell_space import Cell
910

11+
T = TypeVar("T", bound="Cell")
1012

11-
@runtime_checkable
12-
class CellHolder(Protocol):
13-
cell: Cell | None
13+
14+
class DiscreteSpaceAgent(Protocol[T]):
15+
cell: T | None
16+
space: DiscreteSpace[T]
17+
18+
def move_to(self, cell: T) -> None:
19+
...
20+
21+
def move_relative(self, directions: tuple[int, ...], distance: int = 1):
22+
...
1423

1524

1625
class CellAgent:
17-
cell: Cell | None
18-
space: DiscreteSpace[Cell]
19-
"""Cell Agent is an extension of the Agent class and adds behavior for moving in discrete spaces"""
26+
"""Cell Agent is an Agent class that adds behavior for moving in discrete spaces
27+
28+
Attributes:
29+
space (DiscreteSpace): the discrete space the agent is in
30+
cell (Cell): the cell the agent is in
31+
"""
2032

21-
def move_to(self: CellHolder, cell: Cell) -> None:
33+
def __init__(
34+
self,
35+
space: DiscreteSpace[Cell],
36+
cell: Cell | None = None,
37+
*args: tuple[Any],
38+
**kwargs: dict[str, Any],
39+
):
40+
super().__init__(*args, **kwargs)
41+
self.space = space
42+
self.cell = cell
43+
if cell is not None:
44+
cell.add_agent(self)
45+
46+
@property
47+
def coordinate(self) -> tuple[int, ...]:
48+
return self.cell.coordinate if self.cell else ()
49+
50+
def move_to(self, cell: Cell) -> None:
2251
if self.cell is not None:
2352
self.cell.remove_agent(self)
2453
self.cell = cell
@@ -34,10 +63,8 @@ def move_relative(self, directions: tuple[int, ...], distance: int = 1):
3463
self.move_to(new_cell)
3564

3665

37-
class Grid2DMovingAgent(CellAgent):
38-
grid: Grid[Cell]
39-
40-
def move(self, direction: str, distance: int = 1):
66+
class Grid2DMovingAgent:
67+
def move(self: DiscreteSpaceAgent[Cell], direction: str, distance: int = 1):
4168
match direction:
4269
case "N" | "North" | "Up":
4370
self.move_relative((-1, 0), distance)

0 commit comments

Comments
 (0)