Skip to content

Track all row variables in Constraint.vars - #1737

Draft
jackthepunished wants to merge 2 commits into
NVIDIA:mainfrom
jackthepunished:fix/constraint-vars
Draft

Track all row variables in Constraint.vars#1737
jackthepunished wants to merge 2 commits into
NVIDIA:mainfrom
jackthepunished:fix/constraint-vars

Conversation

@jackthepunished

Copy link
Copy Markdown
Contributor

Description

  • Constraint.vars on a quadratic row held only the linear-term variables; variables that appear only in quadratic terms were missing.
  • updateConstraint did not add newly referenced variables to Constraint.vars, so compute_slack raised KeyError for them.
  • Both pre-existing; split out of Add algebraic __str__ and detailed __repr__ to Python LP API classes #1400 at review request. Stores references to existing Variables only, no coefficient copies.

Checklist

  • I am familiar with the Contributing Guidelines.
  • Testing
    • New or existing tests cover these changes
    • Added tests
    • Created an issue to follow-up
    • NA
  • Documentation
    • The documentation is up to date with these changes
    • Added new documentation
    • NA

@copy-pr-bot

copy-pr-bot Bot commented Aug 18, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

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>
@Iroy30

Iroy30 commented Aug 20, 2026

Copy link
Copy Markdown
Member

@jackthepunished Thanks for putting up this PR! Just for context, we're updating the slack computation in #1615, and currently self.vars in the Constraint class is only used for that purpose—it isn't part of the public API intended for users. As part of that work, we'll also be re-evaluating whether we need to store variables in the Constraint class at all.

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):

@Iroy30 Iroy30 Aug 20, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants