Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test for earlier SNOPT versions #370

Merged
merged 5 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Check warning on line 18 in pyoptsparse/pySNOPT/pySNOPT.py

View check run for this annotation

Codecov / codecov/patch

pyoptsparse/pySNOPT/pySNOPT.py#L18

Added line #L18 was not covered by tests

# Local modules
from ..pyOpt_error import Error
Expand Down Expand Up @@ -520,6 +521,12 @@
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
Loading