【问题标题】:OpenMDAO v0.10.3.2: problems using a case iterator driver in the workflow of an optimizer driverOpenMDAO v0.10.3.2:在优化器驱动程序的工作流程中使用案例迭代器驱动程序的问题
【发布时间】:2015-07-17 18:37:29
【问题描述】:

出于兼容性原因,我使用的是 OpenMDAO v0.10.3.2

我正在尝试在 OpenMDAO 中设置一个优化问题,该问题需要在优化器的工作流程中使用案例迭代器驱动程序。为了说明,我创建了如下所示的简单示例。代码似乎可以运行,但优化器似乎没有更改参数并在其工作流运行两次后退出。

我正在寻找有关设置此类问题的建议,以及我当前的表述可能出现的问题的见解。

from openmdao.main.api import Assembly, Component
from openmdao.lib.datatypes.api import Float, Array, List
from openmdao.lib.drivers.api import DOEdriver, SLSQPdriver, CaseIteratorDriver

import numpy as np


class component1(Component):

    x = 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


class component2(Component):

    x = Float(iotype='in')
    y = Float(iotype='in')
    term1 = Float(iotype='in')
    f = Float(iotype='out')

    def execute(self):

        y = self.y
        x = self.x
        term1 = self.term1

        f = term1 + x + y**2

        self.f = f


class summer(Component):

    fs = Array(iotype='in', desc='f values from all cases')
    total = Float(iotype='out', desc='sum of all f values')

    def execute(self):
        self.total = sum(self.fs)
        print 'In summer, fs = %s and total = %s' % (self.fs, self.total)


class assembly(Assembly):

    cases_a = Array(iotype='in', desc='list of cases')
    x = Float(iotype='in')
    y = Float(iotype='in')
    f = Float(iotype='out')
    total = Float(iotype='out', default_value=100)

    def configure(self):

        # create instances of components
        self.add('component1', component1())
        self.add('component2', component2())
        self.add('summer', summer())

        # set up main driver (optimizer)
        self.add('driver', SLSQPdriver())
        self.driver.iprint = 1
        self.driver.maxiter = 100
        self.driver.accuracy = 1.0e-6
        self.driver.add('summer', summer())
        self.driver.add_parameter('x', low=-5., high=5.)
        self.driver.add_parameter('y', low=-5., high=5.)
        self.driver.add_objective('summer.total')

        # set up case iterator driver
        self.add('case_driver', CaseIteratorDriver())
        self.case_driver.workflow.add(['component1', 'component2'])
        self.case_driver.add_parameter('component1.a')
        self.case_driver.add_response('component2.f')

        # Set up connections
        self.connect('x', 'component1.x')
        self.connect('y', 'component2.y')
        self.connect('component1.x', 'component2.x')
        self.connect('component1.term1', 'component2.term1')
        self.connect('component2.f', 'f')
        self.connect('cases_a', 'case_driver.case_inputs.component1.a')
        self.connect('case_driver.case_outputs.component2.f', 'summer.fs')
        self.connect('summer.total', 'total')

        # establish main workflow
        self.driver.workflow.add(['case_driver', 'summer'])

if __name__ == "__main__":
    """ the result should be -1 at (x, y) = (-0.5, 0) """

    import time

    test = assembly()
    values = [1, 1, 1, 1]
    test.cases_a = np.array(values)
    test.x = 4
    test.y = 4

    tt = time.time()
    test.run()

    print "Elapsed time: ", time.time()-tt, "seconds"

    print 'result = ', test.total
    print '(x, y) = (%s, %s)' % (test.x, test.y)

【问题讨论】:

  • 我已经使用 ALPSO、NSGA2、MIDACO、COBYLA 和 ALHSO 解决了这个问题,而 SLSQP、CONMIN、KSOPT 和 SOLVOPT 失败了。这意味着可以使用无梯度优化方法解决该问题,但对于基于梯度的方法则失败。然而,潜在的问题应该是可以使用基于梯度的算法轻松解决的问题。这让我觉得问题与使用case迭代器有关。

标签: optimization iterator workflow openmdao


【解决方案1】:

在 CID 驱动程序中传播衍生品存在很多挑战,而且我们从未完全按照我们想要的方式工作。因此,我建议另一种方法,您可以为每个要运行的案例创建一个单独的实例。这会更好,特别是如果您计划在某个时候使用解析导数

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

import numpy as np


class component1(Component):

    x = 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


class component2(Component):

    x = Float(iotype='in')
    y = Float(iotype='in')
    term1 = Float(iotype='in')
    f = Float(iotype='out')

    def execute(self):

        y = self.y
        x = self.x
        term1 = self.term1

        f = term1 + x + y**2

        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', default_value=100)

    def __init__(self, a_vals=[1, 1, 1, 1]):
        self.a_vals = a_vals

        super(assembly, self).__init__()



    def configure(self):

        #add the driver first, so I don't overwrite the workflow later on
        self.add('driver', SLSQPdriver())


        #create this first, so we can connect to it
        self.add('summer', summer(size=len(self.a_vals)))
        self.connect('summer.total', 'total')

        # create instances of components
        for i, a in enumerate(self.a_vals):
            c1 = self.add('comp1_%d'%i, component1())
            c1.a = a
            c2 = self.add('comp2_%d'%i, component2())

            self.connect('x', ['comp1_%d.x'%i,'comp2_%d.x'%i])
            self.connect('y', 'comp2_%d.y'%i)
            self.connect( 'comp1_%d.term1'%i, 'comp2_%d.term1'%i)

            self.connect('comp2_%d.f'%i, 'summer.fs[%d]'%i)

            self.driver.workflow.add(['comp1_%d'%i, 'comp2_%d'%i])

        # establish main workflow


        # 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_objective('summer.total')


if __name__ == "__main__":
    """ the result should be -1 at (x, y) = (-0.5, 0) """

    import time

    test = assembly([1, 1, 1, 1])

    test.x = 2
    test.y = 4

    tt = time.time()
    test.run()

    print "Elapsed time: ", time.time()-tt, "seconds"

    print 'result = ', test.total
    print '(x, y) = (%s, %s)' % (test.x, test.y)

【讨论】:

  • 感谢您的回复。这看起来是一个很好的解决方案,但是,当我运行您的代码时,我收到与第 77 行有关的错误“AttributeError: object has no attribute 'a_vals'”。看起来配置无法识别由 init 方法添加的属性。您对为什么这不起作用有任何想法吗?我没有太多使用 super() 并且无法找出问题所在。
  • 从打印结果来看,configure方法似乎在init方法之前运行。
  • 在旧版本的 OpenMDAO(0.12.0 和更早版本)中,来自 Assembly 的基本 init 实现在其所有子级上调用 configure。这在 0.13.0 中发生了变化,但为了使我的脚本在旧版本中工作,我对其进行了编辑以在调用 Super(父类的 init)之前创建 a_vals 属性。我编辑了我的答案以包含这个小修复。
  • 即使在修复之后,我也无法让它在 OpenMDAO 0.10.3.2 中运行,但它在 0.9.7 中运行。我将继续使用该版本。谢谢。
  • 我怀疑 0.10.3.2 中存在错误。它应该可以在更高版本(0.12 和 0.13)中使用
猜你喜欢
  • 2022-01-13
  • 2017-09-10
  • 2017-09-11
  • 1970-01-01
  • 2017-03-20
  • 1970-01-01
  • 1970-01-01
  • 2017-05-24
  • 2021-05-14
相关资源
最近更新 更多