-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Strongly enforce SLSQP Bounds (#413)
* Strongly enforce SLSQP Bounds * added test and clipping for final result * update test * fix typo * isort and black fixes * added warning * use py opt warnings * typo * formatting fixes --------- Co-authored-by: Marco Mangano <[email protected]>
- Loading branch information
1 parent
bc021e4
commit f0db0af
Showing
3 changed files
with
68 additions
and
3 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
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,46 @@ | ||
"""Test class for SLSQP specific tests""" | ||
|
||
# Standard Python modules | ||
import unittest | ||
|
||
# First party modules | ||
from pyoptsparse import OPT, Optimization | ||
|
||
|
||
class TestSLSQP(unittest.TestCase): | ||
def test_slsqp_strong_bound_enforcement(self): | ||
""" | ||
Test that SLSQP will never evaluate the function or gradient outside | ||
the design variable bounds. Without strong bound enforcement, the | ||
optimizer will step outside the bounds and a ValueError will be raised. | ||
With strong bound enforement, this code will run without raising any | ||
errors | ||
""" | ||
|
||
def objfunc(xdict): | ||
x = xdict["xvars"] | ||
funcs = {} | ||
if x[0] < 0: | ||
raise ValueError("Function cannot be evaluated below 0.") | ||
funcs["obj"] = (x[0] + 1.0) ** 2 | ||
fail = False | ||
return funcs, fail | ||
|
||
def sens(xdict, funcs): | ||
x = xdict["xvars"] | ||
if x[0] < 0: | ||
raise ValueError("Function cannot be evaluated below 0.") | ||
funcsSens = { | ||
"obj": {"xvars": [2 * (x[0] + 1.0)]}, | ||
} | ||
fail = False | ||
return funcsSens, fail | ||
|
||
optProb = Optimization("Problem with Error Region", objfunc) | ||
optProb.addVarGroup("xvars", 1, lower=[0], value=[2]) | ||
optProb.addObj("obj") | ||
opt = OPT("SLSQP") | ||
sol = opt(optProb, sens=sens) | ||
self.assertEqual(sol.optInform["value"], 0) | ||
self.assertGreaterEqual(sol.xStar["xvars"][0], 0) | ||
self.assertAlmostEqual(sol.xStar["xvars"][0], 0, places=9) |