-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.py
338 lines (317 loc) · 14.4 KB
/
benchmark.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
import sys
from pyomo.environ import *
from pyomo.dae import *
from pyomo.gdp import Disjunct
import pyomo.contrib.gdpopt.enumerate
from datetime import datetime
import os
import json
# =================================================================================================
# from models.three_stage_dynamic_model_switching import build_model
# example = "three_stage_dynamic_model_switching"
# timelimit = 900
# =================================================================================================
from models.three_stage_dynamic_model_switching_ordering import build_model
example = "three_stage_dynamic_model_switching"
timelimit = 900
# =================================================================================================
# from models.four_stage_dynamic_model_switching_nonlinear import build_model
# example = "four_stage_dynamic_model_switching_nonlinear"
# timelimit = 900
# =================================================================================================
# from models.five_stage_dynamic_model_switching_nonlinear import build_model
# example = "five_stage_dynamic_model_switching_nonlinear"
# timelimit = 900
# =================================================================================================
# from models.six_stage_dynamic_model_switching_nonlinear import build_model
# example = "six_stage_dynamic_model_switching_nonlinear"
# timelimit = 900
# =================================================================================================
# from models.seven_stage_dynamic_model_switching_nonlinear import build_model
# example = "seven_stage_dynamic_model_switching_nonlinear"
# timelimit = 1800
# =================================================================================================
# from models.eight_stage_dynamic_model_switching_nonlinear import build_model
# example = "eight_stage_dynamic_model_switching_nonlinear"
# timelimit = 1800
# =================================================================================================
# from models.nine_stage_dynamic_model_switching_nonlinear import build_model
# example = "nine_stage_dynamic_model_switching_nonlinear"
# timelimit = 3600
# =================================================================================================
# from models.ten_stage_dynamic_model_switching_nonlinear import build_model
# example = "ten_stage_dynamic_model_switching_nonlinear"
# timelimit = 3600
# =================================================================================================
nfe = 30
current_time = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
result_dir = 'results/' + example + '/' + 'nfe' + str(nfe) + '/' + current_time
os.makedirs(result_dir, exist_ok=True)
MIP_solver = 'gurobi'
MINLP_solvers = ['dicopt', 'knitro', 'baron']
NLP_solvers = ['ipopth', 'knitro', 'conopt', 'baron']
strategy_list = [
'gdp.bigm',
'gdp.hull',
'gdpopt.enumerate',
'gdpopt.loa',
'gdpopt.gloa',
'gdpopt.ldsda',
]
json_result = {}
def get_and_discretize_model(mode_transfer=False):
# Build the dynamic model
model = build_model(mode_transfer)
# Discretize the model using dae.collocation
discretizer = TransformationFactory('dae.collocation')
discretizer.apply_to(model, nfe=nfe, ncp=3, scheme='LAGRANGE-RADAU')
# We need to reconstruct the constraints in disjuncts after discretization.
# This is a bug in Pyomo.dae. https://github.com/Pyomo/pyomo/issues/3101
for disjunct in model.component_data_objects(ctype=Disjunct):
for constraint in disjunct.component_objects(ctype=Constraint):
constraint._constructed = False
constraint.construct()
for dxdt in model.component_data_objects(ctype=Var, descend_into=True):
if 'dxdt' in dxdt.name:
dxdt.setlb(-300)
dxdt.setub(300)
return model
stdout = sys.stdout
for strategy in strategy_list:
# Solve the dynamic optimization problem
if strategy in ['gdp.bigm', 'gdp.hull']:
for MINLP_solver in MINLP_solvers:
print('Benchmarking', strategy, MINLP_solver)
model = get_and_discretize_model()
# Reformulation
model.BigM = Suffix(direction=Suffix.LOCAL)
model.BigM[None] = 1000000
TransformationFactory(strategy).apply_to(model)
with open(
result_dir + '/' + strategy + '_' + MINLP_solver + '.log', 'w'
) as sys.stdout:
solver = SolverFactory("gams")
add_options = ['option reslim=' + str(timelimit) + ';']
if MINLP_solver == 'dicopt':
add_options.append('GAMS_MODEL.optfile=1')
add_options.append('$onecho > dicopt.opt')
add_options.append('stop 1')
add_options.append('$offecho')
if MINLP_solver == 'knitro':
add_options.append('GAMS_MODEL.optfile=1')
add_options.append('$onecho > knitro.opt')
add_options.append('mip_multistart 1')
add_options.append('$offecho')
results = solver.solve(
model, tee=True, solver=MINLP_solver, add_options=add_options
)
print(results)
sys.stdout = stdout
with open(
result_dir + '/' + strategy + '_' + MINLP_solver + '.json', 'w'
) as f:
json.dump(results.json_repn(), f)
elif strategy in ['gdpopt.loa', 'gdpopt.gloa', 'gdpopt.enumerate']:
for NLP_solver in NLP_solvers:
print('Benchmarking', strategy, NLP_solver)
model = get_and_discretize_model()
solver = SolverFactory(strategy)
with open(
result_dir + '/' + strategy + '_' + NLP_solver + '.log', 'w'
) as sys.stdout:
results = solver.solve(
model,
tee=True,
nlp_solver='gams',
nlp_solver_args=dict(solver=NLP_solver),
mip_solver=MIP_solver,
time_limit=timelimit,
)
print(results)
sys.stdout = stdout
with open(
result_dir + '/' + strategy + '_' + NLP_solver + '.json', 'w'
) as f:
json.dump(results.json_repn(), f)
elif strategy == 'gdpopt.lbb':
for MINLP_solver in MINLP_solvers:
# DICOPT does not work with gdpopt.lbb
if MINLP_solver == 'dicopt':
continue
print('Benchmarking', strategy, MINLP_solver)
model = get_and_discretize_model()
solver = SolverFactory(strategy)
with open(
result_dir + '/' + strategy + '_' + MINLP_solver + '.log', 'w'
) as sys.stdout:
results = solver.solve(
model,
tee=True,
minlp_solver='gams',
minlp_solver_args=dict(solver=MINLP_solver),
time_limit=timelimit,
)
print(results)
sys.stdout = stdout
with open(
result_dir + '/' + strategy + '_' + MINLP_solver + '.json', 'w'
) as f:
json.dump(results.json_repn(), f)
elif strategy == 'gdpopt.ldsda':
for NLP_solver in NLP_solvers:
print('Benchmarking', strategy, NLP_solver)
mode_transfer_list = [False, True]
direction_norm_list = ['L2', 'Linf']
for mode_transfer in mode_transfer_list:
for direction_norm in direction_norm_list:
model = get_and_discretize_model(mode_transfer)
solver = SolverFactory(strategy)
if mode_transfer:
if "three" in example:
continue
with open(
result_dir
+ '/'
+ strategy
+ '_'
+ NLP_solver
+ '_'
+ str(direction_norm)
+ '_mode_transfer'
+ '.log',
'w',
) as sys.stdout:
results = solver.solve(
model,
tee=True,
direction_norm=direction_norm,
minlp_solver='gams',
minlp_solver_args=dict(solver=NLP_solver),
starting_point=[1, 2],
logical_constraint_list=[
model.mode_transfer_lc1.name,
model.mode_transfer_lc2.name,
],
time_limit=timelimit,
)
print(results)
else:
if 'three' in example:
disjunction_list = [
model.d[1].name,
model.d[2].name,
model.d[3].name,
]
starting_point = [1, 1, 1]
elif 'four' in example:
disjunction_list = [
model.d[1].name,
model.d[2].name,
model.d[3].name,
model.d[4].name,
]
starting_point = [1, 2, 3, 3]
elif 'five' in example:
disjunction_list = [
model.d[1].name,
model.d[2].name,
model.d[3].name,
model.d[4].name,
model.d[5].name,
]
starting_point = [1, 2, 3, 3, 3]
elif 'six' in example:
disjunction_list = [
model.d[1].name,
model.d[2].name,
model.d[3].name,
model.d[4].name,
model.d[5].name,
model.d[6].name,
]
starting_point = [1, 2, 3, 3, 3, 3]
elif 'seven' in example:
disjunction_list = [
model.d[1].name,
model.d[2].name,
model.d[3].name,
model.d[4].name,
model.d[5].name,
model.d[6].name,
model.d[7].name,
]
starting_point = [1, 2, 3, 3, 3, 3, 3]
elif 'eight' in example:
disjunction_list = [
model.d[1].name,
model.d[2].name,
model.d[3].name,
model.d[4].name,
model.d[5].name,
model.d[6].name,
model.d[7].name,
model.d[8].name,
]
starting_point = [1, 2, 3, 3, 3, 3, 3, 3]
elif 'nine' in example:
disjunction_list = [
model.d[1].name,
model.d[2].name,
model.d[3].name,
model.d[4].name,
model.d[5].name,
model.d[6].name,
model.d[7].name,
model.d[8].name,
model.d[9].name,
]
starting_point = [1, 2, 3, 3, 3, 3, 3, 3, 3]
elif 'ten' in example:
disjunction_list = [
model.d[1].name,
model.d[2].name,
model.d[3].name,
model.d[4].name,
model.d[5].name,
model.d[6].name,
model.d[7].name,
model.d[8].name,
model.d[9].name,
model.d[10].name,
]
starting_point = [1, 2, 3, 3, 3, 3, 3, 3, 3, 3]
with open(
result_dir
+ '/'
+ strategy
+ '_'
+ NLP_solver
+ '_'
+ str(direction_norm)
+ '.log',
'w',
) as sys.stdout:
results = solver.solve(
model,
tee=True,
direction_norm=direction_norm,
minlp_solver='gams',
minlp_solver_args=dict(solver=NLP_solver),
starting_point=starting_point,
disjunction_list=disjunction_list,
time_limit=timelimit,
)
print(results)
sys.stdout = stdout
with open(
result_dir
+ '/'
+ strategy
+ '_'
+ NLP_solver
+ '_'
+ str(direction_norm)
+ '.json',
'w',
) as f:
json.dump(results.json_repn(), f)