forked from mdolab/pyoptsparse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_snopt_bugfix.py
More file actions
188 lines (139 loc) · 5.2 KB
/
Copy pathtest_snopt_bugfix.py
File metadata and controls
188 lines (139 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# This example shows a bug where pySNOPT wouldn't optimize a model that has
# only equality constraints because it thought the problem was trivial. The
# problem is a simple paraboloid. The minimum should be at (7.166667,
# -7.833334), but with the bug, x and y stay at zero.
# Standard Python modules
import unittest
# External modules
import numpy as np
from numpy.testing import assert_allclose
# First party modules
from pyoptsparse import SNOPT, Optimization
from pyoptsparse.pyOpt_error import Error
def objfunc(xdict):
"""Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3"""
x = xdict["x"]
y = xdict["y"]
funcs = {}
funcs["obj"] = (x - 3.0) ** 2 + x * y + (y + 4.0) ** 2 - 3.0
conval = -x + y
funcs["con"] = conval
fail = False
return funcs, fail
def objfunc_no_con(xdict):
"""Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3"""
x = xdict["x"]
y = xdict["y"]
funcs = {}
funcs["obj"] = (x - 3.0) ** 2 + x * y + (y + 4.0) ** 2 - 3.0
fail = False
return funcs, fail
def objfunc_2con(xdict):
"""Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3"""
x = xdict["x"]
y = xdict["y"]
funcs = {}
funcs["obj"] = (x - 3.0) ** 2 + x * y + (y + 4.0) ** 2 - 3.0
conval = -x + y
funcs["con"] = conval * np.ones(2)
funcs["con2"] = (conval + 1) * np.ones(3)
fail = False
return funcs, fail
def sens(xdict, funcs):
"""f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3"""
x = xdict["x"]
y = xdict["y"]
funcsSens = {
"obj": {
"x": 2.0 * x - 6.0 + y,
"y": 2.0 * y + 8.0 + x,
}
}
fail = False
return funcsSens, fail
con_jac = {}
con_jac["x"] = np.array(-1.0)
con_jac["y"] = np.array(1.0)
class TestSNOPTBug(unittest.TestCase):
def test_opt(self):
# Optimization Object
optProb = Optimization("Paraboloid", objfunc)
# Design Variables
optProb.addVarGroup("x", 1, varType="c", lower=-50.0, upper=50.0, value=0.0)
optProb.addVarGroup("y", 1, varType="c", lower=-50.0, upper=50.0, value=0.0)
# Objective
optProb.addObj("obj")
# Equality Constraint
optProb.addConGroup("con", 1, lower=-15.0, upper=-15.0, wrt=["x", "y"], linear=True, jac=con_jac)
# Check optimization problem:
print(optProb)
test_name = "bugfix_SNOPT_test_opt"
optOptions = {
"Major feasibility tolerance": 1e-1,
"Print file": f"{test_name}.out",
"Summary file": f"{test_name}_summary.out",
}
# Optimizer
try:
opt = SNOPT(options=optOptions)
except Error:
raise unittest.SkipTest("Optimizer not available: SNOPT")
sol = opt(optProb, sens=sens)
# Check Solution 7.166667, -7.833334
tol = 1e-6
assert_allclose(sol.variables["x"][0].value, 7.166667, atol=tol, rtol=tol)
assert_allclose(sol.variables["y"][0].value, -7.833333, atol=tol, rtol=tol)
def test_opt_bug1(self):
# Due to a new feature, there is a TypeError when you optimize a model without a constraint.
optProb = Optimization("Paraboloid", objfunc_no_con)
# Design Variables
optProb.addVarGroup("x", 1, varType="c", lower=-50.0, upper=50.0, value=0.0)
optProb.addVarGroup("y", 1, varType="c", lower=-50.0, upper=50.0, value=0.0)
# Objective
optProb.addObj("obj")
test_name = "bugfix_SNOPT_bug1"
optOptions = {
"Major feasibility tolerance": 1e-1,
"Print file": f"{test_name}.out",
"Summary file": f"{test_name}_summary.out",
}
# Optimizer
try:
opt = SNOPT(options=optOptions)
except Error:
raise unittest.SkipTest("Optimizer not available: SNOPT")
opt(optProb, sens=sens)
def test_opt_bug_print_2con(self):
# Optimization Object
optProb = Optimization("Paraboloid", objfunc_2con)
# Design Variables
optProb.addVarGroup("x", 1, varType="c", lower=-50.0, upper=50.0, value=0.0)
optProb.addVarGroup("y", 1, varType="c", lower=-50.0, upper=50.0, value=0.0)
# Objective
optProb.addObj("obj")
con_jac2 = {}
con_jac2["x"] = -np.ones((2, 1))
con_jac2["y"] = np.ones((2, 1))
con_jac3 = {}
con_jac3["x"] = -np.ones((3, 1))
con_jac3["y"] = np.ones((3, 1))
# Equality Constraint
optProb.addConGroup("con", 2, lower=-15.0, upper=-15.0, wrt=["x", "y"], linear=True, jac=con_jac2)
optProb.addConGroup("con2", 3, lower=-15.0, upper=-15.0, wrt=["x", "y"], linear=True, jac=con_jac3)
# Check optimization problem:
print(optProb)
test_name = "bugfix_SNOPT_bug_print_2con"
optOptions = {
"Major feasibility tolerance": 1e-1,
"Print file": f"{test_name}.out",
"Summary file": f"{test_name}_summary.out",
}
# Optimizer
try:
opt = SNOPT(options=optOptions)
except Error:
raise unittest.SkipTest("Optimizer not available: SNOPT")
sol = opt(optProb, sens=sens)
print(sol)
if __name__ == "__main__":
unittest.main()