Overload List
Overload 1: function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; IHess: TMtx; out StopReason: TOptStopReason; const FloatPrecision: TMtxFloatPrecision; MaxIter: Integer; Tol: Double; GradTol: Double; Lambda0: Double): Integer;
Minimizes the function of several variables by using the Marquardt optimization method with no log.
| # | Name | Type | Description |
|---|---|---|---|
| 1 | Fun | TRealFunction | |
| 2 | GradHess | TGradHess | |
| 3 | Pars | TDoubleArray | |
| 4 | Consts | TDoubleArray | |
| 5 | ObjConst | TObjectArray | |
| 6 | FMin | Double | |
| 7 | IHess | TMtx | source TMtx |
| 8 | StopReason | TOptStopReason | |
| 9 | FloatPrecision | TMtxFloatPrecision | |
| 10 | MaxIter | Integer | |
| 11 | Tol | Double | scalar |
| 12 | GradTol | Double | scalar |
| 13 | Lambda0 | Double | scalar |
Returns: Int32
Overload 2: function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; IHess: TMtx; out StopReason: TOptStopReason; const FloatPrecision: TMtxFloatPrecision; MaxIter: Integer; Tol: Double; GradTol: Double; Lambda0: Double; Verbose: TStrings): Integer;
Minimizes the function of several variables by using the Marquardt optimization algorithm.
| # | Name | Description |
|---|---|---|
| 1 | Fun | Real function (must be of TRealFunction type) to be minimized. |
| 2 | GradHess | The gradient and Hessian procedure (must be of TGradHess type), used for calculating the gradient and Hessian matrix. |
| 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 | ObjConst | Additional Fun constant parameteres (can be/is usually nil). |
| 6 | FloatPrecision | Specifies the floating point precision to be used by the routine. |
| 7 | FMin | Returns function value at minimum. |
| 8 | IHess | Returns inverse Hessian matrix. |
| 9 | StopReason | Returns reason why minimum search stopped (see TOptStopReason). |
| 10 | MaxIter | Maximum allowed numer of minimum search iterations. |
| 11 | Tol | Desired Pars - minimum position tolerance. |
| 12 | GradTol | Minimum allowed gradient C-Norm. |
| 13 | Lambda0 | Initial lambda step, used in Marquardt algorithm. |
| 14 | 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 Levenberg-Marquardt method, solving a damped Newton step (H+lambda I) delta=-g and adapting the damping lambda (started from Lambda0) between gradient-descent and Newton behaviour. It needs both the gradient and the Hessian via GradHess. The standard test objective is
f(x_0,x_1) = 100 (x_1 - x_0^2)^2 + (1 - x_0)^2
with global minimum f=0 at , 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)
and Hessian
H(f) = [ 1200 x_0^2 - 400 x_1 + 2 -400 x_0 ]
[ -400 x_0 200 ]
Domain. Pars holds the n starting coordinates;
GradHess returns the gradient and (symmetric) Hessian; MaxIter > 0; Tol,GradTol ≥ 0; Lambda0 > 0.
Defined behaviour. Returns the iteration count; Pars
holds the minimizer, FMin the minimum and IHess the inverse Hessian. StopReason reports the cause - TOptStopReason.OptResConverged/TOptStopReason.OptResSmallGrad on success, TOptStopReason.optNANValue if the objective returned NaN, or TOptStopReason.OptResBigLambda/TOptStopReason.OptResSmallLambda/ TOptStopReason.OptResMaxIter otherwise.
Uses MtxVec, Math387, Optimization;
function Banana(const Pars: TVec; const Consts: TVec; const PConsts: array of TObject): double;
begin
Banana := 100*Sqr(Pars[1]-Sqr(Pars[0]))+Sqr(1-Pars[0]);
end;
procedure GradHessBanana(Fun: TRealFunction; const Pars: TVec; const Consts: TVec; const PConsts: array of TObject; const Grad: TVec; const Hess: TMtx);
begin
Grad[0] := -400*(Pars[1] - Sqr(Pars[0]))*Pars[0] - 2*(1 - Pars[0]);
Grad[1] := 200*(Pars[1] - Sqr(Pars[0]));
Hess[0,0] := -400*Pars[1] + 1200*Sqr(Pars[0])+2;
Hess[0,1] := -400*Pars[0];
Hess[1,0] := Hess.Values[0,1]; // symmetric !
Hess[1,1] := 200;
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 := Marquardt(Banana,GradHessBanana,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;
Overload 3: function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; const FloatPrecision: TMtxFloatPrecision): Integer;
Minimizes the function of several variables by using the Marquardt optimization method with no log.
| # | Name | Type | Description |
|---|---|---|---|
| 1 | Fun | TRealFunction | |
| 2 | GradHess | TGradHess | |
| 3 | Pars | TDoubleArray | |
| 4 | Consts | TDoubleArray | |
| 5 | ObjConst | TObjectArray | |
| 6 | FMin | Double | |
| 7 | FloatPrecision | TMtxFloatPrecision |
Returns: Int32
Overload 4: function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; out StopReason: TOptStopReason; const FloatPrecision: TMtxFloatPrecision): Integer;
Minimizes the function of several variables by using the Marquardt optimization method with no log.
| # | Name | Type | Description |
|---|---|---|---|
| 1 | Fun | TRealFunction | |
| 2 | GradHess | TGradHess | |
| 3 | Pars | TDoubleArray | |
| 4 | Consts | TDoubleArray | |
| 5 | ObjConst | TObjectArray | |
| 6 | FMin | Double | |
| 7 | StopReason | TOptStopReason | |
| 8 | FloatPrecision | TMtxFloatPrecision |
Returns: Int32