-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add possibility for nonlinear objective function (#781)
* addPiecewiseLinearCons method and test * Update CHANGELOG * Fix recommendations * Add possibility for nonlinear objective * Add nonlinear objective test * Modify other tests * Update CHANGELOG * Add untested changes to fix test and assert * Move to recipes folder * Update CHANGELOG * Fix segfault * Reset locale to standard * Fix test * Small refactoring --------- Co-authored-by: Mark Turner <[email protected]> Co-authored-by: Mohammed Ghannam <[email protected]>
- Loading branch information
1 parent
30148ab
commit e8ec238
Showing
9 changed files
with
96 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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