Pyomo / pyomo

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

SCIPAMPL solver does not assign values to all variables #3110

Open lpierezan opened 8 months ago

lpierezan commented 8 months ago

Summary

Using the solver obtained by SolverFactory('scip'):

If a variable, active in the Pyomo model, does not appear in any constraint (or appears with a coefficient 0) then after solve, no value was assigned to that variable.

It is expected that all model variables have a value assigned when the solver status returned is an optimal solution.

Steps to reproduce the issue

m = pyo.ConcreteModel()
m.x = pyo.Var(domain = pyo.Binary)
m.y = pyo.Var(domain = pyo.Binary)
m.z = pyo.Var(domain = pyo.Binary)
m.k = pyo.Param(initialize = 0)
m.c = pyo.Constraint(expr = m.k * m.x + m.y <= 1)
m.obj = pyo.Objective(expr = m.y)
opt = pyo.SolverFactory('scip', tee = False)
r = opt.solve(m)
print(r.solver)
assert m.x.value is None
assert m.z.value is None

Information on your system

Pyomo version: 6.7.0 Python version: 3.9.16 Operating system: Win 11 How Pyomo was installed (PyPI, conda, source): pip Solver (if applicable): scip 8.1.0

Additional information

In fact, the nl file doesn't even define the variable.

michaelbynum commented 8 months ago

In my opinion, this is expected behavior. The value of a variable that does not actively participate in a model can be anything within its domain.

lpierezan commented 8 months ago

Hi @michaelbynum , thanks for your reply.

I don't know of any solver that has this behavior. I quickly tested with scip (pyscipopt), gurobi (gurobipy) and highs (highspy): if a viable solution is reported, all variables defined in the model have an assigned value (even if they do not appear in constraints or objective function).

So in this case Pyomo is adding behavior that is, to say the least, quite unusual.

Still using Pyomo, if I change SolverFactory to 'appsi_highs', the behavior changes to what I think is correct, as the MPS writer specifies all the variables defined in the Pyomo model.

In my opinion, it wouldn't be up to Pyomo to decide to "remove" some variables, it would be enough to pass them all to the chosen solver.