Skip to content

Commit

Permalink
fix: Get all traders in neighborhood instead of 1 trader each cell
Browse files Browse the repository at this point in the history
  • Loading branch information
rht committed Nov 30, 2023
1 parent 654f832 commit 0301897
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions examples/sugarscape_g1mt/sugarscape_g1mt/trader_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ def get_distance(pos_1, pos_2):
return math.sqrt(dx**2 + dy**2)


def flatten(list_of_lists):
"""
helper function to collapse a list of lists into one list. See np.ravel in NumPy.
"""
return [item for sublist in list_of_lists for item in sublist]


class Trader(mesa.Agent):
"""
Trader:
Expand Down Expand Up @@ -92,16 +99,13 @@ def get_spice_amount(self, pos):
return spice_patch.amount
return 0

def get_trader(self, pos):
def get_traders(self, pos):
"""
helper function used in self.trade_with_neighbors()
"""

this_cell = self.model.grid.get_cell_list_contents(pos)

for agent in this_cell:
if isinstance(agent, Trader):
return agent
return [agent for agent in this_cell if isinstance(agent, Trader)]

def is_occupied_by_other(self, pos):
"""
Expand Down Expand Up @@ -357,13 +361,15 @@ def trade_with_neighbors(self):
3- collect data
"""

neighbor_agents = [
self.get_trader(pos)
for pos in self.model.grid.get_neighborhood(
self.pos, self.moore, False, self.vision
)
if self.is_occupied_by_other(pos)
]
neighbor_agents = flatten(
[
self.get_traders(pos)
for pos in self.model.grid.get_neighborhood(
self.pos, self.moore, False, self.vision
)
if self.is_occupied_by_other(pos)
]
)

if len(neighbor_agents) == 0:
return
Expand Down

0 comments on commit 0301897

Please sign in to comment.