问题
The sample case for recording/accessing derivatives in this link works fine for me, if i simply copy paste the code. I am trying to figure out why the similar method does not work for my own problem . I set the recording options identical. I tried to list some differences but not sure if they can be the cause of this:
- I have an extra maxiter option so optimizer ''fails'' after 3 iterations.
- I can access all other recorded variables (objectives, constraints,list_output)
- I can not access to list_inputs though this is also the case for the sample code in the previously mentioned link.
- I have added the N2 too, thinking that the single component wrapped/"stucked" into one group can be the cause; and then I changed the sellar problems structure I still can access derivatives in the sellar problem but not in mine.
I am not sure what more insight I can give. There can be an obvious thing i am missing. Any idea?
Here is the N^2 diagrams of my problem and the sample problem.
Here is the code snippet.
prob = Problem()
probname = prob.model = Group()
recordername='recorder.sql'
GLOBAL_DESIGN_VAR = IndepVarComp()
#"The design variables Indepvar - Promotes none of the variables"
probname.add_subsystem('GLOBAL_DESIGN_VAR', GLOBAL_DESIGN_VAR)
listofloadcases=[inp.fatiguename]
AERO_GroupName='AERO%s' %''.join(listofloadcases)
probname.add_subsystem(AERO_GroupName, AERO(loadcase=listofloadcases))
for key,val in infodict['sysdes']['desvar'].items():
GLOBAL_DESIGN_VAR.add_output(key, val['init'])
probname.add_design_var('GLOBAL_DESIGN_VAR.{}'.format(key),lower=val['min'], upper=val['max'])
probname.connect('GLOBAL_DESIGN_VAR.{}'.format(key), '{}.{}'.format(AERO_GroupName,key))
probname.add_objective('{}.cumPSDerror'.format(AERO_GroupName))
probname.add_constraint('{}.cumDELerror'.format(AERO_GroupName),upper=0.1)
prob.driver=ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'
prob.driver.options['disp'] = True
prob.driver.options['tol'] = 1e-9
recorder = SqliteRecorder(recordername)
prob.driver.add_recorder(recorder)
prob.driver.recording_options['includes'] = []
prob.driver.recording_options['record_inputs'] = True
# prob.driver.recording_options['record_outputs'] = True
prob.driver.recording_options['record_objectives'] = True
prob.driver.recording_options['record_constraints'] = True
prob.driver.recording_options['record_desvars'] = True
prob.driver.recording_options['record_derivatives'] = True
prob.setup(check=True)
prob.run_driver()
prob.cleanup()
cr = CaseReader('recorder.sql')
# Get derivatives associated with the last iteration.
derivs = cr.get_case(0).jacobian
# check that derivatives have been recorded.
print(set(derivs.keys()))
this is what I get when i run this
case = cr.get_case(-1)
print(case)
print(case.jacobian.keys())
so print(case) works but jacobian is empty
driver rank0:SLSQP|20 {'GLOBAL_DESIGN_VAR.d2100': array([1.29472574]), 'GLOBAL_DESIGN_VAR.Factor_c': array([1.29491178]), 'GLOBAL_DESIGN_VAR.lus': array([1.28847898]),
'GLOBAL_DESIGN_VAR.ng': array([1.29981202]), 'GLOBAL_DESIGN_VAR.bl': array([1.2948257]), 'GLOBAL_DESIGN_VAR.d1700': array([1.29472449]),
'GLOBAL_DESIGN_VAR.sFactor_c': array([1.29981202]), 'GoldflexFLS12.error': array([3.04801276]), 'GoldflexFLS12.Lerror': array([0.73301603])}
Traceback (most recent call last):
File "<ipython-input-6-9a0bfa8ec35f>", line 5, in <module>
print(case.jacobian.keys())
AttributeError: 'NoneType' object has no attribute 'keys'
NEW CODE that one can run;
from openmdao.api import Problem, ScipyOptimizeDriver, ExecComp, IndepVarComp, SqliteRecorder, CaseReader
from openmdao.api import Group
from openmdao.api import ExplicitComponent
class Exp(ExplicitComponent):
def setup(self):
self.add_input('des1',val=1)
self.add_input('des2',val=1)
self.add_output('out',val=1)
self.add_output('con',val=1)
self.declare_partials('*', '*',method='fd',step=0.001)
def compute(self, inputs, outputs):
outputs['out']=inputs['des1']**2+inputs['des2']
outputs['con']=inputs['des1']
class AERO(Group):
def setup(self):
self.add_subsystem('Exp',Exp(),promotes=['*'])
infodict={'desvar':{'des1':{"fdstep": 0.1,"init": 1.0,"max": 1.3,"min": 0.8},'des2':{"fdstep": 0.1,"init": 2.0,"max": 1.3,"min": 0.8}}}
prob = Problem()
probname = prob.model = Group()
recordername='recorder.sql'
GLOBAL_DESIGN_VAR = IndepVarComp()
probname.add_subsystem('GLOBAL_DESIGN_VAR', GLOBAL_DESIGN_VAR,promotes=['*'])
probname.add_subsystem('AERO', AERO(),promotes=['*'])
for key,val in infodict['desvar'].items():
GLOBAL_DESIGN_VAR.add_output(key, val['init'])
probname.add_design_var(key,lower=val['min'], upper=val['max'])
probname.add_objective('out')
probname.add_constraint('con',upper=0.1)
prob.driver=ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'
prob.driver.options['disp'] = True
prob.driver.options['tol'] = 1e-9
recorder = SqliteRecorder(recordername)
prob.driver.add_recorder(recorder)
prob.driver.recording_options['includes'] = []
prob.driver.recording_options['record_inputs'] = True
# prob.driver.recording_options['record_outputs'] = True
prob.driver.recording_options['record_objectives'] = True
prob.driver.recording_options['record_constraints'] = True
prob.driver.recording_options['record_desvars'] = True
prob.driver.recording_options['record_derivatives'] = True
prob.setup(check=True)
prob.run_driver()
prob.cleanup()
cr = CaseReader(recordername)
# Get derivatives associated with the last iteration.
derivs = cr.get_case(0).jacobian
# check that derivatives have been recorded.
print(set(derivs.keys()))
回答1:
Assuming you are using the default driver (RunOnceDriver) and calling run_driver(), then the reason you don't see any derivatives is that the default driver never asks for them, so they are never computed.
So you need to pick a gradient based driver, and then you also need to be sure to set
prob.driver.recording_options['record_derivatives'] = True
Here is a minimum working example:
from openmdao.api import Problem, ScipyOptimizeDriver, ExecComp, IndepVarComp, SqliteRecorder, CaseReader
# build the model
prob = Problem()
indeps = prob.model.add_subsystem('indeps', IndepVarComp())
indeps.add_output('x', 3.0)
indeps.add_output('y', -4.0)
prob.model.add_subsystem('paraboloid', ExecComp('f = (x-3)**2 + x*y + (y+4)**2 - 3'))
prob.model.connect('indeps.x', 'paraboloid.x')
prob.model.connect('indeps.y', 'paraboloid.y')
prob.driver = ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'
recorder = SqliteRecorder("cases.sql")
prob.driver.add_recorder(recorder)
prob.driver.recording_options['record_derivatives'] = True
prob.model.add_design_var('indeps.x', lower=-50, upper=50)
prob.model.add_design_var('indeps.y', lower=-50, upper=50)
prob.model.add_objective('paraboloid.f')
prob.setup()
prob.run_driver()
# minimum value
print(prob['paraboloid.f'])
# location of the minimum
print(prob['indeps.x'])
print(prob['indeps.y'])
prob.cleanup()
cr = CaseReader("cases.sql")
# driver_cases = cr.list_cases('driver')
# Get derivatives associated with the last iteration.
case = cr.get_case(-1)
print(case)
# check that derivatives have been recorded.
print(case.jacobian.keys())
来源:https://stackoverflow.com/questions/54801157/i-can-not-manage-to-record-access-derivatives