-
Notifications
You must be signed in to change notification settings - Fork 275
Change addConsLocal(), addConsNode() to accept ExprCons
#1151
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
Open
Joao-Dionisio
wants to merge
3
commits into
master
Choose a base branch
from
addConsNode-ExprCons
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| from pyscipopt import Branchrule, SCIP_RESULT | ||
| from helpers.utils import random_mip_1 | ||
|
|
||
|
|
||
| class MyBranchrule(Branchrule): | ||
| """ | ||
| A branching rule that tests addConsNode by adding constraints to child nodes. | ||
| """ | ||
|
|
||
| def __init__(self, model): | ||
| self.model = model | ||
| self.addConsNode_called = False | ||
|
|
||
| def branchexeclp(self, allowaddcons): | ||
| if not allowaddcons: | ||
| return {"result": SCIP_RESULT.DIDNOTRUN} | ||
|
|
||
| # Branch on the first variable (just to test) | ||
| var = self.model.getVars()[0] | ||
| self.branch_var = var | ||
|
|
||
| # Create two child nodes | ||
| child1 = self.model.createChild(1, self.model.getLPObjVal()) | ||
| child2 = self.model.createChild(1, self.model.getLPObjVal()) | ||
|
|
||
| # Test addConsNode with ExprCons | ||
| cons1 = self.model.addConsNode(child1, var == var.getLbGlobal(), name="branch_down") | ||
| self.addConsNode_called = True | ||
| assert cons1 is not None, "addConsNode should return a Constraint" | ||
|
|
||
| # Making it infeasible to ensure down branch is taken | ||
| cons2 = self.model.addConsNode(child2, var <= var.getLbGlobal()-1, name="branch_up") | ||
| assert cons2 is not None, "addConsNode should return a Constraint" | ||
|
|
||
| return {"result": SCIP_RESULT.BRANCHED} | ||
|
|
||
| def branchexecps(self, allowaddcons): | ||
| return {"result": SCIP_RESULT.DIDNOTRUN} | ||
|
|
||
|
|
||
| class MyBranchruleLocal(Branchrule): | ||
| """ | ||
| A branching rule that tests addConsLocal by adding constraints to the current node. | ||
| """ | ||
|
|
||
| def __init__(self, model): | ||
| self.model = model | ||
| self.addConsLocal_called = False | ||
| self.call_count = 0 | ||
|
|
||
| def branchexeclp(self, allowaddcons): | ||
| if not allowaddcons: | ||
| return {"result": SCIP_RESULT.DIDNOTRUN} | ||
|
|
||
| self.call_count += 1 | ||
|
|
||
| # Only test on the first call | ||
| if self.call_count > 1: | ||
| return {"result": SCIP_RESULT.DIDNOTRUN} | ||
|
|
||
| # Get branching candidates | ||
| branch_cands, branch_cand_sols, branch_cand_fracs, ncands, npriocands, nimplcands = self.model.getLPBranchCands() | ||
|
|
||
| if npriocands == 0: | ||
| return {"result": SCIP_RESULT.DIDNOTRUN} | ||
|
|
||
| v = self.model.getVars()[0] | ||
| cons = self.model.addConsLocal(v <= v.getLbGlobal() - 1) | ||
| self.addConsLocal_called = True | ||
| assert cons is not None, "addConsLocal should return a Constraint" | ||
|
|
||
| return {"result": SCIP_RESULT.BRANCHED} | ||
|
|
||
| def branchexecps(self, allowaddcons): | ||
| return {"result": SCIP_RESULT.DIDNOTRUN} | ||
|
|
||
|
|
||
| def test_addConsNode(): | ||
| """Test that addConsNode works with ExprCons.""" | ||
| m = random_mip_1(node_lim=3, small=True) | ||
|
|
||
| branchrule = MyBranchrule(m) | ||
| m.includeBranchrule( | ||
| branchrule, | ||
| "test_addConsNode", | ||
| "test addConsNode with ExprCons", | ||
| priority=10000000, | ||
| maxdepth=-1, | ||
| maxbounddist=1 | ||
| ) | ||
|
|
||
| var_to_be_branched = m.getVars()[0] | ||
| var_to_be_branched_lb = var_to_be_branched.getLbGlobal() | ||
|
|
||
| m.optimize() | ||
|
|
||
| assert branchrule.addConsNode_called, "addConsNode should have been called" | ||
|
|
||
| var_to_be_branched_val = m.getSolVal(expr=var_to_be_branched, sol=None) | ||
| assert var_to_be_branched_val == var_to_be_branched_lb, \ | ||
| f"Variable should be equal to its lower bound {var_to_be_branched_lb}, but got {var_to_be_branched_val}" | ||
|
|
||
|
|
||
|
|
||
| def test_addConsLocal(): | ||
| """Test that addConsLocal works with ExprCons.""" | ||
| m = random_mip_1(node_lim=500, small=True) | ||
|
|
||
| branchrule = MyBranchruleLocal(m) | ||
| m.includeBranchrule( | ||
| branchrule, | ||
| "test_addConsLocal", | ||
| "test addConsLocal with ExprCons", | ||
| priority=10000000, | ||
| maxdepth=-1, | ||
| maxbounddist=1 | ||
| ) | ||
|
|
||
| m.optimize() | ||
| assert branchrule.addConsLocal_called, "addConsLocal should have been called" | ||
| assert m.getStatus() == "infeasible", "The problem should be infeasible after adding the local constraint" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.