import mesa
import solara
from matplotlib.figure import Figure
from mesa.visualization import SolaraViz, make_plot_component, make_space_component
from mesa.visualization.utils import update_counter
# Import the local MoneyModel.py
from MoneyModel import MoneyModel
def agent_portrayal(agent):
size = 10
color = "tab:red"
if agent.wealth > 0:
size = 50
color = "tab:blue"
return {"size": size, "color": color}
@solara.component
def Histogram(model):
update_counter.get() # This is required to update the counter
# Note: you must initialize a figure using this method instead of
# plt.figure(), for thread safety purpose
fig = Figure()
ax = fig.subplots()
wealth_vals = [agent.wealth for agent in model.agents]
# Note: you have to use Matplotlib's OOP API instead of plt.hist
# because plt.hist is not thread-safe.
ax.hist(wealth_vals, bins=10)
solara.FigureMatplotlib(fig)
model_params = {
"n": {
"type": "SliderInt",
"value": 50,
"label": "Number of agents:",
"min": 10,
"max": 100,
"step": 1,
},
"width": 10,
"height": 10,
}
# Create initial model instance
model1 = MoneyModel(50, 10, 10)
SpaceGraph = make_space_component(agent_portrayal)
GiniPlot = make_plot_component("Gini")
#Create the Dashboard
page = SolaraViz(
model1,
components=[SpaceGraph, GiniPlot, Histogram],
model_params=model_params,
name="Boltzmann Wealth Model",
)
# This is required to render the visualization in the Jupyter notebook
page