Skip to content

Commit c1e051b

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 <[email protected]>
1 parent 90d668c commit c1e051b

File tree

5 files changed

+12
-18
lines changed
  • examples/basic

5 files changed

+12
-18
lines changed

examples/basic/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/basic/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/basic/conways_game_of_life/conways_game_of_life/cell.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self, pos, model, init_state=DEAD):
1111
"""
1212
Create a cell, in the given state, at the given x, y position.
1313
"""
14-
super().__init__(pos, model)
14+
super().__init__(model)
1515
self.x, self.y = pos
1616
self.state = init_state
1717
self._nextState = None

examples/basic/schelling/model.py

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

9-
def __init__(self, unique_id, model, agent_type):
9+
def __init__(self, model, agent_type):
1010
"""
1111
Create a new Schelling agent.
1212
1313
Args:
14-
unique_id: Unique identifier for the agent.
1514
x, y: Agent initial location.
1615
agent_type: Indicator for the agent's type (minority=1, majority=0)
1716
"""
18-
super().__init__(unique_id, model)
17+
super().__init__(model)
1918
self.type = agent_type
2019

2120
def step(self):
@@ -82,7 +81,7 @@ def __init__(
8281
for _, pos in self.grid.coord_iter():
8382
if self.random.random() < self.density:
8483
agent_type = 1 if self.random.random() < self.minority_pc else 0
85-
agent = SchellingAgent(self.next_id(), self, agent_type)
84+
agent = SchellingAgent(self, agent_type)
8685
self.grid.place_agent(agent, pos)
8786

8887
self.datacollector.collect(self)

examples/basic/virus_on_network/virus_on_network/model.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ def __init__(
6565
)
6666

6767
# Create agents
68-
for i, node in enumerate(self.G.nodes()):
68+
for node in self.G.nodes():
6969
a = VirusAgent(
70-
i,
7170
self,
7271
State.SUSCEPTIBLE,
7372
self.virus_spread_chance,
@@ -112,15 +111,14 @@ class VirusAgent(mesa.Agent):
112111

113112
def __init__(
114113
self,
115-
unique_id,
116114
model,
117115
initial_state,
118116
virus_spread_chance,
119117
virus_check_frequency,
120118
recovery_chance,
121119
gain_resistance_chance,
122120
):
123-
super().__init__(unique_id, model)
121+
super().__init__(model)
124122

125123
self.state = initial_state
126124

0 commit comments

Comments
 (0)