Skip to content

Commit 6b804bd

Browse files
committed
Revert basic example models to current grids
Revert the basic models back to the current stable grids
1 parent acbff93 commit 6b804bd

File tree

5 files changed

+55
-51
lines changed

5 files changed

+55
-51
lines changed

examples/boltzmann_wealth_model/model.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,19 @@ class BoltzmannWealthModel(mesa.Model):
2020
def __init__(self, N=100, width=10, height=10):
2121
super().__init__()
2222
self.num_agents = N
23-
self.grid = mesa.experimental.cell_space.OrthogonalMooreGrid(
24-
(width, height), torus=True, random=self.random
25-
)
23+
self.grid = mesa.space.MultiGrid(width, height, True)
2624

2725
self.datacollector = mesa.DataCollector(
2826
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
2927
)
3028
# Create agents
3129
for _ in range(self.num_agents):
32-
agent = MoneyAgent(self)
30+
a = MoneyAgent(self)
3331

3432
# Add the agent to a random grid cell
35-
x = self.random.randrange(width)
36-
y = self.random.randrange(height)
37-
agent.move_to(self.grid[(x, y)])
33+
x = self.random.randrange(self.grid.width)
34+
y = self.random.randrange(self.grid.height)
35+
self.grid.place_agent(a, (x, y))
3836

3937
self.running = True
4038
self.datacollector.collect(self)
@@ -49,23 +47,31 @@ def run_model(self, n):
4947
self.step()
5048

5149

52-
class MoneyAgent(mesa.experimental.cell_space.CellAgent):
50+
class MoneyAgent(mesa.Agent):
5351
"""An agent with fixed initial wealth."""
5452

5553
def __init__(self, model):
5654
super().__init__(model)
5755
self.wealth = 1
5856

57+
def move(self):
58+
possible_steps = self.model.grid.get_neighborhood(
59+
self.pos, moore=True, include_center=False
60+
)
61+
new_position = self.random.choice(possible_steps)
62+
self.model.grid.move_agent(self, new_position)
63+
5964
def give_money(self):
60-
cellmates = [
61-
agent for agent in self.cell.agents if agent is not self
62-
] # Ensure agent is not giving money to itself
65+
cellmates = self.model.grid.get_cell_list_contents([self.pos])
66+
cellmates.pop(
67+
cellmates.index(self)
68+
) # Ensure agent is not giving money to itself
6369
if len(cellmates) > 0:
6470
other = self.random.choice(cellmates)
6571
other.wealth += 1
6672
self.wealth -= 1
6773

6874
def step(self):
69-
self.cell = self.cell.neighborhood.select_random_cell()
75+
self.move()
7076
if self.wealth > 0:
7177
self.give_money()
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
from mesa.experimental.cell_space import CellAgent
1+
import mesa
22

33

4-
class Cell(CellAgent):
4+
class Cell(mesa.Agent):
55
"""Represents a single ALIVE or DEAD cell in the simulation."""
66

77
DEAD = 0
88
ALIVE = 1
99

10-
def __init__(self, model, init_state=DEAD):
10+
def __init__(self, pos, model, init_state=DEAD):
1111
"""
1212
Create a cell, in the given state, at the given x, y position.
1313
"""
1414
super().__init__(model)
15+
self.x, self.y = pos
1516
self.state = init_state
16-
self._next_state = None
17+
self._nextState = None
1718

1819
@property
19-
def is_alive(self):
20+
def isAlive(self):
2021
return self.state == self.ALIVE
2122

2223
@property
2324
def neighbors(self):
24-
return self.cell.neighborhood.agents
25+
return self.model.grid.iter_neighbors((self.x, self.y), True)
2526

2627
def determine_state(self):
2728
"""
@@ -34,19 +35,19 @@ def determine_state(self):
3435

3536
# Get the neighbors and apply the rules on whether to be alive or dead
3637
# at the next tick.
37-
live_neighbors = sum(neighbor.is_alive for neighbor in self.neighbors)
38+
live_neighbors = sum(neighbor.isAlive for neighbor in self.neighbors)
3839

3940
# Assume nextState is unchanged, unless changed below.
40-
self._next_state = self.state
41-
if self.is_alive:
41+
self._nextState = self.state
42+
if self.isAlive:
4243
if live_neighbors < 2 or live_neighbors > 3:
43-
self._next_state = self.DEAD
44+
self._nextState = self.DEAD
4445
else:
4546
if live_neighbors == 3:
46-
self._next_state = self.ALIVE
47+
self._nextState = self.ALIVE
4748

4849
def assume_state(self):
4950
"""
5051
Set the state to the new computed state -- computed in step().
5152
"""
52-
self.state = self._next_state
53+
self.state = self._nextState

examples/conways_game_of_life/conways_game_of_life/model.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@ def __init__(self, width=50, height=50):
1515
"""
1616
super().__init__()
1717
# Use a simple grid, where edges wrap around.
18-
self.grid = mesa.experimental.cell_space.OrthogonalMooreGrid(
19-
(width, height), torus=True
20-
)
18+
self.grid = mesa.space.SingleGrid(width, height, torus=True)
2119

