-
Notifications
You must be signed in to change notification settings - Fork 269
Add recipe for getting infeasible constraints #857
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
9 commits
Select commit
Hold shift + click to select a range
41c3b63
Add recipe for infeasibilities
Joao-Dionisio 53f61d6
Add tests
Joao-Dionisio 2bc9014
Set feasibility emphasis
Joao-Dionisio 36f0180
Merge branch 'master' into get_infeasibilities
Joao-Dionisio 14d0b52
Changed comment
Joao-Dionisio 195016d
Add binary variable
Joao-Dionisio 23e00b3
Merge branch 'get_infeasibilities' of https://github.com/Joao-Dionisi…
Joao-Dionisio fecd0c9
Overwrite old objective
Joao-Dionisio 6b886e8
Mark comments
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, quicksum | ||
|
||
|
||
def get_infeasible_constraints(orig_model: Model, verbose=False): | ||
""" | ||
Given a model, adds slack variables to all the constraints and minimizes a binary variable that indicates if they're positive. | ||
Positive slack variables correspond to infeasible constraints. | ||
""" | ||
|
||
model = Model(sourceModel=orig_model, origcopy=True) # to preserve the model | ||
|
||
slack = {} | ||
aux = {} | ||
binary = {} | ||
aux_binary = {} | ||
|
||
for c in model.getConss(): | ||
|
||
slack[c.name] = model.addVar(lb=-float("inf"), name="s_"+c.name) | ||
model.addConsCoeff(c, slack[c.name], 1) | ||
binary[c.name] = model.addVar(vtype="B") # Binary variable to get minimum infeasible constraints. See PR #857. | ||
|
||
# getting the absolute value because of <= and >= constraints | ||
aux[c.name] = model.addVar() | ||
model.addCons(aux[c.name] >= slack[c.name]) | ||
model.addCons(aux[c.name] >= -slack[c.name]) | ||
|
||
# modeling aux > 0 => binary = 1 constraint. See https://or.stackexchange.com/q/12142/5352 for an explanation | ||
aux_binary[c.name] = model.addVar(vtype="B") | ||
model.addCons(binary[c.name]+aux_binary[c.name] == 1) | ||
model.addConsSOS1([aux[c.name], aux_binary[c.name]]) | ||
|
||
model.setObjective(quicksum(binary[c.name] for c in orig_model.getConss())) | ||
model.hideOutput() | ||
model.optimize() | ||
|
||
n_infeasibilities_detected = 0 | ||
for c in binary: | ||
if model.isGT(model.getVal(binary[c]), 0): | ||
n_infeasibilities_detected += 1 | ||
print("Constraint %s is causing an infeasibility." % c) | ||
|
||
if verbose: | ||
if n_infeasibilities_detected > 0: | ||
print("If the constraint names are unhelpful, consider giving them\ | ||
a suitable name when creating the model with model.addCons(..., name=\"the_name_you_want\")") | ||
else: | ||
print("Model is feasible.") | ||
|
||
return n_infeasibilities_detected, aux |
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,29 @@ | ||
from pyscipopt import Model | ||
from pyscipopt.recipes.infeasibilities import get_infeasible_constraints | ||
|
||
|
||
def test_get_infeasible_constraints(): | ||
m = Model() | ||
|
||
x = m.addVar(lb=0) | ||
m.setObjective(2*x) | ||
|
||
m.addCons(x <= 4) | ||
|
||
n_infeasibilities_detected = get_infeasible_constraints(m)[0] | ||
assert n_infeasibilities_detected == 0 | ||
|
||
m.addCons(x <= -1) | ||
|
||
n_infeasibilities_detected = get_infeasible_constraints(m)[0] | ||
assert n_infeasibilities_detected == 1 | ||
|
||
m.addCons(x == 2) | ||
|
||
n_infeasibilities_detected = get_infeasible_constraints(m)[0] | ||
assert n_infeasibilities_detected == 1 | ||
|
||
m.addCons(x == -4) | ||
|
||
n_infeasibilities_detected = get_infeasible_constraints(m)[0] | ||
assert n_infeasibilities_detected == 2 |
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.