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 empty hist #399

Merged
merged 4 commits into from
May 8, 2024
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
24 changes: 23 additions & 1 deletion pyoptsparse/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "2.11.0"
__version__ = "2.11.1"

from .pyOpt_history import History
from .pyOpt_variable import Variable
Expand All @@ -19,3 +19,25 @@
from .pyNSGA2.pyNSGA2 import NSGA2
from .pyALPSO.pyALPSO import ALPSO
from .pyParOpt.ParOpt import ParOpt

__all__ = [
"History",
"Variable",
"Gradient",
"Constraint",
"Objective",
"Optimization",
"Optimizer",
"OPT",
"Optimizers",
"Solution",
"SNOPT",
"IPOPT",
"SLSQP",
"CONMIN",
"PSQP",
"NLPQLP",
"NSGA2",
"ALPSO",
"ParOpt",
]
5 changes: 4 additions & 1 deletion pyoptsparse/pyOpt_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,10 @@ def getValues(self, names=None, callCounters=None, major=True, scale=False, stac
# reshape lists into numpy arrays
for name in names:
# we just stack along axis 0
data[name] = np.stack(data[name], axis=0)
if len(data[name]) > 0:
data[name] = np.stack(data[name], axis=0)
else:
data[name] = np.array(data[name])
# we cast 1D arrays to 2D, for scalar values
if data[name].ndim == 1:
data[name] = np.expand_dims(data[name], 1)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_hs015.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ def test_snopt_snstop(self):
# we should get 70/74
self.assert_inform_equal(sol, optInform=74)

def test_snopt_failed_initial(self):
def failed_fun(x_dict):
funcs = {"obj": 0.0, "con": [np.nan, np.nan]}
fail = True
return funcs, fail

self.optName = "SNOPT"
self.setup_optProb()
# swap obj to report NaN
self.optProb.objFun = failed_fun
sol = self.optimize(optOptions={}, storeHistory=True)
self.assert_inform_equal(sol, optInform=61)
# make sure empty history does not error out
hist = History(self.histFileName, flag="r")
hist.getValues()


if __name__ == "__main__":
unittest.main()
Loading