运行以下示例代码时:
from openmdao.api import Problem, Group, IndepVarComp, ImplicitComponent, ScipyOptimizeDriver
class Test1Comp(ImplicitComponent):
def setup(self):
self.add_input('x', 0.5)
self.add_input('design_x', 1.0)
self.add_output('z', val=0.0)
self.add_output('obj')
self.declare_partials(of='*', wrt='*', method='fd', form='central', step=1.0e-4)
def apply_nonlinear(self, inputs, outputs, resids):
x = inputs['x']
design_x = inputs['design_x']
z = outputs['z']
resids['z'] = x*z + z - 4
resids['obj'] = (z/5.833333 - design_x)**2
if __name__ == "__main__":
prob = Problem()
model = prob.model = Group()
model.add_subsystem('p1', IndepVarComp('x', 0.5))
model.add_subsystem('d1', IndepVarComp('design_x', 1.0))
model.add_subsystem('comp', Test1Comp())
model.connect('p1.x', 'comp.x')
model.connect('d1.design_x', 'comp.design_x')
prob.driver = ScipyOptimizeDriver()
prob.driver.options["optimizer"] = 'SLSQP'
model.add_design_var("d1.design_x", lower=0.5, upper=1.5)
model.add_objective('comp.obj')
prob.setup()
prob.run_model()
print(prob['comp.z'])
我明白了:
Traceback (most recent call last):
File "C:/Users/jonat/Desktop/mockup_component3.py", line 40, in <module>
prob.setup()
File "C:\Python\openmdao\core\problem.py", line 409, in setup
model._setup(comm, 'full', mode)
File "C:\Python\openmdao\core\system.py", line 710, in _setup
self._setup_relevance(mode, self._relevant)
File "C:\Python\openmdao\core\system.py", line 1067, in _setup_relevance
self._relevant = relevant = self._init_relevance(mode)
File "C:\Python\openmdao\core\group.py", line 693, in _init_relevance
return get_relevant_vars(self._conn_global_abs_in2out, desvars, responses, mode)
File "C:\Python\openmdao\core\group.py", line 1823, in get_relevant_vars
if 'type_' in nodes[node]:
TypeError: 'instancemethod' object has no attribute '__getitem__'
有人可以解释原因吗?我已成功运行类似的组件,但没有优化,所以我怀疑错误来自优化结构。例如,我是否必须在ExplicitComponent中定义目标?
答案 0 :(得分:0)
我跑的时候会收到更具描述性的信息:
KeyError: 'Variable name "comp.y" not found.'
这意味着组件“comp”没有名为“y”(或“z”)的变量。
答案 1 :(得分:0)
这个问题似乎是由OpenMDAO的错误安装引起的。我之前尝试通过下载包含OpenMDAO的zip文件来安装。现在我改为使用pip安装,错误消失了。