Pyomo / pyomo

An object-oriented algebraic modeling language in Python for structured optimization problems.
https://www.pyomo.org
Other
2.02k stars 518 forks source link

Solving an infeasible problem using HiGHS raises `RuntimeError` #2920

Closed victorgarcia98 closed 1 year ago

victorgarcia98 commented 1 year ago

Summary

I run HiGHS in an infeasible problem but the solve step raises the RuntimeError exception, instead of returning a results object with the termination status being infeasible. Tried using both using the solver class Highs and the SolverFactory("appsi_highs"), but both produce the same behavior. Moreover, I followed the indication opt.config.load_solution=False, but the exception still raised.

Steps to reproduce the issue

$ python highs_error.py
WARNING: Loading a SolverResults object with a warning status into
model.name="unknown";
    - termination condition: infeasible
    - message from solver: <undefined>
Termination Condition:  infeasible
Running HiGHS 1.5.3 [date: 2023-05-16, git hash: 594fa5a9d-dirty]
Copyright (c) 2023 HiGHS under MIT licence terms
RuntimeError
Running HiGHS 1.5.3 [date: 2023-05-16, git hash: 594fa5a9d-dirty]
Copyright (c) 2023 HiGHS under MIT licence terms
RuntimeError
Running HiGHS 1.5.3 [date: 2023-05-16, git hash: 594fa5a9d-dirty]
Copyright (c) 2023 HiGHS under MIT licence terms
RuntimeError
## highs_error.py
import pyomo.environ as pyo
from pyomo.opt import SolverFactory
from pyomo.contrib.appsi.solvers.highs import Highs

######################
# Infeasible Problem #
######################

model = pyo.ConcreteModel()
model.x = pyo.Var(domain=pyo.NonNegativeReals)

model.infeasible_constraint = pyo.Constraint(expr = model.x <= -1)
model.objective = pyo.Objective(expr = model.x)

####################
# Solving with CBC #
####################

opt_cbc = SolverFactory("cbc")
results = opt_cbc.solve(
    model
)

print("Termination Condition: ", results.solver.termination_condition) # -> Termination Condition:  infeasible

######################
# Solving with HiGhs #
######################

opt_highs = Highs()
try:
    results = opt_highs.solve(model) # -> RuntimeError
except RuntimeError:
    print("RuntimeError")

opt_highs_factory = SolverFactory("appsi_highs")
try:
    results = opt_highs_factory.solve(model) # -> RuntimeError
except RuntimeError:
    print("RuntimeError")

opt_highs_factory = SolverFactory("appsi_highs")
opt_highs_factory.config.load_solution=False
try:
    results = opt_highs_factory.solve(model) # -> RuntimeError
except RuntimeError:
    print("RuntimeError")

Error Message

Running HiGHS 1.5.3 [date: 2023-05-16, git hash: 594fa5a9d-dirty]
Copyright (c) 2023 HiGHS under MIT licence terms
Traceback (most recent call last):
  File "/home/victor/Work/Seita/flexmeasures/pyomo_mve.py", line 23, in <module>
    results = opt.solve(
  File "/home/victor/Work/Seita/flexmeasures/venv/lib/python3.10/site-packages/pyomo/contrib/appsi/solvers/highs.py", line 250, in solve
    res = self._solve(timer)
  File "/home/victor/Work/Seita/flexmeasures/venv/lib/python3.10/site-packages/pyomo/contrib/appsi/solvers/highs.py", line 234, in _solve
    return self._postsolve(timer)
  File "/home/victor/Work/Seita/flexmeasures/venv/lib/python3.10/site-packages/pyomo/contrib/appsi/solvers/highs.py", line 657, in _postsolve
    raise RuntimeError(
RuntimeError: A feasible solution was not found, so no solution can be loaded.Please set opt.config.load_solution=False and check results.termination_condition and results.best_feasible_objective before loading a solution.

Information on your system

Pyomo version: Pyomo 6.6.1 (CPython 3.10.6 on Linux 6.2.6-76060206-generic) Python version: Python 3.10.6 Operating system: Pop Os How Pyomo was installed (PyPI, conda, source): pip Solver: HiGHs (v.1.5.3), installed through the pip package highspy (v.1.5.3).

michaelbynum commented 1 year ago

opt.config.load_solution = False only works with opt_highs = Highs(). If you use the solver factory, you have to use results = opt_highs_factory.solve(model, load_solutions=False).

victorgarcia98 commented 1 year ago

Thanks @michaelbynum! :smile: