-
Notifications
You must be signed in to change notification settings - Fork 7
/
dash_html.py
406 lines (365 loc) · 15.3 KB
/
dash_html.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
"""
This file is forked from apps/dash-clinical-analytics/app.py under the following license
MIT License
Copyright (c) 2019 Plotly
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Modifications are licensed under
Apache License, Version 2.0
(see ./LICENSE for details)
"""
from __future__ import annotations
from dash import dcc, html
from app_configs import (
CLASSICAL_TAB_LABEL,
DESCRIPTION,
DWAVE_TAB_LABEL,
MAIN_HEADER,
SCENARIOS,
SHOW_CQM,
SOLVER_TIME,
THEME_COLOR_SECONDARY,
THUMBNAIL,
)
from src.job_shop_scheduler import HybridSamplerType, SamplerType
SAMPLER_TYPES = {
SamplerType.HYBRID: "Quantum Hybrid" if SHOW_CQM else "Quantum Hybrid (NL)",
SamplerType.HIGHS: "Classical (HiGHS)",
}
HYBRID_SAMPLER_TYPES = {
HybridSamplerType.NL: "Nonlinear (NL)",
HybridSamplerType.CQM: "Constrained Quadratic Model (CQM)"
}
def description_card():
"""A Div containing dashboard title & descriptions."""
return html.Div(
id="description-card",
children=[html.H1(MAIN_HEADER), html.P(DESCRIPTION)],
)
def dropdown(
label: str, id: str, options: list, wrapper_id: str = "", wrapper_class_name: str = ""
) -> html.Div:
"""Slider element for value selection."""
return html.Div(
id=wrapper_id,
className=wrapper_class_name,
children=[
html.Label(label),
dcc.Dropdown(
id=id,
options=options,
value=options[0]["value"],
clearable=False,
searchable=False,
),
],
)
def checklist(label: str, id: str, options: list) -> html.Div:
"""Slider element for value selection."""
return html.Div([
html.Label(label),
dcc.Checklist(
id=id,
options=options,
value=[options[0]["value"]],
)
])
def generate_graph(visible: bool, type: str, index: int) -> html.Div:
"""Generates graph either hidden or visible."""
return html.Div(
id={
"type": f"gantt-chart-{'visible' if visible else 'hidden'}-wrapper",
"index": index,
},
className="graph" if visible else "display-none",
children=[
dcc.Graph(
id={"type": f"gantt-chart-{type}", "index": index},
responsive=True,
config={"displayModeBar": False},
),
],
)
def generate_solution_tab(label: str, title: str, tab: str, index: int) -> dcc.Tab:
"""Generates solution tab containing, solution graphs, sort functionality, and
problem details dropdown.
Returns:
dcc.Tab: A Tab containing the solution graph and problem details.
"""
return dcc.Tab(
label=label,
id=f"{tab}-tab",
className="tab",
value=f"{tab}-tab",
disabled=True,
children=[
html.Div(
className="solution-card",
children=[
html.Div(
className="gantt-chart-card",
children=[
html.Div(
className="gantt-heading",
children=[
html.H3(
[title, html.Span(id=f"{tab}-gantt-title-span")],
className="gantt-title",
),
html.Button(
id={"type": "gantt-heading-button", "index": index},
className="gantt-heading-button",
children="Sort by start time",
n_clicks=0,
),
],
),
html.Div(
className="graph-wrapper",
children=[
generate_graph(True, "jobsort", index),
generate_graph(False, "startsort", index),
],
),
],
),
html.Div(
[
html.Hr(),
html.Div(problem_details(tab, index), className="problem-details"),
],
className="problem-details-parent",
),
],
),
],
)
def generate_control_card() -> html.Div:
"""Generates the control card for the dashboard.
Contains the dropdowns for selecting the scenario, model, and solver.
Returns:
html.Div: A Div containing the dropdowns for selecting the scenario,
model, and solver.
"""
scenario_options = [{"label": scenario, "value": scenario} for scenario in SCENARIOS]
sampler_options = [
{"label": label, "value": sampler_type.value}
for sampler_type, label in SAMPLER_TYPES.items()
]
hybrid_sampler_options = [
{"label": label, "value": hybrid_sampler_type.value}
for hybrid_sampler_type, label in HYBRID_SAMPLER_TYPES.items()
]
return html.Div(
id="control-card",
children=[
dropdown(
"Scenario (jobs x operations)",
"scenario-select",
scenario_options,
),
checklist(
"Solver (hybrid and/or classical)",
"solver-select",
sorted(sampler_options, key=lambda op: op["value"]),
),
dropdown(
"Quantum Hybrid Solver",
"hybrid-select",
sorted(hybrid_sampler_options, key=lambda op: op["value"]),
"hybrid-select-wrapper",
"" if SHOW_CQM else "display-none",
),
html.Label("Solver Time Limit (seconds)"),
dcc.Input(
id="solver-time-limit",
type="number",
**SOLVER_TIME,
),
html.Div(
id="button-group",
children=[
html.Button(id="run-button", children="Run Optimization", n_clicks=0),
html.Button(
id="cancel-button",
children="Cancel Optimization",
n_clicks=0,
className="display-none",
),
],
),
],
)
def generate_problem_details_table(
scenario: str, solver: str, num_jobs: int, time_limit: int, num_operations: int, wall_clock_time: float
) -> html.Tbody:
"""Generate the problem details table.
Args:
scenario: The scenario that was optimized.
solver: The solver used for optimization.
num_jobs: The number of jobs in the scenario.
time_limit: The solver time limit.
num_operations: The number of operations in the scenario.
wall_clock_time: The overall time to optimize the scenario.
Returns:
html.Tbody: Tbody containing table rows for problem details.
"""
table_rows = (
("Scenario", scenario, "Solver", solver),
("Number of Jobs", num_jobs, "Solver Time Limit", f"{time_limit}s"),
("Number of Operations", num_operations, "Wall Clock Time", f"{round(wall_clock_time, 2)}s")
)
return html.Tbody([html.Tr([html.Td(cell) for cell in row]) for row in table_rows])
def problem_details(solver: str, index: int) -> html.Div:
"""Generate the problem details section.
Args:
solver: Which solver tab to generate the section for. Either "dwave" or "highs"
index: Unique element id to differentiate matching elements.
Returns:
html.Div: Div containing a collapsable table.
"""
return html.Div(
[
html.Div(
id={"type": "to-collapse-class", "index": index},
className="details-collapse-wrapper collapsed",
children=[
html.Div(
className="details-collapse-title",
children=[
html.H5(id=f"{solver}-stats-makespan", className="stats-makespan"),
html.Button(
id={"type": "collapse-trigger", "index": index},
className="details-collapse",
children=[
html.H5("Problem Details"),
html.Div(className="collapse-arrow"),
],
),
],
),
html.Div(
className="details-to-collapse",
children=[
html.Table(
className="solution-stats-table",
id=f"{solver}-solution-stats-table",
),
],
),
],
),
],
)
# set the application HTML
def set_html(app):
app.layout = html.Div(
id="app-container",
children=[
dcc.Store("running-dwave"),
dcc.Store("running-classical"),
# Banner
html.Div(id="banner", children=[html.Img(src=THUMBNAIL)]),
html.Div(
id="columns",
children=[
# Left column
html.Div(
id={"type": "to-collapse-class", "index": 0},
className="left-column",
children=[
html.Div(
[ # Fixed width Div to collapse
html.Div(
[ # Padding and content wrapper
description_card(),
generate_control_card(),
]
)
]
),
html.Div(
html.Button(
id={"type": "collapse-trigger", "index": 0},
className="left-column-collapse",
children=[html.Div(className="collapse-arrow")],
),
),
],
),
# Right column
html.Div(
id="right-column",
children=[
dcc.Tabs(
id="tabs",
value="input-tab",
children=[
dcc.Tab(
label="Input",
value="input-tab",
className="tab",
children=[
html.Div(
className="gantt-chart-card",
children=[
html.Div(
className="gantt-heading",
children=[
html.H3(
"Unscheduled Jobs and Operations",
className="gantt-title",
),
html.Button(
id={
"type": "gantt-heading-button",
"index": 0,
},
className="gantt-heading-button",
children="Show Conflicts",
n_clicks=0,
),
],
),
dcc.Loading(
id="loading-icon-input",
parent_className="graph-wrapper",
type="circle",
delay_show=300,
color=THEME_COLOR_SECONDARY,
children=[
generate_graph(True, "unscheduled", 0),
generate_graph(False, "conflicts", 0),
],
),
],
),
],
),
generate_solution_tab(
DWAVE_TAB_LABEL, "Quantum Hybrid Solver", "dwave", 1
),
generate_solution_tab(
CLASSICAL_TAB_LABEL, "Classical Solver (HiGHS)", "highs", 2
),
],
)
],
),
],
),
],
)