-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
130 lines (119 loc) · 4.53 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import solara
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from strategic_voting import VotingModel, preferences
from mesa.experimental import JupyterViz
colors = ["#fabbbb", "#f78f8f", "#f75c5c", "#f52a2a", "#ff0000", "#000000",
"#023cf7", "#2a5af7", "#4f77f7", "#7997f7", "#93abfa"]
def agent_portrayal(agent):
#portrayal = {"Shape": "circle", "Filled": "true", "r": 0.5}
color = colors[preferences.index(agent.preference)]
return {
"shape": "square",
"color": color,
"size": 30,
}
def scale(model):
fig = Figure()
ax = fig.subplots()
scale = mpimg.imread("scale.png")
ax.axis('off')
imgplot = ax.imshow(scale)
ax.set_title("Representation of the Parties")
solara.FigureMatplotlib(fig)
def piechart(model):
fig = Figure()
ax = fig.subplots()
data = model.datacollector.get_model_vars_dataframe()
#print(data)
if data.size > 0:
data = data.iloc[-1]
# Votes are in the first 11 column.
data = data[:11]
#print(data)
ax.pie(data.values, labels=data.index, colors=colors, autopct='%1.1f%%')
ax.set_title("Voter Distribution")
solara.FigureMatplotlib(fig)
def time_series(model):
fig = Figure()
ax = fig.subplots()
data = model.datacollector.get_model_vars_dataframe()
#print(data.columns.tolist())
if data.size > 0:
for i, p in enumerate(preferences):
ax.plot(data.iloc[:, i].values, color=colors[i])
ax.set_title("Evolution of Popularity of Preferences")
solara.FigureMatplotlib(fig)
def outcomes(model):
fig = Figure()
ax = fig.subplots()
data = model.datacollector.get_model_vars_dataframe()
if data.size > 0:
data = data.iloc[:, -1]
for key in data[0]:
if key != "left_winning_sincere" and key != "right_winning_sincere":
series = [gen[key] for gen in data.values]
ax.plot(series, label=key)
ax.set_title("Evolution of Voter Behaviour (Detailed)")
ax.legend()
solara.FigureMatplotlib(fig)
def sincere_strategic(model):
fig = Figure()
ax = fig.subplots()
data = model.datacollector.get_model_vars_dataframe()
if data.size > 0:
data = data.iloc[:, -1]
# tie
# left_winning_sincere
# left_unsuc_sincere
# right_winning_sincere
# right_unsuc_sincere
# ---
# left_suc_strategic
# left_unsuc_best_try
# right_suc_strategic
# right_unsuc_best_try
# ---
# right_unsuc_random
# left_unsuc_random
tie = [gen["tie"] for gen in data.values]
left_winning_sincere = [gen["left_winning_sincere"] for gen in data.values]
left_unsuc_sincere = [gen["left_unsuc_sincere"] for gen in data.values]
right_winning_sincere = [gen["right_winning_sincere"] for gen in data.values]
right_unsuc_sincere = [gen["right_unsuc_sincere"] for gen in data.values]
sincere_list = [tie, left_winning_sincere, left_unsuc_sincere,
right_winning_sincere, right_unsuc_sincere]
left_suc_strategic = [gen["left_suc_strategic"] for gen in data.values]
left_unsuc_best_try = [gen["left_unsuc_best_try"] for gen in data.values]
right_suc_strategic = [gen["right_suc_strategic"] for gen in data.values]
right_unsuc_best_try = [gen["right_unsuc_best_try"] for gen in data.values]
strategic_list = [left_suc_strategic, left_unsuc_best_try,
right_suc_strategic, right_unsuc_best_try]
sincere = [sum(i) for i in zip(*sincere_list)]
strategic = [sum(i) for i in zip(*strategic_list)]
print("Sincere", sincere)
print("Strategic", strategic)
ax.plot(sincere, label="Sincere votes")
ax.plot(strategic, label="Strategic votes")
ax.set_title("Evolution of Voter Behaviour (Summary)")
ax.legend()
solara.FigureMatplotlib(fig)
model_params = {
"width": {
"type": "SliderInt",
"value": 40,
"label": "Grid Width: ",
"min": 10,
"max": 100,
"step": 5,
}
}
page = JupyterViz(
VotingModel,
model_params,
#measures=["-1", "-3", "-5", "-7", "-9", "0", "1", "3", "5", "7", "9"],
measures=[scale, piechart, time_series, outcomes, sincere_strategic],
name="Strategic Voting Model",
agent_portrayal=agent_portrayal,
)