Skip to content

Commit

Permalink
fix: sugarscape_cg: Ensure agent ids are unique
Browse files Browse the repository at this point in the history
  • Loading branch information
rht authored and tpike3 committed Jul 17, 2022
1 parent 0cb0286 commit 2d59e6d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 6 additions & 4 deletions examples/sugarscape_cg/sugarscape_cg/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ def get_distance(pos_1, pos_2):


class SsAgent(mesa.Agent):
def __init__(self, pos, model, moore=False, sugar=0, metabolism=0, vision=0):
super().__init__(pos, model)
def __init__(
self, unique_id, pos, model, moore=False, sugar=0, metabolism=0, vision=0
):
super().__init__(unique_id, model)
self.pos = pos
self.moore = moore
self.sugar = sugar
Expand Down Expand Up @@ -73,8 +75,8 @@ def step(self):


class Sugar(mesa.Agent):
def __init__(self, pos, model, max_sugar):
super().__init__(pos, model)
def __init__(self, unique_id, pos, model, max_sugar):
super().__init__(unique_id, model)
self.amount = max_sugar
self.max_sugar = max_sugar

Expand Down
7 changes: 5 additions & 2 deletions examples/sugarscape_cg/sugarscape_cg/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ def __init__(self, width=50, height=50, initial_population=100):
import numpy as np

sugar_distribution = np.genfromtxt("sugarscape_cg/sugar-map.txt")
agent_id = 0
for _, x, y in self.grid.coord_iter():
max_sugar = sugar_distribution[x, y]
sugar = Sugar((x, y), self, max_sugar)
sugar = Sugar(agent_id, (x, y), self, max_sugar)
agent_id += 1
self.grid.place_agent(sugar, (x, y))
self.schedule.add(sugar)

Expand All @@ -57,7 +59,8 @@ def __init__(self, width=50, height=50, initial_population=100):
sugar = self.random.randrange(6, 25)
metabolism = self.random.randrange(2, 4)
vision = self.random.randrange(1, 6)
ssa = SsAgent((x, y), self, False, sugar, metabolism, vision)
ssa = SsAgent(agent_id, (x, y), self, False, sugar, metabolism, vision)
agent_id += 1
self.grid.place_agent(ssa, (x, y))
self.schedule.add(ssa)

Expand Down

0 comments on commit 2d59e6d

Please sign in to comment.