Skip to content

Commit 82bbb68

Browse files
ewu63eirikurj
andauthored
Fix test for earlier SNOPT versions (#370)
* fix test for earlier SNOPT versions * don't store obj value for buggy SNOPT * update testing asserts * adjust for scaling * black --------- Co-authored-by: Eirikur Jonsson <[email protected]>
1 parent 75e6479 commit 82bbb68

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pyoptsparse/pySNOPT/pySNOPT.py

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from baseclasses.utils import CaseInsensitiveSet
1616
import numpy as np
1717
from numpy import ndarray
18+
from pkg_resources import parse_version
1819

1920
# Local modules
2021
from ..pyOpt_error import Error
@@ -520,6 +521,12 @@ def __call__(
520521
sol_inform["text"] = self.informs[inform]
521522

522523
# Create the optimization solution
524+
if parse_version(self.version) > parse_version("7.7.0") and parse_version(self.version) < parse_version(
525+
"7.7.7"
526+
):
527+
# SNOPT obj value is buggy and returned as 0, its thus overwritten with the solution objective value
528+
obj = np.array([obj.value * obj.scale for obj in self.optProb.objectives.values()])
529+
523530
sol = self._createSolution(optTime, sol_inform, obj, xs[:nvar], multipliers=pi)
524531
restartDict = {
525532
"cw": cw,

tests/testing_utils.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,16 @@ def assert_solution_allclose(self, sol, tol, partial_x=False):
118118
else:
119119
# assume we have a single solution
120120
self.sol_index = 0
121+
121122
# now we assert against the closest solution
122123
# objective
123124
assert_allclose(sol.fStar, self.fStar[self.sol_index], atol=tol, rtol=tol)
124125
# make sure fStar and sol.objectives values match
125-
assert_allclose(sol.fStar, [obj.value for obj in sol.objectives.values()], rtol=1e-12)
126+
# NOTE this is not true in general, but true for well-behaving optimizations
127+
# which should be the case for all tests
128+
sol_objectives = np.array([obj.value for obj in sol.objectives.values()])
129+
assert_allclose(sol.fStar, sol_objectives, rtol=1e-12)
130+
126131
# x
127132
assert_dict_allclose(sol.xStar, self.xStar[self.sol_index], atol=tol, rtol=tol, partial=partial_x)
128133
dv = sol.getDVs()

0 commit comments

Comments
 (0)