Track all row variables in Constraint.vars - #1737
Conversation
Quadratic rows stored only the variables of their linear terms, and updateConstraint did not record variables it introduced, so mapping a row's column indices back to Variables (compute_slack) could KeyError. Signed-off-by: jackthepunished <kosapinarbahadir@gmail.com>
f53442d to
84c88de
Compare
|
@jackthepunished Thanks for putting up this PR! Just for context, we're updating the slack computation in #1615, and currently self.vars in the That said, it looks like you need this for #1400, so we can merge these fixes for now. I've suggested a few changes—let us know if they address both the bug you reported and the issue in the related PR. |
| # expr.vars holds only the linear terms; id() because Variable | ||
| # overrides __eq__ and is unhashable. | ||
| seen = {} | ||
| for var in (*expr.vars, *expr.qvars1, *expr.qvars2, *expr.qvars): |
There was a problem hiding this comment.
do we need to use id() here? We could perhaps update this part to hash index
self.vars = {}
# Variables may appear only in quadratic terms, so the linear
# terms of the expression are not enough to cover the row.
for var in (*expr.vars, *expr.qvars1, *expr.qvars2, *expr.qvars):
self.vars[var.index] = var
where self.vars is now a dict and can be directly used in compute_slack() rather than reformulating it.
You would need to make the list to dict changes for self.vars in the following lines when expression is Linear, in updateConstraint which would then just add to the dict, plus update the tests.
Description
Constraint.varson a quadratic row held only the linear-term variables; variables that appear only in quadratic terms were missing.updateConstraintdid not add newly referenced variables toConstraint.vars, socompute_slackraisedKeyErrorfor them.Variables only, no coefficient copies.Checklist