Skip to content

Commit 428c900

Browse files
rhttpike3
andauthored
Bump to Mesa 2.0 (#40)
* Bump to Mesa 2.0 * Update to Mesa 2.0 API * Update coord_iter to Mesa 2.0 API --------- Co-authored-by: Tom Pike <[email protected]>
1 parent db2ec03 commit 428c900

File tree

31 files changed

+40
-41
lines changed

31 files changed

+40
-41
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
itertools
2-
mesa~=1.1
2+
mesa~=2.0
33
numpy
44
pandas
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
jupyter
22
matplotlib
3-
mesa~=1.1
3+
mesa~=2.0

examples/boltzmann_wealth_model/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
my_bar.progress((i / num_ticks), text="Simulation progress")
6969
placeholder.text("Step = %d" % i)
7070
for cell in model.grid.coord_iter():
71-
cell_content, x, y = cell
71+
cell_content, (x, y) = cell
7272
agent_count = len(cell_content)
7373
selected_row = df_grid[(df_grid["x"] == x) & (df_grid["y"] == y)]
7474
df_grid.loc[
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mesa~=1.1
1+
mesa~=2.0
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
mesa~=1.1
2-
-e git+https://github.com/projectmesa/mesa-examples
1+
mesa~=2.0
2+
solara
3+
git+https://github.com/projectmesa/mesa-examples

examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,15 @@ def __init__(self, unique_id, model):
5656
def move(self):
5757
possible_steps = [
5858
node
59-
for node in self.model.grid.get_neighbors(self.pos, include_center=False)
59+
for node in self.model.grid.get_neighborhood(self.pos, include_center=False)
6060
if self.model.grid.is_cell_empty(node)
6161
]
6262
if len(possible_steps) > 0:
6363
new_position = self.random.choice(possible_steps)
6464
self.model.grid.move_agent(self, new_position)
6565

6666
def give_money(self):
67-
neighbors_nodes = self.model.grid.get_neighbors(self.pos, include_center=False)
68-
neighbors = self.model.grid.get_cell_list_contents(neighbors_nodes)
67+
neighbors = self.model.grid.get_neighbors(self.pos, include_center=False)
6968
if len(neighbors) > 0:
7069
other = self.random.choice(neighbors)
7170
other.wealth += 1
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
jupyter
22
matplotlib
3-
mesa~=1.1
3+
mesa~=2.0
44
numpy
55
networkx

examples/caching_and_replay/model.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ def __init__(self, width=20, height=20, density=0.8, minority_pc=0.2, homophily=
6363
# the coordinates of a cell as well as
6464
# its contents. (coord_iter)
6565
for cell in self.grid.coord_iter():
66-
x = cell[1]
67-
y = cell[2]
66+
x, y = cell[1]
6867
if self.random.random() < self.density:
6968
agent_type = 1 if self.random.random() < self.minority_pc else 0
7069

examples/charts/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
itertools
2-
mesa~=1.1
2+
mesa~=2.0
33
numpy
44
pandas

examples/color_patches/color_patches/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def __init__(self, width=20, height=20):
8080
# -->but only col & row
8181
# for (contents, col, row) in self._grid.coord_iter():
8282
# replaced content with _ to appease linter
83-
for _, row, col in self._grid.coord_iter():
83+
for _, (row, col) in self._grid.coord_iter():
8484
cell = ColorCell(
8585
(row, col), self, ColorCell.OPINIONS[self.random.randrange(0, 16)]
8686
)

examples/conways_game_of_life/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
model.step()
5050
my_bar.progress((i / num_ticks), text="Simulation progress")
5151
placeholder.text("Step = %d" % i)
52-
for contents, x, y in model.grid.coord_iter():
52+
for contents, (x, y) in model.grid.coord_iter():
5353
# print('x:',x,'y:',y, 'state:',contents)
5454
selected_row = df_grid[(df_grid["x"] == x) & (df_grid["y"] == y)]
5555
df_grid.loc[

examples/conways_game_of_life/conways_game_of_life/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, width=50, height=50):
2727

2828
# Place a cell at each location, with some initialized to
2929
# ALIVE and some to DEAD.
30-
for contents, x, y in self.grid.coord_iter():
30+
for contents, (x, y) in self.grid.coord_iter():
3131
cell = Cell((x, y), self)
3232
if self.random.random() < 0.1:
3333
cell.state = cell.ALIVE

examples/epstein_civil_violence/epstein_civil_violence/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __init__(
8181
unique_id = 0
8282
if self.cop_density + self.citizen_density > 1:
8383
raise ValueError("Cop density + citizen density must be less than 1")
84-
for contents, x, y in self.grid.coord_iter():
84+
for contents, (x, y) in self.grid.coord_iter():
8585
if self.random.random() < self.cop_density:
8686
cop = Cop(unique_id, self, (x, y), vision=self.cop_vision)
8787
unique_id += 1

examples/forest_fire/forest_fire/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(self, width=100, height=100, density=0.65):
2929
)
3030

3131
# Place a tree in each cell with Prob = density
32-
for contents, x, y in self.grid.coord_iter():
32+
for contents, (x, y) in self.grid.coord_iter():
3333
if self.random.random() < density:
3434
# Create a tree
3535
new_tree = TreeCell((x, y), self)

examples/hex_snowflake/hex_snowflake/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def __init__(self, width=50, height=50):
2525
self.grid = mesa.space.HexGrid(width, height, torus=True)
2626

2727
# Place a dead cell at each location.
28-
for contents, x, y in self.grid.coord_iter():
29-
cell = Cell((x, y), self)
30-
self.grid.place_agent(cell, (x, y))
28+
for contents, pos in self.grid.coord_iter():
29+
cell = Cell(pos, self)
30+
self.grid.place_agent(cell, pos)
3131
self.schedule.add(cell)
3232

3333
# activate the center(ish) cell.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mesa~=1.1
1+
mesa~=2.0

examples/pd_grid/analysis.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
" if not ax:\n",
6969
" fig, ax = plt.subplots(figsize=(6, 6))\n",
7070
" grid = np.zeros((model.grid.width, model.grid.height))\n",
71-
" for agent, x, y in model.grid.coord_iter():\n",
71+
" for agent, (x, y) in model.grid.coord_iter():\n",
7272
" if agent.move == \"D\":\n",
7373
" grid[y][x] = 1\n",
7474
" else:\n",

examples/pd_grid/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
jupyter
22
matplotlib
3-
mesa~=1.1
3+
mesa~=2.0

examples/schelling/model.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ def __init__(self, width=20, height=20, density=0.8, minority_pc=0.2, homophily=
6161
# the coordinates of a cell as well as
6262
# its contents. (coord_iter)
6363
for cell in self.grid.coord_iter():
64-
x = cell[1]
65-
y = cell[2]
64+
x, y = cell[1]
6665
if self.random.random() < self.density:
6766
agent_type = 1 if self.random.random() < self.minority_pc else 0
6867

examples/schelling/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
jupyter
22
matplotlib
3-
mesa~=1.1
3+
mesa~=2.0

examples/schelling_experimental/model.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ def __init__(self, width=20, height=20, density=0.8, minority_pc=0.2, homophily=
6161
# the coordinates of a cell as well as
6262
# its contents. (coord_iter)
6363
for cell in self.grid.coord_iter():
64-
x = cell[1]
65-
y = cell[2]
64+
x, y = cell[1]
6665
if self.random.random() < self.density:
6766
agent_type = 1 if self.random.random() < self.minority_pc else 0
6867

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
jupyter
22
matplotlib
3-
mesa~=1.1
4-
-e git+https://github.com/projectmesa/mesa-examples
3+
mesa~=2.0
4+
solara
5+
git+https://github.com/projectmesa/mesa-examples
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mesa~=1.1
1+
mesa~=2.0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
jupyter
2-
mesa~=1.1
2+
mesa~=2.0

examples/sugarscape_cg/sugarscape_cg/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, width=50, height=50, initial_population=100):
4545

4646
sugar_distribution = np.genfromtxt("sugarscape_cg/sugar-map.txt")
4747
agent_id = 0
48-
for _, x, y in self.grid.coord_iter():
48+
for _, (x, y) in self.grid.coord_iter():
4949
max_sugar = sugar_distribution[x, y]
5050
sugar = Sugar(agent_id, (x, y), self, max_sugar)
5151
agent_id += 1

examples/sugarscape_g1mt/app.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ def portray(g):
1515
"trader": {"x": [], "y": [], "c": "tab:red", "marker": "o", "s": 10},
1616
}
1717

18-
# TODO update to Mesa 2.0 API
19-
for content, i, j in g.coord_iter():
18+
for content, (i, j) in g.coord_iter():
2019
for agent in content:
2120
if isinstance(agent, Trader):
2221
layers["trader"]["x"].append(i)

examples/sugarscape_g1mt/sugarscape_g1mt/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def __init__(
8989
spice_distribution = np.flip(sugar_distribution, 1)
9090

9191
agent_id = 0
92-
for _, x, y in self.grid.coord_iter():
92+
for _, (x, y) in self.grid.coord_iter():
9393
max_sugar = sugar_distribution[x, y]
9494
if max_sugar > 0:
9595
sugar = Sugar(agent_id, self, (x, y), max_sugar)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
networkx>=2.0
2-
mesa~=1.1
2+
mesa~=2.0

examples/virus_on_network/virus_on_network/model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ def __init__(
123123
self.gain_resistance_chance = gain_resistance_chance
124124

125125
def try_to_infect_neighbors(self):
126-
neighbors_nodes = self.model.grid.get_neighbors(self.pos, include_center=False)
126+
neighbors_nodes = self.model.grid.get_neighborhood(
127+
self.pos, include_center=False
128+
)
127129
susceptible_neighbors = [
128130
agent
129131
for agent in self.model.grid.get_cell_list_contents(neighbors_nodes)

examples/wolf_sheep/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mesa~=1.1
1+
mesa~=2.0

examples/wolf_sheep/wolf_sheep/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def __init__(
113113

114114
# Create grass patches
115115
if self.grass:
116-
for agent, x, y in self.grid.coord_iter():
116+
for agent, (x, y) in self.grid.coord_iter():
117117
fully_grown = self.random.choice([True, False])
118118

119119
if fully_grown:

0 commit comments

Comments
 (0)