Source code for openmdao.test.rosen_suzuki
"""
Model based on CONMIN test.
"""
from openmdao.main.api import Assembly, Component, set_as_top
from openmdao.lib.casehandlers.api import ListCaseRecorder
from openmdao.main.datatypes.api import Array, Float
from openmdao.lib.drivers.conmindriver import CONMINdriver
from openmdao.lib.drivers.slsqpdriver import SLSQPdriver
[docs]class PreProc(Component):
""" Dummy pre-processor. """
x_in = Array([1., 1., 1., 1.], iotype='in', low=-10, high=99)
x_out = Array(iotype='out')
[docs] def execute(self):
self.x_out = self.x_in
[docs]class ScalingPreProc(PreProc):
""" Scaling pre-processor. """
scaler = Float(1.0, iotype='in')
[docs] def execute(self):
super(ScalingPreProc, self).execute()
self.x_out *= self.scaler
[docs]class OptRosenSuzukiComponent(Component):
x = Array(iotype='in', low=-10, high=99)
result = Float(iotype='out')
opt_objective = Float(iotype='out')
[docs] def execute(self):
"""calculate the new objective value"""
self.result = (self.x[0]**2 - 5.*self.x[0] +
self.x[1]**2 - 5.*self.x[1] +
2.*self.x[2]**2 - 21.*self.x[2] +
self.x[3]**2 + 7.*self.x[3] + 50)
[docs]class PostProc(Component):
""" Dummy post-processor. """
result_in = Float(iotype='in')
result_out = Float(iotype='out')
[docs] def execute(self):
self.result_out = self.result_in
[docs]class ScalingPostProc(PostProc):
""" Scaling post-processor. """
scaler = Float(1.0, iotype='in')
[docs] def execute(self):
super(ScalingPostProc, self).execute()
self.result_out *= self.scaler
[docs]class Simulation(Assembly):
[docs]class NestedSimulation(Assembly):
if __name__ == '__main__':
sim = set_as_top(Simulation())
flow = sim.get_dataflow()
for item in flow['connections']:
print 'connection:', item
for item in flow['parameters']:
print 'parameter:', item
for item in flow['constraints']:
print 'constraint:', item
for item in flow['objectives']:
print 'objective:', item
sim.run()
print 'objective', sim.comp.opt_objective, sim.driver.eval_objective()
for i in range(len(sim.preproc.x_in)):
print 'design_var', i, sim.comp.opt_design_vars[i], sim.preproc.x_in[i]
sim.replace('preproc', ScalingPreProc())
sim.replace('driver', SLSQPdriver())