How to use nested problems in OpenMDAO 1.x?

五迷三道 提交于 2019-11-27 09:36:19

You are right that solve_nonlinear on Problem is never called, because Problem is not an OpenMDAO component and doesn't have a solve_nonlinear method. What you want to do in order to run a submodel problem inside another problem is to encapsulate it in a Component instance. It would look something like this:

class SubOptimization(Component)

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

        # Inputs to this subprob
        self.add_param('z', val=np.zeros(2))
        self.add_param('x', val=0.0)
        self.add_param('y2', val=1.0)

        # Unknowns for this sub prob
        self.add_output('y1', val=1.0)

        self.problem = prob = Problem()
        prob.root = Group()
        prob.add('px', IndepVarComp('x', 1.0), promotes=['*'])
        prob.add('d1', SellarDis1(), promotes=['*'])

        # TODO - add cons/objs for sub prob

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

        prob.driver.add_desvar('x', lower=0., upper=10.0)
        prob.driver.add_objective('obj')
        prob.driver.add_constraint('con1', upper=0.0)
        prob.driver.add_constraint('con2', upper=0.0)

        prob.setup()

        # Must finite difference across optimizer
        self.fd_options['force_fd'] = True

    def solve_nonlinear(self, params, unknowns, resids):

        prob = self.problem

        # Pass values into our problem
        prob['x'] = params['x']
        prob['z'] = params['z']
        prob['y2'] = params['y2']

        # Run problem
        prob.run()

        # Pull values from problem
        unknowns['y1'] = prob['y1']

You can place this component into your main Problem (along with one for discipline 2, though 2 doesn't really need a sub-optimization since it has no local design variabes) and optimize the global design variable around it.

One caveat: this isn't something I have tried (nor have I tested the incomplete code snippet above), but it should get you on the right track. It's possible you may encounter a bug since this isn't really tested much. When I get some time, I will put together a CO test like this for the OpenMDAO tests so that we are safe.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!