99from mesa .experimental .cell_space .cell_collection import CellCollection
1010
1111if TYPE_CHECKING :
12+ from mesa .agent import Agent
1213 from mesa .experimental .cell_space .cell_agent import CellAgent
1314
15+ Coordinate = tuple [int , ...]
16+
1417
1518class Cell :
1619 """The cell represents a position in a discrete space.
@@ -26,7 +29,7 @@ class Cell:
2629
2730 __slots__ = [
2831 "coordinate" ,
29- "_connections " ,
32+ "connections " ,
3033 "agents" ,
3134 "capacity" ,
3235 "properties" ,
@@ -44,7 +47,7 @@ class Cell:
4447
4548 def __init__ (
4649 self ,
47- coordinate : tuple [ int , ...] ,
50+ coordinate : Coordinate ,
4851 capacity : float | None = None ,
4952 random : Random | None = None ,
5053 ) -> None :
@@ -58,20 +61,25 @@ def __init__(
5861 """
5962 super ().__init__ ()
6063 self .coordinate = coordinate
61- self ._connections : list [Cell ] = [] # TODO: change to CellCollection?
62- self .agents = [] # TODO:: change to AgentSet or weakrefs? (neither is very performant, )
64+ self .connections : dict [Coordinate , Cell ] = {}
65+ self .agents : list [
66+ Agent
67+ ] = [] # TODO:: change to AgentSet or weakrefs? (neither is very performant, )
6368 self .capacity = capacity
64- self .properties : dict [str , object ] = {}
69+ self .properties : dict [Coordinate , object ] = {}
6570 self .random = random
6671
67- def connect (self , other : Cell ) -> None :
72+ def connect (self , other : Cell , key : Coordinate | None = None ) -> None :
6873 """Connects this cell to another cell.
6974
7075 Args:
7176 other (Cell): other cell to connect to
77+ key (Tuple[int, ...]): key for the connection. Should resemble a relative coordinate
7278
7379 """
74- self ._connections .append (other )
80+ if key is None :
81+ key = other .coordinate
82+ self .connections [key ] = other
7583
7684 def disconnect (self , other : Cell ) -> None :
7785 """Disconnects this cell from another cell.
@@ -80,7 +88,9 @@ def disconnect(self, other: Cell) -> None:
8088 other (Cell): other cell to remove from connections
8189
8290 """
83- self ._connections .remove (other )
91+ keys_to_remove = [k for k , v in self .connections .items () if v == other ]
92+ for key in keys_to_remove :
93+ del self .connections [key ]
8494
8595 def add_agent (self , agent : CellAgent ) -> None :
8696 """Adds an agent to the cell.
@@ -123,30 +133,34 @@ def __repr__(self): # noqa
123133
124134 # FIXME: Revisit caching strategy on methods
125135 @cache # noqa: B019
126- def neighborhood (self , radius = 1 , include_center = False ):
136+ def neighborhood (self , radius : int = 1 , include_center : bool = False ):
127137 """Returns a list of all neighboring cells."""
128- return CellCollection (
138+ return CellCollection [ Cell ] (
129139 self ._neighborhood (radius = radius , include_center = include_center ),
130140 random = self .random ,
131141 )
132142
133143 # FIXME: Revisit caching strategy on methods
134144 @cache # noqa: B019
135- def _neighborhood (self , radius = 1 , include_center = False ):
145+ def _neighborhood (
146+ self , radius : int = 1 , include_center : bool = False
147+ ) -> dict [Cell , list [Agent ]]:
136148 # if radius == 0:
137149 # return {self: self.agents}
138150 if radius < 1 :
139151 raise ValueError ("radius must be larger than one" )
140152 if radius == 1 :
141- neighborhood = {neighbor : neighbor .agents for neighbor in self ._connections }
153+ neighborhood = {
154+ neighbor : neighbor .agents for neighbor in self .connections .values ()
155+ }
142156 if not include_center :
143157 return neighborhood
144158 else :
145159 neighborhood [self ] = self .agents
146160 return neighborhood
147161 else :
148- neighborhood = {}
149- for neighbor in self ._connections :
162+ neighborhood : dict [ Cell , list [ Agent ]] = {}
163+ for neighbor in self .connections . values () :
150164 neighborhood .update (
151165 neighbor ._neighborhood (radius - 1 , include_center = True )
152166 )
0 commit comments