From 9a32b21831be40951aaca03c4c1d9112a1bca4ab Mon Sep 17 00:00:00 2001 From: "David L. Woodruff" Date: Fri, 1 Mar 2024 09:40:22 -0800 Subject: [PATCH] black --- pyomo/contrib/mis/mis.py | 9 +++++++-- pyomo/contrib/mis/tests/test_mis.py | 11 ++++++----- pyomo/contrib/mis/tests/trivial_mis.py | 5 +++-- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/pyomo/contrib/mis/mis.py b/pyomo/contrib/mis/mis.py index 86832f36933..a4865c0e1a1 100644 --- a/pyomo/contrib/mis/mis.py +++ b/pyomo/contrib/mis/mis.py @@ -8,6 +8,7 @@ See: https://www.sce.carleton.ca/faculty/chinneck/docs/CPAIOR07InfeasibilityTutorial.pdf """ + import logging import pyomo.environ as pyo @@ -22,12 +23,13 @@ try: from idaes.core.solvers import get_solver + have_idaes = True except: have_idaes = False logger = logging.getLogger("pyomo.contrib.iis") -logger.setLevel(logging.INFO) +logger.setLevel(logging.INFO) _default_nl_writer = WriterFactory.get_class("nl") @@ -66,7 +68,9 @@ def _apply_to(self, instance, **kwds): v.setub(None) -def compute_infeasibility_explanation(model, solver=None, tee=False, tolerance=1e-8, logger=logger): +def compute_infeasibility_explanation( + model, solver=None, tee=False, tolerance=1e-8, logger=logger +): """ This function attempts to determine why a given model is infeasible. It deploys two main algorithms: @@ -360,6 +364,7 @@ def _get_constraint(modified_model, v): else: raise RuntimeError("Bad var name {v.name}") + """ WaterTAP Copyright (c) 2020-2023, The Regents of the University of California, through Lawrence Berkeley National Laboratory, Oak Ridge National Laboratory, National Renewable Energy Laboratory, and National Energy Technology Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved. diff --git a/pyomo/contrib/mis/tests/test_mis.py b/pyomo/contrib/mis/tests/test_mis.py index 6776f43f4ce..985bf1911d9 100644 --- a/pyomo/contrib/mis/tests/test_mis.py +++ b/pyomo/contrib/mis/tests/test_mis.py @@ -39,11 +39,12 @@ class TestMIS(unittest.TestCase): ) def test_write_mis_ipopt(self): _test_mis("ipopt") + def test__get_constraint_errors(self): # A not-completely-cyincal way to get the coverage up. m = _get_infeasible_model() # not modified, but who cares? fct = getattr(mis, "_get_constraint") - #fct = mis._get_constraint + # fct = mis._get_constraint m.foo_slack_plus_ = pyo.Var() self.assertRaises(RuntimeError, fct, m, m.foo_slack_plus_) @@ -52,7 +53,7 @@ def test__get_constraint_errors(self): m.foo_bar = pyo.Var() self.assertRaises(RuntimeError, fct, m, m.foo_bar) - + def _check_output(file_name): # pretty simple check for now with open(file_name, "r+") as file1: @@ -71,7 +72,7 @@ def _check_output(file_name): raise RuntimeError(f"Did not find '{nugget}' after '{trigger}' in output") else: pass - + def _test_mis(solver_name): m = _get_infeasible_model() @@ -80,14 +81,14 @@ def _test_mis(solver_name): TempfileManager.push() tmp_path = TempfileManager.create_tempdir() file_name = os.path.join(tmp_path, f"{solver_name}_mis.log") - logger = logging.getLogger(f'test_mis_{solver_name}') + logger = logging.getLogger(f"test_mis_{solver_name}") logger.setLevel(logging.INFO) # create file handler which logs even debug messages print(f"{file_name =}") fh = logging.FileHandler(file_name) fh.setLevel(logging.DEBUG) logger.addHandler(fh) - + mis.compute_infeasibility_explanation(m, opt, logger=logger) _check_output(file_name) diff --git a/pyomo/contrib/mis/tests/trivial_mis.py b/pyomo/contrib/mis/tests/trivial_mis.py index 37f177c7666..29d22d5e5e5 100644 --- a/pyomo/contrib/mis/tests/trivial_mis.py +++ b/pyomo/contrib/mis/tests/trivial_mis.py @@ -1,14 +1,15 @@ import pyomo.environ as pyo + m = pyo.ConcreteModel("Trivial Quad") -m.x = pyo.Var([1,2], bounds=(0,1)) +m.x = pyo.Var([1, 2], bounds=(0, 1)) m.y = pyo.Var(bounds=(0, 1)) m.c = pyo.Constraint(expr=m.x[1] * m.x[2] == -1) m.d = pyo.Constraint(expr=m.x[1] + m.y >= 1) from pyomo.contrib.mis.mis import compute_infeasibility_explanation + # if IDAES is installed, compute_infeasibility_explanation doesn't need to be passed a solver # Note: this particular little problem is quadratic # As of 18Feb DLW is not sure the explanation code works with solvers other than ipopt ipopt = pyo.SolverFactory("ipopt") compute_infeasibility_explanation(m, solver=ipopt) -