11from __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
56from mesa .experimental .cell_space .discrete_space import DiscreteSpace
67
78if 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
1625class 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