-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_suppy_chain.py
221 lines (199 loc) · 6.37 KB
/
main_suppy_chain.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
import pandas as pd
import numpy as np
# import submodules
import supply_chain.data_generation as data
import visualization
from help import (
create_directories,
create_data_frame,
incremental_range,
save_to_json,
save_to_json_d,
below_time_limit,
process_results,
print_log_message,
save_results,
)
from supply_chain.run_gurobipy import run_gurobi, run_fast_gurobi
from supply_chain.run_gams import run_gams
from supply_chain.run_pyomo import run_pyomo, run_fast_pyomo
from supply_chain.run_jump import run_julia
############## Experiment ##########################
def run_experiment(
cardinality_of_i, cardinality_of_j, solve, repeats, number, time_limit
):
np.random.seed(13)
# create empty frames for results
df_jump = create_data_frame()
df_fast_jump = create_data_frame()
df_pyomo = create_data_frame()
df_fast_pyomo = create_data_frame()
df_gurobi = create_data_frame()
df_fast_gurobi = create_data_frame()
df_gams = create_data_frame()
# define the x axis
N = list(incremental_range(50, cardinality_of_i + 1, 50, 50))
# create fixed data and convert to tuples and dicts
J, K, L, M = data.create_fixed_data(m=cardinality_of_j)
# save data to json for JuMP
save_to_json(N, "N", "", "supply_chain")
save_to_json(L, "L", "", "supply_chain")
save_to_json(M, "M", "", "supply_chain")
# run experiment for every n in |I|
for n in N:
# create variable data and convert to tuples
(
I,
ik_tuple,
il_tuple,
im_tuple,
ijk_tuple,
ikl_tuple,
ilm_tuple,
d_dict,
) = data.create_variable_data(n=n, J=J, K=K, L=L, M=M)
# make dictionaries
IK_IJK, IK_IKL, IL_IKL, IL_ILM, IM_ILM = data.data_to_dicts(
ik_tuple, il_tuple, im_tuple, ijk_tuple, ikl_tuple, ilm_tuple
)
# save data to json for JuMP
save_to_json(ik_tuple, "IK", f"_{n}", "supply_chain")
save_to_json(il_tuple, "IL", f"_{n}", "supply_chain")
save_to_json(im_tuple, "IM", f"_{n}", "supply_chain")
save_to_json(ijk_tuple, "IJK", f"_{n}", "supply_chain")
save_to_json(ikl_tuple, "IKL", f"_{n}", "supply_chain")
save_to_json(ilm_tuple, "ILM", f"_{n}", "supply_chain")
save_to_json_d(d_dict, "D", f"_{n}", "supply_chain")
# GurobiPy
if below_time_limit(df_gurobi, time_limit):
rr = run_gurobi(
I=I,
ik=ik_tuple,
il=il_tuple,
im=im_tuple,
ijk=ijk_tuple,
ikl=ikl_tuple,
ilm=ilm_tuple,
D=d_dict,
solve=solve,
repeats=repeats,
number=number,
)
df_gurobi = process_results(rr, df_gurobi)
print_log_message(
language="GurobiPy", n=n, df=df_gurobi
)
# Fast GurobiPy
if below_time_limit(df_fast_gurobi, time_limit):
rr = run_fast_gurobi(
I=I,
ik=ik_tuple,
il=il_tuple,
im=im_tuple,
ijk=ijk_tuple,
ikl=ikl_tuple,
ilm=ilm_tuple,
ik_ijk=IK_IJK,
ik_ikl=IK_IKL,
il_ikl=IL_IKL,
il_ilm=IL_ILM,
im_ilm=IM_ILM,
D=d_dict,
solve=solve,
repeats=repeats,
number=number,
)
df_fast_gurobi = process_results(rr, df_fast_gurobi)
print_log_message(language="Fast GurobiPy", n=n, df=df_fast_gurobi)
# GAMS
if below_time_limit(df_gams, time_limit):
rr = run_gams(
I=I,
J=J,
K=K,
L=L,
M=M,
IK=ik_tuple,
IL=il_tuple,
IM=im_tuple,
IJK=ijk_tuple,
IKL=ikl_tuple,
ILM=ilm_tuple,
D=d_dict,
solve=solve,
N=n,
repeats=repeats,
number=number,
)
df_gams = process_results(rr, df_gams)
print_log_message(language="GAMS", n=n, df=df_gams)
# Pyomo
if below_time_limit(df_pyomo, time_limit):
rr = run_pyomo(
I=I,
IK=ik_tuple,
IL=il_tuple,
IM=im_tuple,
IJK=ijk_tuple,
IKL=ikl_tuple,
ILM=ilm_tuple,
D=d_dict,
solve=solve,
repeats=repeats,
number=number,
)
df_pyomo = process_results(rr, df_pyomo)
print_log_message(language="Pyomo", n=n, df=df_pyomo)
# Fast Pyomo
if below_time_limit(df_fast_pyomo, time_limit):
rr = run_fast_pyomo(
I=I,
IK=ik_tuple,
IL=il_tuple,
IM=im_tuple,
IJK=ijk_tuple,
IKL=ikl_tuple,
ILM=ilm_tuple,
IK_IJK=IK_IJK,
IK_IKL=IK_IKL,
IL_IKL=IL_IKL,
IL_ILM=IL_ILM,
IM_ILM=IM_ILM,
D=d_dict,
solve=solve,
repeats=repeats,
number=number,
)
df_fast_pyomo = process_results(rr, df_fast_pyomo)
print_log_message(language="Fast Pyomo", n=n, df=df_fast_pyomo)
# JuMP
df_fast_jump, df_jump = run_julia(solve, repeats, number, time_limit)
# merge all results
df = pd.concat(
[
df_jump,
df_fast_jump,
df_pyomo,
df_fast_pyomo,
df_gurobi,
df_fast_gurobi,
df_gams
]
).reset_index(drop=True)
# save results
save_results(df, solve, "supply_chain")
# plot results
visualization.plot_results(df, cardinality_of_j, solve, "supply_chain")
if __name__ == "__main__":
CI = 8000
CJ = 20
create_directories("supply_chain")
for solve in [False, True]:
run_experiment(
cardinality_of_i=CI,
cardinality_of_j=CJ,
solve=solve,
repeats=4,
number=1,
time_limit=5,
)