-
Notifications
You must be signed in to change notification settings - Fork 7
/
benchmark.py
111 lines (100 loc) · 3.09 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
import os
import json
import time
import sys
from datetime import datetime
from importlib import import_module
from pyomo.environ import *
def benchmark(model, strategy, timelimit, result_dir, subsolver="scip"):
"""Benchmark the model using the given strategy and subsolver.
The result files include the solver output and the JSON representation of the results.
Parameters
----------
model : PyomoModel
the model to be solved
strategy : string
the strategy used to solve the model
timelimit : int
the time limit for the solver
result_dir : string
the directory to store the benchmark results
Returns
-------
None
"""
model = model.clone()
stdout = sys.stdout
if strategy in ["gdp.bigm", "gdp.hull"]:
transformation_start_time = time.time()
TransformationFactory(strategy).apply_to(model)
transformation_end_time = time.time()
with open(
result_dir + "/" + strategy + "_" + subsolver + ".log", "w"
) as sys.stdout:
results = SolverFactory(subsolver).solve(
model, tee=True, timelimit=timelimit
)
results.solver.transformation_time = (
transformation_end_time - transformation_start_time
)
print(results)
elif strategy in [
"gdpopt.enumerate",
"gdpopt.loa",
"gdpopt.gloa",
"gdpopt.lbb",
"gdpopt.ric",
]:
with open(
result_dir + "/" + strategy + "_" + subsolver + ".log", "w"
) as sys.stdout:
results = SolverFactory(strategy).solve(
model,
tee=True,
nlp_solver=subsolver,
mip_solver=subsolver,
minlp_solver=subsolver,
local_minlp_solver=subsolver,
time_limit=timelimit,
)
print(results)
sys.stdout = stdout
with open(result_dir + "/" + strategy + "_" + subsolver + ".json", "w") as f:
json.dump(results.json_repn(), f)
return None
if __name__ == "__main__":
instance_list = [
# "batch_processing",
# "biofuel",
# "disease_model",
# "gdp_col",
# "hda",
"jobshop",
# "kaibel",
# "positioning",
# "spectralog",
# "med_term_purchasing",
# "methanol",
# "mod_hens",
# "modprodnet",
# "stranded_gas",
# "syngas",
# "water_network",
]
strategy_list = [
"gdp.bigm",
"gdp.hull",
"gdpopt.enumerate",
"gdpopt.loa",
"gdpopt.gloa",
"gdpopt.ric",
]
current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
timelimit = 600
for instance in instance_list:
print("Benchmarking instance: " + instance)
result_dir = "gdplib/" + instance + "/benchmark_result/"
os.makedirs(result_dir, exist_ok=True)
model = import_module("gdplib." + instance).build_model()
for strategy in strategy_list:
benchmark(model, strategy, timelimit, result_dir)