-
Notifications
You must be signed in to change notification settings - Fork 267
Add support for knapsack constraints #975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for knapsack constraints by allowing an optional flag in addCons, and updates tests to validate the new functionality.
- Introduces a new test function for knapsack constraints in tests/test_cons.py
- Modifies tests for linear constraint coefficient retrieval in tests/test_cons.py
- Updates CHANGELOG.md to document the new knapsack constraint support
Reviewed Changes
Copilot reviewed 2 out of 4 changed files in this pull request and generated no comments.
File | Description |
---|---|
tests/test_cons.py | Added tests for knapsack constraint flag and related methods |
CHANGELOG.md | Updated changelog to include knapsack constraint support |
Files not reviewed (2)
- src/pyscipopt/scip.pxd: Language not supported
- src/pyscipopt/scip.pxi: Language not supported
Comments suppressed due to low confidence (2)
tests/test_cons.py:225
- Consider adding negative test cases for constraints without the knapsack flag to verify that knapsack-specific methods are not available in non-knapsack constraints.
knapsack_cons = m.addCons(4*x + 2*y <= 10, knapsack=True)
tests/test_cons.py:251
- Clarify and assert the expected coefficient order from getValsLinear to ensure consistency, especially if constraint transformations occur during optimization.
assert m.getValsLinear(c1) == [2,1]
Updated the default values of the logical constraint names, because they would cause issues (like the one that happened with the indicator constraints - can't find the issue atm) |
Values are |
Can someone tell copilot that pxi and pxd is Python? |
Thank you, @DominikKamp! By the way, how do you feel about the default names for indicator and logical constraints? Ie, if you add 1 AND constraint and two OR constraints without naming them, they default to "c1AND", "c2OR", "c3OR". Also, should we try to lump all the methods together? For example, should And finally, yeah Copilot is still a bit dummy, unfortunately. Maybe next year :) |
If there is a common counter for constraints, it should not be necessary to add the constraint type to the name if there is a way to get the name of the constraint handler separately (without it, this is also more like the generic names in SCIP). Yes, there should be wrappers for |
…into other-con-types
Okay, everything seems to be working now. I decided to keep the same structure as the other constraint handlers, and one needs to do @DominikKamp, there doesn't seem to be a check for the variables being binary on the SCIP side, despite the constraint handler's description. Is this intentional or an oversight? EDIT: My bad, it is there, I was just not running SCIP in debug mode. Still, feels strange to be able to use knapsack constraints with continuous variables and then getting incorrect results. Maybe the check should be in pyscipopt? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request introduces support for knapsack constraints by adding new knapsack-specific methods in both the Python layer and its C interface, and extends the test suite to verify correct behavior.
- New functions for creating, modifying, and querying knapsack constraints have been added (e.g. addConsKnapsack, isKnapsack, addCoefKnapsack, getWeightsKnapsack, getDualsolKnapsack, and getDualfarkasKnapsack).
- Test cases for the knapsack constraint functionality have been implemented in tests/test_cons.py.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
tests/test_cons.py | New tests verifying knapsack creation, coefficient addition, and dual values. |
src/pyscipopt/scip.pxi | New knapsack constraint methods implemented and integrated into Model and Constraint interfaces. |
src/pyscipopt/scip.pxd | External declarations for knapsack functions have been added. |
CHANGELOG.md | Changelog updated to mention knapsack constraint support. |
@mmghannam can you take a quick look? Mostly on the design decisions, which I'd pass on to the other handlers as well.
|
Currently, the knapsack constraint handler expects binary variables and there are multiple assertions on this. But there should also be a direct error because the knapsacks I have seen so far can usually handle multiple copies of an item so it can hardly be inferred that integer variables are not supported. This should happen in SCIP as already for AND, OR, XOR, LogicOR, and SetPPC constraints. |
@DominikKamp I was more concerned with it not being a direct error with continuous variables. This is with SCIP 9.2.3, so it might not apply in master.
|
Any non-binary variable can lead to wrong results, will add an error later. |
|
Hmm I don't know, if we're going to change SCIP's arguments, then imo it should be for something that's easier to use. To me, the tuple way seems more cumbersome. Agreed on the |
Yes adding an assert is better :) |
How do you feel about adding the missing methods? They are On one hand, having more functionality is nice. But maybe these don't belong in the Python interface, and would clutter it maybe unnecessarily. A question of whether PySCIPOpt should start catering to more advanced users, or keep making it somewhat beginner/intermediate friendly. EDIT: okay, going to agree with the past-me. "it's not reasonable to expect all methods to be interfaced, let alone tested." In a future PR, this may be discussed again, but it makes more sense to have basic functionality first. |
Why was the |
@DominikKamp Mo's opinion:
Which I agree with. I think in general, if the naming is not the same, for consistency I'll keep them separate. If it was called |
I just understood Mo differently, to let |
Ah, yeah I can do that for Do you mean wrapping EDIT: My god, there are so many repeat methods, I'm just now realizing. From the documentation
With such a generic name, I think people would incorrectly assume that any constraint is supported. I don't know if wrapping this one makes sense. EDIT2: I should have looked better before commenting. So you're in favor of just splitting between nonlinear constraints and all others? Something like
? |
Indeed, |
Fix #941
Still not done, but this is pretty fun, in a turn off your brain and code kind of way.
Currently having some problems with the compilation, but likely due to my local SCIP, will re-check later.