-
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.
added test and clipping for final result
- Loading branch information
1 parent
874e821
commit 230f92f
Showing
2 changed files
with
51 additions
and
0 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,47 @@ | ||
"""Test class for SLSQP specific tests""" | ||
|
||
# First party modules | ||
from pyoptsparse import Optimization, OPT | ||
|
||
# Local modules | ||
from testing_utils import OptTest | ||
|
||
|
||
class TestSLSQP(OptTest): | ||
|
||
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) |