openmdao/drivers/scipy_optimizer.py
def run(self):
        """
        Optimize the problem using selected Scipy optimizer.

        Returns
        -------
        boolean
            Failure flag; True if failed to converge, False is successful.
        """
        problem = self._problem
        opt = self.options['optimizer']
        model = problem.model
        self.iter_count = 0
        self._total_jac = None

        # Initial Run
        with RecordingDebugging(self.options['optimizer'], self.iter_count, self) as rec:
            model._solve_nonlinear()
            self.iter_count += 1

        self._con_cache = self.get_constraint_values()
        desvar_vals = self.get_design_var_values()
        self._dvlist = list(self._designvars)

        # maxiter and disp get passsed into scipy with all the other options.
        self.opt_settings['maxiter'] = self.options['maxiter']
        self.opt_settings['disp'] = self.options['disp']

        # Size Problem
        nparam = 0
        for param in itervalues(self._designvars):
            nparam += param['size']
        x_init = np.empty(nparam)

        # Initial Design Vars
        i = 0
        use_bounds = (opt in _bounds_optimizers)
        if use_bounds:
            bounds = []
        else:
            bounds = None

        for name, meta in iteritems(self._designvars):
            size = meta['size']
            x_init[i:i + size] = desvar_vals[name]
            i += size

            # Bounds if our optimizer supports them
            if use_bounds:
                meta_low = meta['lower']
                meta_high = meta['upper']
                for j in range(size):

                    if isinstance(meta_low, np.ndarray):
                        p_low = meta_low[j]
                    else:
                        p_low = meta_low

                    if isinstance(meta_high, np.ndarray):
                        p_high = meta_high[j]
                    else:
                        p_high = meta_high

                    bounds.append((p_low, p_high))

        # Constraints
        constraints = []
        i = 1  # start at 1 since row 0 is the objective.  Constraints start at row 1.
        lin_i = 0  # counter for linear constraint jacobian
        lincons = []  # list of linear constraints
        self._obj_and_nlcons = list(self._objs)

        if opt in _constraint_optimizers:
            for name, meta in iteritems(self._cons):
                size = meta['size']
                upper = meta['upper']
                lower = meta['lower']
                equals = meta['equals']
                if 'linear' in meta and meta['linear']:
                    lincons.append(name)
                    self._con_idx[name] = lin_i
                    lin_i += size
                else:
                    self._obj_and_nlcons.append(name)
                    self._con_idx[name] = i
                    i += size

                # In scipy constraint optimizers take constraints in two separate formats
                if opt in ['trust-constr']:  # Type of constraints is list of NonlinearConstraint
                    try:
                        from scipy.optimize import NonlinearConstraint
                    except ImportError:
                        msg = ('The "trust-constr" optimizer is supported for SciPy 1.1.0 and'
                               'above. The installed version is {}')
                        raise ImportError(msg.format(scipy_version))

                    if equals is not None:
                        lb = ub = equals
                    else:
                        lb = lower
                        ub = upper
                    # Loop over every index separately,
                    # because scipy calls each constraint by index.
                    for j in range(size):
                        # Double-sided constraints are accepted by the algorithm
                        args = [name, False, j]
                        # TODO linear constraint if meta['linear']
                        # TODO add option for Hessian
                        con = NonlinearConstraint(fun=signature_extender(self._con_val_func, args),
                                                  lb=lb, ub=ub,
                                                  jac=signature_extender(self._congradfunc, args))
                        constraints.append(con)
                else:  # Type of constraints is list of dict
                    # Loop over every index separately,
                    # because scipy calls each constraint by index.
                    for j in range(size):
                        con_dict = {}
                        if meta['equals'] is not None:
                            con_dict['type'] = 'eq'
                        else:
                            con_dict['type'] = 'ineq'
                        con_dict['fun'] = self._confunc
                        if opt in _constraint_grad_optimizers:
                            con_dict['jac'] = self._congradfunc
                        con_dict['args'] = [name, False, j]
                        constraints.append(con_dict)

                        if isinstance(upper, np.ndarray):
                            upper = upper[j]

                        if isinstance(lower, np.ndarray):
                            lower = lower[j]

                        dblcon = (upper < openmdao.INF_BOUND) and (lower > -openmdao.INF_BOUND)

                        # Add extra constraint if double-sided
                        if dblcon:
                            dcon_dict = {}
                            dcon_dict['type'] = 'ineq'
                            dcon_dict['fun'] = self._confunc
                            if opt in _constraint_grad_optimizers:
                                dcon_dict['jac'] = self._congradfunc
                            dcon_dict['args'] = [name, True, j]
                            constraints.append(dcon_dict)

            # precalculate gradients of linear constraints
            if lincons:
                self._lincongrad_cache = self._compute_totals(of=lincons, wrt=self._dvlist,
                                                              return_format='array')
            else:
                self._lincongrad_cache = None

        # Provide gradients for optimizers that support it
        if opt in _gradient_optimizers:
            jac = self._gradfunc
        else:
            jac = None

        # Hessian calculation method for optimizers, which require it
        if opt in _hessian_optimizers:
            if 'hess' in self.opt_settings:
                hess = self.opt_settings.pop('hess')
            else:
                # Defaults to BFGS, if not in opt_settings
                from scipy.optimize import BFGS
                hess = BFGS()
        else:
            hess = None

        # compute dynamic simul deriv coloring if option is set
        if coloring_mod._use_sparsity and self.options['dynamic_simul_derivs']:
            coloring_mod.dynamic_simul_coloring(self, run_model=False, do_sparsity=False)

        # optimize
        try:
            result = minimize(self._objfunc, x_init,
                              # args=(),
                              method=opt,
                              jac=jac,
                              hess=hess,
                              # hessp=None,
                              bounds=bounds,
                              constraints=constraints,
                              tol=self.options['tol'],
                              # callback=None,
                              options=self.opt_settings)

        # If an exception was swallowed in one of our callbacks, we want to raise it
        # rather than the cryptic message from scipy.
        except Exception as msg:
            if self._exc_info is not None:
                self._reraise()
            else:
                raise

        if self._exc_info is not None:
            self._reraise()

        self.result = result

        if hasattr(result, 'success'):
            self.fail = False if result.success else True
            if self.fail:
                print('Optimization FAILED.')
                print(result.message)
                print('-' * 35)

            elif self.options['disp']:
                print('Optimization Complete')
                print('-' * 35)
        else:
            self.fail = True  # It is not known, so the worst option is assumed
            print('Optimization Complete (success not known)')
            print(result.message)
            print('-' * 35)

        return self.fail

        Optimize the problem using selected Scipy optimizer.

        Returns
        -------
        boolean
            Failure flag; True if failed to converge, False is successful.
        
