Skip to content

Commit 87939d6

Browse files
authored
Remove flat sensitivity dict (#165)
* updated travis * removed dependency 'six' * removed flat sens dict * removed other unused code * Update .travis.yml * updated travis and made nested dicts look better * Update .travis.yml * fixed travis
1 parent d344c12 commit 87939d6

File tree

5 files changed

+63
-78
lines changed

5 files changed

+63
-78
lines changed

.travis.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ services:
1111
- docker
1212
env:
1313
global:
14-
- REPO_NAME=$(eval basename $TRAVIS_REPO_SLUG)
14+
- REPO_NAME=$(basename $TRAVIS_REPO_SLUG)
15+
- PR_FROM=$(dirname $TRAVIS_PULL_REQUEST_SLUG)
1516
- DOCKER_WORKING_DIR=/home/mdolabuser/packages/$REPO_NAME
1617
- DOCKER_MOUNT_DIR=/home/mdolabuser/travis/$REPO_NAME
1718
- SNOPT_DIR=$DOCKER_WORKING_DIR/$REPO_NAME/pySNOPT/source
@@ -25,7 +26,7 @@ env:
2526
- DOCKER_TAG=u20-gcc-ompi-latest
2627

2728
before_install:
28-
- if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then
29+
- if [ $PR_FROM != "mdolab" ]; then
2930
export DOCKER_REPO=public;
3031
else
3132
export DOCKER_REPO=private;
@@ -42,15 +43,15 @@ before_install:
4243

4344
install:
4445
# We back up the proprietary source codes first
45-
- if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
46+
- if [ $PR_FROM == "mdolab" ]; then
4647
docker exec -it app /bin/bash -c "cp -r $SNOPT_DIR \$HOME/SNOPT && cp -r $NLPQLP_DIR \$HOME/NLPQLP";
4748
fi
4849
# We thrown away the existing repo in Docker, and copy the new one in-place
4950
- docker exec -it app /bin/bash -c "rm -rf $DOCKER_WORKING_DIR && cp -r $DOCKER_MOUNT_DIR $DOCKER_WORKING_DIR"
5051
# We also remove the python installation
5152
- docker exec -it app /bin/bash -c "rm -rf $DOCKER_WORKING_DIR/build"
5253
# Copy back the proprietary codes
53-
- if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
54+
- if [ $PR_FROM == "mdolab" ]; then
5455
docker exec -it app /bin/bash -c "cp -r \$HOME/NLPQLP/* $NLPQLP_DIR/ && cp -r \$HOME/SNOPT/* $SNOPT_DIR/";
5556
fi
5657
# Build and install

pyoptsparse/pyOpt_optimization.py

Lines changed: 28 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,55 +1338,29 @@ def processObjectiveGradient(self, funcsSens):
13381338
nobj = len(self.objectives)
13391339
gobj = np.zeros((nobj, self.ndvs))
13401340

1341-
cond = False
1342-
# this version is required for python 3 compatibility
1343-
cond = isinstance(list(funcsSens.keys())[0], str)
1344-
if cond: # we have a nested dictionary
1345-
iObj = 0
1346-
for objKey in self.objectives.keys():
1347-
if objKey in funcsSens:
1348-
for dvGroup in funcsSens[objKey]:
1349-
if dvGroup in dvGroups:
1350-
# Now check that the array is the correct length:
1351-
ss = self.dvOffset[dvGroup]
1352-
tmp = np.array(funcsSens[objKey][dvGroup]).squeeze()
1353-
if tmp.size == ss[1] - ss[0]:
1354-
# Everything checks out so set:
1355-
gobj[iObj, ss[0] : ss[1]] = tmp
1356-
else:
1357-
raise Error(
1358-
(
1359-
"The shape of the objective derivative for dvGroup '{}' is the incorrect length. "
1360-
+ "Expecting a shape of {} but received a shape of {}."
1361-
).format(dvGroup, (ss[1] - ss[0],), funcsSens[objKey][dvGroup].shape)
1362-
)
1363-
else:
1364-
raise Error("The dvGroup key '%s' is not valid" % dvGroup)
1365-
else:
1366-
raise Error("The key for the objective gradient, '%s', was not found." % objKey)
1367-
iObj += 1
1368-
else: # Then it must be a tuple; assume flat dict
1369-
for (objKey, dvGroup), val in funcsSens.items():
1370-
if objKey in self.objectives.keys():
1371-
try:
1372-
iObj = self.objectiveIdx[objKey]
1373-
except KeyError:
1374-
raise Error("The key for the objective gradient, '%s', was not found." % objKey)
1375-
try:
1341+
iObj = 0
1342+
for objKey in self.objectives.keys():
1343+
if objKey in funcsSens:
1344+
for dvGroup in funcsSens[objKey]:
1345+
if dvGroup in dvGroups:
1346+
# Now check that the array is the correct length:
13761347
ss = self.dvOffset[dvGroup]
1377-
except KeyError:
1378-
raise Error("The dvGroup key '%s' is not valid" % dvGroup)
1379-
tmp = np.array(val).squeeze()
1380-
if tmp.size == ss[1] - ss[0]:
1381-
# Everything checks out so set:
1382-
gobj[iObj, ss[0] : ss[1]] = tmp
1348+
tmp = np.array(funcsSens[objKey][dvGroup]).squeeze()
1349+
if tmp.size == ss[1] - ss[0]:
1350+
# Everything checks out so set:
1351+
gobj[iObj, ss[0] : ss[1]] = tmp
1352+
else:
1353+
raise Error(
1354+
(
1355+
"The shape of the objective derivative for dvGroup '{}' is the incorrect length. "
1356+
+ "Expecting a shape of {} but received a shape of {}."
1357+
).format(dvGroup, (ss[1] - ss[0],), funcsSens[objKey][dvGroup].shape)
1358+
)
13831359
else:
1384-
raise Error(
1385-
(
1386-
"The shape of the objective derivative for dvGroup '{}' is the incorrect length. "
1387-
+ "Expecting a shape of {} but received a shape of {}."
1388-
).format(dvGroup, (ss[1] - ss[0],), val.shape)
1389-
)
1360+
raise Error("The dvGroup key '%s' is not valid" % dvGroup)
1361+
else:
1362+
raise Error("The key for the objective gradient, '%s', was not found." % objKey)
1363+
iObj += 1
13901364

13911365
# Note that we looped over the keys in funcsSens[objKey]
13921366
# and not the variable keys since a variable key not in
@@ -1468,21 +1442,17 @@ def processConstraintJacobian(self, gcon):
14681442
ndvs = ss[1] - ss[0]
14691443

14701444
gotDerivative = False
1471-
try: # Try using a nested dictionary return
1445+
try:
14721446
if dvGroup in gcon[iCon]:
14731447
tmp = convertToCOO(gcon[iCon][dvGroup])
14741448
gotDerivative = True
14751449
except KeyError:
1476-
try: # Using tuple dictornary return
1477-
tmp = convertToCOO(gcon[iCon, dvGroup])
1478-
gotDerivative = True
1479-
except KeyError:
1480-
raise Error(
1481-
(
1482-
"The constraint Jacobian entry for '{}' with respect to '{}', as was defined in addConGroup(), "
1483-
+ "was not found in constraint Jacobian dictionary provided."
1484-
).format(con.name, dvGroup)
1485-
)
1450+
raise Error(
1451+
(
1452+
"The constraint Jacobian entry for '{}' with respect to '{}', as was defined in addConGroup(), "
1453+
+ "was not found in constraint Jacobian dictionary provided."
1454+
).format(con.name, dvGroup)
1455+
)
14861456
if not gotDerivative:
14871457
# All keys for this constraint must be returned
14881458
# since the user has explictly specified the wrt.

test/test_large_sparse.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,25 @@ def sens(xdict, funcs):
3535
x = xdict["x"]
3636
y = xdict["y"]
3737
z = xdict["z"]
38-
funcsSens = {}
38+
3939
funcsSens = {
40-
("obj", "x"): [2 * x],
41-
("obj", "y"): 4 * y,
42-
("obj", "z"): 3 * np.ones(2 * N),
43-
("con1", "x"): 2.05 * x * (x * x) ** 0.025,
44-
("con2", "x"): 4 * x ** 3,
45-
("con2", "y"): np.ones(N),
46-
("con2", "z"): 2 * z,
47-
("con3", "x"): 1.0,
48-
("con3", "z"): np.ones(2 * N),
40+
"obj": {
41+
"x": 2 * x,
42+
"y": 4 * y,
43+
"z": 3 * np.ones(2 * N),
44+
},
45+
"con1": {
46+
"x": 2.05 * x * (x * x) ** 0.025,
47+
},
48+
"con2": {
49+
"x": 4 * x ** 3,
50+
"y": np.ones(N),
51+
"z": 2 * z,
52+
},
53+
"con3": {
54+
"x": 1.0,
55+
"z": np.ones(2 * N),
56+
},
4957
}
5058

5159
return funcsSens, False

test/test_snopt_bugfix.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,13 @@ def sens(xdict, funcs):
5656
"""f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3"""
5757
x = xdict["x"]
5858
y = xdict["y"]
59-
funcsSens = {}
6059

61-
funcsSens["obj", "x"] = 2.0 * x - 6.0 + y
62-
funcsSens["obj", "y"] = 2.0 * y + 8.0 + x
60+
funcsSens = {
61+
"obj": {
62+
"x": 2.0 * x - 6.0 + y,
63+
"y": 2.0 * y + 8.0 + x,
64+
}
65+
}
6366

6467
fail = False
6568
return funcsSens, fail

test/test_snopt_user_termination.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ def sens(self, xdict, funcs):
4040
"""f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3"""
4141
x = xdict["x"]
4242
y = xdict["y"]
43-
funcsSens = {}
4443

45-
funcsSens["obj", "x"] = 2.0 * x - 6.0 + y
46-
funcsSens["obj", "y"] = 2.0 * y + 8.0 + x
44+
funcsSens = {
45+
"obj": {
46+
"x": 2.0 * x - 6.0 + y,
47+
"y": 2.0 * y + 8.0 + x,
48+
}
49+
}
4750

4851
if self.sens_count > self.max_sens:
4952
fail = 2

0 commit comments

Comments
 (0)