Skip to content

Commit

Permalink
black
Browse files Browse the repository at this point in the history
  • Loading branch information
DLWoodruff committed Mar 1, 2024
1 parent 3030fdc commit 9a32b21
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
9 changes: 7 additions & 2 deletions pyomo/contrib/mis/mis.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
See: https://www.sce.carleton.ca/faculty/chinneck/docs/CPAIOR07InfeasibilityTutorial.pdf
"""

import logging
import pyomo.environ as pyo

Expand All @@ -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")

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 6 additions & 5 deletions pyomo/contrib/mis/tests/test_mis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_)
Expand All @@ -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:
Expand All @@ -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()
Expand All @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions pyomo/contrib/mis/tests/trivial_mis.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 9a32b21

Please sign in to comment.