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

Bugfix for optimal objective value #364

Merged
merged 5 commits into from
Dec 4, 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
12 changes: 10 additions & 2 deletions pyoptsparse/pyOpt_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
in the Solution object.
"""

Optimization.__init__(self, optProb.name, None)
super().__init__(optProb.name, None)

# Copy over the variables, constraints, and objectives
self.variables = copy.deepcopy(optProb.variables)
Expand All @@ -43,11 +43,19 @@
xopt = optProb._mapXtoOpt(optProb.processXtoVec(xStar))
# Now set the x-values:
i = 0
for dvGroup in self.variables:
for dvGroup in self.variables.keys():
for var in self.variables[dvGroup]:
var.value = xopt[i]
i += 1

# Now set the f-values
if isinstance(fStar, float) or len(fStar) == 1:
self.objectives[list(self.objectives.keys())[0]].value = float(fStar)
fStar = float(fStar)
else:
for f_name, f in self.objectives.items():
f.value = fStar[f_name]

Check warning on line 57 in pyoptsparse/pyOpt_solution.py

View check run for this annotation

Codecov / codecov/patch

pyoptsparse/pyOpt_solution.py#L56-L57

Added lines #L56 - L57 were not covered by tests

self.optTime = info["optTime"]
self.userObjTime = info["userObjTime"]
self.userSensTime = info["userSensTime"]
Expand Down
11 changes: 4 additions & 7 deletions tests/testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from baseclasses.testing.assertions import assert_dict_allclose, assert_equal
import numpy as np
from numpy.testing import assert_allclose
from pkg_resources import parse_version

# First party modules
from pyoptsparse import OPT, History
Expand Down Expand Up @@ -121,16 +120,14 @@ def assert_solution_allclose(self, sol, tol, partial_x=False):
self.sol_index = 0
# now we assert against the closest solution
# objective
# sol.fStar was broken for earlier versions of SNOPT
if self.optName == "SNOPT" and parse_version(self.optVersion) < parse_version("7.7.7"):
sol_objectives = np.array([sol.objectives[key].value for key in sol.objectives])
else:
sol_objectives = sol.fStar
assert_allclose(sol_objectives, self.fStar[self.sol_index], atol=tol, rtol=tol)
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)
# x
assert_dict_allclose(sol.xStar, self.xStar[self.sol_index], atol=tol, rtol=tol, partial=partial_x)
dv = sol.getDVs()
assert_dict_allclose(dv, self.xStar[self.sol_index], atol=tol, rtol=tol, partial=partial_x)
assert_dict_allclose(sol.xStar, dv, rtol=1e-12)
# lambda
if (
hasattr(self, "lambdaStar")
Expand Down
Loading