-
Notifications
You must be signed in to change notification settings - Fork 277
Recipe for getting local constraints #1014
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6bea3e0
Add enableDebugSol
Joao-Dionisio 441bddc
Add recipe for getting local constraints
Joao-Dionisio 8c2a498
Update changelog (and remove some stuff from other branch)
Joao-Dionisio 83d5a34
Correct stage requirements and allow node input
Joao-Dionisio bb68d04
Add negative check
Joao-Dionisio f4de750
SCIPwriteMIP
Joao-Dionisio ea28e5e
Light changes
Joao-Dionisio 6068ed5
Consistency
Joao-Dionisio 4b26aad
Merge branch 'master' into getLocalConss
Joao-Dionisio 654bc74
Copilot suggestions
Joao-Dionisio 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from pyscipopt import Model, Constraint | ||
|
|
||
| def getLocalConss(model: Model, node = None) -> list[list[Constraint]]: | ||
| """ | ||
| Returns local constraints. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| model : Model | ||
| The model from which to retrieve the local constraints. | ||
| node : Node, optional | ||
| The node from which to retrieve the local constraints. If not provided, the current node is used. | ||
|
|
||
| Returns | ||
| ------- | ||
| list[Constraint] | ||
| A list of local constraints. First entry are global constraints, second entry are all the added constraints. | ||
| """ | ||
|
|
||
| if node is None: | ||
| assert model.getStageName() in ["INITPRESOLVE", "PRESOLVING", "EXITPRESOLVE", "SOLVING"], "Model cannot be called in stage %s." % model.getStageName() | ||
| cur_node = model.getCurrentNode() | ||
| else: | ||
| cur_node = node | ||
|
|
||
| added_conss = [] | ||
| while cur_node is not None: | ||
| added_conss = cur_node.getAddedConss() + added_conss | ||
| cur_node = cur_node.getParent() | ||
|
|
||
| return [model.getConss(), added_conss] | ||
|
|
||
| def getNLocalConss(model: Model, node = None) -> tuple[int,int]: | ||
| """ | ||
| Returns the number of local constraints of a node. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| model : Model | ||
| The model from which to retrieve the number of local constraints. | ||
| node : Node, optional | ||
| The node from which to retrieve the number of local constraints. If not provided, the current node is used. | ||
|
|
||
| Returns | ||
| ------- | ||
| list[int] | ||
| A list of the number of local constraints. First entry is the number of global constraints, second entry is the number of all the added constraints. | ||
| """ | ||
| local_conss = getLocalConss(model, node) | ||
| return [len(local_conss[0]), len(local_conss[1])] |
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
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,38 @@ | ||
| from pyscipopt import Model, SCIP_EVENTTYPE | ||
| from pyscipopt.recipes.getLocalConss import * | ||
| from helpers.utils import random_mip_1 | ||
|
|
||
| def localconss(model, event): | ||
| local_conss = getLocalConss(model) | ||
| assert len(local_conss[1]) == getNLocalConss(model)[1] | ||
| assert len(local_conss[0]) == len(model.getConss()) | ||
| assert local_conss[0] == model.getConss() | ||
|
|
||
| vars = model.getVars() | ||
| if model.getCurrentNode().getNumber() == 1: | ||
| pass | ||
|
|
||
| elif model.getCurrentNode().getNumber() == 2: | ||
| model.data["local_cons1"] = model.addCons(vars[0] + vars[1] <= 1, name="c1", local=True) | ||
| assert getNLocalConss(model)[1] == 1 | ||
| assert getLocalConss(model)[1][0] == model.data["local_cons1"] | ||
|
|
||
| elif model.getCurrentNode().getNumber() == 4: | ||
| local_conss = getLocalConss(model) | ||
| model.data["local_cons2"] = model.addCons(vars[1] + vars[2] <= 1, name="c2", local=True) | ||
| model.data["local_cons3"] = model.addCons(vars[2] + vars[3] <= 1, name="c3", local=True) | ||
| assert getNLocalConss(model)[1] == 3 | ||
| assert getLocalConss(model)[1][0] == model.data["local_cons1"] | ||
| assert getLocalConss(model)[1][1] == model.data["local_cons2"] | ||
| assert getLocalConss(model)[1][2] == model.data["local_cons3"] | ||
|
|
||
| elif model.getCurrentNode().getParent().getNumber() not in [2,4]: | ||
Joao-Dionisio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert getLocalConss(model) == [model.getConss(), []] | ||
|
|
||
| def test_getLocalConss(): | ||
| model = random_mip_1(node_lim=4) | ||
| model.data = {} | ||
|
|
||
| model.attachEventHandlerCallback(localconss, [SCIP_EVENTTYPE.NODEFOCUSED]) | ||
| model.optimize() | ||
| assert len(model.data) == 3 | ||
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.