我正在尝试将数组的行连接nxm
到单个组件中的数组,或者将1xm
数组的切片n
连接到单个组件中的1xn*m
数组。然后将原始或数组用作优化参数。问题是当我这样做时,程序集似乎有一些严重的问题。我要么得到明显错误的答案,要么得到尺寸不匹配的错误。1xm
n
nxm
1xn*m
我已经能够通过使用 传入n
1xm
数组来使程序集工作exec()
,但我更喜欢前面解释的方法。如果有人能告诉我如何以适当的方式完成这项工作,我将不胜感激。
我提供了一个简化的代码示例,说明什么是有效的,什么是我想做的。首先显示我想使用的方法,然后显示我已经开始工作但严重不喜欢的方法。
我想做的事
from openmdao.main.api import Assembly, Component
from openmdao.lib.datatypes.api import Float, Array, List
from openmdao.lib.drivers.api import DOEdriver, SLSQPdriver, COBYLAdriver, CaseIteratorDriver
from pyopt_driver.pyopt_driver import pyOptDriver
import numpy as np
class component1(Component):
x = Float(iotype='in')
y = Float(iotype='in')
term1 = Float(iotype='out')
a = Float(iotype='in', default_value=1)
def execute(self):
x = self.x
a = self.a
term1 = a*x**2
self.term1 = term1
print "In comp1", self.name, self.a, self.x, self.term1
def list_deriv_vars(self):
return ('x',), ('term1',)
def provideJ(self):
x = self.x
a = self.a
dterm1_dx = 2.*a*x
J = np.array([[dterm1_dx]])
print 'In comp1, J = %s' % J
return J
class component2(Component):
x = Float(iotype='in')
y = Float(iotype='in')
term1 = Float(iotype='in')
f = Float(iotype='out')
q = Array(np.zeros(2), iotype='in', dtype='float')
def execute(self):
y = self.y + self.q[0]
x = self.x + self.q[1]
term1 = self.term1
f = term1 + x + y**2
print 'q = %s' % self.q
self.f = f
print "In comp2", self.name, self.x, self.y, self.term1, self.f
class summer(Component):
total = Float(iotype='out', desc='sum of all f values')
def __init__(self, size):
super(summer, self).__init__()
self.size = size
self.add('fs', Array(np.zeros(size), iotype='in', desc='f values from all cases'))
def execute(self):
self.total = sum(self.fs)
print 'In summer, fs = %s and total = %s' % (self.fs, self.total)
class assembly(Assembly):
x = Float(iotype='in')
y = Float(iotype='in')
total = Float(iotype='out')
def __init__(self, size):
super(assembly, self).__init__()
self.size = size
self.add('a_vals', Array(np.zeros(size), iotype='in', dtype='float'))
self.add('q', Array(np.zeros((size, 2)), iotype='in', dtype='float'))
self.add('fs', Array(np.zeros(size), iotype='out', dtype='float'))
print 'in init a_vals = %s, fs = %s' % (self.a_vals, self.fs)
def configure(self):
self.add('driver', SLSQPdriver())
# self.add('driver', pyOptDriver())
# self.driver.optimizer = 'SNOPT'
# self.driver.pyopt_diff = True
#create this first, so we can connect to it
self.add('summer', summer(size=len(self.a_vals)))
self.connect('summer.total', 'total')
print 'in configure a_vals = %s' % self.a_vals
# create instances of components
for i in range(0, self.size):
c1 = self.add('comp1_%d' % i, component1())
c1.missing_deriv_policy = 'assume_zero'
c2 = self.add('comp2_%d'%i, component2())
self.connect('a_vals[%d]' % i, 'comp1_%d.a' % i)
self.connect('x', ['comp1_%d.x' % i, 'comp2_%d.x' % i])
self.connect('y', ['comp1_%d.y' % i, 'comp2_%d.y' % i])
self.connect('comp1_%d.term1' % i, 'comp2_%d.term1' % i)
self.connect('q[%d, :]' % i, 'comp2_%d.q' % i)
self.connect('comp2_%d.f' % i, 'summer.fs[%d]' % i)
self.driver.workflow.add(['comp1_%d' % i, 'comp2_%d' % i])
# self.connect('summer.fs[:]', 'fs[:]')
self.driver.workflow.add(['summer'])
# set up main driver (optimizer)
self.driver.iprint = 1
self.driver.maxiter = 100
self.driver.accuracy = 1.0e-6
self.driver.add_parameter('x', low=-5., high=5.)
self.driver.add_parameter('y', low=-5., high=5.)
self.driver.add_parameter('q', low=0., high=5.)
self.driver.add_objective('summer.total')
if __name__ == "__main__":
""" the result should be -1 at (x, y) = (-0.5, 0) """
import time
from openmdao.main.api import set_as_top
a_vals = np.array([1., 1., 1., 1.])
test = set_as_top(assembly(size=len(a_vals)))
test.a_vals = a_vals
print 'in main, test.a_vals = %s, test.fs = %s' % (test.a_vals, test.fs)
test.x = 2.
test.y = -5
test.q = np.tile(np.arange(0., 2.), (4, 1))
print test.q
tt = time.time()
test.run()
print "Elapsed time: ", time.time()-tt, "seconds"
print 'result = ', test.summer.total
print '(x, y) = (%s, %s)' % (test.x, test.y)
print 'fs = %s' % test.fs
print test.fs
----------
什么有效
from openmdao.main.api import Assembly, Component
from openmdao.lib.datatypes.api import Float, Array, List
from openmdao.lib.drivers.api import DOEdriver, SLSQPdriver, COBYLAdriver, CaseIteratorDriver
from pyopt_driver.pyopt_driver import pyOptDriver
import numpy as np
class component1(Component):
x = Float(iotype='in')
y = Float(iotype='in')
term1 = Float(iotype='out')
a = Float(iotype='in', default_value=1)
def execute(self):
x = self.x
a = self.a
term1 = a*x**2
self.term1 = term1
print "In comp1", self.name, self.a, self.x, self.term1
def list_deriv_vars(self):
return ('x',), ('term1',)
def provideJ(self):
x = self.x
a = self.a
dterm1_dx = 2.*a*x
J = np.array([[dterm1_dx]])
# print 'In comp1, J = %s' % J
return J
class component2(Component):
x = Float(iotype='in')
y = Float(iotype='in')
term1 = Float(iotype='in')
f = Float(iotype='out')
q = Array(np.zeros(2), iotype='in', dtype='float')
def execute(self):
y = self.y + self.q[0]
x = self.x + self.q[1]
term1 = self.term1
f = term1 + x + y**2
print 'in comp2 q = %s' % self.q
self.f = f
print "In comp2", self.name, self.x, self.y, self.term1, self.f
class summer(Component):
total = Float(iotype='out', desc='sum of all f values')
def __init__(self, size):
super(summer, self).__init__()
self.size = size
self.add('fs', Array(np.zeros(size), iotype='in', desc='f values from all cases'))
def execute(self):
self.total = sum(self.fs)
print 'In summer, fs = %s and total = %s' % (self.fs, self.total)
class assembly(Assembly):
x = Float(iotype='in')
y = Float(iotype='in')
total = Float(iotype='out')
def __init__(self, size):
super(assembly, self).__init__()
self.size = size
self.add('a_vals', Array(np.zeros(size), iotype='in', dtype='float'))
self.add('fs', Array(np.zeros(size), iotype='out', dtype='float'))
for i in range(0, size):
self.add('q_%d' % i, Array(np.zeros(2), iotype='in', dtype='float'))
print 'in init a_vals = %s, fs = %s' % (self.a_vals, self.fs)
def configure(self):
# self.add('driver', SLSQPdriver())
self.add('driver', pyOptDriver())
self.driver.optimizer = 'SNOPT'
# self.driver.pyopt_diff = True
#create this first, so we can connect to it
self.add('summer', summer(size=len(self.a_vals)))
self.connect('summer.total', 'total')
print 'in configure a_vals = %s' % self.a_vals
# print 'in configure q = %s' % self.q
# create instances of components
for i in range(0, self.size):
c1 = self.add('comp1_%d' % i, component1())
c1.missing_deriv_policy = 'assume_zero'
c2 = self.add('comp2_%d'%i, component2())
self.connect('a_vals[%d]' % i, 'comp1_%d.a' % i)
self.connect('x', ['comp1_%d.x' % i, 'comp2_%d.x' % i])
self.connect('y', ['comp1_%d.y' % i, 'comp2_%d.y' % i])
self.connect('comp1_%d.term1' % i, 'comp2_%d.term1' % i)
self.connect('q_%d' % i, 'comp2_%d.q' % i)
self.connect('comp2_%d.f' % i, 'summer.fs[%d]' % i)
self.driver.workflow.add(['comp1_%d' % i, 'comp2_%d' % i])
# self.connect('summer.fs[:]', 'fs[:]')
self.driver.workflow.add(['summer'])
# set up main driver (optimizer)
self.driver.iprint = 1
self.driver.maxiter = 100
self.driver.accuracy = 1.0e-6
self.driver.add_parameter('x', low=-5., high=5.)
self.driver.add_parameter('y', low=0., high=5.)
for i in range(0, self.size):
self.driver.add_parameter('q_%d' % i, low=0., high=5.)
self.driver.add_objective('summer.total')
if __name__ == "__main__":
""" the result should be -1 at (x, y) = (-0.5, 0) """
import time
from openmdao.main.api import set_as_top
a_vals = np.array([1., 1., 1., 1.])
test = set_as_top(assembly(size=len(a_vals)))
test.a_vals = a_vals
print 'in main, test.a_vals = %s, test.fs = %s' % (test.a_vals, test.fs)
test.x = 2.
test.y = -5
q = np.tile(np.arange(1., 3.), (4, 1))
for i in range(0, len(a_vals)):
exec('test.q_%d = q[%d]' % (i, i))
exec('print test.q_%d' % i)
print test.q_0, test.q_1, test.q_2, test.q_3
tt = time.time()
test.run()
print "Elapsed time: ", time.time()-tt, "seconds"
print 'result = ', test.summer.total
print '(x, y) = (%s, %s)' % (test.x, test.y)
print 'fs = %s' % test.fs
print test.fs
for i in range(0, len(a_vals)):
exec('print test.q_%d' % i)