1
1
from __future__ import annotations
2
2
3
- from typing import TYPE_CHECKING , Protocol , runtime_checkable
3
+ from typing import TYPE_CHECKING , Any , Protocol , TypeVar
4
4
5
5
from mesa .experimental .cell_space .discrete_space import DiscreteSpace
6
6
7
7
if TYPE_CHECKING :
8
- from mesa .experimental .cell_space import Cell , Grid
8
+ from mesa .experimental .cell_space import Cell
9
9
10
+ T = TypeVar ("T" , bound = "Cell" )
10
11
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
+ ...
14
22
15
23
16
24
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
+ """
20
31
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 :
22
50
if self .cell is not None :
23
51
self .cell .remove_agent (self )
24
52
self .cell = cell
@@ -34,10 +62,8 @@ def move_relative(self, directions: tuple[int, ...], distance: int = 1):
34
62
self .move_to (new_cell )
35
63
36
64
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 ):
41
67
match direction :
42
68
case "N" | "North" | "Up" :
43
69
self .move_relative ((- 1 , 0 ), distance )
0 commit comments