-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_dash.py
434 lines (391 loc) · 17.2 KB
/
main_dash.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import platform
import matplotlib
if platform.system().lower().startswith('win'):
matplotlib.use('tkagg')
from matplotlib.pyplot import title
from Simulator import Simulator
from Player import Player
from Strategies import *
from Taskrunner import SimulatorProcess, ProcessMsg
import time
from test_simulator import *
from dash.dependencies import Input, Output, State, MATCH, ALL
import plotly.express as px
import pandas as pd
from dash.exceptions import PreventUpdate
import typing
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objects as go
import multiprocessing as mp
import numpy as np
from queue import Empty
from analysis import vis_dpc, vis_poo, Strategies
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
fig = go.Figure()
poo_plot = go.Figure()
dpc_plot = go.Figure()
cd_plot = go.Figure()
cvc_plot = go.Figure()
ppc_plot = go.Figure()
colorscales = px.colors.named_colorscales()
task_queue = None
res_queue = None
app.layout = html.Div(
className="main_app",
children=[
# This feels really dirty but how can I have dash callback without an output? Callbacks are output dependend...but you can have a dummy one just add a hidden p or div and return whatever
dcc.Store(id='local_start', storage_type='local'),
# Local Store to keep the Simulator
dcc.Store(id='local_stop', storage_type='local'),
# Local Store to keep the Simulator
dcc.Store(id='local_toggle', storage_type='local'),
html.H1(
children='Controversies in Game Theory 2021 - Repeated Prisoners Dilemma'),
html.P(children='''
Made by Fabrice Egger, Robin Staab, Jan Urech, Kim Nik Baumgartner, Nando Käslin'''),
html.P(children=''' If you have any troubles don't hesitate to open a github issue :)
'''),
html.Div(
className="content",
children=[
html.Div(className="header", children=[
html.Div(className="sliders", children=[
html.Div(className="strategies", children=[
html.H3(children='Strategies'),
html.Div([
html.Div(children=[
html.P(children=f"{_}"),
dcc.Input(
id={'role': 'strategy_input', 'index': _},
type="number",
placeholder=f"Number of players playing {_}",
),
dcc.Slider(
id={'role': 'strategy_slider', 'index': _},
min=0,
max=200,
step=1,
marks={
0: '0',
50: '50',
100: '100',
150: '150',
200: '200',
},
value=50,
persistence=True,
)
]) for _, n in Strategies.items()
],),
]),
html.Div(className="rewards", children=[
html.H3(
children='Regulating Rewards'),
html.Table(children=[
html.Thead(children=[
html.Tr(children=[html.Td(""), html.Td(
"Cooperate"), html.Td("Defection")])
]),
html.Tbody(children=[
html.Tr(children=[html.Td("Cooperate"), html.Td(
"R, R"), html.Td("S, T")]),
html.Tr(children=[html.Td("Defection"), html.Td(
"T, S"), html.Td("P, P")])
])
]),
html.Div(className="rewardSlider", children=[
html.P(children="T"),
dcc.Slider(
id="slider-T",
min=0,
max=40,
step=1,
marks={
1: '1',
10: '10',
20: '20',
30: '30',
40: '40',
},
value=30,
persistence=True,
),
html.P(children="R"),
dcc.Slider(
id="slider-R",
min=0,
max=40,
step=1,
marks={
1: '1',
10: '10',
20: '20',
30: '30',
40: '40',
},
value=20,
persistence=True,
),
html.P(children="P"),
dcc.Slider(
id="slider-P",
min=0,
max=40,
step=1,
marks={
1: '1',
10: '10',
20: '20',
30: '30',
40: '40',
},
value=10,
persistence=True,
),
html.P(children="S"),
dcc.Slider(
id="slider-S",
min=0,
max=40,
step=1,
marks={
1: '1',
10: '10',
20: '20',
30: '30',
40: '40'
},
value=5,
persistence=True,
)]),
])
]),
html.Div(className="simulationSliders", children=[
html.Div(children=[
html.Label("Width of Field"),
dcc.Input(
id="grid_x",
type="number",
value=20
)]),
html.Div(children=[
html.Label("Height of Field"),
dcc.Input(
id="grid_y",
type="number",
value=20,
)]),
html.Div(children=[
html.Label("Window play size"),
dcc.Input(
id="play_window",
type="number",
value=1,
), ]),
html.Div(children=[
html.Label("Migration range"),
dcc.Input(
id="travel_window",
type="number",
value=3,
), ]),
html.Div(children=[
html.Label("Random Seed"),
dcc.Input(
id="rand_seed",
type="number",
value=12345,
placeholder="Random Seed"
), ]),
html.Div(children=[
html.Label("Imitation probability"),
dcc.Input(
id="imit_prob",
type="number",
value=0.2,
min=0, max=1, step=0.01
)]),
html.Div(children=[
html.Label("Migration probability"),
dcc.Input(
id="migrate_prob",
type="number",
value=0.1,
min=0, max=1, step=0.01
)]),
html.Div(children=[
html.Label("Step Size"),
dcc.Input(
id="step_size",
type="number",
value=10,
min=1, max=100, step=1
), ]),
html.Div(children=[
html.Label("Omega probability"),
dcc.Input(
id="omega",
type="number",
value=0.9,
min=0, max=1, step=0.1
)]),
])
]),
html.Div(className="simulation", children=[
dcc.Dropdown(
id='colorscale',
options=[{"value": x, "label": x}
for x in colorscales],
value='viridis',
style={"display": "none"}
),
html.H2("Simulation"),
html.Div(className="game", children=[
html.Div(children=[
html.Div(className="controlbuttons", children=[
html.Button('Start', id='start', n_clicks=0),
html.Button('Pause', id='toggle', n_clicks=0),
html.Button('Reset', id='stop', n_clicks=0),
html.Button('Refresh', id='refresh', n_clicks=0)
]),
dcc.Graph(id='play-graph', figure=fig),
dcc.Interval(id='interval-component', interval=1000, # in milliseconds
n_intervals=0)]),
html.Div(children=[
html.Div(className="timeline",
children="Timeline"),
html.Div(className="plots", children=[
dcc.Graph(id='cd-plot', figure=cd_plot),
dcc.Graph(id='poo-plot', figure=poo_plot),
dcc.Graph(id='dpc-plot', figure=dpc_plot),
dcc.Graph(id='cvc-plot', figure=cvc_plot),
dcc.Graph(id='ppc-plot', figure=ppc_plot),
])
])
]),
html.Div(id='results-1',
children='Summary will be displayed here'),
]),
]),
])
@ app.callback(
Output('play-graph', 'figure'),
Output('cd-plot', 'figure'),
Output('poo-plot', 'figure'),
Output('dpc-plot', 'figure'),
Output('cvc-plot', 'figure'),
Output('ppc-plot', 'figure'),
[Input('interval-component', 'n_intervals')],
[State("slider-T", 'value'),
State("slider-R", 'value'),
State("slider-P", 'value'),
State("slider-S", 'value'),
State({'role': 'strategy_slider', 'index': ALL}, 'value'),
], prevent_initial_call=True)
def update_figure(n_intervals, val_T: int = 1, val_R: int = 1, val_P: int = 1, val_S: int = 1, *args: tuple):
# NOTE I just removed this for testing purposes
# inputs = [val_T, val_R, val_P, val_S] + list(args[0]) # This excludes any inputs not regulated through sliders but we can change this later if needed
try:
pot_res = res_queue.get(False) # Non-Blocking get
print(f"Update: {pot_res['epoch']} - {pot_res['strategies']}")
fig_grid = vis_grid(pot_res["grid"], pot_res['epoch'])
fig_cd = vis_cd(pot_res['df_cd'])
fig_poo = vis_poo(pot_res['df_poo'])
fig_dpc = vis_dpc(pot_res['df_dpc'])
fig_cvc = vis_cvc(pot_res['df_cvc'])
fig_ppc = vis_ppc(pot_res['df_ppc'])
#fig_poo = px.line(pot_res['df_poo'], y="res", title='Percentage of Optimum over time')
print("Done")
return fig_grid, fig_cd, fig_poo, fig_dpc, fig_cvc, fig_ppc
except Empty:
return dash.no_update
@ app.callback(
Output('local_start', 'data'),
[Input('start', 'n_clicks')],
[State("slider-T", 'value'),
State("slider-R", 'value'),
State("slider-P", 'value'),
State("slider-S", 'value'),
State("grid_x", "value"),
State("grid_y", "value"),
State("play_window", "value"),
State("travel_window", "value"),
State("imit_prob", "value"),
State("migrate_prob", "value"),
State("omega", "value"),
State("step_size", "value"),
State("rand_seed", "value"),
# NOTE Please append all fixed features before here so that we can use *args for the strategies
State({'role': 'strategy_slider', 'index': ALL}, 'value')],
prevent_initial_call=True)
def start_sim(clicks, val_T: int = 1, val_R: int = 1, val_P: int = 1, val_S: int = 1, grid_x: int = 40, grid_y: int = 40, play_window: int = 1, travel_window: int = 3, imit_prob: float = 0.8, migrate_prob: float = 0.8, omega: float = 0.9, step_size: int = 42, rand_seed: int = 42, *args: tuple):
print("Started Simulation")
strategy_counts = args[0]
strategy_names = list(Strategies.keys())
msg_dict = {
'T': val_T,
'R': val_R,
'S': val_S,
'P': val_P,
'strategies': strategy_names,
'counts': strategy_counts,
'grid_x': grid_x,
'grid_y': grid_y,
'num_players': sum(strategy_counts),
'play_window': play_window,
'migrate_window': travel_window,
'imit_prob': imit_prob,
'migrate_prob': migrate_prob,
'omega': omega,
'epochs': 10,
'step-size': step_size,
'rand_seed': rand_seed
}
task_queue.put(ProcessMsg("RESTART", msg_content=msg_dict))
return 1
@ app.callback(
Output('local_stop', 'data'),
[Input('stop', 'n_clicks')],
prevent_initial_call=True)
def stop_sim(*args):
print("Reset Simulation")
task_queue.put(ProcessMsg("RESET", msg_content=None))
return 1
@ app.callback(
Output('local_toggle', 'data'),
[Input('toggle', 'n_clicks')],
prevent_initial_call=True)
def toggle_sim(*args):
print("Toggled Simulation")
task_queue.put(ProcessMsg("TOGGLE", msg_content=None))
return 1
@ app.callback(
Output({'role': 'strategy_slider', 'index': MATCH}, 'value'),
[Input({'role': 'strategy_input', 'index': MATCH}, 'value')], prevent_initial_call=True)
def update_output(value: int):
if not value:
raise PreventUpdate
return int(value)
# This is stupid but I cant have the same output/input pair....
@ app.callback(
Output({'role': 'strategy_input', 'index': MATCH}, 'placeholder'),
[Input({'role': 'strategy_slider', 'index': MATCH}, 'value')])
def update_output(value: int):
if not value:
raise PreventUpdate
return int(value)
if __name__ == '__main__':
# Establish communication queues
task_queue = mp.Queue()
res_queue = mp.Queue()
num_servers = 1 # mp.cpu_count() * 2
print('Creating {} consumers'.format(num_servers))
consumers = [SimulatorProcess(task_queue, res_queue)
for i in range(num_servers)]
for w in consumers:
w.start()
app.run_server(debug=True)