Skip to content

A row whose coefficients are all zero is dropped at solve time, even when it cannot hold (0 >= 5 solves "optimal") #1170

Open

Description

@FabianHofmann

Note

The following content was generated by AI.

Reproducible Example

import linopy
import xarray as xr

m = linopy.Model()
x = m.add_variables(lower=0, upper=10, coords=[range(2)], name="x")
c = xr.DataArray([0.0, 1.0], coords=[range(2)])
con = m.add_constraints(c * x >= 5, name="con")
m.add_objective(x.sum())
print(con)
print("rows before solve:", m.constraints.ncons)
print(m.solve("highs", output_flag=False))
print("rows after solve:", m.constraints.ncons, "| rows HiGHS got:", m.solver_model.getNumRow())
print("x =", x.solution.values)

Actual output

Constraint `con` [dim_0: 2]:
----------------------------
[0]: +0 x[0] ≥ 5.0
[1]: +1 x[1] ≥ 5.0
rows before solve: 2
('ok', 'optimal')
rows after solve: 1 | rows HiGHS got: 1
x = [0. 5.]

No warning is emitted (also with -W always).

Expected Behavior

Row 0 reads 0 >= 5, which no x can satisfy. The solve should end infeasible, or linopy should raise or warn and name the row. Dropping an all-zero row is safe only when 0 sense rhs holds (e.g. 0 >= -5). Model.restrict already does this check for the rows it empties.

Installed Versions

  • linopy master cfedd1e (reports 0.10.0.post1.dev94+gbda47bc7f)
  • highspy 1.15.1, Python 3.13.18
Where the row goes, and the same bug in Model.from_spec

Mechanism. c * x keeps x[0] as a term with coefficient 0, so the row counts as having a variable and add_constraints accepts it. solve(sanitize_zeros=True) (the default) removes the zero term in place, the row is then empty, and matrix assembly skips it. The solve also changes the model: m.constraints.ncons goes from 2 to 1.

sanitize_zeros=False helps only for mps and direct; the LP writers also leave out a row without nonzero terms:

io_api sanitize_zeros rows HiGHS gets result
lp, lp-polars, mps, direct True 1 ok, optimal
lp, lp-polars False 1 ok, optimal
mps, direct False 2 warning, infeasible

The same happens for 0 * x >= 5. A related gap: LinearExpression(12, m) == 13 builds and solves "optimal", although #518 made LinearExpression(12, m) == LinearExpression(13, m) raise.

Model.from_spec. _check_live (linopy/spec/builder.py) is meant to refuse a row that the data leaves without variable terms. It tests store.has_terms(), which counts a zero-coefficient term as live, so the row passes and is then dropped as above:

import pandas as pd
import xarray as xr
from linopy import Model

T = pd.Index([0, 1], name="t")
spec = {
    "dimensions": {"t": {"dtype": "int"}},
    "parameters": {"c": {"dims": ["t"]}},
    "variables": {"x": {"dims": ["t"], "bounds": {"lower": 0, "upper": 10}}},
    "constraints": {"row": {"dims": ["t"], "expression": "c * x >= 5"}},
    "objective": {"sense": "minimize", "expression": "sum(x, over=t)"},
}
m = Model.from_spec(spec, {"t": T, "c": xr.DataArray([0.0, 1.0], coords=[T])})
print(m.solve("highs", output_flag=False))  # ('ok', 'optimal'), x = [0, 5]

_check_live also looks only at the side it calls term. So sum(shift(y, along=k, offset=1, edge=0), over=k) <= x is refused ("hold no variable term"), while the same row written as x >= sum(shift(y, along=k, offset=1, edge=0), over=k) builds as x >= 0. The reviewer notes of #1164 mention this as pre-existing.

Related: #517 (constant-only constraint ignored, closed by #518), #1147 / #1164 (spec rows emptied by data), #993 and #1135 (rows dropped silently by absence), #645 (model diagnostics).


Generated by Claude Code

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions