当前位置: 代码迷 >> 综合 >> minimize函数的使用(scipy.optimize)
  详细解决方案

minimize函数的使用(scipy.optimize)

热度:99   发布时间:2023-12-05 02:36:06.0

minimize函数的使用

    • 1.如何查看函数
    • 2.minimize函数的寻找参数
    • 3.minimize求解约束函数最小值
    • 4.minimize函数官方说明

1.如何查看函数

python中对某个函数进行查看,按Ctrl然后鼠标点击或者Ctrl+B即可跳转到该函数的定义处,同时里面包含该函数使用的example。
在这里插入图片描述在这里插入图片描述

2.minimize函数的寻找参数

我接触到fminunc函数是在看吴恩达机器学习的视频时,对于在matlab中该函数的使用:需要自己定义一个costFunction(theta),在该函数中计算损失函数以及梯度,将返回二者的值,然后将该函数的地址,初始的theta,以及options传入fminunct函数中,然后该函数会返回theta,优化后的损失值,以及exitFlag(值为1代表收敛,为0不收敛)。该函数会自动的选择共轭梯度,bfgs以及L-bfgs算法中的一种自动进行学习率的选择,从而去优化梯度下降函数。

在python的scipy.optimize库中包含该函数的替代函数minimize(),该函数的使用与matlab的fminunc函数有些不同,下面总结下,自己在使用的过程中遇到的问题。
1.首先查看下该函数:
官方声明过长,我把它放在该篇博客的最后面:

//这是其声明,我认为去查看函数的说明可以达到事半功倍的效果,千万别忽略
def minimize(fun, x0, args=(), method=None, jac=None, hess=None,hessp=None, bounds=None, constraints=(), tol=None,callback=None, options=None):

着重介一些重要参数代表什么:
fun:该参数就是costFunction你要去最小化的损失函数,将costFunction的名字传给fun
官方给的提示:

The objective function to be minimized.
fun(x, *args) -> float
where x is an 1-D array with shape (n,) and args
is a tuple of the fixed parameters needed to completely
specify the function.

意思就是损失函数在定义时,**theta必须为第一个参数且其shape必须为(n,)**即一维数组。在计算损失函数的时候用到的其他参数以元组的形式传入到args参数中(其他参数具体指X,Y,lambda等),最后返回损失的值,可以为数组形式,也可以为一个实数.

参数x0就是初始化的theta,其shape必须为shape(n,)即一维数组.

method:该参数代表采用的方式,默认是BFGS, L-BFGS-B, SLSQP中的一种,可选TNC

jac:该参数就是计算梯度的函数,和fun参数类似,第一个必须为theta且其shape必须为(n,)即一维数组,最后返回的梯度也必须为一个一维数组
options:用来控制最大的迭代次数,以字典的形式来进行设置,例如:options={‘maxiter’:400}

用到的参数主要是这几个,应用的例子如下(吴恩达机器学习ex2用到的地方):

def costFunction(theta,X,Y,lmd):theta = theta.reshape((len(theta), 1))Y=Y.reshape(len(Y),1)m=X.shape[0]J=0first=-(Y.T)@np.log(sigmoid(X@theta))second=(1-Y.T)@np.log(1-sigmoid(X@theta))theta2=theta[1:,:]assert (theta2.shape==(theta.size-1,1))J=(first-second)/m+lmd/(2*m)*(theta2.T@theta2)return J
def gradient(theta,X,Y,lmd):theta = theta.reshape((len(theta), 1))Y = Y.reshape(len(Y), 1)reg = (lmd / len(X)) * thetareg[0] = 0grad=(X.T @ (sigmoid(X @ theta) - Y)) / len(X)return (grad+reg).ravel()result=op.minimize(fun=costFunction,x0=theta.reshape(28,),args=(X,Y,1),method='TNC',jac=gradient,options={
    'maxiter':400})
print(result)
final_theta = result.x
结果:
fun: array([[0.52900273]])jac: array([-2.15010332e-06,  6.79564299e-07, -3.48680372e-07,  8.76012223e-07,-4.07509002e-08, -9.33423708e-07, -5.14520466e-07,  1.71377727e-08,1.55330746e-08, -9.72472529e-07,  6.96683054e-08,  3.55303286e-08,-2.79411735e-07,  1.79649627e-07,  2.33480997e-07,  1.47186558e-07,-2.11705227e-07,  6.16713286e-07, -9.29181534e-08, -5.27541662e-08,-1.48146987e-06,  2.31241473e-07,  1.80347588e-07, -1.31898660e-07,-7.16759904e-08, -4.12328300e-07,  1.65360544e-08, -7.34643861e-07])message: 'Converged (|f_n-f_(n-1)| ~= 0)'nfev: 32nit: 7status: 1success: Truex: array([ 1.27271027,  0.62529965,  1.18111686, -2.019874  , -0.91743189,-1.4316693 ,  0.12393227, -0.36553118, -0.35725403, -0.17516292,-1.45817009, -0.05098418, -0.61558553, -0.27469166, -1.19271298,-0.2421784 , -0.20603297, -0.04466178, -0.27778952, -0.29539513,-0.45645982, -1.04319155,  0.02779373, -0.29244871,  0.0155576 ,-0.32742406, -0.1438915 , -0.92467487])

