Overload List
Overload 1: function ConjGrad(Fun: TRealFunction; Grad: TGrad; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; const FloatPrecision: TMtxFloatPrecision; FletcherAlgo: Boolean): Integer;
Minimizes the function of several variables by using the Conjugate gradient optimization method with no log.
| # | Name | Type | Description |
|---|---|---|---|
| 1 | Fun | TRealFunction | |
| 2 | Grad | TGrad | |
| 3 | Pars | TDoubleArray | |
| 4 | Consts | TDoubleArray | |
| 5 | ObjConst | TObjectArray | |
| 6 | FMin | Double | |
| 7 | FloatPrecision | TMtxFloatPrecision | |
| 8 | FletcherAlgo | Boolean |
Returns: Int32
Overload 2: function ConjGrad(Fun: TRealFunction; Grad: TGrad; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; out StopReason: TOptStopReason; const FloatPrecision: TMtxFloatPrecision; FletcherAlgo: Boolean; SoftLineSearch: Boolean): Integer;
Minimizes the function of several variables by using the Conjugate gradient optimization method with no log.
| # | Name | Type | Description |
|---|---|---|---|
| 1 | Fun | TRealFunction | |
| 2 | Grad | TGrad | |
| 3 | Pars | TDoubleArray | |
| 4 | Consts | TDoubleArray | |
| 5 | ObjConst | TObjectArray | |
| 6 | FMin | Double | |
| 7 | StopReason | TOptStopReason | |
| 8 | FloatPrecision | TMtxFloatPrecision | |
| 9 | FletcherAlgo | Boolean | |
| 10 | SoftLineSearch | Boolean |
Returns: Int32
Overload 3: function ConjGrad(Fun: TRealFunction; Grad: TGrad; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; out StopReason: TOptStopReason; const FloatPrecision: TMtxFloatPrecision; FletcherAlgo: Boolean; SoftLineSearch: Boolean; MaxIter: Integer; Tol: Double; GradTol: Double): Integer;
Minimizes the function of several variables by using the Conjugate gradient optimization method with no log.
| # | Name | Type | Description |
|---|---|---|---|
| 1 | Fun | TRealFunction | |
| 2 | Grad | TGrad | |
| 3 | Pars | TDoubleArray | |
| 4 | Consts | TDoubleArray | |
| 5 | ObjConst | TObjectArray | |
| 6 | FMin | Double | |
| 7 | StopReason | TOptStopReason | |
| 8 | FloatPrecision | TMtxFloatPrecision | |
| 9 | FletcherAlgo | Boolean | |
| 10 | SoftLineSearch | Boolean | |
| 11 | MaxIter | Integer | |
| 12 | Tol | Double | scalar |
| 13 | GradTol | Double | scalar |
Returns: Int32
Overload 4: function ConjGrad(Fun: TRealFunction; Grad: TGrad; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; out StopReason: TOptStopReason; const FloatPrecision: TMtxFloatPrecision; FletcherAlgo: Boolean; SoftLineSearch: Boolean; MaxIter: Integer; Tol: Double; GradTol: Double; const Verbose: TStrings): Integer;
Minimizes the function of several variables by using the Conjugate gradient optimization algorithm.
| # | Name | Description |
|---|---|---|
| 1 | Fun | Real function (must be of TRealFunction type) to be minimized. |
| 2 | Grad | The gradient and Hessian procedure (must be of TGrad type), used for calculating the gradient. |
| 3 | Pars | Stores the initial estimates for parameters (minimum estimate). After the call to routine returns adjusted calculated values (minimum position). |
| 4 | Consts | Additional Fun constant parameteres (can be/is usually nil). |
| 5 | FloatPrecision | Specifies the floating point precision to be used by the routine. |
| 6 | ObjConst | Additional Fun constant parameteres (can be/is usually nil). |
| 7 | FMin | Returns function value at minimum. |
| 8 | StopReason | Returns reason why minimum search stopped (see TOptStopReason). |
| 9 | FletcherAlgo | If True, ConjGrad procedure will use Fletcher-Reeves method. If false, ConjGrad procedure will use Polak-Ribiere method. |
| 10 | SoftLineSearch | Selects the line-search acceptance rule used along each conjugate direction. It is a true/false parameter and does not turn the gradient on or off - both modes are gradient based (the line search evaluates the gradient at each trial step). True (soft / inexact, the default) accepts the first step satisfying the Armijo sufficient-decrease condition f(x+alpha d) <= f(x) + rho alpha grad f^T d and is the robust choice for a NUMERICAL (finite-difference) gradient |
| 11 | False (exact / Wolfe) additionally enforces the curvature condition |grad f(x+alpha d)^T d| <= beta |grad f^T d| and should be used only with an exact gradient. "Soft off" is still a gradient-based search, just a stricter one. See also the same parameter on [see | Dew.Math.Units.Optimization.BFGS]. |
| 12 | MaxIter | Maximum allowed numer of minimum search iterations. |
| 13 | Tol | Desired Pars - minimum position tolerance. |
| 14 | GradTol | Minimum allowed gradient C-Norm. |
| 15 | Verbose | If assigned, stores Fun, evaluated at each iteration step. Optionally, you can also pass TOptControl object to the Verbose parameter. This allows the optimization procedure to be interrupted from another thread and optionally also allows logging and iteration count monitoring. |
Returns: Int32 - the number of iterations required to reach the solution(minimum) within given tolerance.
What it computes. Minimizes a smooth real function of several
variables with the nonlinear conjugate-gradient method, generating search directions that are conjugate with respect to the local curvature (Fletcher-Reeves when FletcherAlgo=True, Polak-Ribiere when False) and a line search along each. The standard test objective is
f(x_0,x_1) = 100 (x_1 - x_0^2)^2 + (1 - x_0)^2
with the global minimum f=0 at and gradient
(d f)/(d x_0) = -400 (x_1 - x_0^2) x_0 - 2 (1 - x_0), (d f)/(d x_1) = 200 (x_1 - x_0^2)
Domain. Pars holds the n starting coordinates;
Grad returns the gradient (exact, or numerical with SoftLineSearch=True); MaxIter > 0; Tol,GradTol ≥ 0.
Defined behaviour. Returns the iteration count; Pars
holds the minimizer and FMin the minimum value. StopReason is an optimality cause (TOptStopReason.OptResConverged, TOptStopReason.OptResSmallGrad or
TOptStopReason.OptResSmallStep) or a non-convergence cause such as TOptStopReason.OptResMaxIter. A NaN from the objective propagates
into FMin.
Uses MtxVec, Math387, Optimization;
function Banana(const Pars: TVec; const Consts: TVec; const OConsts: array of TObject): double;
begin
Banana := 100*Sqr(Pars[1]-Sqr(Pars[0]))+Sqr(1-Pars[0]);
end;
procedure Example;
var Iters : integer;
Pars: array [0..1] of double;
StopReason: TOptStopReason;
begin
// initial estimates for x1 and x2
Pars[0] := 0;
Pars[1] := 0;
Iters := ConjGrad(Banana,NumericGradRichardson,Pars,[],[],FMin,StopReason,mvDouble,IHess);
//stop if Iters > 500 or Tolerance < 1e-8
// Returns Pars = [1,1] and FMin = 0, meaning x1=1, x2=1 and minimum value is 0
end;