<s> """<STR_LIT>""" <EOL> from math import isnan <EOL> from numpy import zeros , array <EOL> from slsqp . slsqp import slsqp , closeunit , pyflush <EOL> from openmdao . main . datatypes . api import Enum , Float , Int , Str <EOL> from openmdao . main . driver_uses_derivatives import Driver <EOL> from openmdao . main . hasparameters import HasParameters <EOL> from openmdao . main . hasconstraints import HasConstraints <EOL> from openmdao . main . hasobjective import HasObjective <EOL> from openmdao . main . interfaces import IHasParameters , IHasConstraints , IHasObjective , implements , IOptimizer <EOL> from openmdao . util . decorators import add_delegate <EOL> @ add_delegate ( HasParameters , HasConstraints , HasObjective ) <EOL> class SLSQPdriver ( Driver ) : <EOL> """<STR_LIT>""" <EOL> implements ( IHasParameters , IHasConstraints , IHasObjective , IOptimizer ) <EOL> accuracy = Float ( <NUM_LIT> , iotype = '<STR_LIT>' , <EOL> desc = '<STR_LIT>' ) <EOL> maxiter = Int ( <NUM_LIT:50> , iotype = '<STR_LIT>' , <EOL> desc = '<STR_LIT>' ) <EOL> iprint = Enum ( <NUM_LIT:0> , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , iotype = '<STR_LIT>' , <EOL> desc = '<STR_LIT>' ) <EOL> iout = Int ( <NUM_LIT:6> , iotype = '<STR_LIT>' , <EOL> desc = '<STR_LIT>' ) <EOL> output_filename = Str ( '<STR_LIT>' , iotype = '<STR_LIT>' , <EOL> desc = '<STR_LIT>' ) <EOL> error_code = Int ( <NUM_LIT:0> , iotype = '<STR_LIT>' , <EOL> desc = '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> super ( SLSQPdriver , self ) . __init__ ( ) <EOL> self . error_messages = { <EOL> - <NUM_LIT:1> : "<STR_LIT>" , <EOL> <NUM_LIT:1> : "<STR_LIT>" , <EOL> <NUM_LIT:2> : "<STR_LIT>" , <EOL> <NUM_LIT:3> : "<STR_LIT>" , <EOL> <NUM_LIT:4> : "<STR_LIT>" , <EOL> <NUM_LIT:5> : "<STR_LIT>" , <EOL> <NUM_LIT:6> : "<STR_LIT>" , <EOL> <NUM_LIT:7> : "<STR_LIT>" , <EOL> <NUM_LIT:8> : "<STR_LIT>" , <EOL> <NUM_LIT:9> : "<STR_LIT>" , <EOL> } <EOL> self . x = zeros ( <NUM_LIT:0> , '<STR_LIT:d>' ) <EOL> self . x_lower_bounds = zeros ( <NUM_LIT:0> , '<STR_LIT:d>' ) <EOL> self . x_upper_bounds = zeros ( <NUM_LIT:0> , '<STR_LIT:d>' ) <EOL> self . inputs = None <EOL> self . obj = None <EOL> self . con = None <EOL> self . nparam = None <EOL> self . ncon = None <EOL> self . neqcon = None <EOL> self . ff = <NUM_LIT:0> <EOL> self . nfunc = <NUM_LIT:0> <EOL> self . ngrad = <NUM_LIT:0> <EOL> self . _continue = None <EOL> def start_iteration ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( SLSQPdriver , self ) . run_iteration ( ) <EOL> self . inputs = self . list_param_group_targets ( ) <EOL> self . obj = self . list_objective_targets ( ) <EOL> self . con = self . list_constraint_targets ( ) <EOL> self . nparam = self . total_parameters ( ) <EOL> self . ncon = self . total_constraints ( ) <EOL> self . neqcon = self . total_eq_constraints ( ) <EOL> self . x = self . eval_parameters ( self . parent ) <EOL> self . x_lower_bounds = self . get_lower_bounds ( ) <EOL> self . x_upper_bounds = self . get_upper_bounds ( ) <EOL> self . ff = <NUM_LIT:0> <EOL> self . nfunc = <NUM_LIT:0> <EOL> self . ngrad = <NUM_LIT:0> <EOL> self . _continue = True <EOL> def run_iteration ( self ) : <EOL> """<STR_LIT>""" <EOL> n = self . nparam <EOL> m = self . ncon <EOL> meq = self . neqcon <EOL> la = max ( m , <NUM_LIT:1> ) <EOL> gg = zeros ( [ la ] , '<STR_LIT:d>' ) <EOL> df = zeros ( [ n + <NUM_LIT:1> ] , '<STR_LIT:d>' ) <EOL> dg = zeros ( [ la , n + <NUM_LIT:1> ] , '<STR_LIT:d>' ) <EOL> mineq = m - meq + <NUM_LIT:2> * ( n + <NUM_LIT:1> ) <EOL> lsq = ( n + <NUM_LIT:1> ) * ( ( n + <NUM_LIT:1> ) + <NUM_LIT:1> ) + meq * ( ( n + <NUM_LIT:1> ) + <NUM_LIT:1> ) + mineq * ( ( n + <NUM_LIT:1> ) + <NUM_LIT:1> ) <EOL> lsi = ( ( n + <NUM_LIT:1> ) - meq + <NUM_LIT:1> ) * ( mineq + <NUM_LIT:2> ) + <NUM_LIT:2> * mineq <EOL> lsei = ( ( n + <NUM_LIT:1> ) + mineq ) * ( ( n + <NUM_LIT:1> ) - meq ) + <NUM_LIT:2> * meq + ( n + <NUM_LIT:1> ) <EOL> slsqpb = ( n + <NUM_LIT:1> ) * ( n / <NUM_LIT:2> ) + <NUM_LIT:2> * m + <NUM_LIT:3> * n + <NUM_LIT:3> * ( n + <NUM_LIT:1> ) + <NUM_LIT:1> <EOL> lw = lsq + lsi + lsei + slsqpb + n + m <EOL> w = zeros ( [ lw ] , '<STR_LIT:d>' ) <EOL> ljw = max ( mineq , ( n + <NUM_LIT:1> ) - meq ) <EOL> jw = zeros ( [ ljw ] , '<STR_LIT:i>' ) <EOL> try : <EOL> dg , self . error_code , self . nfunc , self . ngrad = slsqp ( self . ncon , self . neqcon , la , self . nparam , <EOL> self . x , self . x_lower_bounds , self . x_upper_bounds , <EOL> self . ff , gg , df , dg , self . accuracy , self . maxiter , <EOL> self . iprint - <NUM_LIT:1> , self . iout , self . output_filename , <EOL> self . error_code , w , lw , jw , ljw , <EOL> self . nfunc , self . ngrad , <EOL> self . _func , self . _grad ) <EOL> except Exception as err : <EOL> self . _logger . error ( str ( err ) ) <EOL> raise <EOL> if self . iprint > <NUM_LIT:0> : <EOL> closeunit ( self . iout ) <EOL> if self . error_code != <NUM_LIT:0> : <EOL> self . _logger . warning ( self . error_messages [ self . error_code ] ) <EOL> self . _continue = False <EOL> def _func ( self , m , me , la , n , f , g , xnew ) : <EOL> """<STR_LIT>""" <EOL> self . set_parameters ( xnew ) <EOL> super ( SLSQPdriver , self ) . run_iteration ( ) <EOL> f = self . eval_objective ( ) <EOL> if isnan ( f ) : <EOL> msg = "<STR_LIT>" <EOL> self . raise_exception ( msg , RuntimeError ) <EOL> if self . ncon > <NUM_LIT:0> : <EOL> g = - <NUM_LIT:1.> * array ( self . eval_constraints ( self . parent ) ) <EOL> if self . iprint > <NUM_LIT:0> : <EOL> pyflush ( self . iout ) <EOL> return f , g <EOL> def _grad ( self , m , me , la , n , f , g , df , dg , xnew ) : <EOL> """<STR_LIT>""" <EOL> J = self . _calc_gradient ( self . inputs , self . obj + self . con ) <EOL> df [ <NUM_LIT:0> : self . nparam ] = J [ <NUM_LIT:0> , : ] . ravel ( ) <EOL> if self . ncon > <NUM_LIT:0> : <EOL> dg [ <NUM_LIT:0> : self . ncon , <NUM_LIT:0> : self . nparam ] = - J [ <NUM_LIT:1> : <NUM_LIT:1> + self . ncon , : ] <EOL> return df , dg <EOL> def requires_derivs ( self ) : <EOL> """<STR_LIT>""" <EOL> return True </s>
