<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from openmdao . main . api import Driver , Architecture <EOL> from openmdao . lib . drivers . api import SLSQPdriver <EOL> from openmdao . main . datatypes . api import Float , Array <EOL> class CO ( Architecture ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( CO , self ) . __init__ ( * args , ** kwargs ) <EOL> self . param_types = [ '<STR_LIT>' ] <EOL> self . constraint_types = [ '<STR_LIT>' ] <EOL> self . num_allowed_objectives = <NUM_LIT:1> <EOL> self . has_coupling_vars = True <EOL> self . has_global_des_vars = True <EOL> def configure ( self ) : <EOL> global_dvs = self . parent . get_global_des_vars ( ) <EOL> local_dvs = self . parent . get_local_des_vars ( ) <EOL> all_dvs_by_comp = self . parent . get_des_vars_by_comp ( ) <EOL> objective = self . parent . get_objectives ( ) <EOL> constraints = self . parent . list_constraints ( ) <EOL> constraints_by_comp = self . parent . get_constraints_by_comp ( ) <EOL> coupling = self . parent . list_coupling_vars ( ) <EOL> coupl_indeps_by_comp = self . parent . get_coupling_indeps_by_comp ( ) <EOL> coupl_deps_by_comp = self . parent . get_coupling_deps_by_comp ( ) <EOL> self . target_var_map = dict ( ) <EOL> global_opt = self . parent . add ( '<STR_LIT>' , SLSQPdriver ( ) ) <EOL> global_opt . recorders = self . data_recorders <EOL> for comp , param in global_dvs : <EOL> param . initialize ( self . parent , self . parent ) <EOL> for comp , param in local_dvs : <EOL> param . initialize ( self . parent , self . parent ) <EOL> for key , couple in coupling . iteritems ( ) : <EOL> couple . indep . set ( couple . start ) <EOL> initial_conditions = np . array ( [ param . evaluate ( ) for comp , param in global_dvs ] ) . flatten ( ) <EOL> self . parent . add_trait ( '<STR_LIT>' , Array ( initial_conditions , iotype = "<STR_LIT>" ) ) <EOL> for i , ( comp , param ) in enumerate ( global_dvs ) : <EOL> target_var = '<STR_LIT>' % i <EOL> global_opt . add_parameter ( target_var , low = param . low , high = param . high ) <EOL> for var in param . targets : <EOL> self . target_var_map [ var ] = target_var <EOL> initial_conditions = [ couple . indep . evaluate ( ) for key , couple in coupling . iteritems ( ) ] <EOL> self . parent . add_trait ( '<STR_LIT>' , Array ( initial_conditions , iotype = "<STR_LIT>" ) ) <EOL> for i , ( key , couple ) in enumerate ( coupling . iteritems ( ) ) : <EOL> target_var = '<STR_LIT>' % i <EOL> low = couple . indep . low or - <NUM_LIT> <EOL> high = couple . indep . high or <NUM_LIT> <EOL> global_opt . add_parameter ( target_var , low = low , high = high ) <EOL> self . target_var_map [ couple . indep . target ] = target_var <EOL> self . target_var_map [ couple . dep . target ] = target_var <EOL> initial_conditions = [ param . evaluate ( ) [ <NUM_LIT:0> ] for comp , param in local_dvs ] <EOL> self . parent . add_trait ( "<STR_LIT>" , Array ( initial_conditions , iotype = "<STR_LIT>" ) ) <EOL> for i , ( comp , param ) in enumerate ( local_dvs ) : <EOL> target_var = '<STR_LIT>' % i <EOL> self . target_var_map [ param . target ] = target_var <EOL> global_opt . add_parameter ( target_var , low = param . low , high = param . high ) <EOL> obj = objective . items ( ) [ <NUM_LIT:0> ] <EOL> new_objective = str ( obj [ <NUM_LIT:1> ] . text ) <EOL> for old_var , new_var in sorted ( self . target_var_map . items ( ) , key = lambda x : len ( x [ <NUM_LIT:0> ] ) , reverse = True ) : <EOL> new_objective = new_objective . replace ( old_var , new_var ) <EOL> global_opt . add_objective ( new_objective ) <EOL> for comp , params in all_dvs_by_comp . iteritems ( ) : <EOL> local_opt = self . parent . add ( '<STR_LIT>' % comp , SLSQPdriver ( ) ) <EOL> local_opt . iprint = <NUM_LIT:0> <EOL> global_opt . workflow . add ( local_opt . name ) <EOL> residuals = [ ] <EOL> for param in params : <EOL> local_opt . add_parameter ( param . target , low = param . low , high = param . high ) <EOL> residuals . append ( "<STR_LIT>" % ( self . target_var_map [ param . target ] , param . target ) ) <EOL> if comp in coupl_indeps_by_comp : <EOL> for couple in coupl_indeps_by_comp [ comp ] : <EOL> if couple . indep . low is not None : <EOL> low = couple . indep . low <EOL> else : <EOL> low = - <NUM_LIT> <EOL> if couple . indep . high is not None : <EOL> high = couple . indep . high <EOL> else : <EOL> high = <NUM_LIT> <EOL> local_opt . add_parameter ( couple . indep . target , low = low , high = high ) <EOL> residuals . append ( "<STR_LIT>" % ( self . target_var_map [ couple . indep . target ] , couple . indep . target ) ) <EOL> if comp in coupl_deps_by_comp : <EOL> for couple in coupl_deps_by_comp [ comp ] : <EOL> residuals . append ( "<STR_LIT>" % ( self . target_var_map [ couple . dep . target ] , couple . dep . target ) ) <EOL> if comp in constraints_by_comp : <EOL> for const in constraints_by_comp [ comp ] : <EOL> local_opt . add_constraint ( str ( const ) ) <EOL> residuals = "<STR_LIT:+>" . join ( residuals ) <EOL> global_constraint = "<STR_LIT>" % residuals <EOL> global_opt . add_constraint ( global_constraint ) <EOL> local_opt . add_objective ( residuals ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> from openmdao . lib . optproblems . api import SellarProblem <EOL> prob = SellarProblem ( ) <EOL> prob . architecture = CO ( ) <EOL> prob . run ( ) <EOL> print "<STR_LIT:\n>" <EOL> print "<STR_LIT>" % ( prob . dis1 . z1 , prob . dis1 . z2 , prob . dis1 . x1 ) <EOL> print "<STR_LIT>" % ( prob . global_des_var_targets [ <NUM_LIT:0> ] , prob . global_des_var_targets [ <NUM_LIT:1> ] , prob . local_des_var_targets [ <NUM_LIT:0> ] ) <EOL> print "<STR_LIT>" % ( prob . dis1 . y1 , prob . dis2 . y2 ) <EOL> print "<STR_LIT>" % ( prob . coupling_var_targets [ <NUM_LIT:0> ] , prob . coupling_var_targets [ <NUM_LIT:1> ] ) <EOL> print "<STR_LIT>" , prob . driver . eval_objective ( ) </s>