注意:
1.theta一定为一个一维数组形式传入到costFunction和gradient函数中,同时X0参数为初始化的theta,必须为一维数组,gradient返回值必须为一维数组即梯度以一维数组的形式保存。
2.数组使用的时候注意shape,要学会合理使用reshape确保操作无误。
3.对于一维数组a=[0,1,2,3],a[0].shape=(),a[0].size=1,对于整型以及float型的变量无size和shape属性,size属性表示的是元素的个数。

3.minimize求解约束函数最小值

fun: 求最小值的目标函数

x0:变量的初始猜测值,如果有多个变量,需要给每个变量一个初始猜测值。minimize会出现局部最优的情况,所以这块的处理方法需要寻找。

args:常数值,后面例子会讲解,fun中没有数字,都以变量的形式表示,对于常数项,需要在这里给值

method:求极值的方法,官方文档给了很多种。一般使用默认。每种方法我理解是计算误差,反向传播的方式不同而已,这块有很大理论研究空间

constraints:约束条件,针对fun中为参数的部分进行约束限制

1.计算 1/x+x 的最小值# coding=utf-8
from scipy.optimize import minimize
import numpy as np#demo 1
#计算 1/x+x 的最小值def fun(args):a=argsv=lambda x:a/x[0] +x[0]return vif __name__ == "__main__":args = (1)  #ax0 = np.asarray((2))  # 初始猜测值res = minimize(fun(args), x0, method='SLSQP')print(res.fun)print(res.success)print(res.x)

执行结果:函数的最小值为2点多
在这里插入图片描述
该块参考链接:https://blog.csdn.net/ljyljyok/article/details/100552618

4.minimize函数官方说明

scipy api:https://docs.scipy.org/doc/scipy-0.18.1/reference/index.html

