Skip to content

Commit

Permalink
Renaming according to Qis suggestion - now there are two methods - on…
Browse files Browse the repository at this point in the history
…e that checks and one that asserts
  • Loading branch information
carldlaird committed Mar 13, 2020
1 parent d191edf commit cff363f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
3 changes: 2 additions & 1 deletion pyomo/opt/results/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 16 additions & 5 deletions pyomo/opt/results/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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):

Expand Down
14 changes: 5 additions & 9 deletions pyomo/opt/tests/base/test_sol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit cff363f

Please sign in to comment.