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
+ from mesa .agent import Agent
5
6
from mesa .experimental .cell_space .discrete_space import DiscreteSpace
6
7
7
8
if TYPE_CHECKING :
8
- from mesa .experimental .cell_space import Cell , Grid
9
+ from mesa .experimental .cell_space import Cell
9
10
11
+ T = TypeVar ("T" , bound = "Cell" )
10
12
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
+ ...
14
23
15
24
16
25
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
+ """
20
32
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 :
22
51
if self .cell is not None :
23
52
self .cell .remove_agent (self )
24
53
self .cell = cell
@@ -34,10 +63,8 @@ def move_relative(self, directions: tuple[int, ...], distance: int = 1):
34
63
self .move_to (new_cell )
35
64
36
65
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 ):
41
68
match direction :
42
69
case "N" | "North" | "Up" :
43
70
self .move_relative ((- 1 , 0 ), distance )
0 commit comments