我正试图从''connect''改为''''为抛物面示例(没有约束),我收到输出错误。 在系统''中找不到响应'parab.f_xy'的输出。
请随意查看下面的代码,我尝试将其与“使用促销与连接链接变量”中解释的主题类似。代码独立于已发布的抛物面组件。
from openmdao.api import Problem, ScipyOptimizeDriver, IndepVarComp
from openmdao.core.explicitcomponent import ExplicitComponent
class Paraboloid(ExplicitComponent):
def setup(self):
self.add_input('x', val=0.0)
self.add_input('y', val=0.0)
self.add_output('f_xy', val=0.0)
self.declare_partials('*', '*')
def compute(self, inputs, outputs):
x = inputs['x']
y = inputs['y']
outputs['f_xy'] = (x-3.0)**2 + x*y + (y+4.0)**2 - 3.0
def compute_partials(self, inputs, partials):
x = inputs['x']
y = inputs['y']
partials['f_xy', 'x'] = 2.0*x - 6.0 + y
partials['f_xy', 'y'] = 2.0*y + 8.0 + x
# build the model
prob = Problem()
indeps = prob.model.add_subsystem('indeps', IndepVarComp(), promotes=['*'])
indeps.add_output('x', 3.0)
indeps.add_output('y', -4.0)
prob.model.add_subsystem('parab', Paraboloid(), promotes_inputs=['x','y'] , promotes_outputs=['f_xy'])
# setup the optimization
prob.driver = ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'
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('parab.f_xy')
prob.setup()
prob.run_driver()
答案 0 :(得分:3)
因为你从'parab'组件中提升'f_xy',你的目标现在应该命名为'f_xy'而不是'parab.f_xy',因为你从'indeps'提升了'x'和'y' '组件,您的设计变量应命名为'x'和'y',而不是'indeps.x'和'indeps.y'。