Skip to content

Commit 51b116b

Browse files
committed
Merge remote-tracking branch 'upstream/main' into new_spaces
2 parents 0c178cb + 9897884 commit 51b116b

File tree

78 files changed

+556
-532
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+556
-532
lines changed

.github/workflows/test_examples.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
# with:
2626
# python-version: "3.12"
2727
# - name: Install dependencies
28-
# run: pip install mesa pytest
28+
# run: pip install mesa[network] pytest
2929
# - name: Test with pytest
3030
# run: pytest -rA -Werror test_examples.py
3131

@@ -39,7 +39,7 @@ jobs:
3939
python-version: "3.12"
4040
- name: Install dependencies
4141
run: |
42-
pip install mesa --pre
42+
pip install mesa[network] --pre
4343
pip install .[test]
4444
- name: Test with pytest
4545
run: pytest -rA -Werror -Wdefault::FutureWarning test_examples.py
@@ -55,6 +55,6 @@ jobs:
5555
- name: Install dependencies
5656
run: |
5757
pip install .[test]
58-
pip install -U git+https://github.com/projectmesa/mesa@main#egg=mesa
58+
pip install -U git+https://github.com/projectmesa/mesa@main#egg=mesa[network]
5959
- name: Test with pytest
6060
run: pytest -rA -Werror -Wdefault::FutureWarning test_examples.py

examples/aco_tsp/aco_tsp/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def step(self):
233233
"""
234234
A model step. Used for activating the agents and collecting data.
235235
"""
236-
self.agents.shuffle().do("step")
236+
self.agents.shuffle_do("step")
237237
self.update_pheromone()
238238

239239
# Check len of cities visited by an agent

examples/aco_tsp/app.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import solara
77
from aco_tsp.model import AcoTspModel, TSPGraph
88
from matplotlib.figure import Figure
9-
from mesa.visualization import SolaraViz
9+
from mesa.visualization import SolaraViz, make_plot_measure
1010

1111

1212
def circle_portrayal_example(agent):
@@ -35,6 +35,8 @@ def circle_portrayal_example(agent):
3535
},
3636
}
3737

38+
model = AcoTspModel()
39+
3840

3941
def make_graph(model):
4042
fig = Figure()
@@ -55,7 +57,7 @@ def make_graph(model):
5557
edge_color="gray",
5658
)
5759

58-
solara.FigureMatplotlib(fig)
60+
return solara.FigureMatplotlib(fig)
5961

6062

6163
def ant_level_distances(model):
@@ -67,10 +69,8 @@ def ant_level_distances(model):
6769

6870

6971
page = SolaraViz(
70-
AcoTspModel,
71-
model_params,
72-
space_drawer=None,
73-
measures=["best_distance_iter", "best_distance", make_graph],
74-
agent_portrayal=circle_portrayal_example,
72+
model,
73+
components=[make_plot_measure(["best_distance_iter", "best_distance"]), make_graph],
74+
model_params=model_params,
7575
play_interval=1,
7676
)

examples/bank_reserves/bank_reserves/model.py

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

156156
def step(self):
157157
# tell all the agents in the model to run their step function
158-
self.agents.shuffle().do("step")
158+
self.agents.shuffle_do("step")
159159
# collect data
160160
self.datacollector.collect(self)
161161

examples/bank_reserves/batch_run.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,78 @@ def track_run(model):
3737
return model.uid
3838

3939

40+
class BankReservesModel(mesa.Model):
41+
# id generator to track run number in batch run data
42+
id_gen = itertools.count(1)
43+
44+
# grid height
45+
grid_h = 20
46+
# grid width
47+
grid_w = 20
48+
49+
"""init parameters "init_people", "rich_threshold", and "reserve_percent"
50+
are all set via Slider"""
51+
52+
def __init__(
53+
self,
54+
height=grid_h,
55+
width=grid_w,
56+
init_people=2,
57+
rich_threshold=10,
58+
reserve_percent=50,
59+
):
60+
super().__init__()
61+
self.uid = next(self.id_gen)
62+
self.height = height
63+
self.width = width
64+
self.init_people = init_people
65+
66+
self.grid = mesa.space.MultiGrid(self.width, self.height, torus=True)
67+
# rich_threshold is the amount of savings a person needs to be considered "rich"
68+
self.rich_threshold = rich_threshold
69+
self.reserve_percent = reserve_percent
70+
# see datacollector functions above
71+
self.datacollector = mesa.DataCollector(
72+
model_reporters={
73+
"Rich": get_num_rich_agents,
74+
"Poor": get_num_poor_agents,
75+
"Middle Class": get_num_mid_agents,
76+
"Savings": get_total_savings,
77+
"Wallets": get_total_wallets,
78+
"Money": get_total_money,
79+
"Loans": get_total_loans,
80+
"Model Params": track_params,
81+
"Run": track_run,
82+
},
83+
agent_reporters={"Wealth": "wealth"},
84+
)
85+
86+
# create a single bank object for the model
87+
self.bank = Bank(self, self.reserve_percent)
88+
89+
# create people for the model according to number of people set by user
90+
for i in range(self.init_people):
91+
# set x coordinate as a random number within the width of the grid
92+
x = self.random.randrange(self.width)
93+
# set y coordinate as a random number within the height of the grid
94+
y = self.random.randrange(self.height)
95+
p = Person(i, (x, y), self, True, self.bank, self.rich_threshold)
96+
# place the Person object on the grid at coordinates (x, y)
97+
self.grid.place_agent(p, (x, y))
98+
99+
self.running = True
100+
101+
def step(self):
102+
# collect data
103+
self.datacollector.collect(self)
104+
# tell all the agents in the model to run their step function
105+
self.agents.shuffle_do("step")
106+
107+
def run_model(self):
108+
for i in range(self.run_time):
109+
self.step()
110+
111+
40112
# parameter lists for each parameter to be tested in batch run
41113
br_params = {
42114
"init_people": [25, 100],

examples/boid_flockers/app.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from boid_flockers.model import BoidFlockers
2-
from mesa.visualization import SolaraViz
2+
from mesa.visualization import SolaraViz, make_space_matplotlib
33

44

55
def boid_draw(agent):
@@ -15,11 +15,12 @@ def boid_draw(agent):
1515
"separation": 2,
1616
}
1717

18+
model = BoidFlockers(100, 100, 100, 5, 10, 2)
19+
1820
page = SolaraViz(
19-
model_class=BoidFlockers,
21+
model,
22+
[make_space_matplotlib(agent_portrayal=boid_draw)],
2023
model_params=model_params,
21-
measures=[],
2224
name="BoidFlockers",
23-
agent_portrayal=boid_draw,
2425
)
2526
page # noqa

examples/boid_flockers/boid_flockers/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,4 @@ def make_agents(self):
143143
self.space.place_agent(boid, pos)
144144

145145
def step(self):
146-
self.agents.shuffle().do("step")
146+
self.agents.shuffle_do("step")

examples/boltzmann_wealth_model/boltzmann_wealth_model/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __init__(self, N=100, width=10, height=10):
4040
self.datacollector.collect(self)
4141

4242
def step(self):
43-
self.agents.shuffle().do("step")
43+
self.agents.shuffle_do("step")
4444
# collect data
4545
self.datacollector.collect(self)
4646

examples/boltzmann_wealth_model_experimental/app.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
from mesa.visualization import SolaraViz, make_plot_measure, make_space_matplotlib
1+
from mesa.visualization import (
2+
SolaraViz,
3+
make_plot_measure,
4+
make_space_matplotlib,
5+
)
26
from model import BoltzmannWealthModel
37

48

@@ -29,9 +33,9 @@ def agent_portrayal(agent):
2933

3034
# Create visualization elements. The visualization elements are solara components
3135
# that receive the model instance as a "prop" and display it in a certain way.
32-
# Under the hood these are just functions that receive the model instance.
33-
# You can also author your own visualization elements, they just have to return
34-
# a valid solara component or an ipywidget.
36+
# Under the hood these are just classes that receive the model instance.
37+
# You can also author your own visualization elements, which can also be functions
38+
# that receive the model instance and return a valid solara component.
3539
SpaceGraph = make_space_matplotlib(agent_portrayal)
3640
GiniPlot = make_plot_measure("Gini")
3741

examples/boltzmann_wealth_model_experimental/model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __init__(self, N=100, width=10, height=10):
4040
self.datacollector.collect(self)
4141

4242
def step(self):
43-
self.agents.shuffle().do("step")
43+
self.agents.shuffle_do("step")
4444
# collect data
4545
self.datacollector.collect(self)
4646

0 commit comments

Comments
 (0)