1

我想使用缩放器并最终使用 OpenMDAO 1.x 测试OpenMDAO 0.x文档中的抛物面示例,但无论是否使用缩放器,我都会得到奇怪的结果。这是代码:

从 __future__ 导入 print_function
导入系统

从 openmdao.api 导入 IndepVarComp,组件,问题,组,ScipyOptimizer

类抛物面(组件):

    def __init__(self):
        超级(抛物面,自我).__init__()

        self.add_param('x', 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):

        x = 参数['x']
        y = 参数['y']

        #unknowns['f_xy'] = (x-3.0)**2 + x*y + (y+4.0)**2 - 3.0
        未知数['f_xy'] = (1000.*x-3.)**2 + (1000.*x)*(0.01*y) + (0.01*y+4.)**2 - 3。

    def 线性化(自我,参数,未知数,残差):
        """ 抛物面的雅可比行列式。"""
        x = 参数['x']
        y = 参数['y']
        J = {}

        #J['f_xy', 'x'] = 2.0*x - 6.0 + y
        #J['f_xy', 'y'] = 2.0*y + 8.0 + x
        J['f_xy', 'x'] = 2000000.0*x - 6000.0 + 10.0*y
        J['f_xy', 'y'] = 0.0002*y + 0.08 + 10.0*x

        返回 J

如果 __name__ == "__main__":

    顶部 = 问题()

    root = top.root = Group()

    root.add('p1', IndepVarComp('x', 3.0))
    root.add('p2', IndepVarComp('y', -4.0))
    root.add('p', 抛物面())

    root.connect('p1.x', 'px')
    root.connect('p2.y', 'py')

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

    top.driver.add_desvar('p1.x', lower=-1000, upper=1000, scaler=0.001)
    top.driver.add_desvar('p2.y', lower=-1000, upper=1000, scaler=1000.)
    top.driver.add_objective('p.f_xy')

    top.setup()
    顶部运行()

    打印('\n')
    print('在 (%f, %f) 处找到 %f 的最小值' % (top['p.f_xy'], top['px'], top['py']))

当我在我的系统上运行它时,它给出:

2.7.11 |Anaconda 2.5.0(64 位)| (默认,2016 年 1 月 29 日,14:26:21)[MSC v.1500 64 位 (AMD64)]
Python 键入“help”、“copyright”、“credits”或“license”以获取更多信息。
[评估 paraboloid_optimize_scaled.py]
#############################################
设置:检查潜在问题...

没有指定记录器,因此不会保存数据。

设置:检查完成。
#############################################

优化成功终止。(退出模式 0)
            当前函数值:[8981902.27846645]
            迭代次数:1
            功能评估:12
            梯度评估:1
优化完成
----------------------------------


8981902.278466 的最小值位于 (3.000000, -4.000000)

我错过了什么?

4

2 回答 2

1

缩放器在 OpenMDAO 1.x 中的定义与 0.x 中的相反。在 1.x 中,使用了以下缩放关系。

driver_value = (model_value + adder)*scaler

因此,与旧教程中的缩放器相比,您需要更改缩放器。但是,还有一个次要问题,即您的分析导数中的一个错误,该错误也在下面得到纠正。

from __future__ import print_function
import sys

from openmdao.api import IndepVarComp, Component, Problem, Group, ScipyOptimizer

class Paraboloid(Component):

    def __init__(self):
        super(Paraboloid, self).__init__()

        self.add_param('x', 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):

        x = params['x']
        y = params['y']

        #unknowns['f_xy'] = (x-3.0)**2 + x*y + (y+4.0)**2 - 3.0
        unknowns['f_xy'] = (1000.*x-3.)**2 + (1000.*x)*(0.01*y) + (0.01*y+4.)**2 - 3.

    def linearize(self, params, unknowns, resids):
        """ Jacobian for our paraboloid."""
        x = params['x']
        y = params['y']
        J = {}

        #J['f_xy', 'x'] = 2.0*x - 6.0 + y
        #J['f_xy', 'y'] = 2.0*y + 8.0 + x
        J['f_xy', 'x'] = 2000000.0*x - 6000.0 + 10.0*y
        J['f_xy', 'y'] = 0.0002*y + 0.08 + 10.0*x

        return J

if __name__ == "__main__":

    top = Problem()

    root = top.root = Group()
    root.fd_options['force_fd'] = True

    root.add('p1', IndepVarComp('x', 3.0))
    root.add('p2', IndepVarComp('y', -4.0))
    root.add('p', Paraboloid())

    root.connect('p1.x', 'p.x')
    root.connect('p2.y', 'p.y')

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

    top.driver.add_desvar('p1.x', lower=-1000, upper=1000, scaler=1000.)
    top.driver.add_desvar('p2.y', lower=-1000, upper=1000,scaler=.001)
    top.driver.add_objective('p.f_xy')

    top.setup()
    top.run()

    print('\n')
    print('Minimum of %f found at (%f, %f)' % (top['p.f_xy'], top['p.x'], top['p.y']))

给出:

Optimization terminated successfully.    (Exit mode 0)
            Current function value: [-27.333333]
            Iterations: 3
            Function evaluations: 6
            Gradient evaluations: 3
Optimization Complete
-----------------------------------


Minimum of -27.333333 found at (0.006666, -733.299996)
于 2016-02-22T14:33:51.453 回答
0

我无法直接复制您的问题。与您引用的openmdao 0.x 教程相比,您的 solve_nonlinear 和 linearize 方法中有一些奇怪的缩放比例。但是当我清理它们时,我得到了正确的答案,对于合理的缩放器值,甚至对于一些不合理的值(你选择的那些有点极端)。当您使用 add_desvar 中的缩放器/加法器时,您根本不需要修改模型。这些值只是修改优化器看到的值以帮助缩放,但在传递给模型之前会正确转换回未缩放的值。

from __future__ import print_function
import sys

from openmdao.api import IndepVarComp, Component, Problem, Group, ScipyOptimizer

class Paraboloid(Component):

    def __init__(self):
        super(Paraboloid, self).__init__()

        self.add_param('x', 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):

        x = params['x']
        y = params['y']

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

    def linearize(self, params, unknowns, resids):
        """ Jacobian for our paraboloid."""
        x = params['x']
        y = params['y']
        J = {}

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

        return J

if __name__ == "__main__":

    top = Problem()

    root = top.root = Group()

    root.add('p1', IndepVarComp('x', 3.0))
    root.add('p2', IndepVarComp('y', -4.0))
    root.add('p', Paraboloid())

    root.connect('p1.x', 'p.x')
    root.connect('p2.y', 'p.y')

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

    # top.driver.add_desvar('p1.x', lower=-1000, upper=1000)
    # top.driver.add_desvar('p2.y', lower=-1000, upper=1000)

    top.driver.add_desvar('p1.x', lower=-1000, upper=1000, scaler=.001)
    top.driver.add_desvar('p2.y', lower=-1000, upper=1000, scaler=1000.)
    top.driver.add_objective('p.f_xy')

    top.setup()
    top.run()

    print('\n')
    print('Minimum of %f found at (%f, %f)' % (top['p.f_xy'], top['p.x'], top['p.y']))

给出:

##############################################
Setup: Checking for potential issues...

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

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

Optimization terminated successfully.    (Exit mode 0)
            Current function value: [-27.33333333]
            Iterations: 12
            Function evaluations: 15
            Gradient evaluations: 12
Optimization Complete
-----------------------------------
于 2016-02-21T21:47:47.070 回答