-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.py
276 lines (246 loc) · 9.24 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
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
from werkzeug.serving import run_simple
import dash
from plotly.subplots import make_subplots
from dash.dependencies import Input, Output, State
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import numpy as np
import pandas as pd
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import requests
import state
import json
API_URL = "http://0.0.0.0:8000"
available_team = [1, 2, 3, 4, 5]
sim_index = 0
external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"]
external_stylesheets = [
dbc.themes.BOOTSTRAP,
"https://codepen.io/chriddyp/pen/bWLwgP.css",
]
app = dash.Dash(
__name__,
external_stylesheets=external_stylesheets,
meta_tags=[{"name": "viewport", "content": "width=device-width"}],
)
app.layout = html.Div(
[
dbc.Row(
[
html.Button("Start Simulation", id="start", n_clicks=0),
html.Button("Stop Simulation", id="stop", n_clicks=0),
html.Button("Reset Simulation", id="reset", n_clicks=0),
html.Button("Reset Team", id="reset-team", n_clicks=0),
dbc.Col(
dcc.Dropdown(
id="dropdown",
options=[
{"label": "Light", "value": "light"},
{"label": "Water", "value": "water"},
{"label": "Heat", "value": "heat"},
],
value="light",
),
width={"size": 1},
),
dcc.Input(
id="index",
type="number",
placeholder="Index",
min=0,
step=1,
debounce=True,
),
dcc.Input(
id="team",
type="number",
placeholder="Team",
min=1,
max=5,
step=1,
debounce=True,
),
html.Button("Update Tableau", id="update-tableau", n_clicks=0),
html.Div(id="updated"),
]
),
dcc.Interval(
id="interval-component", interval=2000, n_intervals=0 # in milliseconds
),
dbc.Row(
[
dbc.Col(dcc.Graph(id="live-action-graph"), width={"size": 6}),
dbc.Col(dcc.Graph(id="live-reward-graph"), width={"size": 6}),
]
),
dbc.Row(
dbc.Col(dcc.Graph(id="live-obs-graph"), width={"size": 10, "offset": 1}),
style={"height": "50vh"},
),
]
)
@app.callback(
dash.dependencies.Output("interval-component", "disabled"),
[
dash.dependencies.Input("stop", "n_clicks"),
dash.dependencies.Input("start", "n_clicks"),
],
)
def start_stop_live(start, stop):
ctx = dash.callback_context
if ctx.triggered[0]["prop_id"].split(".")[0] == "start":
return False
return True
@app.callback(
Output("updated", "children"),
[Input("update-tableau", "n_clicks"), Input("dropdown", "value")],
)
def update_data_tableau(n, cat):
ctx = dash.callback_context
if ctx.triggered[0]["prop_id"].split(".")[0] == "update-tableau":
requests.get(API_URL + "/{}/updatetableau".format(cat))
return "Updated"
return ""
def reset_graph(cat, reset_team=False):
state.actions = {}
for c in state.c1:
state.actions[c] = {}
for category in state.c2:
state.actions[c][category] = [0]
state.rewards = {}
for c in state.c1:
state.rewards[c] = {}
for category in state.c2:
if category != "orginal":
state.rewards[c][category] = [0]
state.obs = pd.DataFrame(columns=state.params)
# call api to reset
if reset_team:
r = requests.get(API_URL + "/{}/reset_team".format(cat))
if r.status_code != 200:
raise Exception("status response is not 200")
obs = r.json()["data"][0]
else:
r = requests.get(API_URL + "/{}/reset".format(cat))
if r.status_code != 200:
raise Exception("status response is not 200")
obs = r.json()["data"][0]
# print(obs)
# call api to make predictions
payload = json.dumps({k: v for (k, v) in zip(state.params, obs)})
# print(payload)
r = requests.post(API_URL + "/{}/predict".format(cat), data=payload)
if r.status_code != 200:
raise Exception("status response is not 200")
state.actions[cat]["predicted"].append(r.json()["data"][0])
fig1 = go.Figure()
fig1.add_trace(
go.Scatter(y=state.actions[cat]["predicted"], name="predicted actions")
)
fig1.add_trace(go.Scatter(y=state.actions[cat]["random"], name="random actions"))
fig1.add_trace(go.Scatter(y=state.actions[cat]["original"], name="orignal actions"))
fig2 = go.Figure()
fig2.add_trace(
go.Scatter(y=state.rewards[cat]["predicted"], name="reward predicted actions")
)
fig2.add_trace(
go.Scatter(y=state.rewards[cat]["random"], name="reward random actions")
)
data = [
go.Scatter(y=state.obs[col], name=col) for col in state.params if col != "time"
]
fig3 = go.Figure(data=data)
return fig1, fig2, fig3
@app.callback(
Output("live-action-graph", "figure"),
Output("live-reward-graph", "figure"),
Output("live-obs-graph", "figure"),
[
Input("interval-component", "n_intervals"),
Input("reset", "n_clicks"),
Input("reset-team", "n_clicks"),
Input("index", "value"),
Input("team", "value"),
Input("dropdown", "value"),
],
)
def update_graph(n, reset, reset_team, index, team, cat):
ctx = dash.callback_context
# reset graph
if ctx.triggered[0]["prop_id"].split(".")[0] == "reset":
return reset_graph(cat)
if ctx.triggered[0]["prop_id"].split(".")[0] == "dropdown":
return reset_graph(cat)
if ctx.triggered[0]["prop_id"].split(".")[0] == "reset-team":
return reset_graph(cat, reset_team=True)
if (
ctx.triggered[0]["prop_id"].split(".")[0] == "index"
or ctx.triggered[0]["prop_id"].split(".")[0] == "team"
):
if not team:
team = 0
if not index:
index = 0
fig = reset_graph(cat)
payload = json.dumps({"team": team, "index": index})
requests.post(API_URL + "/{}/teamnindex".format(cat), data=payload)
return fig
for i in range(5):
payload = json.dumps({"action": state.actions[cat]["predicted"][-1]})
# print(payload)
r = requests.post(API_URL + "/{}/environment".format(cat), data=payload)
if r.status_code != 200:
raise Exception("status response is not 200")
data = r.json()["data"][0]
obs = data["obs"]
reward = data["reward"]
obs_series = pd.Series(obs, index=state.params)
state.obs = state.obs.append(obs_series, ignore_index=True)
# print(state.obs)
# print(reward)
state.rewards[cat]["predicted"].append(
reward + state.rewards[cat]["predicted"][-1]
)
# call api to make predictions
payload = json.dumps({k: v for (k, v) in zip(state.params, obs)})
r = requests.post(API_URL + "/{}/predict".format(cat), data=payload)
if r.status_code != 200:
raise Exception("status response is not 200")
state.actions[cat]["predicted"].append(r.json()["data"][0])
r = requests.get(API_URL + "/{}/randomstep".format(cat))
if r.status_code != 200:
raise Exception("status response is not 200")
data = r.json()["data"][0]
state.actions[cat]["random"].append(data["randomaction"])
state.rewards[cat]["random"].append(
data["randomreward"] + state.rewards[cat]["random"][-1]
)
state.actions[cat]["original"].append(data["action"])
# call api to get random action random reward and orignal action
# print(state.actions[cat])
# print(state.rewards_assim)
fig1 = go.Figure()
fig1.add_trace(
go.Scatter(y=state.actions[cat]["predicted"], name="predicted actions")
)
fig1.add_trace(go.Scatter(y=state.actions[cat]["random"], name="random actions"))
fig1.add_trace(go.Scatter(y=state.actions[cat]["original"], name="orignal actions"))
fig2 = go.Figure()
fig2.add_trace(
go.Scatter(y=state.rewards[cat]["predicted"], name="reward predicted actions")
)
fig2.add_trace(
go.Scatter(y=state.rewards[cat]["random"], name="reward random actions")
)
data = [
go.Scatter(y=state.obs[col], name=col) for col in state.params if col != "time"
]
fig3 = go.Figure(data=data)
return fig1, fig2, fig3
server = app.server
app.title = "AGC | RL"
if __name__ == "__main__":
run_simple("0.0.0.0", 8050, app.server, use_debugger=True)