diff --git a/pyomo/opt/results/__init__.py b/pyomo/opt/results/__init__.py index 12d29f37cb3..0d5a66eb363 100644 --- a/pyomo/opt/results/__init__.py +++ b/pyomo/opt/results/__init__.py @@ -11,7 +11,8 @@ #from old_results import * from pyomo.opt.results.container import * import pyomo.opt.results.problem -from pyomo.opt.results.solver import SolverStatus, TerminationCondition, check_optimal_termination +from pyomo.opt.results.solver import SolverStatus, TerminationCondition, \ + check_optimal_termination, assert_optimal_termination from pyomo.opt.results.problem import ProblemSense from pyomo.opt.results.solution import SolutionStatus, Solution from pyomo.opt.results.results_ import SolverResults diff --git a/pyomo/opt/results/solver.py b/pyomo/opt/results/solver.py index 63dc614bd0b..ae61067f5f8 100644 --- a/pyomo/opt/results/solver.py +++ b/pyomo/opt/results/solver.py @@ -8,7 +8,7 @@ # This software is distributed under the 3-clause BSD License. # ___________________________________________________________________________ -__all__ = ['SolverInformation', 'SolverStatus', 'TerminationCondition', 'check_optimal_termination'] +__all__ = ['SolverInformation', 'SolverStatus', 'TerminationCondition', 'check_optimal_termination', 'assert_optimal_termination'] from pyutilib.enum import Enum from pyomo.opt.results.container import MapContainer, ScalarType @@ -68,7 +68,7 @@ ) -def check_optimal_termination(results, suppress_exception=False): +def check_optimal_termination(results): """ This function returns True if the termination condition for the solver is 'optimal', 'locallyOptimal', or 'globallyOptimal', and the status is 'ok' @@ -86,14 +86,25 @@ def check_optimal_termination(results, suppress_exception=False): or results.solver.termination_condition == TerminationCondition.locallyOptimal or results.solver.termination_condition == TerminationCondition.globallyOptimal): return True + return False + + +def assert_optimal_termination(results): + """ + This function checks if the termination condition for the solver + is 'optimal', 'locallyOptimal', or 'globallyOptimal', and the status is 'ok' + and it raises a RuntimeError exception if this is not true. - if not suppress_exception: + Parameters + ---------- + results : Pyomo results object returned from solver.solve + """ + if not check_optimal_termination(results): msg = 'Solver failed to return an optimal solution. ' \ 'Solver status: {}, Termination condition: {}'.format(results.solver.status, results.solver.termination_condition) raise RuntimeError(msg) - return False - + class BranchAndBoundStats(MapContainer): diff --git a/pyomo/opt/tests/base/test_sol.py b/pyomo/opt/tests/base/test_sol.py index 1b150ec3ef2..0f3040cd453 100644 --- a/pyomo/opt/tests/base/test_sol.py +++ b/pyomo/opt/tests/base/test_sol.py @@ -23,7 +23,8 @@ from pyomo.opt import (TerminationCondition, SolutionStatus, SolverStatus, - check_optimal_termination) + check_optimal_termination, + assert_optimal_termination) old_tempdir = pyutilib.services.TempfileManager.tempdir @@ -62,13 +63,10 @@ def test_infeasible1(self): self.assertEqual(soln.solver.status, SolverStatus.warning) - self.assertFalse(check_optimal_termination(soln, suppress_exception=True)) + self.assertFalse(check_optimal_termination(soln)) with self.assertRaises(RuntimeError): - check_optimal_termination(soln, suppress_exception=False) - - with self.assertRaises(RuntimeError): - check_optimal_termination(soln) + assert_optimal_termination(soln) def test_infeasible2(self): with pyomo.opt.ReaderFactory("sol") as reader: @@ -94,9 +92,7 @@ def test_conopt_optimal(self): self.assertEqual(soln.solver.status, SolverStatus.ok) self.assertTrue(check_optimal_termination(soln)) - self.assertTrue(check_optimal_termination(soln, suppress_exception=True)) - self.assertTrue(check_optimal_termination(soln, suppress_exception=False)) - + assert_optimal_termination(soln) def test_bad_options(self): with pyomo.opt.ReaderFactory("sol") as reader: