Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn when infeasibility tools will not log output #2666

Merged
merged 2 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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