Skip to content

Commit

Permalink
update benchmark.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ZedongPeng committed Aug 28, 2024
1 parent 46bc880 commit 20c205e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,4 @@ dmypy.json
# Pycharm
.idea/

gdplib/*/benchmark_result/
gdplib/*/benchmark_result/*.log
102 changes: 59 additions & 43 deletions benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from datetime import datetime
from importlib import import_module
from pyomo.environ import *
import sys
from contextlib import redirect_stdout


def benchmark(model, strategy, timelimit, result_dir, subsolver="scip"):
Expand All @@ -27,44 +29,49 @@ def benchmark(model, strategy, timelimit, result_dir, subsolver="scip"):
-------
None
"""
# We clone the model to avoid the solver starting from the optimal solution from previous solve.
model = model.clone()
stdout = sys.stdout

# Direct the solver output to a file
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)
with open(result_dir + "/" + strategy + "_" + subsolver + ".log", "w") as f:
with redirect_stdout(f):
transformation_start_time = time.time()
TransformationFactory(strategy).apply_to(model)
transformation_end_time = time.time()
results = SolverFactory(subsolver).solve(
model,
tee=True,
add_options=["option reslim=3600;option threads=1;"],
)
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)
with open(result_dir + "/" + strategy + "_" + subsolver + ".log", "w") as f:
with redirect_stdout(f):
results = SolverFactory(strategy).solve(
model,
tee=True,
nlp_solver=subsolver,
nlp_solver_args=dict(add_options=["option threads=1;"]),
mip_solver=subsolver,
mip_solver_args=dict(add_options=["option threads=1;"]),
minlp_solver=subsolver,
minlp_solver_args=dict(add_options=["option threads=1;"]),
local_minlp_solver=subsolver,
local_minlp_solver_args=dict(add_options=["option threads=1;"]),
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
Expand All @@ -73,20 +80,23 @@ def benchmark(model, strategy, timelimit, result_dir, subsolver="scip"):
if __name__ == "__main__":
instance_list = [
# "batch_processing",
# "biofuel",
# "disease_model",
# "gdp_col",
# "hda",
# "biofuel", # enumeration got stuck
# "cstr",
"disease_model",
"ex1_linan_2023",
"gdp_col",
"hda",
"jobshop",
# "kaibel",
# "positioning",
# "spectralog",
# "med_term_purchasing",
# "methanol",
# "mod_hens",
# "modprodnet",
# "stranded_gas",
# "syngas",
"med_term_purchasing",
"methanol",
"mod_hens",
"modprodnet",
"positioning",
"small_batch",
"spectralog",
"stranded_gas",
"syngas",
]
strategy_list = [
"gdp.bigm",
Expand All @@ -95,16 +105,22 @@ def benchmark(model, strategy, timelimit, result_dir, subsolver="scip"):
"gdpopt.loa",
"gdpopt.gloa",
"gdpopt.ric",
"gdpopt.lbb",
]
current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
timelimit = 600
timelimit = 3600

for instance in instance_list:
print("Benchmarking instance: " + instance)
result_dir = "gdplib/" + instance + "/benchmark_result/"
os.makedirs(result_dir, exist_ok=True)

print("Benchmarking instance: ", instance)
model = import_module("gdplib." + instance).build_model()

for strategy in strategy_list:
benchmark(model, strategy, timelimit, result_dir)
if os.path.exists(result_dir + "/" + strategy + "_" + "gams" + ".json"):
continue
try:
benchmark(model, strategy, timelimit, result_dir, "gams")
except:
pass

0 comments on commit 20c205e

Please sign in to comment.