2220
# Place a cell at each location, with some initialized to
2321
# ALIVE and some to DEAD.
24-
for cell in self.grid.all_cells:
25-
cell_agent = Cell(self)
22+
for contents, (x, y) in self.grid.coord_iter():
23+
cell = Cell((x, y), self)
2624
if self.random.random() < 0.1:
27-
cell_agent.state = cell_agent.ALIVE
28-
cell_agent.move_to(cell)
25+
cell.state = cell.ALIVE
26+
self.grid.place_agent(cell, (x, y))
2927

3028
self.running = True
3129

examples/schelling/model.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import mesa
2-
from mesa.experimental.cell_space import CellAgent, OrthogonalMooreGrid
32

43

5-
class SchellingAgent(CellAgent):
4+
class SchellingAgent(mesa.Agent):
65
"""
76
Schelling segregation agent
87
"""
@@ -17,15 +16,15 @@ def __init__(self, model: mesa.Model, agent_type: int) -> None:
1716
super().__init__(model)
1817
self.type = agent_type
1918

20-
def step(self):
21-
neighbors = self.cell.get_neighborhood(radius=self.model.radius).agents
22-
similar = len(
23-
[neighbor for neighbor in neighbors if neighbor.type == self.type]
19+
def step(self) -> None:
20+
neighbors = self.model.grid.iter_neighbors(
21+
self.pos, moore=True, radius=self.model.radius
2422
)
23+
similar = sum(1 for neighbor in neighbors if neighbor.type == self.type)
2524

2625
# If unhappy, move:
2726
if similar < self.model.homophily:
28-
self.cell = self.model.grid.select_random_empty_cell()
27+
self.model.grid.move_to_empty(self)
2928
else:
3029
self.model.happy += 1
3130

@@ -61,7 +60,7 @@ def __init__(
6160
self.homophily = homophily
6261
self.radius = radius
6362

64-
self.grid = OrthogonalMooreGrid((width, height), torus=True)
63+
self.grid = mesa.space.SingleGrid(width, height, torus=True)
6564

6665
self.happy = 0
6766
self.datacollector = mesa.DataCollector(
@@ -72,11 +71,11 @@ def __init__(
7271
# We use a grid iterator that returns
7372
# the coordinates of a cell as well as
7473
# its contents. (coord_iter)
75-
for cell in self.grid.all_cells:
74+
for _, pos in self.grid.coord_iter():
7675
if self.random.random() < density:
7776
agent_type = 1 if self.random.random() < minority_pc else 0
7877
agent = SchellingAgent(self, agent_type)
79-
agent.cell = cell
78+
self.grid.place_agent(agent, pos)
8079

8180
self.datacollector.collect(self)
8281

examples/virus_on_network/virus_on_network/model.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import mesa
55
import networkx as nx
6-
from mesa.experimental.cell_space import FixedAgent, Network
76

87

98
class State(Enum):
@@ -13,7 +12,7 @@ class State(Enum):
1312

1413

1514
def number_state(model, state):
16-
return sum(1 for a in model.agents if a.state is state)
15+
return sum(1 for a in model.grid.get_all_cell_contents() if a.state is state)
1716

1817

1918
def number_infected(model):
@@ -47,7 +46,7 @@ def __init__(
4746
self.num_nodes = num_nodes
4847
prob = avg_node_degree / self.num_nodes
4948
self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=prob)
50-
self.grid = Network(self.G)
49+
self.grid = mesa.space.NetworkGrid(self.G)
5150

5251
self.initial_outbreak_size = (
5352
initial_outbreak_size if initial_outbreak_size <= num_nodes else num_nodes
@@ -77,13 +76,12 @@ def __init__(
7776
)
7877

7978
# Add the agent to the node
80-
a.cell = self.grid[node]
79+
self.grid.place_agent(a, node)
8180

8281
# Infect some nodes
8382
infected_nodes = self.random.sample(list(self.G), self.initial_outbreak_size)
84-
for node in infected_nodes:
85-
for agent in self.grid[node].agents:
86-
agent.state = State.INFECTED
83+
for a in self.grid.get_cell_list_contents(infected_nodes):
84+
a.state = State.INFECTED
8785

8886
self.running = True
8987
self.datacollector.collect(self)
@@ -106,7 +104,7 @@ def run_model(self, n):
106104
self.step()
107105

108106

109-
class VirusAgent(FixedAgent):
107+
class VirusAgent(mesa.Agent):
110108
"""
111109
Individual Agent definition and its properties/interaction methods
112110
"""
@@ -130,10 +128,12 @@ def __init__(
130128
self.gain_resistance_chance = gain_resistance_chance
131129

132130
def try_to_infect_neighbors(self):
133-
neighbors_nodes = self.cell.neighborhood
131+
neighbors_nodes = self.model.grid.get_neighborhood(
132+
self.pos, include_center=False
133+
)
134134
susceptible_neighbors = [
135135
agent
136-
for agent in neighbors_nodes.agents
136+
for agent in self.model.grid.get_cell_list_contents(neighbors_nodes)
137137
if agent.state is State.SUSCEPTIBLE
138138
]
139139
for a in susceptible_neighbors:

0 commit comments

Comments
 (0)