Skip to content

fix(examples): bank_reserves #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions examples/bank_reserves/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from bank_reserves.agents import Person
from bank_reserves.model import BankReservesModel
from mesa.visualization import (
SolaraViz,
make_plot_component,
make_space_component,
)
from mesa.visualization.user_param import (
Slider,
)

# The colors here are taken from Matplotlib's tab10 palette
# Green
RICH_COLOR = "#2ca02c"
# Red
POOR_COLOR = "#d62728"
# Blue
MID_COLOR = "#1f77b4"


def person_portrayal(agent):
if agent is None:
return

portrayal = {}

# update portrayal characteristics for each Person object
if isinstance(agent, Person):
portrayal["Shape"] = "circle"
portrayal["r"] = 0.5
portrayal["Layer"] = 0
portrayal["Filled"] = "true"

color = MID_COLOR

# set agent color based on savings and loans
if agent.savings > agent.model.rich_threshold:
color = RICH_COLOR
if agent.savings < 10 and agent.loans < 10:
color = MID_COLOR
if agent.loans > 10:
color = POOR_COLOR

portrayal["color"] = color

return portrayal


def post_process_space(ax):
ax.set_aspect("equal")
ax.set_xticks([])
ax.set_yticks([])


def post_process_lines(ax):
ax.legend(loc="center left", bbox_to_anchor=(1, 0.9))


# dictionary of user settable parameters - these map to the model __init__ parameters
model_params = {
"init_people": Slider(
"People",
25,
1,
200,
# description="Initial Number of People"
),
"rich_threshold": Slider(
"Rich Threshold",
10,
1,
20,
# description="Upper End of Random Initial Wallet Amount",
),
"reserve_percent": Slider(
"Reserves",
50,
1,
100,
# description="Percent of deposits the bank has to hold in reserve",
),
}

space_component = make_space_component(
person_portrayal,
draw_grid=False,
post_process=post_process_space,
)
lineplot_component = make_plot_component(
{"Rich": RICH_COLOR, "Poor": POOR_COLOR, "Middle Class": MID_COLOR},
post_process=post_process_lines,
)
model = BankReservesModel()

page = SolaraViz(
model,
components=[space_component, lineplot_component],
model_params=model_params,
name="Bank Reserves Model",
)
page # noqa
90 changes: 0 additions & 90 deletions examples/bank_reserves/bank_reserves/server.py

This file was deleted.

4 changes: 2 additions & 2 deletions examples/bank_reserves/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
itertools
mesa~=2.0
mesa[viz]>=3.1.4
networkx
numpy
pandas
3 changes: 0 additions & 3 deletions examples/bank_reserves/run.py

This file was deleted.

Loading