0

我以为我理解了一月份关于在优化中使用元模型的讨论。但到目前为止,我的尝试失败了。我有点曲折。我想使用几个学科之一的元模型,其中一月份讨论的示例仅针对元模型的结果进行了优化。我基于抛物面优化教程示例构建了一个示例问题。我添加了第二个组件,将两个组件耦合在一个组中并对其进行了优化。然后我尝试将其中一个组件替换为该组件的元模型。以下是带有真实模型的版本。

from __future__ import print_function

from openmdao.api import IndepVarComp, Component, Problem, Group, ExecComp, NLGaussSeidel, ScipyOptimizer, UniformDriver, DumpRecorder, InMemoryRecorder

import math
import numpy as np

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('z', val=0.0)
    self.add_param('y', val=0.0)

    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['z']
    y = params['y']

    f_xy = (x-3.0)**2 + x*y + (y+4.0)**2 - 3.0
    unknowns['f_xy'] = f_xy

def linearize(self, params, unknowns, resids):
    """ Jacobian for our paraboloid."""

    x = params['z']
    y = params['y']
    J = {}

    J['f_xy', 'z'] = 2.0*x - 6.0 + y
    J['f_xy', 'y'] = 2.0*y + 8.0 + x
    return J

class Sinx(Component):
""" Evaluates the equation f(x) = sin(x) """

def __init__(self):
    super(Sinx, self).__init__()
self.C = 50.

    self.add_param('x', val=0.0)

    self.add_output('z', val=0.0)

def solve_nonlinear(self, params, unknowns, resids):
    """f(x) = 10.* sin(x/C)
    """

    x = params['x']

    sinx = math.sin(x/self.C) *10.
    unknowns['z'] = sinx

def linearize(self, params, unknowns, resids):
    """ Jacobian for our paraboloid."""

    x = params['x']
    J = {}

    J['z', 'x'] = math.cos(x/self.C)/self.C *10.
    return J

class Mda(Group):
def __init__(self):
    super(Mda, self).__init__()

self.f1in=['x']
self.f1out=['z']
self.f2in=['z', 'y']
self.f2out=['f_xy']

self.add('x', IndepVarComp('x', 10.), promotes=['*'])
self.add('y', IndepVarComp('y', 0.), promotes=['*'])

self.add('f1', Sinx(), promotes=self.f1in+self.f1out)
self.add('f2', Paraboloid(), promotes=self.f2in+self.f2out)
self.add('obj_cmp', ExecComp('obj = f_xy'), promotes=['f_xy', 'obj'])
self.nl_solver = NLGaussSeidel()


if __name__ == "__main__":

top = Problem()

root = top.root = Mda()

top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'SLSQP'

top.driver.add_desvar('x', lower=-50, upper=50)
top.driver.add_desvar('y', lower=-50, upper=50)
top.driver.add_objective('obj')

top.setup()
top.run()

print('obj =%f at (%f,%f)'%(top['obj'],top['x'],top['y']))

以下是我认为可以用作元模型的课程。

class SinxSurr(MetaModel):
''' Evaluate surrogate of Sinx '''
def __init__(self):
super(SinxSurr, self).__init__()

    self.add_param('x', val=0.)
    self.add_output('z', val=0., surrogate=FloatKrigingSurrogate())

以下是运行它的部分:

if __name__ == "__main__":
# build training data by sampling
top = Problem()
root = top.root = Group()

root.add('sinx', Sinx())
root.add('p1', IndepVarComp('x', 50.0))
root.connect('p1.x', 'sinx.x')

top.driver = UniformDriver(num_samples=500)
top.driver.add_desvar('p1.x', lower=-50, upper=50)
top.driver.add_objective('sinx.z')
recorder = InMemoryRecorder()
top.driver.add_recorder(recorder)

top.setup()
top.run()
top.cleanup()

# retrieve sampling results from recorder
xlist=[]
slist=[]
for line in recorder.iters:
xlist.append(line['unknowns']['p1.x'])
slist.append(line['unknowns']['sinx.z'])
recorder.close()

if len(xlist) != len(slist):
print('Recorder parse failure')
sys.exit(1)
x=10.

top = Problem()
root = top.root = Mda()

top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'SLSQP'

top.driver.add_desvar('x', lower=-50, upper=50)
top.driver.add_desvar('y', lower=-50, upper=50)
top.driver.add_objective('obj')

top.setup()
top.root['f1.train:x'] = xlist
top.root['f1.train:z'] = slist
top.run()

print('obj =%f at (%f,%f)'%(top['obj'],top['x'],top['y']))

当我运行它时,我收到错误:

Setup: Checking for potential issues...

No recorders have been specified, so no data will be saved.

The following parameters have no associated unknowns:
f1.train:x
f1.train:z

Setup: Check complete.
##############################################

Traceback (most recent call last):
  File "surrDisc.py", line 164, in <module>
top.root['f1.train:x'] = xlist
TypeError: 'Mda' object does not support item assignment

我不明白如何(至少)将元模型中的数据与 Group 交互。

4

1 回答 1

0

您是否打算在创建代理项时将变量“z”重命名为“z:float”?

self.add_output('z:float', val=0., surrogate=FloatKrigingSurrogate())

如果您确实想重命名它,那么您必须明确地将 'f1.z:float' 连接到 'f2.z' 并且不要在您列出的促销中包含 'z'self.f1outself.f2in

于 2016-06-22T19:50:49.053 回答