bundles / scipy 1.17.1 / scipy / optimize / _optimize / fmin_cg
function
scipy.optimize._optimize:fmin_cg
Signature
def fmin_cg ( f , x0 , fprime = None , args = () , gtol = 1e-05 , norm = inf , epsilon = 1.4901161193847656e-08 , maxiter = None , full_output = 0 , disp = 1 , retall = 0 , callback = None , c1 = 0.0001 , c2 = 0.4 ) Summary
Minimize a function using a nonlinear conjugate gradient algorithm.
Parameters
f: callable, ``f(x, *args)``Objective function to be minimized. Here
xmust be a 1-D array of the variables that are to be changed in the search for a minimum, andargsare the other (fixed) parameters off.x0: ndarrayA user-supplied initial estimate of xopt, the optimal value of
x. It must be a 1-D array of values.fprime: callable, ``fprime(x, *args)``, optionalA function that returns the gradient of
fatx. Herexandargsare as described above forf. The returned value must be a 1-D array. Defaults to None, in which case the gradient is approximated numerically (seeepsilon, below).args: tuple, optionalParameter values passed to
fandfprime. Must be supplied whenever additional fixed parameters are needed to completely specify the functionsfandfprime.gtol: float, optionalStop when the norm of the gradient is less than
gtol.norm: float, optionalOrder to use for the norm of the gradient (
-np.infis min,np.infis max).epsilon: float or ndarray, optionalStep size(s) to use when
fprimeis approximated numerically. Can be a scalar or a 1-D array. Defaults tosqrt(eps), with eps the floating point machine precision. Usuallysqrt(eps)is about 1.5e-8.maxiter: int, optionalMaximum number of iterations to perform. Default is
200 * len(x0).full_output: bool, optionalIf True, return fopt, func_calls, grad_calls, and warnflag in addition to xopt. See the Returns section below for additional information on optional return values.
disp: bool, optionalIf True, return a convergence message, followed by xopt.
retall: bool, optionalIf True, add to the returned values the results of each iteration.
callback: callable, optionalAn optional user-supplied function, called after each iteration. Called as
callback(xk), wherexkis the current value ofx0.c1: float, default: 1e-4Parameter for Armijo condition rule.
c2: float, default: 0.4Parameter for curvature condition rule.
Returns
xopt: ndarrayParameters which minimize f, i.e.,
f(xopt) == fopt.fopt: float, optionalMinimum value found, f(xopt). Only returned if
full_outputis True.func_calls: int, optionalThe number of function_calls made. Only returned if
full_outputis True.grad_calls: int, optionalThe number of gradient calls made. Only returned if
full_outputis True.warnflag: int, optionalInteger value with warning status, only returned if
full_outputis True.0Success.
1The maximum number of iterations was exceeded.
2
2
3NaN result encountered.
allvecs: list of ndarray, optionalList of arrays, containing the results at each iteration. Only returned if
retallis True.
Notes
This conjugate gradient algorithm is based on that of Polak and Ribiere [1].
Conjugate gradient methods tend to work better when:
fhas a unique global minimizing point, and no local minima or other stationary points,fis, at least locally, reasonably well approximated by a quadratic function of the variables,fis continuous and has a continuous gradient,fprimeis not too large, e.g., has a norm less than 1000,The initial guess,
x0, is reasonably close tof's global minimizing point, xopt.
Parameters c1 and c2 must satisfy 0 < c1 < c2 < 1.
Examples
Example 1: seek the minimum value of the expression ``a*u**2 + b*u*v + c*v**2 + d*u + e*v + f`` for given values of the parameters and an initial guess ``(u, v) = (0, 0)``.import numpy as np args = (2, 3, 7, 8, 9, 10) # parameter values def f(x, *args): u, v = x a, b, c, d, e, f = args return a*u**2 + b*u*v + c*v**2 + d*u + e*v + f def gradf(x, *args): u, v = x a, b, c, d, e, f = args gu = 2*a*u + b*v + d # u-component of the gradient gv = b*u + 2*c*v + e # v-component of the gradient return np.asarray((gu, gv)) x0 = np.asarray((0, 0)) # Initial guess. from scipy import optimize res1 = optimize.fmin_cg(f, x0, fprime=gradf, args=args)✓
res1
✗opts = {'maxiter' : None, # default value. 'disp' : True, # non-default value. 'gtol' : 1e-5, # default value. 'norm' : np.inf, # default value. 'eps' : 1.4901161193847656e-08} # default value.✓
res2 = optimize.minimize(f, x0, jac=gradf, args=args, method='CG', options=opts) res2.x # minimum found✗
See also
- minimize
common interface to all
scipy.optimizealgorithms for unconstrained and constrained minimization of multivariate functions. It provides an alternative way to callfmin_cg, by specifyingmethod='CG'.
Aliases
-
scipy.optimize.fmin_cg