Skip to content

Commit

Permalink
Fix test for earlier SNOPT versions (#370)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
ewu63 and eirikurj authored Dec 13, 2023
1 parent 75e6479 commit 82bbb68
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pyoptsparse/pySNOPT/pySNOPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from baseclasses.utils import CaseInsensitiveSet
import numpy as np
from numpy import ndarray
from pkg_resources import parse_version

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

# Create the optimization solution
if parse_version(self.version) > parse_version("7.7.0") and parse_version(self.version) < parse_version(
"7.7.7"
):
# SNOPT obj value is buggy and returned as 0, its thus overwritten with the solution objective value
obj = np.array([obj.value * obj.scale for obj in self.optProb.objectives.values()])

sol = self._createSolution(optTime, sol_inform, obj, xs[:nvar], multipliers=pi)
restartDict = {
"cw": cw,
Expand Down
7 changes: 6 additions & 1 deletion tests/testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,16 @@ def assert_solution_allclose(self, sol, tol, partial_x=False):
else:
# assume we have a single solution
self.sol_index = 0

# now we assert against the closest solution
# objective
assert_allclose(sol.fStar, self.fStar[self.sol_index], atol=tol, rtol=tol)
# make sure fStar and sol.objectives values match
assert_allclose(sol.fStar, [obj.value for obj in sol.objectives.values()], rtol=1e-12)
# NOTE this is not true in general, but true for well-behaving optimizations
# which should be the case for all tests
sol_objectives = np.array([obj.value for obj in sol.objectives.values()])
assert_allclose(sol.fStar, sol_objectives, rtol=1e-12)

# x
assert_dict_allclose(sol.xStar, self.xStar[self.sol_index], atol=tol, rtol=tol, partial=partial_x)
dv = sol.getDVs()
Expand Down

0 comments on commit 82bbb68

Please sign in to comment.