Skip to content

Commit 660102f

Browse files
quaquelEwoutH
andauthored
Remove unique_id and model.next_id (#194)
All examples can be updated to no longer pass a unique id, nor use model.next_id. This only fixes the examples, not the gis-examples or rl examples. Co-authored-by: Ewout ter Hoeven <E.M.terHoeven@student.tudelft.nl>
1 parent ec07d89 commit 660102f

File tree

38 files changed

+98
-130
lines changed

38 files changed

+98
-130
lines changed

examples/aco_tsp/aco_tsp/model.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,13 @@ class AntTSP(mesa.Agent):
8282
An agent
8383
"""
8484

85-
def __init__(self, unique_id, model, alpha: float = 1.0, beta: float = 5.0):
85+
def __init__(self, model, alpha: float = 1.0, beta: float = 5.0):
8686
"""
8787
Customize the agent
8888
"""
89-
self.unique_id = unique_id
89+
super().__init__(model)
9090
self.alpha = alpha
9191
self.beta = beta
92-
super().__init__(unique_id, model)
9392
self._cities_visited = []
9493
self._traveled_distance = 0
9594
self.tsp_solution = []
@@ -176,8 +175,8 @@ def __init__(
176175
self.max_steps = max_steps
177176
self.grid = mesa.space.NetworkGrid(tsp_graph.g)
178177

179-
for i in range(self.num_agents):
180-
agent = AntTSP(unique_id=i, model=self, alpha=ant_alpha, beta=ant_beta)
178+
for _ in range(self.num_agents):
179+
agent = AntTSP(model=self, alpha=ant_alpha, beta=ant_beta)
181180

182181
city = tsp_graph.cities[self.random.randrange(self.num_cities)]
183182
self.grid.place_agent(agent, city)

examples/bank_reserves/bank_reserves/agents.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ def bank_balance(self):
4545

4646
# subclass of RandomWalker, which is subclass to Mesa Agent
4747
class Person(RandomWalker):
48-
def __init__(self, unique_id, model, moore, bank, rich_threshold):
48+
def __init__(self, model, moore, bank, rich_threshold):
4949
# init parent class with required parameters
50-
super().__init__(unique_id, model, moore=moore)
50+
super().__init__(model, moore=moore)
5151
# the amount each person has in savings
5252
self.savings = 0
5353
# total loan amount person has outstanding

examples/bank_reserves/bank_reserves/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ def __init__(
139139
self.bank = Bank(self, self.reserve_percent)
140140

141141
# create people for the model according to number of people set by user
142-
for i in range(self.init_people):
142+
for _ in range(self.init_people):
143143
# set x, y coords randomly within the grid
144144
x = self.random.randrange(self.width)
145145
y = self.random.randrange(self.height)
146-
p = Person(i, self, True, self.bank, self.rich_threshold)
146+
p = Person(self, True, self.bank, self.rich_threshold)
147147
# place the Person object on the grid at coordinates (x, y)
148148
self.grid.place_agent(p, (x, y))
149149

examples/bank_reserves/bank_reserves/random_walk.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ class RandomWalker(mesa.Agent):
2424
# use a Moore neighborhood
2525
moore = True
2626

27-
def __init__(self, unique_id, model, moore=True):
27+
def __init__(self, model, moore=True):
2828
"""
2929
grid: The MultiGrid object in which the agent lives.
3030
x: The agent's current x coordinate
3131
y: The agent's current y coordinate
3232
moore: If True, may move in all 8 directions.
3333
Otherwise, only up, down, left, right.
3434
"""
35-
super().__init__(unique_id, model)
35+
super().__init__(model)
3636
self.moore = moore
3737

3838
def random_move(self):

examples/boid_flockers/boid_flockers/model.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class Boid(mesa.Agent):
2626

2727
def __init__(
2828
self,
29-
unique_id,
3029
model,
3130
speed,
3231
direction,
@@ -40,7 +39,6 @@ def __init__(
4039
Create a new Boid flocker agent.
4140
4241
Args:
43-
unique_id: Unique agent identifier.
4442
speed: Distance to move per step.
4543
direction: numpy vector for the Boid's direction of movement.
4644
vision: Radius to look around for nearby Boids.
@@ -49,7 +47,7 @@ def __init__(
4947
separate: the relative importance of avoiding close neighbors
5048
match: the relative importance of matching neighbors' headings
5149
"""
52-
super().__init__(unique_id, model)
50+
super().__init__(model)
5351
self.speed = speed
5452
self.direction = direction
5553
self.vision = vision
@@ -129,13 +127,12 @@ def make_agents(self):
129127
"""
130128
Create self.population agents, with random positions and starting headings.
131129
"""
132-
for i in range(self.population):
130+
for _ in range(self.population):
133131
x = self.random.random() * self.space.x_max
134132
y = self.random.random() * self.space.y_max
135133
pos = np.array((x, y))
136134
direction = np.random.random(2) * 2 - 1
137135
boid = Boid(
138-
unique_id=i,
139136
model=self,
140137
speed=self.speed,
141138
direction=direction,

examples/boltzmann_wealth_model/boltzmann_wealth_model/model.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def __init__(self, N=100, width=10, height=10):
2626
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
2727
)
2828
# Create agents
29-
for i in range(self.num_agents):
30-
a = MoneyAgent(i, self)
29+
for _ in range(self.num_agents):
30+
a = MoneyAgent(self)
3131

3232
# Add the agent to a random grid cell
3333
x = self.random.randrange(self.grid.width)
@@ -50,8 +50,8 @@ def run_model(self, n):
5050
class MoneyAgent(mesa.Agent):
5151
"""An agent with fixed initial wealth."""
5252

53-
def __init__(self, unique_id, model):
54-
super().__init__(unique_id, model)
53+
def __init__(self, model):
54+
super().__init__(model)
5555
self.wealth = 1
5656

5757
def move(self):

examples/boltzmann_wealth_model_experimental/model.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def __init__(self, N=100, width=10, height=10):
2626
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
2727
)
2828
# Create agents
29-
for i in range(self.num_agents):
30-
a = MoneyAgent(i, self)
29+
for _ in range(self.num_agents):
30+
a = MoneyAgent(self)
3131

