From 718ac0da0689c53ec594cc48af6370ed18465fb6 Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Sat, 5 Dec 2020 11:14:15 -0600 Subject: [PATCH] Reformat additional files --- Makefile | 2 +- benchmark/benchmark.py | 181 +++++++++++++++++++++-------------------- setup.py | 34 ++++---- 3 files changed, 113 insertions(+), 104 deletions(-) diff --git a/Makefile b/Makefile index 59a3ee1..18cc3b1 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ uninstall: $(PIP) uninstall miplearn reformat: - $(PYTHON) -m black miplearn + $(PYTHON) -m black . test: $(PYTEST) $(PYTEST_ARGS) diff --git a/benchmark/benchmark.py b/benchmark/benchmark.py index 8a7c6bf..9f2bb87 100755 --- a/benchmark/benchmark.py +++ b/benchmark/benchmark.py @@ -14,22 +14,26 @@ Usage: Options: -h --help Show this screen """ -from docopt import docopt -import importlib, pathlib -from miplearn import (LearningSolver, BenchmarkRunner) -from numpy import median -import pyomo.environ as pe -import pickle +import importlib import logging +import pathlib +import pickle import sys -logging.basicConfig(format='%(asctime)s %(levelname).1s %(name)s: %(message)12s', - datefmt='%H:%M:%S', - level=logging.INFO, - stream=sys.stdout) -logging.getLogger('gurobipy').setLevel(logging.ERROR) -logging.getLogger('pyomo.core').setLevel(logging.ERROR) -logging.getLogger('miplearn').setLevel(logging.INFO) +from docopt import docopt +from numpy import median + +from miplearn import LearningSolver, BenchmarkRunner + +logging.basicConfig( + format="%(asctime)s %(levelname).1s %(name)s: %(message)12s", + datefmt="%H:%M:%S", + level=logging.INFO, + stream=sys.stdout, +) +logging.getLogger("gurobipy").setLevel(logging.ERROR) +logging.getLogger("pyomo.core").setLevel(logging.ERROR) +logging.getLogger("miplearn").setLevel(logging.INFO) logger = logging.getLogger("benchmark") n_jobs = 10 @@ -46,28 +50,31 @@ def save(obj, filename): logger.info("Writing %s..." % filename) with open(filename, "wb") as file: pickle.dump(obj, file) - - + + def load(filename): import pickle + with open(filename, "rb") as file: - return pickle.load(file) - - + return pickle.load(file) + + def train(): problem_name, challenge_name = args[""].split("/") pkg = importlib.import_module("miplearn.problems.%s" % problem_name) challenge = getattr(pkg, challenge_name)() train_instances = challenge.training_instances - test_instances = challenge.test_instances - solver = LearningSolver(time_limit=train_time_limit, - solver=internal_solver, - components={}) + test_instances = challenge.test_instances + solver = LearningSolver( + time_limit=train_time_limit, + solver=internal_solver, + components={}, + ) solver.parallel_solve(train_instances, n_jobs=n_jobs) save(train_instances, "%s/train_instances.bin" % basepath) save(test_instances, "%s/test_instances.bin" % basepath) - - + + def test_baseline(): test_instances = load("%s/test_instances.bin" % basepath) solvers = { @@ -79,8 +86,8 @@ def test_baseline(): benchmark = BenchmarkRunner(solvers) benchmark.parallel_solve(test_instances, n_jobs=n_jobs) benchmark.save_results("%s/benchmark_baseline.csv" % basepath) - - + + def test_ml(): logger.info("Loading instances...") train_instances = load("%s/train_instances.bin" % basepath) @@ -105,17 +112,18 @@ def test_ml(): benchmark.parallel_solve(test_instances, n_jobs=n_jobs) benchmark.save_results("%s/benchmark_ml.csv" % basepath) - + def charts(): import matplotlib.pyplot as plt import seaborn as sns + sns.set_style("whitegrid") sns.set_palette("Blues_r") benchmark = BenchmarkRunner({}) benchmark.load_results("%s/benchmark_ml.csv" % basepath) results = benchmark.raw_results() results["Gap (%)"] = results["Gap"] * 100.0 - + sense = results.loc[0, "Sense"] if sense == "min": primal_column = "Relative Upper Bound" @@ -125,70 +133,71 @@ def charts(): primal_column = "Relative Lower Bound" obj_column = "Lower Bound" predicted_obj_column = "Predicted LB" - - palette={ - "baseline": "#9b59b6", - "ml-exact": "#3498db", - "ml-heuristic": "#95a5a6" - } - fig, (ax1, ax2, ax3, ax4) = plt.subplots(nrows=1, - ncols=4, - figsize=(12,4), - gridspec_kw={'width_ratios': [2, 1, 1, 2]}, - ) - sns.stripplot(x="Solver", - y="Wallclock Time", - data=results, - ax=ax1, - jitter=0.25, - palette=palette, - size=4.0, - ); - sns.barplot(x="Solver", - y="Wallclock Time", - data=results, - ax=ax1, - errwidth=0., - alpha=0.4, - palette=palette, - estimator=median, - ); - ax1.set(ylabel='Wallclock Time (s)') + + palette = {"baseline": "#9b59b6", "ml-exact": "#3498db", "ml-heuristic": "#95a5a6"} + fig, (ax1, ax2, ax3, ax4) = plt.subplots( + nrows=1, + ncols=4, + figsize=(12, 4), + gridspec_kw={"width_ratios": [2, 1, 1, 2]}, + ) + sns.stripplot( + x="Solver", + y="Wallclock Time", + data=results, + ax=ax1, + jitter=0.25, + palette=palette, + size=4.0, + ) + sns.barplot( + x="Solver", + y="Wallclock Time", + data=results, + ax=ax1, + errwidth=0.0, + alpha=0.4, + palette=palette, + estimator=median, + ) + ax1.set(ylabel="Wallclock Time (s)") ax2.set_ylim(-0.5, 5.5) - sns.stripplot(x="Solver", - y="Gap (%)", - jitter=0.25, - data=results[results["Solver"] != "ml-heuristic"], - ax=ax2, - palette=palette, - size=4.0, - ); - ax3.set_ylim(0.95,1.05) - sns.stripplot(x="Solver", - y=primal_column, - jitter=0.25, - data=results[results["Solver"] == "ml-heuristic"], - ax=ax3, - palette=palette, - ); - - sns.scatterplot(x=obj_column, - y=predicted_obj_column, - hue="Solver", - data=results[results["Solver"] == "ml-exact"], - ax=ax4, - palette=palette, - ); + sns.stripplot( + x="Solver", + y="Gap (%)", + jitter=0.25, + data=results[results["Solver"] != "ml-heuristic"], + ax=ax2, + palette=palette, + size=4.0, + ) + ax3.set_ylim(0.95, 1.05) + sns.stripplot( + x="Solver", + y=primal_column, + jitter=0.25, + data=results[results["Solver"] == "ml-heuristic"], + ax=ax3, + palette=palette, + ) + + sns.scatterplot( + x=obj_column, + y=predicted_obj_column, + hue="Solver", + data=results[results["Solver"] == "ml-exact"], + ax=ax4, + palette=palette, + ) xlim, ylim = ax4.get_xlim(), ax4.get_ylim() - ax4.plot([-1e10, 1e10], [-1e10, 1e10], ls='-', color="#cccccc"); + ax4.plot([-1e10, 1e10], [-1e10, 1e10], ls="-", color="#cccccc") ax4.set_xlim(xlim) ax4.set_ylim(ylim) ax4.get_legend().remove() - + fig.tight_layout() - plt.savefig("%s/performance.png" % basepath, - bbox_inches='tight', - dpi=150) + plt.savefig("%s/performance.png" % basepath, bbox_inches="tight", dpi=150) + if __name__ == "__main__": if args["train"]: diff --git a/setup.py b/setup.py index e7b7527..bdc1594 100644 --- a/setup.py +++ b/setup.py @@ -4,27 +4,27 @@ with open("README.md", "r") as fh: long_description = fh.read() setup( - name='miplearn', - version='0.2.0', - author='Alinson S. Xavier', - author_email='axavier@anl.gov', + name="miplearn", + version="0.2.0", + author="Alinson S. Xavier", + author_email="axavier@anl.gov", description="Extensible framework for Learning-Enhanced Mixed-Integer Optimization", long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/ANL-CEEESA/MIPLearn/", packages=find_namespace_packages(), - python_requires='>=3.6', + python_requires=">=3.6", install_requires=[ - 'docopt', - 'matplotlib', - 'networkx', - 'numpy', - 'pandas', - 'p_tqdm', - 'pyomo', - 'python-markdown-math', - 'seaborn', - 'sklearn', - 'tqdm', - ], + "docopt", + "matplotlib", + "networkx", + "numpy", + "pandas", + "p_tqdm", + "pyomo", + "python-markdown-math", + "seaborn", + "sklearn", + "tqdm", + ], )