Skip to content

Commit

Permalink
Merge pull request #2666 from jsiirola/infeasibility-logger-warning
Browse files Browse the repository at this point in the history
Warn when infeasibility tools will not log output
  • Loading branch information
jsiirola authored Dec 15, 2022
2 parents c2fa5db + 6129401 commit 3119236
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pyomo/util/infeasible.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ def log_infeasible_constraints(
log_variables (bool): If true, prints the constraint variable names and values
"""
if logger.getEffectiveLevel() > logging.INFO:
logger.warning(
'log_infeasible_constraints() called with a logger whose '
'effective level is higher than logging.INFO: no output '
'will be logged regardless of constraint feasibility'
)

# Iterate through all active constraints on the model
for constr in m.component_data_objects(
ctype=Constraint, active=True, descend_into=True):
Expand Down Expand Up @@ -126,6 +133,13 @@ def log_infeasible_bounds(m, tol=1E-6, logger=logger):
tol (float): feasibility tolerance
"""
if logger.getEffectiveLevel() > logging.INFO:
logger.warning(
'log_infeasible_bounds() called with a logger whose '
'effective level is higher than logging.INFO: no output '
'will be logged regardless of bound feasibility'
)

for var in m.component_data_objects(
ctype=Var, descend_into=True):
var_value = var.value
Expand All @@ -149,6 +163,13 @@ def log_close_to_bounds(m, tol=1E-6, logger=logger):
m (Block): Pyomo block or model to check
tol (float): bound tolerance
"""
if logger.getEffectiveLevel() > logging.INFO:
logger.warning(
'log_close_to_bounds() called with a logger whose '
'effective level is higher than logging.INFO: no output '
'will be logged regardless of bound status'
)

for var in m.component_data_objects(
ctype=Var, descend_into=True):
if var.fixed:
Expand Down
30 changes: 30 additions & 0 deletions pyomo/util/tests/test_infeasible.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def build_model(self):
def test_log_infeasible_constraints(self):
"""Test for logging of infeasible constraints."""
m = self.build_model()

with LoggingIntercept(None, 'pyomo.util.infeasible') as LOG:
log_infeasible_constraints(m)
self.assertEqual(
'log_infeasible_constraints() called with a logger whose '
'effective level is higher than logging.INFO: no output '
'will be logged regardless of constraint feasibility',
LOG.getvalue().strip(),
)

output = StringIO()
with LoggingIntercept(output, 'pyomo.util.infeasible', logging.INFO):
log_infeasible_constraints(m)
Expand All @@ -70,6 +80,16 @@ def test_log_infeasible_bounds(self):
m = self.build_model()
m.x.setlb(2)
m.x.setub(0)

with LoggingIntercept(None, 'pyomo.util.infeasible') as LOG:
log_infeasible_bounds(m)
self.assertEqual(
'log_infeasible_bounds() called with a logger whose '
'effective level is higher than logging.INFO: no output '
'will be logged regardless of bound feasibility',
LOG.getvalue().strip(),
)

output = StringIO()
with LoggingIntercept(output, 'pyomo.util', logging.INFO):
log_infeasible_bounds(m)
Expand Down Expand Up @@ -99,6 +119,16 @@ def test_log_active_constraints(self):
def test_log_close_to_bounds(self):
"""Test logging of variables and constraints near bounds."""
m = self.build_model()

with LoggingIntercept(None, 'pyomo.util.infeasible') as LOG:
log_close_to_bounds(m)
self.assertEqual(
'log_close_to_bounds() called with a logger whose '
'effective level is higher than logging.INFO: no output '
'will be logged regardless of bound status',
LOG.getvalue().strip(),
)

output = StringIO()
with LoggingIntercept(output, 'pyomo.util.infeasible', logging.INFO):
log_close_to_bounds(m)
Expand Down

0 comments on commit 3119236

Please sign in to comment.