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

Setting opt.config.load_solution = False still loads the model when running opt.solve #3130

Closed
lukasbiton opened this issue Feb 10, 2024 · 3 comments

Comments

@lukasbiton
Copy link
Contributor

Summary

When I feed pyomo an unfeasible model, I get the following 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.

Following the instructions and setting opt.config.load_solution = False does not stop the RuntimeError from being raised, so I still can't check the termination condition. I would expect that when choosing this setting, no error would be raised and I would be able to look at my results object.

Looking through the stack trace, the final error happens here: https://github.com/Pyomo/pyomo/blob/main/pyomo/contrib/appsi/solvers/highs.py#L671
This block of code seems to be indented incorrectly, resulting in unexpected behavior given the provided error message. More specifically, the else statement that raises the RuntimeError is aligned with if has_feasible_solution: on line 661 and not with if config.load_solution: on line 660. Aligning the indent to line 660 induces the expected behavior given the error message provided.

Steps to reproduce the issue

I am using pyomo with the HiGHS solver. I installed the HiGHS solver following the instructions on their website: https://ergo-code.github.io/HiGHS/dev/interfaces/cpp/.
I then locally installed highspy to make sure pyomo could find the solver.

In order to reproduce my output, you need to install pyomo and highspy. Then, run the test.py script presented below with the following command:

$ pyhton -m test

Here is my example script: (stolen with a small added constraint from https://pyomo.readthedocs.io/en/stable/pyomo_overview/simple_examples.html)

# test.py

import pyomo.environ as pyo

model = pyo.ConcreteModel()

model.x = pyo.Var([1,2], domain = pyo.NonNegativeReals)

model.OBJ = pyo.Objective(expr = 2 * model.x[1] + 3 * model.x[2])

model.Constraint1 = pyo.Constraint(expr = 3 * model.x[1] + 4 * model.x[2] >= 1)
# New constraint makes the problem unfeasible
model.Constraint2 = pyo.Constraint(expr = model.x[1] + model.x[2] == -10)

opt = pyo.SolverFactory("appsi_highs")
opt.config.load_solution = False
results = opt.solve(model)

Error Message

Here is the entire stack trace:

$ python -m test
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "~/Projects/pyomo-test/test.py", line 17, in <module>
    results = opt.solve(model)
  File "~/Projects/pyomo-test/.venv/lib/python3.11/site-packages/pyomo/contrib/appsi/base.py", line 1557, in solve
    results: Results = super(LegacySolverInterface, self).solve(model)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/Projects/pyomo-test/.venv/lib/python3.11/site-packages/pyomo/contrib/appsi/solvers/highs.py", line 250, in solve
    res = self._solve(timer)
          ^^^^^^^^^^^^^^^^^^
  File "~/Projects/pyomo-test/.venv/lib/python3.11/site-packages/pyomo/contrib/appsi/solvers/highs.py", line 234, in _solve
    return self._postsolve(timer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "~/Projects/pyomo-test/.venv/lib/python3.11/site-packages/pyomo/contrib/appsi/solvers/highs.py", line 671, 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.7.0 (CPython 3.11.2 on Linux 6.6.10-76060610-generic)
Python version: 3.11.2
Operating system: Pop!_OS
How Pyomo was installed (PyPI, conda, source): pip (via poetry)
Solver (if applicable): HiGHS (with highspy)

@lpierezan
Copy link

Hi @lukasbiton

I believe it is a documentation problem. If you are getting your solver object from the SolverFactory, the correct way to use it is:

opt.solve(model, load_solutions = False)

The opt.config.load_solution only works if you are directly instantiating an object from the PersistentSolver class, like

opt = appsi.solvers.Highs()

@lukasbiton
Copy link
Contributor Author

Hi @lpierezan ,

Thank you for answering, your solution indeed works. I guess the stack trace was a red herring in this case! If you want I can write a PR to make the error message more explicit. I don't want to clutter your PR backlog and couldn't find a "minimum size" of PR to submit but I'll defer to you.

@mrmundt
Copy link
Contributor

mrmundt commented Feb 15, 2024

@lukasbiton - we love small PRs. They are easy to review. Please feel free!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants