Skip to content

Commit b23f89c

Browse files
authored
fix(examples): bank_reserves (#267)
* fix(pip): update requirements.txt * fix(examples): update bank_reserves for mesa>=3 * fix(examples): use correct key for portrayal color
1 parent 689bae0 commit b23f89c

File tree

4 files changed

+103
-95
lines changed

4 files changed

+103
-95
lines changed

examples/bank_reserves/app.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
from bank_reserves.agents import Person
2+
from bank_reserves.model import BankReservesModel
3+
from mesa.visualization import (
4+
SolaraViz,
5+
make_plot_component,
6+
make_space_component,
7+
)
8+
from mesa.visualization.user_param import (
9+
Slider,
10+
)
11+
12+
# The colors here are taken from Matplotlib's tab10 palette
13+
# Green
14+
RICH_COLOR = "#2ca02c"
15+
# Red
16+
POOR_COLOR = "#d62728"
17+
# Blue
18+
MID_COLOR = "#1f77b4"
19+
20+
21+
def person_portrayal(agent):
22+
if agent is None:
23+
return
24+
25+
portrayal = {}
26+
27+
# update portrayal characteristics for each Person object
28+
if isinstance(agent, Person):
29+
portrayal["Shape"] = "circle"
30+
portrayal["r"] = 0.5
31+
portrayal["Layer"] = 0
32+
portrayal["Filled"] = "true"
33+
34+
color = MID_COLOR
35+
36+
# set agent color based on savings and loans
37+
if agent.savings > agent.model.rich_threshold:
38+
color = RICH_COLOR
39+
if agent.savings < 10 and agent.loans < 10:
40+
color = MID_COLOR
41+
if agent.loans > 10:
42+
color = POOR_COLOR
43+
44+
portrayal["color"] = color
45+
46+
return portrayal
47+
48+
49+
def post_process_space(ax):
50+
ax.set_aspect("equal")
51+
ax.set_xticks([])
52+
ax.set_yticks([])
53+
54+
55+
def post_process_lines(ax):
56+
ax.legend(loc="center left", bbox_to_anchor=(1, 0.9))
57+
58+
59+
# dictionary of user settable parameters - these map to the model __init__ parameters
60+
model_params = {
61+
"init_people": Slider(
62+
"People",
63+
25,
64+
1,
65+
200,
66+
# description="Initial Number of People"
67+
),
68+
"rich_threshold": Slider(
69+
"Rich Threshold",
70+
10,
71+
1,
72+
20,
73+
# description="Upper End of Random Initial Wallet Amount",
74+
),
75+
"reserve_percent": Slider(
76+
"Reserves",
77+
50,
78+
1,
79+
100,
80+
# description="Percent of deposits the bank has to hold in reserve",
81+
),
82+
}
83+
84+
space_component = make_space_component(
85+
person_portrayal,
86+
draw_grid=False,
87+
post_process=post_process_space,
88+
)
89+
lineplot_component = make_plot_component(
90+
{"Rich": RICH_COLOR, "Poor": POOR_COLOR, "Middle Class": MID_COLOR},
91+
post_process=post_process_lines,
92+
)
93+
model = BankReservesModel()
94+
95+
page = SolaraViz(
96+
model,
97+
components=[space_component, lineplot_component],
98+
model_params=model_params,
99+
name="Bank Reserves Model",
100+
)
101+
page # noqa

examples/bank_reserves/bank_reserves/server.py

Lines changed: 0 additions & 90 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
itertools
2-
mesa~=2.0
1+
mesa[viz]>=3.1.4
2+
networkx
33
numpy
44
pandas

examples/bank_reserves/run.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)