Skip to content

Commit

Permalink
Fix empty hist (#399)
Browse files Browse the repository at this point in the history
* add failing test

* fix reshaping

* version bump

* add __all__
  • Loading branch information
ewu63 committed May 8, 2024
1 parent 0fd3dd7 commit 4ed7ce9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
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()

0 comments on commit 4ed7ce9

Please sign in to comment.