Optimize the problem using selected Scipy optimizer.
Returns

boolean
Failure flag; True if failed to converge, False is successful.
Optimize the problem using selected Scipy optimizer . Returns boolean Failure flag ; True if failed to converge False is successful .
def run(self):
        problem = self._problem
        opt = self.options['optimizer']
        model = problem.model
        self.iter_count = 0
        self._total_jac = None
        with RecordingDebugging(self.options['optimizer'], self.iter_count, self) as rec:
            model._solve_nonlinear()
            self.iter_count += 1
        self._con_cache = self.get_constraint_values()
        desvar_vals = self.get_design_var_values()
        self._dvlist = list(self._designvars)
        self.opt_settings['maxiter'] = self.options['maxiter']
        self.opt_settings['disp'] = self.options['disp']
        nparam = 0
        for param in itervalues(self._designvars):
            nparam += param['size']
        x_init = np.empty(nparam)
        i = 0
        use_bounds = (opt in _bounds_optimizers)
        if use_bounds:
            bounds = []
        else:
            bounds = None
        for name, meta in iteritems(self._designvars):
            size = meta['size']
            x_init[i:i + size] = desvar_vals[name]
            i += size
            if use_bounds:
                meta_low = meta['lower']
                meta_high = meta['upper']
                for j in range(size):
                    if isinstance(meta_low, np.ndarray):
                        p_low = meta_low[j]
                    else:
                        p_low = meta_low
                    if isinstance(meta_high, np.ndarray):
                        p_high = meta_high[j]
                    else:
                        p_high = meta_high
                    bounds.append((p_low, p_high))
        constraints = []
        i = 1  
        lin_i = 0  
        lincons = []  
        self._obj_and_nlcons = list(self._objs)
        if opt in _constraint_optimizers:
            for name, meta in iteritems(self._cons):
                size = meta['size']
                upper = meta['upper']
                lower = meta['lower']
                equals = meta['equals']
                if 'linear' in meta and meta['linear']:
                    lincons.append(name)
                    self._con_idx[name] = lin_i
                    lin_i += size
                else:
                    self._obj_and_nlcons.append(name)
                    self._con_idx[name] = i
                    i += size
                if opt in ['trust-constr']:  
                    try:
                        from scipy.optimize import NonlinearConstraint
                    except ImportError:
                        msg = ('The "trust-constr" optimizer is supported for SciPy 1.1.0 and'
                               'above. The installed version is {}')
                        raise ImportError(msg.format(scipy_version))
                    if equals is not None:
                        lb = ub = equals
                    else:
                        lb = lower
                        ub = upper
                    for j in range(size):
                        args = [name, False, j]
                        con = NonlinearConstraint(fun=signature_extender(self._con_val_func, args),
                                                  lb=lb, ub=ub,
                                                  jac=signature_extender(self._congradfunc, args))
                        constraints.append(con)
                else:  
                    for j in range(size):
                        con_dict = {}
                        if meta['equals'] is not None:
                            con_dict['type'] = 'eq'
                        else:
                            con_dict['type'] = 'ineq'
                        con_dict['fun'] = self._confunc
                        if opt in _constraint_grad_optimizers:
                            con_dict['jac'] = self._congradfunc
                        con_dict['args'] = [name, False, j]
                        constraints.append(con_dict)
                        if isinstance(upper, np.ndarray):
                            upper = upper[j]
                        if isinstance(lower, np.ndarray):
                            lower = lower[j]
                        dblcon = (upper < openmdao.INF_BOUND) and (lower > -openmdao.INF_BOUND)
                        if dblcon:
                            dcon_dict = {}
                            dcon_dict['type'] = 'ineq'
                            dcon_dict['fun'] = self._confunc
                            if opt in _constraint_grad_optimizers:
                                dcon_dict['jac'] = self._congradfunc
                            dcon_dict['args'] = [name, True, j]
                            constraints.append(dcon_dict)
            if lincons:
                self._lincongrad_cache = self._compute_totals(of=lincons, wrt=self._dvlist,
                                                              return_format='array')
            else:
                self._lincongrad_cache = None
        if opt in _gradient_optimizers:
            jac = self._gradfunc
        else:
            jac = None
        if opt in _hessian_optimizers:
            if 'hess' in self.opt_settings:
                hess = self.opt_settings.pop('hess')
            else:
                from scipy.optimize import BFGS
                hess = BFGS()
        else:
            hess = None
        if coloring_mod._use_sparsity and self.options['dynamic_simul_derivs']:
            coloring_mod.dynamic_simul_coloring(self, run_model=False, do_sparsity=False)
        try:
            result = minimize(self._objfunc, x_init,
                              method=opt,
                              jac=jac,
                              hess=hess,
                              bounds=bounds,
                              constraints=constraints,
                              tol=self.options['tol'],
                              options=self.opt_settings)
        except Exception as msg:
            if self._exc_info is not None:
                self._reraise()
            else:
                raise
        if self._exc_info is not None:
            self._reraise()
        self.result = result
        if hasattr(result, 'success'):
            self.fail = False if result.success else True
            if self.fail:
                print('Optimization FAILED.')
                print(result.message)
                print('-' * 35)
            elif self.options['disp']:
                print('Optimization Complete')
                print('-' * 35)
        else:
            self.fail = True  
            print('Optimization Complete (success not known)')
            print(result.message)
            print('-' * 35)
        return self.fail
