-
Notifications
You must be signed in to change notification settings - Fork 269
Add possibility for nonlinear objective function #781
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
19 commits
Select commit
Hold shift + click to select a range
a146f52
addPiecewiseLinearCons method and test
Joao-Dionisio 8eae73a
Update CHANGELOG
Joao-Dionisio 1a32614
Fix recommendations
Joao-Dionisio d3f5ba8
Add possibility for nonlinear objective
Joao-Dionisio 0f3f4c3
Add nonlinear objective test
Joao-Dionisio 995796b
Modify other tests
Joao-Dionisio 0bc5b27
Update CHANGELOG
Joao-Dionisio 8eb76bb
Merge branch 'master' into non_linear_objective
Joao-Dionisio 0d78d09
Merge branch 'master' into non_linear_objective
Joao-Dionisio 0b9adcb
Add untested changes to fix test and assert
Opt-Mucca 951ffd8
Merge branch 'master' of https://github.com/scipopt/PySCIPOpt into no…
Joao-Dionisio c8939c7
Move to recipes folder
Joao-Dionisio 970c5a9
Update CHANGELOG
Joao-Dionisio 7678009
Merge branch 'master' of https://github.com/scipopt/PySCIPOpt into no…
Joao-Dionisio c208ee7
Segfault commit
Joao-Dionisio a82bfd5
Fix segfault
Joao-Dionisio d2f9999
Reset locale to standard
Joao-Dionisio 37d4f7a
Fix test
mmghannam ed3de60
Small refactoring
mmghannam 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,18 @@ | ||
from pyscipopt import Model | ||
|
||
def set_nonlinear_objective(model: Model, expr, sense="minimize"): | ||
""" | ||
Takes a nonlinear expression and performs an epigraph reformulation. | ||
""" | ||
|
||
assert expr.degree() > 1, "For linear objectives, please use the setObjective method." | ||
new_obj = model.addVar(lb=-float("inf"),obj=1) | ||
if sense == "minimize": | ||
model.addCons(expr <= new_obj) | ||
model.setMinimize() | ||
elif sense == "maximize": | ||
model.addCons(expr >= new_obj) | ||
model.setMaximize() | ||
else: | ||
raise Warning("unrecognized optimization sense: %s" % sense) | ||
|
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
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,52 @@ | ||
from pyscipopt import Model, exp, log, sqrt, sin | ||
from pyscipopt.recipes.nonlinear import set_nonlinear_objective | ||
|
||
def test_nonlinear_objective(): | ||
model = Model() | ||
|
||
v = model.addVar() | ||
w = model.addVar() | ||
x = model.addVar() | ||
y = model.addVar() | ||
z = model.addVar() | ||
|
||
obj = 0 | ||
obj += exp(v) | ||
obj += log(w) | ||
obj += sqrt(x) | ||
obj += sin(y) | ||
obj += z**3 * y | ||
|
||
model.addCons(v + w + x + y + z <= 1) | ||
set_nonlinear_objective(model, obj, sense='maximize') | ||
|
||
model2 = Model() | ||
|
||
a = model2.addVar() | ||
b = model2.addVar() | ||
c = model2.addVar() | ||
d = model2.addVar() | ||
e = model2.addVar() | ||
|
||
obj2 = 0 | ||
obj2 += exp(a) | ||
obj2 += log(b) | ||
obj2 += sqrt(c) | ||
obj2 += sin(d) | ||
obj2 += e**3 * d | ||
|
||
model2.addCons(a + b + c + d + e <= 1) | ||
|
||
t = model2.addVar(lb=-float("inf"),obj=1) | ||
model2.addCons(t <= obj2) | ||
model2.setMaximize() | ||
|
||
obj_expr = model.getObjective() | ||
assert obj_expr.degree() == 1 | ||
|
||
model.setParam("numerics/epsilon", 10**(-5)) # bigger eps due to nonlinearities | ||
model2.setParam("numerics/epsilon", 10**(-5)) | ||
|
||
model.optimize() | ||
model2.optimize() | ||
assert model.isEQ(model.getObjVal(), model2.getObjVal()) |
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
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.