diff --git a/.gitignore b/.gitignore index b9c4f32..1a6f574 100644 --- a/.gitignore +++ b/.gitignore @@ -131,4 +131,4 @@ dmypy.json # Pycharm .idea/ -gdplib/*/benchmark_result/ +gdplib/*/benchmark_result/*.log diff --git a/benchmark.py b/benchmark.py index 21dac5d..91f3abe 100644 --- a/benchmark.py +++ b/benchmark.py @@ -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"): @@ -27,22 +29,25 @@ 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", @@ -50,21 +55,23 @@ def benchmark(model, strategy, timelimit, result_dir, subsolver="scip"): "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 @@ -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", @@ -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