Skip to content

Non-explicit error when SOS1 constraint is orphaned #3719

@Enderdead

Description

@Enderdead

Summary

While developing a custom constraint relying on an SOS1 constraint, I encountered a non-explicit error that appears related to an orphaned SOS1 constraint (missing or mismatched linkage with the objective function).

Steps to reproduce the issue

I have two minimal toy examples: the first fails with the error below; the second runs successfully.

import pyomo.environ as pyo

# Model
m = pyo.ConcreteModel()

# Two items with (value, weight)
items = ["A", "B"]
value  = {"A": 4, "B": 3}
weight = {"A": 2, "B": 2}
capacity = 3

# Decision: take item i (1) or not (0)
m.y = pyo.Var(items, within=pyo.Binary)

# Objective: maximize total value
m.obj = pyo.Objective(expr=sum(value[i]*m.y[i] for i in items), sense=pyo.maximize)

# Constraint: respect weight capacity
m.cap = pyo.Constraint(expr=sum(weight[i]*m.y[i] for i in items) <= capacity)

m.lam = pyo.Var(range(4), domain=pyo.Binary)

m.const_sos = pyo.SOSConstraint(sos=1, var=m.lam, weights=list(range(4)))

pyo.SolverFactory("scip").solve(m, tee=False)
import pyomo.environ as pyo

# Model
m = pyo.ConcreteModel()

# Two items with (value, weight)
items = ["A", "B"]
value  = {"A": 4, "B": 3}
weight = {"A": 2, "B": 2}
capacity = 3

# Decision: take item i (1) or not (0)
m.y = pyo.Var(items, within=pyo.Binary)

# Objective: maximize total value
m.obj = pyo.Objective(expr=sum(value[i]*m.y[i] for i in items), sense=pyo.maximize)

# Constraint: respect weight capacity
m.cap = pyo.Constraint(expr=sum(weight[i]*m.y[i] for i in items) <= capacity)

m.lam = pyo.Var(range(4), domain=pyo.Binary)

m.const_sos = pyo.SOSConstraint(sos=1, var=m.lam, weights=list(range(4)))

# Add the link the rest of the problem
m.link_const = pyo.Constraint(expr=m.y["A"]<=sum( [ i*m.lam[i] for i in range(4)],0.0))

pyo.SolverFactory("scip").solve(m, tee=False)

Error Message

File "/home/francois/Documents/git/pureSolve/.venv/lib/python3.10/site-packages/pyomo/opt/base/solvers.py", line 598, in solve
    self._presolve(*args, **kwds)
  File "/home/francois/Documents/git/pureSolve/.venv/lib/python3.10/site-packages/pyomo/opt/solver/shellcmd.py", line 223, in _presolve
    OptSolver._presolve(self, *args, **kwds)
  File "/home/francois/Documents/git/pureSolve/.venv/lib/python3.10/site-packages/pyomo/opt/base/solvers.py", line 704, in _presolve
    self._convert_problem(
  File "/home/francois/Documents/git/pureSolve/.venv/lib/python3.10/site-packages/pyomo/opt/base/solvers.py", line 756, in _convert_problem
    return convert_problem(
  File "/home/francois/Documents/git/pureSolve/.venv/lib/python3.10/site-packages/pyomo/opt/base/convert.py", line 97, in convert_problem
    problem_files, symbol_map = converter.apply(*tmp, **tmpkw)
  File "/home/francois/Documents/git/pureSolve/.venv/lib/python3.10/site-packages/pyomo/solvers/plugins/converter/model.py", line 184, in apply
    (problem_filename, symbol_map_id) = instance.write(
  File "/home/francois/Documents/git/pureSolve/.venv/lib/python3.10/site-packages/pyomo/core/base/block.py", line 1916, in write
    (filename, smap) = problem_writer(self, filename, solver_capability, io_options)
  File "/home/francois/Documents/git/pureSolve/.venv/lib/python3.10/site-packages/pyomo/repn/plugins/nl_writer.py", line 313, in __call__
    info = self.write(model, FILE, ROWFILE, COLFILE, config=config)
  File "/home/francois/Documents/git/pureSolve/.venv/lib/python3.10/site-packages/pyomo/repn/plugins/nl_writer.py", line 371, in write
    return impl.write(model)
  File "/home/francois/Documents/git/pureSolve/.venv/lib/python3.10/site-packages/pyomo/repn/plugins/nl_writer.py", line 1451, in write
    _init_lines = [
  File "/home/francois/Documents/git/pureSolve/.venv/lib/python3.10/site-packages/pyomo/repn/plugins/nl_writer.py", line 1451, in <listcomp>
    _init_lines = [
KeyError: 137135353954592

Information on your system

Pyomo version: 6.8.0
Python version: 3.10.12
Operating system: linux
How Pyomo was installed (PyPI, conda, source): PyPi and virtual env
Solver (if applicable): SCIP

Additional information & Request

It would be great if this issue could be handled more gracefully, to help users better understand and resolve the problem.

Thank you for the great work on this library!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions