是否可以在OpenMDAO 1.X中优化单个列表条目?在这个例子中,我希望添加/优化z的第一个条目。在我真正的问题中,我不可能将整个矢量添加为设计变量(我使用的是NREL的DAKOTA驱动程序)。我以为我可以将虚拟变量连接到z
,但看起来像1.X不支持将变量连接到列表条目。
ppb.py:
from __future__ import print_function
import numpy as np
from dakota_driver.driver import pydakdriver
from openmdao.api import ScipyOptimizer
from openmdao.api import IndepVarComp, Component, Problem, Group
class Paraboloid(Component):
""" Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3 """
def __init__(self):
super(Paraboloid, self).__init__()
self.add_param('x', val=6.0)
self.add_param('y', val=-7.0)
self.add_param('z', val=np.array([2., 2., 2.]))
self.add_output('f_xy', val=0.0)
def solve_nonlinear(self, params, unknowns, resids):
"""f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3
"""
x = params['x']
y = params['y']
z = params['z']
unknowns['f_xy'] = z[0]*(x-3.0)**2 + x*y + (y+4.0)**2 - 3.0 + abs(z[0]-4)
def linearize(self, params, unknowns, resids):
""" Jacobian for our paraboloid."""
x = params['x']
y = params['y']
z = params['z']
J = {}
J['f_xy', 'x'] = 2.0*x - 6.0 + y
J['f_xy', 'y'] = 2.0*y + 8.0 + x
J['f_xy', 'z'] = (x-3.0)**2 + 1
return J
top = Problem()
root = top.root = Group()
root.add('p1', IndepVarComp('x', 13.0))
root.add('p2', IndepVarComp('y', -14.0))
root.add('p3', IndepVarComp('z', np.array([0.0, 0., 0.])))
root.add('p', Paraboloid())
root.connect('p1.x', 'p.x')
root.connect('p2.y', 'p.y')
root.connect('p3.z', 'p.z')
top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'Powell'
top.driver.add_desvar('p3.z[0]', lower=-30., upper=30.) # <--- Is it possible to do this?
top.driver.add_objective('p.f_xy')
top.setup()
top['p1.x'] = 3.0
top['p2.y'] = -4.0
#top['p3.z'] = 2.0
top['p3.z'] = np.array([0., 0., 0.])
top.run()
运行结果:
$ python ppb.py
Traceback (most recent call last):
File "ppb.py", line 75, in <module>
top.setup()
File "/scratch/jquick/1.0/lib/python2.7/site-packages/openmdao/core/problem.py", line 586, in setup
raise NameError("Can't find param of interest '%s'." % v)
NameError: Can't find param of interest 'p3.z[0]'.
答案 0 :(得分:2)
使用indices
关键字指定要优化的索引:
top.driver.add_desvar('p3.z', lower=-30., upper=30., indices=[0])