3232
# Add the agent to a random grid cell
3333
x = self.random.randrange(self.grid.width)
@@ -50,8 +50,8 @@ def run_model(self, n):
5050
class MoneyAgent(mesa.Agent):
5151
"""An agent with fixed initial wealth."""
5252

53-
def __init__(self, unique_id, model):
54-
super().__init__(unique_id, model)
53+
def __init__(self, model):
54+
super().__init__(model)
5555
self.wealth = 1
5656

5757
def move(self):

examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self, num_agents=7, num_nodes=10):
2929

3030
# Create agents
3131
for i in range(self.num_agents):
32-
a = MoneyAgent(i, self)
32+
a = MoneyAgent(self)
3333

3434
# Add the agent to a random node
3535
self.grid.place_agent(a, list_of_random_nodes[i])
@@ -50,8 +50,8 @@ def run_model(self, n):
5050
class MoneyAgent(mesa.Agent):
5151
"""An agent with fixed initial wealth."""
5252

53-
def __init__(self, unique_id, model):
54-
super().__init__(unique_id, model)
53+
def __init__(self, model):
54+
super().__init__(model)
5555
self.wealth = 1
5656

5757
def move(self):

examples/caching_and_replay/model.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ class SchellingAgent(mesa.Agent):
88
Schelling segregation agent
99
"""
1010

11-
def __init__(self, unique_id, model, agent_type):
11+
def __init__(self, model, agent_type):
1212
"""
1313
Create a new Schelling agent.
1414
1515
Args:
16-
unique_id: Unique identifier for the agent.
1716
x, y: Agent initial location.
1817
agent_type: Indicator for the agent's type (minority=1, majority=0)
1918
"""
20-
super().__init__(unique_id, model)
19+
super().__init__(model)
2120
self.type = agent_type
2221

2322
def step(self):
@@ -84,7 +83,7 @@ def __init__(
8483
for _, pos in self.grid.coord_iter():
8584
if self.random.random() < self.density:
8685
agent_type = 1 if self.random.random() < self.minority_pc else 0
87-
agent = SchellingAgent(self.next_id(), self, agent_type)
86+
agent = SchellingAgent(self, agent_type)
8887
self.grid.place_agent(agent, pos)
8988

9089
self.datacollector.collect(self)

examples/charts/charts/agents.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ def bank_balance(self):
4545

4646
# subclass of RandomWalker, which is subclass to Mesa Agent
4747
class Person(RandomWalker):
48-
def __init__(self, unique_id, model, moore, bank, rich_threshold):
48+
def __init__(self, model, moore, bank, rich_threshold):
4949
# init parent class with required parameters
50-
super().__init__(unique_id, model, moore=moore)
50+
super().__init__(model, moore=moore)
5151
# the amount each person has in savings
5252
self.savings = 0
5353
# total loan amount person has outstanding

0 commit comments

Comments
 (0)