11import math
22
3- from mesa .experimental .cell_space import CellAgent , FixedAgent
3+ from mesa .experimental .cell_space import CellAgent
44
55
66# Helper function
@@ -18,31 +18,6 @@ def get_distance(cell_1, cell_2):
1818 return math .sqrt (dx ** 2 + dy ** 2 )
1919
2020
21- class Resource (FixedAgent ):
22- """
23- Resource:
24- - contains an amount of sugar and spice
25- - grows 1 amount of sugar at each turn
26- - grows 1 amount of spice at each turn
27- """
28-
29- def __init__ (self , model , max_sugar , max_spice , cell ):
30- super ().__init__ (model )
31- self .sugar_amount = max_sugar
32- self .max_sugar = max_sugar
33- self .spice_amount = max_spice
34- self .max_spice = max_spice
35- self .cell = cell
36-
37- def step (self ):
38- """
39- Growth function, adds one unit of sugar and spice each step up to
40- max amount
41- """
42- self .sugar_amount = min ([self .max_sugar , self .sugar_amount + 1 ])
43- self .spice_amount = min ([self .max_spice , self .spice_amount + 1 ])
44-
45-
4621class Trader (CellAgent ):
4722 """
4823 Trader:
@@ -70,12 +45,6 @@ def __init__(
7045 self .prices = []
7146 self .trade_partners = []
7247
73- def get_resource (self , cell ):
74- for agent in cell .agents :
75- if isinstance (agent , Resource ):
76- return agent
77- raise Exception (f"Resource agent not found in the position { cell .coordinate } " )
78-
7948 def get_trader (self , cell ):
8049 """
8150 helper function used in self.trade_with_neighbors()
@@ -85,17 +54,6 @@ def get_trader(self, cell):
8554 if isinstance (agent , Trader ):
8655 return agent
8756
88- def is_occupied_by_other (self , cell ):
89- """
90- helper function part 1 of self.move()
91- """
92-
93- if cell is self .cell :
94- # agent's position is considered unoccupied as agent can stay there
95- return False
96- # get contents of each cell in neighborhood
97- return any (isinstance (a , Trader ) for a in cell .agents )
98-
9957 def calculate_welfare (self , sugar , spice ):
10058 """
10159 helper function
@@ -264,15 +222,15 @@ def move(self):
264222 neighboring_cells = [
265223 cell
266224 for cell in self .cell .get_neighborhood (self .vision , include_center = True )
267- if not self . is_occupied_by_other ( cell )
225+ if cell . is_empty
268226 ]
269227
270228 # 2. determine which move maximizes welfare
271229
272230 welfares = [
273231 self .calculate_welfare (
274- self .sugar + self . get_resource ( cell ). sugar_amount ,
275- self .spice + self . get_resource ( cell ). spice_amount ,
232+ self .sugar + cell . sugar ,
233+ self .spice + cell . spice ,
276234 )
277235 for cell in neighboring_cells
278236 ]
@@ -282,6 +240,7 @@ def move(self):
282240 # find the highest welfare in welfares
283241 max_welfare = max (welfares )
284242 # get the index of max welfare cells
243+ # fixme: rewrite using enumerate and single loop
285244 candidate_indices = [
286245 i for i in range (len (welfares )) if math .isclose (welfares [i ], max_welfare )
287246 ]
@@ -296,19 +255,17 @@ def move(self):
296255 for cell in candidates
297256 if math .isclose (get_distance (self .cell , cell ), min_dist , rel_tol = 1e-02 )
298257 ]
258+
299259 # 4. Move Agent
300260 self .cell = self .random .choice (final_candidates )
301261
302262 def eat (self ):
303- patch = self .get_resource (self .cell )
304- if patch .sugar_amount > 0 :
305- self .sugar += patch .sugar_amount
306- patch .sugar_amount = 0
263+ self .sugar += self .cell .sugar
264+ self .cell .sugar = 0
307265 self .sugar -= self .metabolism_sugar
308266
309- if patch .spice_amount > 0 :
310- self .spice += patch .spice_amount
311- patch .spice_amount = 0
267+ self .spice += self .cell .spice
268+ self .cell .spice = 0
312269 self .spice -= self .metabolism_spice
313270
314271 def maybe_die (self ):
@@ -327,18 +284,8 @@ def trade_with_neighbors(self):
327284 2- trade (2 sessions)
328285 3- collect data
329286 """
330-
331- neighbor_agents = [
332- self .get_trader (cell )
333- for cell in self .cell .get_neighborhood (radius = self .vision )
334- if self .is_occupied_by_other (cell )
335- ]
336-
337- if len (neighbor_agents ) == 0 :
338- return
339-
340- # iterate through traders in neighboring cells and trade
341- for a in neighbor_agents :
287+ # iterate through traders in neighboring cells and trade
288+ for a in self .cell .get_neighborhood (radius = self .vision ).agents :
342289 self .trade (a )
343290
344291 return
0 commit comments