OpenMDAO:如何将约束边界设置为另一个组件的输出?

OpenMDAO: How to set a constraint bound as the output of another component?

我想最小化一个组件的输出,同时确保它大于第二个组件的输出。

add_constraint 期望 "Iterable of numeric values, or a scalar numeric value" 当我向它提供带有输出名称的字符串时。

当给定 prob['name'] 时,错误是“'NoneType' 对象不可订阅”

这一定是简单且有记录的东西,但我还没有找到它。

import openmdao.api as om

prob=om.Problem()
independant = prob.model.add_subsystem('independant', om.IndepVarComp())
independant.add_output('x', val = 3.0)

prob.model.add_subsystem('steep_line', om.ExecComp('f = x'))
prob.model.add_subsystem('shallow_line', om.ExecComp('g = 0.5*x + 1.0'))

prob.model.connect('independant.x', ['steep_line.x', 'shallow_line.x'])

prob.driver = om.ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'

prob.model.add_design_var('independant.x', lower=0.0, upper=3.0)

#Change which of the next two lines is commented out to see both errors I
#have encountered.
prob.model.add_constraint('steep_line.f', lower='shallow_line.g')
#prob.model.add_constraint('steep_line.f', lower=prob['shallow_line.g'])

prob.model.add_objective('steep_line.f')

prob.setup()
prob.run_driver()

print('x:', prob['independant.x'])

期望的结果是达到 independant.x = 2.0

的优化

提前感谢您提供的任何帮助。

您不能指定非常量下限、上限或等于界限。要使这项工作正常进行,您需要添加(另一个)ExecComp,然后将这两个值彼此相减。然后,您可以将此新 comp 的结果输出设置为具有 0

的下限
import openmdao.api as om

prob=om.Problem()
independant = prob.model.add_subsystem('independant', om.IndepVarComp())
independant.add_output('x', val = 3.0)

prob.model.add_subsystem('steep_line', om.ExecComp('f = x'))
prob.model.add_subsystem('shallow_line', om.ExecComp('f = 0.5*x + 1.0'))

prob.model.add_subsystem('constraint', 
                         om.ExecComp('g = f_computed - lower'))

prob.model.connect('independant.x', ['steep_line.x', 'shallow_line.x'])
prob.model.connect('shallow_line.f', 'constraint.lower')
prob.model.connect('steep_line.f', 'constraint.f_computed')

prob.driver = om.ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'

prob.model.add_design_var('independant.x', lower=0.0, upper=3.0)

prob.model.add_constraint('constraint.g', lower=0)

prob.model.add_objective('steep_line.f')

prob.setup()
prob.run_driver()

print('x:', prob['independant.x'])