离散独立/设计变量? openmdao 2.2.0

时间:2018-03-09 12:52:15

标签: openmdao

可以确定设计变量的下限和上限,例如在抛物面样本中:

top.model.add_design_var('p1.x', lower=-50, upper=50)

但可以强制优化器通过用户输入步骤扫描设计变量吗?

类似

top.model.add_design_var('p1.x', lower=-50, upper=50, increment=2)

或者可能将其作为数组引入

top.model.add_design_var('p1.x', [-50,-25,25,50])

1 个答案:

答案 0 :(得分:1)

使用不可能的基于渐变的优化器。您需要使用无梯度方法。与OpenMDAO 2.2一样,没有任何内置方法可以强制执行这种离散化。您需要在问题类周围使用外部循环才能使其工作。

这是一个简单的例子:

import numpy as np
from openmdao.api import Problem, ScipyOptimizeDriver, ExecComp, IndepVarComp

# build the model
prob = Problem()
indeps = prob.model.add_subsystem('indeps', IndepVarComp())
indeps.add_output('x', 3.0)
indeps.add_output('y', -4.0)

prob.model.add_subsystem('paraboloid', ExecComp('f = (x-3)**2 + x*y + (y+4)**2 - 3'))

prob.model.connect('indeps.x', 'paraboloid.x')
prob.model.connect('indeps.y', 'paraboloid.y')

# setup the optimization
prob.driver = ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'

prob.model.add_design_var('indeps.y', lower=-50, upper=50)
prob.model.add_objective('paraboloid.f')

prob.setup()


for x in np.arange(-10,12,2): 
    prob['indeps.x'] = x

    # could call just run_model if no optimization was desired
    #prob.run_model()

    # for each value of x, optimize for y
    prob.run_driver()

    # minimum value
    print(prob['paraboloid.f'])

    # location of the minimum
    print(prob['indeps.x'])
    print(prob['indeps.y'])