def run ( self ) : problem = self . _problem opt = self . options [ 'optimizer' ] model = problem . model self . iter_count = 0 self . _total_jac = None with RecordingDebugging ( self . options [ 'optimizer' ] , self . iter_count , self ) as rec : model . _solve_nonlinear ( ) self . iter_count += 1 self . _con_cache = self . get_constraint_values ( ) desvar_vals = self . get_design_var_values ( ) self . _dvlist = list ( self . _designvars ) self . opt_settings [ 'maxiter' ] = self . options [ 'maxiter' ] self . opt_settings [ 'disp' ] = self . options [ 'disp' ] nparam = 0 for param in itervalues ( self . _designvars ) : nparam += param [ 'size' ] x_init = np . empty ( nparam ) i = 0 use_bounds = ( opt in _bounds_optimizers ) if use_bounds : bounds = [ ] else : bounds = None for name , meta in iteritems ( self . _designvars ) : size = meta [ 'size' ] x_init [ i : i + size ] = desvar_vals [ name ] i += size if use_bounds : meta_low = meta [ 'lower' ] meta_high = meta [ 'upper' ] for j in range ( size ) : if isinstance ( meta_low , np . ndarray ) : p_low = meta_low [ j ] else : p_low = meta_low if isinstance ( meta_high , np . ndarray ) : p_high = meta_high [ j ] else : p_high = meta_high bounds . append ( ( p_low , p_high ) ) constraints = [ ] i = 1 lin_i = 0 lincons = [ ] self . _obj_and_nlcons = list ( self . _objs ) if opt in _constraint_optimizers : for name , meta in iteritems ( self . _cons ) : size = meta [ 'size' ] upper = meta [ 'upper' ] lower = meta [ 'lower' ] equals = meta [ 'equals' ] if 'linear' in meta and meta [ 'linear' ] : lincons . append ( name ) self . _con_idx [ name ] = lin_i lin_i += size else : self . _obj_and_nlcons . append ( name ) self . _con_idx [ name ] = i i += size if opt in [ 'trust-constr' ] : try : from scipy . optimize import NonlinearConstraint except ImportError : msg = ( 'The "trust-constr" optimizer is supported for SciPy 1.1.0 and' 'above. The installed version is {}' ) raise ImportError ( msg . format ( scipy_version ) ) if equals is not None : lb = ub = equals else : lb = lower ub = upper for j in range ( size ) : args = [ name , False , j ] con = NonlinearConstraint ( fun = signature_extender ( self . _con_val_func , args ) , lb = lb , ub = ub , jac = signature_extender ( self . _congradfunc , args ) ) constraints . append ( con ) else : for j in range ( size ) : con_dict = { } if meta [ 'equals' ] is not None : con_dict [ 'type' ] = 'eq' else : con_dict [ 'type' ] = 'ineq' con_dict [ 'fun' ] = self . _confunc if opt in _constraint_grad_optimizers : con_dict [ 'jac' ] = self . _congradfunc con_dict [ 'args' ] = [ name , False , j ] constraints . append ( con_dict ) if isinstance ( upper , np . ndarray ) : upper = upper [ j ] if isinstance ( lower , np . ndarray ) : lower = lower [ j ] dblcon = ( upper < openmdao . INF_BOUND ) and ( lower > - openmdao . INF_BOUND ) if dblcon : dcon_dict = { } dcon_dict [ 'type' ] = 'ineq' dcon_dict [ 'fun' ] = self . _confunc if opt in _constraint_grad_optimizers : dcon_dict [ 'jac' ] = self . _congradfunc dcon_dict [ 'args' ] = [ name , True , j ] constraints . append ( dcon_dict ) if lincons : self . _lincongrad_cache = self . _compute_totals ( of = lincons , wrt = self . _dvlist , return_format = 'array' ) else : self . _lincongrad_cache = None if opt in _gradient_optimizers : jac = self . _gradfunc else : jac = None if opt in _hessian_optimizers : if 'hess' in self . opt_settings : hess = self . opt_settings . pop ( 'hess' ) else : from scipy . optimize import BFGS hess = BFGS ( ) else : hess = None if coloring_mod . _use_sparsity and self . options [ 'dynamic_simul_derivs' ] : coloring_mod . dynamic_simul_coloring ( self , run_model = False , do_sparsity = False ) try : result = minimize ( self . _objfunc , x_init , method = opt , jac = jac , hess = hess , bounds = bounds , constraints = constraints , tol = self . options [ 'tol' ] , options = self . opt_settings ) except Exception as msg : if self . _exc_info is not None : self . _reraise ( ) else : raise if self . _exc_info is not None : self . _reraise ( ) self . result = result if hasattr ( result , 'success' ) : self . fail = False if result . success else True if self . fail : print ( 'Optimization FAILED.' ) print ( result . message ) print ( '-' * 35 ) elif self . options [ 'disp' ] : print ( 'Optimization Complete' ) print ( '-' * 35 ) else : self . fail = True print ( 'Optimization Complete (success not known)' ) print ( result . message ) print ( '-' * 35 ) return self . fail
Optimize the problem using selected Scipy optimizer.
Optimize the problem using selected Scipy optimizer .
"""
        Optimize the problem using selected Scipy optimizer.

        Returns
        -------
        boolean
            Failure flag; True if failed to converge, False is successful.
        """ # Initial Run # maxiter and disp get passsed into scipy with all the other options. # Size Problem # Initial Design Vars # Bounds if our optimizer supports them # Constraints # start at 1 since row 0 is the objective.  Constraints start at row 1. # counter for linear constraint jacobian # list of linear constraints # In scipy constraint optimizers take constraints in two separate formats # Type of constraints is list of NonlinearConstraint # Loop over every index separately, # because scipy calls each constraint by index. # Double-sided constraints are accepted by the algorithm # TODO linear constraint if meta['linear'] # TODO add option for Hessian # Type of constraints is list of dict # Loop over every index separately, # because scipy calls each constraint by index. # Add extra constraint if double-sided # precalculate gradients of linear constraints # Provide gradients for optimizers that support it # Hessian calculation method for optimizers, which require it # Defaults to BFGS, if not in opt_settings # compute dynamic simul deriv coloring if option is set # optimize # args=(), # hessp=None, # callback=None, # If an exception was swallowed in one of our callbacks, we want to raise it # rather than the cryptic message from scipy. # It is not known, so the worst option is assumed
self