I am trying to implement Collaborative Optimization & other multi-level architectures on OpenMDAO. I read here that this can be done by defining a separate solve_nonline
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.