如果我只是复制粘贴代码,那么在this link中记录/访问导数的示例案例对我来说效果很好。 我试图弄清楚为什么类似的方法不能解决我自己的问题。我将录制选项设置为相同。我试图列出一些差异,但不确定它们是否可能是造成这种情况的原因:
我不确定我还能提供什么更多见解。我可能缺少明显的东西。 任何想法?
这是我的问题和示例问题的N ^ 2图。
这是代码段。
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()))
这是我运行此程序时得到的
case = cr.get_case(-1)
print(case)
print(case.jacobian.keys())
所以print(case)有效,但是jacobian为空
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'
可以运行的新代码;
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()))
答案 0 :(得分:0)
假设您正在使用默认驱动程序(RunOnceDriver)并调用run_driver(),则看不到任何派生的原因是默认驱动程序从不要求它们,因此也就不会对其进行计算。
因此,您需要选择一个基于梯度的驱动程序,然后还需要确保设置
prob.driver.recording_options['record_derivatives'] = True
这是一个最小的工作示例:
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())