11from __future__ import annotations
22
3- from typing import TYPE_CHECKING , Protocol , runtime_checkable
3+ from typing import TYPE_CHECKING , Any , Protocol , TypeVar
44
55from mesa .experimental .cell_space .discrete_space import DiscreteSpace
66
77if 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
1624class 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