Skip to content

Commit

Permalink
hotfix for saving self.metadata when using hotstart, with some refact…
Browse files Browse the repository at this point in the history
…oring (#80)
  • Loading branch information
eirikurj authored Mar 5, 2020
1 parent 907b558 commit 32a61fe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
48 changes: 30 additions & 18 deletions pyoptsparse/pyOpt_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from collections import OrderedDict
import datetime
import subprocess
from .pyOpt_MPI import MPI
eps = numpy.finfo(numpy.float64).eps

# =============================================================================
Expand Down Expand Up @@ -182,6 +183,9 @@ def _setHistory(self, storeHistory, hotStart):
self.hist.writeData('conInfo', conInfo)
if objInfo is not None:
self.hist.writeData('objInfo', objInfo)
self._setMetadata()
self.hist.writeData('metadata',self.metadata)


def _masterFunc(self, x, evaluate):
"""
Expand Down Expand Up @@ -551,24 +555,7 @@ def _masterFunc2(self, x, evaluate, writeHist=True):
self.hist.writeData('varInfo', varInfo)
self.hist.writeData('conInfo', conInfo)
self.hist.writeData('objInfo', objInfo)
# we also add some metadata
from pyoptsparse import __version__ as pyoptsparse_version
from .pyOpt_MPI import MPI
options = copy.deepcopy(self.options)
options.pop('defaults') # remove the default list
# we retrieve only the second item which is the actual value
for key,val in options.items():
options[key] = val[1]
# we store the metadata now, and write it later in optimizer calls
# since we need the runtime at the end of optimization
self.metadata = {
'version' : pyoptsparse_version,
'optimizer' : self.name,
'optName' : self.optProb.name,
'nprocs' : MPI.COMM_WORLD.size,
'optOptions': options,
'startTime' : datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
}
self._setMetadata()
self.hist.writeData('metadata',self.metadata)

# Write history if necessary
Expand Down Expand Up @@ -796,6 +783,31 @@ def _communicateSolution(self, sol):
sol.comm = self.optProb.comm

return sol

def _setMetadata(self):
"""
This function is used to set the self.metadata object.
Importantly, this sets the startTime, so should be called just before the start
of the optimization. endTime should be directly appended to the dictionary
after optimization finishes.
"""
options = copy.deepcopy(self.options)
options.pop('defaults') # remove the default list
# we retrieve only the second item which is the actual value
for key,val in options.items():
options[key] = val[1]

from .__init__ import __version__ # importing the pyoptsparse version
# we store the metadata now, and write it later in optimizer calls
# since we need the runtime at the end of optimization
self.metadata = {
'version' : __version__,
'optimizer' : self.name,
'optName' : self.optProb.name,
'nprocs' : MPI.COMM_WORLD.size,
'optOptions': options,
'startTime' : datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
}

def _on_setOption(self, name, value):
"""
Expand Down
20 changes: 17 additions & 3 deletions test/test_hs015.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ def optimize(self, optName, optOptions={}, storeHistory=False,places=5, hotStart
raise unittest.SkipTest('Optimizer not available:', optName)

# Solution
if storeHistory:
self.histFileName = '%s_hs015_Hist.hst' % (optName.lower())
if storeHistory is not None:
if storeHistory == True:
self.histFileName = '%s_hs015_Hist.hst' % (optName.lower())
elif isinstance(storeHistory,str):
self.histFileName = storeHistory
else:
self.histFileName = None

print('histFileName: ', self.histFileName)
print('hotStart: ', hotStart)
sol = opt(optProb, sens=self.sens, storeHistory=self.histFileName, hotStart=hotStart)

# Test printing solution to screen
Expand Down Expand Up @@ -121,6 +125,16 @@ def test_snopt(self):
# now we should do the same optimization without calling them
self.assertEqual(self.nf,0)
self.assertEqual(self.ng,0)
# another test with hotstart, this time with storeHistory = hotStart
self.optimize('snopt',storeHistory=True,hotStart=self.histFileName)
# now we should do the same optimization without calling them
self.assertEqual(self.nf,0)
self.assertEqual(self.ng,0)
# final test with hotstart, this time with a different storeHistory
self.optimize('snopt',storeHistory='snopt_new_hotstart.hst',hotStart=self.histFileName)
# now we should do the same optimization without calling them
self.assertEqual(self.nf,0)
self.assertEqual(self.ng,0)

def test_slsqp(self):
self.optimize('slsqp', storeHistory=True)
Expand Down

0 comments on commit 32a61fe

Please sign in to comment.