""" Unified interfaces to minimization algorithms.Functions --------- - minimize : minimization of a function of several variables. - minimize_scalar : minimization of a function of one variable. """
from __future__ import division, print_function, absolute_import__all__ = ['minimize', 'minimize_scalar']from warnings import warnimport numpy as npfrom scipy._lib.six import callablefrom scipy.sparse.linalg import LinearOperator# unconstrained minimization
from .optimize import (_minimize_neldermead, _minimize_powell, _minimize_cg,_minimize_bfgs, _minimize_newtoncg,_minimize_scalar_brent, _minimize_scalar_bounded,_minimize_scalar_golden, MemoizeJac)
from ._trustregion_dogleg import _minimize_dogleg
from ._trustregion_ncg import _minimize_trust_ncg
from ._trustregion_krylov import _minimize_trust_krylov
from ._trustregion_exact import _minimize_trustregion_exact
from ._trustregion_constr import _minimize_trustregion_constr
from ._constraints import Bounds, new_bounds_to_old, old_bound_to_new# constrained minimization
from .lbfgsb import _minimize_lbfgsb
from .tnc import _minimize_tnc
from .cobyla import _minimize_cobyla
from .slsqp import _minimize_slsqpdef minimize(fun, x0, args=(), method=None, jac=None, hess=None,hessp=None, bounds=None, constraints=(), tol=None,callback=None, options=None):"""Minimization of scalar function of one or more variables.Parameters----------fun : callableThe objective function to be minimized.``fun(x, *args) -> float``where x is an 1-D array with shape (n,) and `args`is a tuple of the fixed parameters needed to completelyspecify the function.x0 : ndarray, shape (n,)Initial guess. Array of real elements of size (n,),where 'n' is the number of independent variables.args : tuple, optionalExtra arguments passed to the objective function and itsderivatives (`fun`, `jac` and `hess` functions).method : str or callable, optionalType of solver. Should be one of- 'Nelder-Mead' :ref:`(see here) <optimize.minimize-neldermead>`- 'Powell' :ref:`(see here) <optimize.minimize-powell>`- 'CG' :ref:`(see here) <optimize.minimize-cg>`- 'BFGS' :ref:`(see here) <optimize.minimize-bfgs>`- 'Newton-CG' :ref:`(see here) <optimize.minimize-newtoncg>`- 'L-BFGS-B' :ref:`(see here) <optimize.minimize-lbfgsb>`- 'TNC' :ref:`(see here) <optimize.minimize-tnc>`- 'COBYLA' :ref:`(see here) <optimize.minimize-cobyla>`- 'SLSQP' :ref:`(see here) <optimize.minimize-slsqp>`- 'trust-constr':ref:`(see here) <optimize.minimize-trustconstr>`- 'dogleg' :ref:`(see here) <optimize.minimize-dogleg>`- 'trust-ncg' :ref:`(see here) <optimize.minimize-trustncg>`- 'trust-exact' :ref:`(see here) <optimize.minimize-trustexact>`- 'trust-krylov' :ref:`(see here) <optimize.minimize-trustkrylov>`- custom - a callable object (added in version 0.14.0),see below for description.If not given, chosen to be one of ``BFGS``, ``L-BFGS-B``, ``SLSQP``,depending if the problem has constraints or bounds.jac : {callable, '2-point', '3-point', 'cs', bool}, optionalMethod for computing the gradient vector. Only for CG, BFGS,Newton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg, trust-krylov,trust-exact and trust-constr. If it is a callable, it should be afunction that returns the gradient vector:``jac(x, *args) -> array_like, shape (n,)``where x is an array with shape (n,) and `args` is a tuple withthe fixed parameters. Alternatively, the keywords{'2-point', '3-point', 'cs'} select a finitedifference scheme for numerical estimation of the gradient. Options'3-point' and 'cs' are available only to 'trust-constr'.If `jac` is a Boolean and is True, `fun` is assumed to return thegradient along with the objective function. If False, the gradientwill be estimated using '2-point' finite difference estimation.hess : {callable, '2-point', '3-point', 'cs', HessianUpdateStrategy}, optionalMethod for computing the Hessian matrix. Only for Newton-CG, dogleg,trust-ncg, trust-krylov, trust-exact and trust-constr. If it iscallable, it should return the Hessian matrix:``hess(x, *args) -> {LinearOperator, spmatrix, array}, (n, n)``where x is a (n,) ndarray and `args` is a tuple with the fixedparameters. LinearOperator and sparse matrix returns areallowed only for 'trust-constr' method. Alternatively, the keywords{'2-point', '3-point', 'cs'} select a finite difference schemefor numerical estimation. Or, objects implementing`HessianUpdateStrategy` interface can be used to approximatethe Hessian. Available quasi-Newton methods implementingthis interface are:- `BFGS`;- `SR1`.Whenever the gradient is estimated via finite-differences,the Hessian cannot be estimated with options{'2-point', '3-point', 'cs'} and needs to beestimated using one of the quasi-Newton strategies.Finite-difference options {'2-point', '3-point', 'cs'} and`HessianUpdateStrategy` are available only for 'trust-constr' method.hessp : callable, optionalHessian of objective function times an arbitrary vector p. Only forNewton-CG, trust-ncg, trust-krylov, trust-constr.Only one of `hessp` or `hess` needs to be given. If `hess` isprovided, then `hessp` will be ignored. `hessp` must compute theHessian times an arbitrary vector:``hessp(x, p, *args) -> ndarray shape (n,)``where x is a (n,) ndarray, p is an arbitrary vector withdimension (n,) and `args` is a tuple with the fixedparameters.bounds : sequence or `Bounds`, optionalBounds on variables for L-BFGS-B, TNC, SLSQP andtrust-constr methods. There are two ways to specify the bounds:1. Instance of `Bounds` class.2. Sequence of ``(min, max)`` pairs for each element in `x`. Noneis used to specify no bound.constraints : {Constraint, dict} or List of {Constraint, dict}, optionalConstraints definition (only for COBYLA, SLSQP and trust-constr).Constraints for 'trust-constr' are defined as a single object or alist of objects specifying constraints to the optimization problem.Available constraints are:- `LinearConstraint`- `NonlinearConstraint`Constraints for COBYLA, SLSQP are defined as a list of dictionaries.Each dictionary with fields:type : strConstraint type: 'eq' for equality, 'ineq' for inequality.fun : callableThe function defining the constraint.jac : callable, optionalThe Jacobian of `fun` (only for SLSQP).args : sequence, optionalExtra arguments to be passed to the function and Jacobian.Equality constraint means that the constraint function result is tobe zero whereas inequality means that it is to be non-negative.Note that COBYLA only supports inequality constraints.tol : float, optionalTolerance for termination. For detailed control, use solver-specificoptions.options : dict, optionalA dictionary of solver options. All methods accept the followinggeneric options:maxiter : intMaximum number of iterations to perform.disp : boolSet to True to print convergence messages.For method-specific options, see :func:`show_options()`.callback : callable, optionalCalled after each iteration. For 'trust-constr' it is a callable withthe signature:``callback(xk, OptimizeResult state) -> bool``where ``xk`` is the current parameter vector. and ``state``is an `OptimizeResult` object, with the same fieldsas the ones from the return. If callback returns Truethe algorithm execution is terminated.For all the other methods, the signature is:``callback(xk)``where ``xk`` is the current parameter vector.Returns-------res : OptimizeResultThe optimization result represented as a ``OptimizeResult`` object.Important attributes are: ``x`` the solution array, ``success`` aBoolean flag indicating if the optimizer exited successfully and``message`` which describes the cause of the termination. See`OptimizeResult` for a description of other attributes.See also--------minimize_scalar : Interface to minimization algorithms for scalarunivariate functionsshow_options : Additional options accepted by the solversNotes-----This section describes the available solvers that can be selected by the'method' parameter. The default method is *BFGS*.**Unconstrained minimization**Method :ref:`Nelder-Mead <optimize.minimize-neldermead>` uses theSimplex algorithm [1]_, [2]_. This algorithm is robust in manyapplications. However, if numerical computation of derivative can betrusted, other algorithms using the first and/or second derivativesinformation might be preferred for their better performance ingeneral.Method :ref:`Powell <optimize.minimize-powell>` is a modificationof Powell's method [3]_, [4]_ which is a conjugate directionmethod. It performs sequential one-dimensional minimizations alongeach vector of the directions set (`direc` field in `options` and`info`), which is updated at each iteration of the mainminimization loop. The function need not be differentiable, and noderivatives are taken.Method :ref:`CG <optimize.minimize-cg>` uses a nonlinear conjugategradient algorithm by Polak and Ribiere, a variant of theFletcher-Reeves method described in [5]_ pp. 120-122. Only thefirst derivatives are used.Method :ref:`BFGS <optimize.minimize-bfgs>` uses the quasi-Newtonmethod of Broyden, Fletcher, Goldfarb, and Shanno (BFGS) [5]_pp. 136. It uses the first derivatives only. BFGS has proven goodperformance even for non-smooth optimizations. This method alsoreturns an approximation of the Hessian inverse, stored as`hess_inv` in the OptimizeResult object.Method :ref:`Newton-CG <optimize.minimize-newtoncg>` uses aNewton-CG algorithm [5]_ pp. 168 (also known as the truncatedNewton method). It uses a CG method to the compute the searchdirection. See also *TNC* method for a box-constrainedminimization with a similar algorithm. Suitable for large-scaleproblems.Method :ref:`dogleg <optimize.minimize-dogleg>` uses the dog-legtrust-region algorithm [5]_ for unconstrained minimization. Thisalgorithm requires the gradient and Hessian; furthermore theHessian is required to be positive definite.Method :ref:`trust-ncg <optimize.minimize-trustncg>` uses theNewton conjugate gradient trust-region algorithm [5]_ forunconstrained minimization. This algorithm requires the gradientand either the Hessian or a function that computes the product ofthe Hessian with a given vector. Suitable for large-scale problems.Method :ref:`trust-krylov <optimize.minimize-trustkrylov>` usesthe Newton GLTR trust-region algorithm [14]_, [15]_ for unconstrainedminimization. This algorithm requires the gradientand either the Hessian or a function that computes the product ofthe Hessian with a given vector. Suitable for large-scale problems.On indefinite problems it requires usually less iterations than the`trust-ncg` method and is recommended for medium and large-scale problems.Method :ref:`trust-exact <optimize.minimize-trustexact>`is a trust-region method for unconstrained minimization in whichquadratic subproblems are solved almost exactly [13]_. Thisalgorithm requires the gradient and the Hessian (which is*not* required to be positive definite). It is, in manysituations, the Newton method to converge in fewer iteractionand the most recommended for small and medium-size problems.**Bound-Constrained minimization**Method :ref:`L-BFGS-B <optimize.minimize-lbfgsb>` uses the L-BFGS-Balgorithm [6]_, [7]_ for bound constrained minimization.Method :ref:`TNC <optimize.minimize-tnc>` uses a truncated Newtonalgorithm [5]_, [8]_ to minimize a function with variables subjectto bounds. This algorithm uses gradient information; it is alsocalled Newton Conjugate-Gradient. It differs from the *Newton-CG*method described above as it wraps a C implementation and allowseach variable to be given upper and lower bounds.**Constrained Minimization**Method :ref:`COBYLA <optimize.minimize-cobyla>` uses theConstrained Optimization BY Linear Approximation (COBYLA) method[9]_, [10]_, [11]_. The algorithm is based on linearapproximations to the objective function and each constraint. Themethod wraps a FORTRAN implementation of the algorithm. Theconstraints functions 'fun' may return either a single numberor an array or list of numbers.Method :ref:`SLSQP <optimize.minimize-slsqp>` uses SequentialLeast SQuares Programming to minimize a function of severalvariables with any combination of bounds, equality and inequalityconstraints. The method wraps the SLSQP Optimization subroutineoriginally implemented by Dieter Kraft [12]_. Note that thewrapper handles infinite values in bounds by converting them intolarge floating values.Method :ref:`trust-constr <optimize.minimize-trustconstr>` is atrust-region algorithm for constrained optimization. It swichesbetween two implementations depending on the problem definition.It is the most versatile constrained minimization algorithmimplemented in SciPy and the most appropriate for large-scale problems.For equality constrained problems it is an implementation of Byrd-OmojokunTrust-Region SQP method described in [17]_ and in [5]_, p. 549. Wheninequality constraints are imposed as well, it swiches to the trust-regioninterior point method described in [16]_. This interior point algorithm,in turn, solves inequality constraints by introducing slack variablesand solving a sequence of equality-constrained barrier problemsfor progressively smaller values of the barrier parameter.The previously described equality constrained SQP method isused to solve the subproblems with increasing levels of accuracyas the iterate gets closer to a solution.**Finite-Difference Options**For Method :ref:`trust-constr <optimize.minimize-trustconstr>`the gradient and the Hessian may be approximated usingthree finite-difference schemes: {'2-point', '3-point', 'cs'}.The scheme 'cs' is, potentially, the most accurate but itrequires the function to correctly handles complex inputs and tobe differentiable in the complex plane. The scheme '3-point' is moreaccurate than '2-point' but requires twice as much operations.**Custom minimizers**It may be useful to pass a custom minimization method, for examplewhen using a frontend to this method such as `scipy.optimize.basinhopping`or a different library. You can simply pass a callable as the ``method``parameter.The callable is called as ``method(fun, x0, args, **kwargs, **options)``where ``kwargs`` corresponds to any other parameters passed to `minimize`(such as `callback`, `hess`, etc.), except the `options` dict, which hasits contents also passed as `method` parameters pair by pair. Also, if`jac` has been passed as a bool type, `jac` and `fun` are mangled so that`fun` returns just the function values and `jac` is converted to a functionreturning the Jacobian. The method shall return an ``OptimizeResult``object.The provided `method` callable must be able to accept (and possibly ignore)arbitrary parameters; the set of parameters accepted by `minimize` mayexpand in future versions and then these parameters will be passed tothe method. You can find an example in the scipy.optimize tutorial... versionadded:: 0.11.0References----------.. [1] Nelder, J A, and R Mead. 1965. A Simplex Method for FunctionMinimization. The Computer Journal 7: 308-13... [2] Wright M H. 1996. Direct search methods: Once scorned, nowrespectable, in Numerical Analysis 1995: Proceedings of the 1995Dundee Biennial Conference in Numerical Analysis (Eds. D FGriffiths and G A Watson). Addison Wesley Longman, Harlow, UK.191-208... [3] Powell, M J D. 1964. An efficient method for finding the minimum ofa function of several variables without calculating derivatives. TheComputer Journal 7: 155-162... [4] Press W, S A Teukolsky, W T Vetterling and B P Flannery.Numerical Recipes (any edition), Cambridge University Press... [5] Nocedal, J, and S J Wright. 2006. Numerical Optimization.Springer New York... [6] Byrd, R H and P Lu and J. Nocedal. 1995. A Limited MemoryAlgorithm for Bound Constrained Optimization. SIAM Journal onScientific and Statistical Computing 16 (5): 1190-1208... [7] Zhu, C and R H Byrd and J Nocedal. 1997. L-BFGS-B: Algorithm778: L-BFGS-B, FORTRAN routines for large scale bound constrainedoptimization. ACM Transactions on Mathematical Software 23 (4):550-560... [8] Nash, S G. Newton-Type Minimization Via the Lanczos Method.1984. SIAM Journal of Numerical Analysis 21: 770-778... [9] Powell, M J D. A direct search optimization method that modelsthe objective and constraint functions by linear interpolation.1994. Advances in Optimization and Numerical Analysis, eds. S. Gomezand J-P Hennart, Kluwer Academic (Dordrecht), 51-67... [10] Powell M J D. Direct search algorithms for optimizationcalculations. 1998. Acta Numerica 7: 287-336... [11] Powell M J D. A view of algorithms for optimization withoutderivatives. 2007.Cambridge University Technical Report DAMTP2007/NA03.. [12] Kraft, D. A software package for sequential quadraticprogramming. 1988. Tech. Rep. DFVLR-FB 88-28, DLR German AerospaceCenter -- Institute for Flight Mechanics, Koln, Germany... [13] Conn, A. R., Gould, N. I., and Toint, P. L.Trust region methods. 2000. Siam. pp. 169-200... [14] F. Lenders, C. Kirches, A. Potschka: "trlib: A vector-freeimplementation of the GLTR method for iterative solution ofthe trust region problem", https://arxiv.org/abs/1611.04718.. [15] N. Gould, S. Lucidi, M. Roma, P. Toint: "Solving theTrust-Region Subproblem using the Lanczos Method",SIAM J. Optim., 9(2), 504--525, (1999)... [16] Byrd, Richard H., Mary E. Hribar, and Jorge Nocedal. 1999.An interior point algorithm for large-scale nonlinear programming.SIAM Journal on Optimization 9.4: 877-900... [17] Lalee, Marucha, Jorge Nocedal, and Todd Plantega. 1998. On theimplementation of an algorithm for large-scale equality constrainedoptimization. SIAM Journal on Optimization 8.3: 682-706.Examples--------Let us consider the problem of minimizing the Rosenbrock function. Thisfunction (and its respective derivatives) is implemented in `rosen`(resp. `rosen_der`, `rosen_hess`) in the `scipy.optimize`.>>> from scipy.optimize import minimize, rosen, rosen_derA simple application of the *Nelder-Mead* method is:>>> x0 = [1.3, 0.7, 0.8, 1.9, 1.2]>>> res = minimize(rosen, x0, method='Nelder-Mead', tol=1e-6)>>> res.xarray([ 1., 1., 1., 1., 1.])Now using the *BFGS* algorithm, using the first derivative and a fewoptions:>>> res = minimize(rosen, x0, method='BFGS', jac=rosen_der,... options={'gtol': 1e-6, 'disp': True})Optimization terminated successfully.Current function value: 0.000000Iterations: 26Function evaluations: 31Gradient evaluations: 31>>> res.xarray([ 1., 1., 1., 1., 1.])>>> print(res.message)Optimization terminated successfully.>>> res.hess_invarray([[ 0.00749589, 0.01255155, 0.02396251, 0.04750988, 0.09495377], # may vary[ 0.01255155, 0.02510441, 0.04794055, 0.09502834, 0.18996269],[ 0.02396251, 0.04794055, 0.09631614, 0.19092151, 0.38165151],[ 0.04750988, 0.09502834, 0.19092151, 0.38341252, 0.7664427 ],[ 0.09495377, 0.18996269, 0.38165151, 0.7664427, 1.53713523]])Next, consider a minimization problem with several constraints (namelyExample 16.4 from [5]_). The objective function is:>>> fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2There are three constraints defined as:>>> cons = ({'type': 'ineq', 'fun': lambda x: x[0] - 2 * x[1] + 2},... {'type': 'ineq', 'fun': lambda x: -x[0] - 2 * x[1] + 6},... {'type': 'ineq', 'fun': lambda x: -x[0] + 2 * x[1] + 2})And variables must be positive, hence the following bounds:>>> bnds = ((0, None), (0, None))The optimization problem is solved using the SLSQP method as:>>> res = minimize(fun, (2, 0), method='SLSQP', bounds=bnds,... constraints=cons)It should converge to the theoretical solution (1.4 ,1.7)."""x0 = np.asarray(x0)if x0.dtype.kind in np.typecodes["AllInteger"]:x0 = np.asarray(x0, dtype=float)if not isinstance(args, tuple):args = (args,)if method is None:# Select automaticallyif constraints:method = 'SLSQP'elif bounds is not None:method = 'L-BFGS-B'else:method = 'BFGS'if callable(method):meth = "_custom"else:meth = method.lower()if options is None:options = {
    }# check if optional parameters are supported by the selected method# - jacif meth in ('nelder-mead', 'powell', 'cobyla') and bool(jac):warn('Method %s does not use gradient information (jac).' % method,RuntimeWarning)# - hessif meth not in ('newton-cg', 'dogleg', 'trust-ncg', 'trust-constr','trust-krylov', 'trust-exact', '_custom') and hess is not None:warn('Method %s does not use Hessian information (hess).' % method,RuntimeWarning)# - hesspif meth not in ('newton-cg', 'dogleg', 'trust-ncg', 'trust-constr','trust-krylov', '_custom') \and hessp is not None:warn('Method %s does not use Hessian-vector product ''information (hessp).' % method, RuntimeWarning)# - constraints or boundsif (meth in ('nelder-mead', 'powell', 'cg', 'bfgs', 'newton-cg', 'dogleg','trust-ncg') and (bounds is not None or np.any(constraints))):warn('Method %s cannot handle constraints nor bounds.' % method,RuntimeWarning)if meth in ('l-bfgs-b', 'tnc') and np.any(constraints):warn('Method %s cannot handle constraints.' % method,RuntimeWarning)if meth == 'cobyla' and bounds is not None:warn('Method %s cannot handle bounds.' % method,RuntimeWarning)# - callbackif (meth in ('cobyla',) and callback is not None):warn('Method %s does not support callback.' % method, RuntimeWarning)# - return_allif (meth in ('l-bfgs-b', 'tnc', 'cobyla', 'slsqp') andoptions.get('return_all', False)):warn('Method %s does not support the return_all option.' % method,RuntimeWarning)# check gradient vectorif meth == 'trust-constr':if type(jac) is bool:if jac:fun = MemoizeJac(fun)jac = fun.derivativeelse:jac = '2-point'elif not callable(jac) and jac not in ('2-point', '3-point', 'cs'):raise ValueError("Unsupported jac definition.")else:if jac in ('2-point', '3-point', 'cs'):if jac in ('3-point', 'cs'):warn("Only 'trust-constr' method accept %s ""options for 'jac'. Using '2-point' instead." % jac)jac = Noneelif not callable(jac):if bool(jac):fun = MemoizeJac(fun)jac = fun.derivativeelse:jac = None# set default tolerancesif tol is not None:options = dict(options)if meth == 'nelder-mead':options.setdefault('xatol', tol)options.setdefault('fatol', tol)if meth in ('newton-cg', 'powell', 'tnc'):options.setdefault('xtol', tol)if meth in ('powell', 'l-bfgs-b', 'tnc', 'slsqp'):options.setdefault('ftol', tol)if meth in ('bfgs', 'cg', 'l-bfgs-b', 'tnc', 'dogleg','trust-ncg', 'trust-exact', 'trust-krylov'):options.setdefault('gtol', tol)if meth in ('cobyla', '_custom'):options.setdefault('tol', tol)if meth == 'trust-constr':options.setdefault('xtol', tol)options.setdefault('gtol', tol)options.setdefault('barrier_tol', tol)if bounds is not None:if meth == 'trust-constr':if not isinstance(bounds, Bounds):lb, ub = old_bound_to_new(bounds)bounds = Bounds(lb, ub)elif meth in ('l-bfgs-b', 'tnc', 'slsqp'):if isinstance(bounds, Bounds):bounds = new_bounds_to_old(bounds.lb, bounds.ub, x0.shape[0])if meth == '_custom':return method(fun, x0, args=args, jac=jac, hess=hess, hessp=hessp,bounds=bounds, constraints=constraints,callback=callback, **options)elif meth == 'nelder-mead':return _minimize_neldermead(fun, x0, args, callback, **options)elif meth == 'powell':return _minimize_powell(fun, x0, args, callback, **options)elif meth == 'cg':return _minimize_cg(fun, x0, args, jac, callback, **options)elif meth == 'bfgs':return _minimize_bfgs(fun, x0, args, jac, callback, **options)elif meth == 'newton-cg':return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback,**options)elif meth == 'l-bfgs-b':return _minimize_lbfgsb(fun, x0, args, jac, bounds,callback=callback, **options)elif meth == 'tnc':return _minimize_tnc(fun, x0, args, jac, bounds, callback=callback,**options)elif meth == 'cobyla':return _minimize_cobyla(fun, x0, args, constraints, **options)elif meth == 'slsqp':return _minimize_slsqp(fun, x0, args, jac, bounds,constraints, callback=callback, **options)elif meth == 'trust-constr':return _minimize_trustregion_constr(fun, x0, args, jac, hess, hessp,bounds, constraints,callback=callback, **options)elif meth == 'dogleg':return _minimize_dogleg(fun, x0, args, jac, hess,callback=callback, **options)elif meth == 'trust-ncg':return _minimize_trust_ncg(fun, x0, args, jac, hess, hessp,callback=callback, **options)elif meth == 'trust-krylov':return _minimize_trust_krylov(fun, x0, args, jac, hess, hessp,callback=callback, **options)elif meth == 'trust-exact':return _minimize_trustregion_exact(fun, x0, args, jac, hess,callback=callback, **options)else:raise ValueError('Unknown solver %s' % method)def minimize_scalar(fun, bracket=None, bounds=None, args=(),method='brent', tol=None, options=None):"""Minimization of scalar function of one variable.Parameters----------fun : callableObjective function.Scalar function, must return a scalar.bracket : sequence, optionalFor methods 'brent' and 'golden', `bracket` defines the bracketinginterval and can either have three items ``(a, b, c)`` so that``a < b < c`` and ``fun(b) < fun(a), fun(c)`` or two items ``a`` and``c`` which are assumed to be a starting interval for a downhillbracket search (see `bracket`); it doesn't always mean that theobtained solution will satisfy ``a <= x <= c``.bounds : sequence, optionalFor method 'bounded', `bounds` is mandatory and must have two itemscorresponding to the optimization bounds.args : tuple, optionalExtra arguments passed to the objective function.method : str or callable, optionalType of solver. Should be one of:- 'Brent' :ref:`(see here) <optimize.minimize_scalar-brent>`- 'Bounded' :ref:`(see here) <optimize.minimize_scalar-bounded>`- 'Golden' :ref:`(see here) <optimize.minimize_scalar-golden>`- custom - a callable object (added in version 0.14.0), see belowtol : float, optionalTolerance for termination. For detailed control, use solver-specificoptions.options : dict, optionalA dictionary of solver options.maxiter : intMaximum number of iterations to perform.disp : boolSet to True to print convergence messages.See :func:`show_options()` for solver-specific options.Returns-------res : OptimizeResultThe optimization result represented as a ``OptimizeResult`` object.Important attributes are: ``x`` the solution array, ``success`` aBoolean flag indicating if the optimizer exited successfully and``message`` which describes the cause of the termination. See`OptimizeResult` for a description of other attributes.See also--------minimize : Interface to minimization algorithms for scalar multivariatefunctionsshow_options : Additional options accepted by the solversNotes-----This section describes the available solvers that can be selected by the'method' parameter. The default method is *Brent*.Method :ref:`Brent <optimize.minimize_scalar-brent>` uses Brent'salgorithm to find a local minimum. The algorithm uses inverseparabolic interpolation when possible to speed up convergence ofthe golden section method.Method :ref:`Golden <optimize.minimize_scalar-golden>` uses thegolden section search technique. It uses analog of the bisectionmethod to decrease the bracketed interval. It is usuallypreferable to use the *Brent* method.Method :ref:`Bounded <optimize.minimize_scalar-bounded>` canperform bounded minimization. It uses the Brent method to find alocal minimum in the interval x1 < xopt < x2.**Custom minimizers**It may be useful to pass a custom minimization method, for examplewhen using some library frontend to minimize_scalar. You can simplypass a callable as the ``method`` parameter.The callable is called as ``method(fun, args, **kwargs, **options)``where ``kwargs`` corresponds to any other parameters passed to `minimize`(such as `bracket`, `tol`, etc.), except the `options` dict, which hasits contents also passed as `method` parameters pair by pair. The methodshall return an ``OptimizeResult`` object.The provided `method` callable must be able to accept (and possibly ignore)arbitrary parameters; the set of parameters accepted by `minimize` mayexpand in future versions and then these parameters will be passed tothe method. You can find an example in the scipy.optimize tutorial... versionadded:: 0.11.0Examples--------Consider the problem of minimizing the following function.>>> def f(x):... return (x - 2) * x * (x + 2)**2Using the *Brent* method, we find the local minimum as:>>> from scipy.optimize import minimize_scalar>>> res = minimize_scalar(f)>>> res.x1.28077640403Using the *Bounded* method, we find a local minimum with specifiedbounds as:>>> res = minimize_scalar(f, bounds=(-3, -1), method='bounded')>>> res.x-2.0000002026"""if not isinstance(args, tuple):args = (args,)if callable(method):meth = "_custom"else:meth = method.lower()if options is None:options = {
    }if tol is not None:options = dict(options)if meth == 'bounded' and 'xatol' not in options:warn("Method 'bounded' does not support relative tolerance in x; ""defaulting to absolute tolerance.", RuntimeWarning)options['xatol'] = tolelif meth == '_custom':options.setdefault('tol', tol)else:options.setdefault('xtol', tol)if meth == '_custom':return method(fun, args=args, bracket=bracket, bounds=bounds, **options)elif meth == 'brent':return _minimize_scalar_brent(fun, bracket, args, **options)elif meth == 'bounded':if bounds is None:raise ValueError('The `bounds` parameter is mandatory for ''method `bounded`.')# replace boolean "disp" option, if specified, by an integer value, as# expected by _minimize_scalar_bounded()disp = options.get('disp')if isinstance(disp, bool):options['disp'] = 2 * int(disp)return _minimize_scalar_bounded(fun, bounds, args, **options)elif meth == 'golden':return _minimize_scalar_golden(fun, bracket, args, **options)else:raise ValueError('Unknown solver %s' % method)
  相关解决方案