Skip to content

Commit ec36c08

Browse files
committed
restructure and rename
1 parent ff611c9 commit ec36c08

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

mesa/experimental/cell_space/cell.py

+3-3
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

+39-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,52 @@
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

55
from mesa.experimental.cell_space.discrete_space import DiscreteSpace
66

77
if TYPE_CHECKING:
8-
from mesa.experimental.cell_space import Cell, Grid
8+
from mesa.experimental.cell_space import Cell
99

10+
T = TypeVar("T", bound="Cell")
1011

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

1523

1624
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"""
25+
"""Cell Agent is an Agent class that adds behavior for moving in discrete spaces
26+
27+
Attributes:
28+
space (DiscreteSpace): the discrete space the agent is in
29+
cell (Cell): the cell the agent is in
30+
"""
2031

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

3664

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

0 commit comments

Comments
 (0)