-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
90 lines (75 loc) · 2.64 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# solara/jupyterviz app
from mesa.experimental import JupyterViz
import pandas as pd
import altair as alt
import solara
from simulatingrisk.hawkdove.model import HawkDoveSingleRiskModel
from simulatingrisk.hawkdove.server import (
agent_portrayal,
jupyterviz_params,
draw_hawkdove_agent_space,
)
def plot_wealth(model):
"""histogram plot of agent wealth levels across risk levels;
for use with jupyterviz/solara"""
# generate a histogram of points across risk levels
risk_wealth = [(agent.risk_level, agent.points) for agent in model.schedule.agents]
df = pd.DataFrame(risk_wealth, columns=["risk_level", "wealth"])
chart = (
alt.Chart(df)
.mark_bar()
.encode(y="wealth", x=alt.X("risk_level", title="risk attitude"))
)
return solara.FigureAltair(chart)
def plot_hawks(model):
"""plot percent of agents who chose hawk over last several rounds;
for use with jupyterviz/solara"""
model_df = model.datacollector.get_model_vars_dataframe().reset_index()
# limit to last N rounds (how many ?)
last_n_rounds = model_df.tail(50)
# determine domain of the chart;
# starting domain 0-50 so it doesn't jump / expand as much
max_index = max(model_df.last_valid_index() or 0, 50)
min_index = max(max_index - 50, 0)
bar_chart = (
alt.Chart(last_n_rounds)
.mark_bar(color="orange")
.encode(
x=alt.X(
"index", title="Step", scale=alt.Scale(domain=[min_index, max_index])
),
y=alt.Y(
"percent_hawk",
title="Percent who chose hawk",
scale=alt.Scale(domain=[0, 1]),
),
)
)
# graph rolling average as a line over the bar chart,
# once we have enough rounds
if model_df.rolling_percent_hawk.any():
line = (
alt.Chart(last_n_rounds)
.mark_line(color="blue")
.encode(
x=alt.X("index", title="Step"),
y=alt.Y(
"rolling_percent_hawk",
title="% hawk (rolling average)",
scale=alt.Scale(domain=[0, 1]),
),
)
)
# add the rolling average line on top of the bar chart
bar_chart += line
return solara.FigureAltair(bar_chart)
page = JupyterViz(
HawkDoveSingleRiskModel,
jupyterviz_params,
measures=[plot_hawks],
name="Hawk/Dove game with risk attitudes; all agents have the same risk attitude",
agent_portrayal=agent_portrayal,
space_drawer=draw_hawkdove_agent_space,
)
# required to render the visualization with Jupyter/Solara
page