-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
304 lines (266 loc) · 8.95 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
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
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table
from desdeo_dash import Plotter
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from desdeov2.problem.Problem import ScalarDataProblem
from desdeov2.methods.Nautilus import ENautilus
# notes: thousands separator, lot size 48th and safety stock 49th, value paths are nice, max text with arrow, days on hand, 254/inventory turnover (days on hand)
data = np.genfromtxt("./data/first_data.csv", delimiter=",")
xs = data[:, :50]
fs = data[:, 50:]
objective_names = [
"Purchasing and ordering cost",
"Holding cost",
"Cycle service level",
"Probability of product availability",
"Inventory turnoever",
]
variable_names = ["x{}".format(i + 1) for i in range(xs.shape[1])]
is_max = [False, False, True, True, True]
# scale the data
scaler = MinMaxScaler((-1, 1))
scaler.fit(np.where(is_max, -fs, fs))
fs_norm = scaler.transform(np.where(is_max, -fs, fs))
# create the problem
problem = ScalarDataProblem(xs, fs_norm)
enautilus = ENautilus(problem)
total_iters = 5
points_shown = 4
nadir, ideal = enautilus.initialize(total_iters, points_shown)
plotter = Plotter(nadir, ideal, scaler, is_max)
# this is bad!
intermediate_points = []
intermediate_ranges = []
current_best_idx = 0
previous_best = None
###
# fot the parallel axes
columns, data = plotter.make_table(
zs=np.array([nadir, ideal]),
names=objective_names,
labels=["nadir", "ideal"],
)
external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(
[
# First row
html.Div(
[
html.H3(
"E-NAUTILUS: Iterations left {}".format(enautilus.ith),
id="title",
className="row",
),
html.H4(
(
"Select the best candidate and iterate. "
"Or just iterate if first iteration."
),
className="six columns",
),
dcc.RadioItems(
id="candidate-selection",
options=[
{"label": "Candidate {}".format(ind + 1), "value": val}
for (ind, val) in enumerate(
range(len(intermediate_points))
)
],
value=-1,
labelStyle={"display": "inline-block"},
className="three columns",
),
html.Button(
id="iterate-button",
n_clicks=0,
children="ITERATE",
className="three columns",
),
],
className="row",
),
# Second row
html.Div(
[
# First column
html.Div(
[
html.H5("Spider plots"),
html.P("info", id="info"),
dcc.Graph(
id="spider-plots",
figure=plotter.spider_plot_candidates(
np.array([])
),
),
],
className="six columns",
),
# Second column
html.Div(
[
html.H5(
"Value paths (double click on first axis to show all paths)"
),
html.P("Final lot size and safety stocks will be shown "
"here when solutions are reached.", id="final"),
dcc.Graph(
id="value-paths",
figure=plotter.value_path_plot_candidates(
np.array([nadir, ideal]),
objective_names,
labels=["nadir", "ideal"],
),
),
],
className="six columns",
),
],
className="row",
),
# Third row
html.Div(
[
html.Div(
[
html.H5("Tabled candidate objective values"),
dash_table.DataTable(
id="table", columns=columns, data=data
),
],
className="row",
),
html.Div(
[
html.H5("Tabled candidate best reachable values"),
dash_table.DataTable(id="table-best"),
],
className="row",
),
],
className="row",
),
]
)
@app.callback(
[
Output("table", "style_data_conditional"),
Output("table-best", "style_data_conditional"),
Output("spider-plots", "figure"),
Output("value-paths", "figure"),
Output("info", "children"),
],
[Input("candidate-selection", "value")],
)
def highlight_table_row(candidate_index):
global intermediate_points
global intermediate_ranges
global current_best_idx
global previous_best
if candidate_index == -1:
raise PreventUpdate
# just one solution
if intermediate_points.ndim == 1:
candidate_index = 0
style = [
{
"if": {"row_index": candidate_index},
"backgroundColor": "#0000FF",
"color": "white",
}
]
if len(intermediate_points) == 0:
zs = np.array([])
else:
zs = intermediate_points
spider_plots = plotter.spider_plot_candidates(
zs,
names=objective_names,
best=intermediate_ranges,
previous=previous_best,
selection=candidate_index,
)
value_paths = plotter.value_path_plot_candidates(
zs, objective_names, selection=candidate_index
)
# original scale zs
if zs.ndim == 1:
orig_zs = scaler.inverse_transform(zs.reshape(1, -1))
else:
orig_zs = scaler.inverse_transform(zs)
orig_zs = np.where(is_max, -orig_zs, orig_zs)
info = ("Inventory turnover for currently selected candidate: {:,.2f}").format(
254 / orig_zs[candidate_index, 4]
)
return style, style, spider_plots, value_paths, info
@app.callback(
[
Output("candidate-selection", "options"),
Output("title", "children"),
Output("table", "columns"),
Output("table", "data"),
Output("table-best", "columns"),
Output("table-best", "data"),
Output("candidate-selection", "value"),
Output("final", "children")
],
[Input("iterate-button", "n_clicks")],
[State("candidate-selection", "value")],
)
def update_candidates(n_clicks, candidate_index):
global intermediate_points
global intermediate_ranges
global current_best_idx
global previous_best
if enautilus.ith == 0:
raise PreventUpdate
if n_clicks == 0:
raise PreventUpdate
if n_clicks == 1:
# first iteration, do not interact
zs, best = enautilus.iterate()
else:
previous_best = intermediate_points[current_best_idx]
current_best_idx = candidate_index
x = enautilus.interact(
intermediate_points[current_best_idx],
intermediate_ranges[current_best_idx],
)
print(x)
zs, best = enautilus.iterate()
intermediate_points = zs
intermediate_ranges = best
default_candidate = 0
options = [
{"label": "Candidate {}".format(ind + 1), "value": val}
for (ind, val) in enumerate(range(len(intermediate_points)))
]
if enautilus.ith > 1:
title = "E-NAUTILUS: Iterations left {}".format(enautilus.ith)
final = ""
elif enautilus.ith == 1:
title = "Select the final solution."
final = ""
else:
title = "Done. Final solution displayed"
final = "Lot size: {:,.2f} Safety stock: {:,.2f}".format(x[0][48], x[0][49])
default_candidate = current_best_idx
intermediate_points = zs[current_best_idx]
intermediate_ranges = best[current_best_idx]
previous_best = None
options = []
columns, data = plotter.make_table(zs=intermediate_points, names=objective_names)
columns_best, data_best = plotter.make_table(
zs=intermediate_ranges, names=objective_names, row_name=["Best reachable"]
)
return (options, title, columns, data, columns_best, data_best, default_candidate, final)
def main():
app.run_server(debug=True)
if __name__ == "__main__":
main()