Dew MtxVec NET
Marquardt Routines
Summary
Minimizes the function of several variables by using the Marquardt optimization algorithm.

Unit
Optimization

Declaration
Function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: array of TSample; const Consts: array of TSample; const ObjConst: array of TObject; out FMin: TSample): Integer;


Declaration
Function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: array of TSample; const Consts: array of TSample; const ObjConst: array of TObject; out FMin: TSample; IHess: TMtx; out StopReason: TOptStopReason; MaxIter: Integer = 500; Tol: TSample = 1.0E-8; GradTol: TSample = 1.0E-8; Lambda0: TSample = 1e-2; Verbose: TStrings = nil): Integer;
 Parameter  Description 
Fun Real function (must be of TRealFunction type) to be minimized. 
GradHess The gradient and Hessian procedure (must be of TGradHess type), used for calculating the gradient and Hessian matrix. 
Pars Stores the initial estimates for parameters (minimum estimate). After the call to routine returns adjusted calculated values (minimum position). 
Consts,PConsts Additional Fun constant parameteres (can be/is usually nil). 
FMin Returns function value at minimum. 
IHess Returns inverse Hessian matrix. 
StopReason Returns reason why minimum search stopped (see TOptStopReason). 
MaxIter Maximum allowed numer of minimum search iterations. 
Tol Desired Pars - minimum position tolerance. 
GradTol Minimum allowed gradient C-Norm. 
Lambda0 Initial lambda step, used in Marquardt algorithm. 
Verbose If assigned, stores Fun, evaluated at each iteration step. 
Result
the number of iterations required to reach the solution(minimum) within given tolerance.
Description
Minimizes the function of several variables by using the Marquardt optimization algorithm.
Categories
Optimization routines
 See Also 
TGradHess 
TRealFunction 
NumericGradHess 

Example 1

Problem: Find the minimum of the "Banana" function by using the Marquardt method.
Solution:The Banana function is defined by the following equation:

Also, Marquardt method requires the gradient and Hessian matrix of the function. The gradient of the Banana function is:

and the Hessian matrix is :

Uses MtxVec, Math387, Optimization; function Banana(Pars: TVec; const Consts : Array of TSample; const PConsts: Array of TObject): TSample; begin Result := 100*Sqr(Pars[1]-Sqr(Pars[0]))+Sqr(1-Pars[0]); end; procedure GradHessBanana(Fun: TRealFunction; Pars: TVec; const consts: Array of TSample; Const PConsts: Array of TObject; Grad: TVec; Hess: TMtx); begin Grad.Values[0] := -400*(Pars[1]-Sqr(Pars[0]))*Pars[0]-2*(1-Pars[0]); Grad.Values[1] := 200*(Pars[1]-Sqr(Pars[0])); Hess.Values[0,0] := -400*Pars[1]+1200*Sqr(Pars[0])+2; Hess.Values[0,1] := -400*Pars[0]; Hess.Values[1,0] := Hess.Values[0,1]; // symmetric ! Hess.Values[1,1] := 200; end; procedure Example; var Iters : integer; Pars : Array [0..1] of TSample; StopReason : TOptStopReason; begin // initial estimates for x1 and x2 Pars[0] := 0; Pars[1] := 0; Iters := Marquardt(Banana,GradHessBanana,Pars,[],[],FMin,StopReason,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;
#include "MtxVecCpp.h" //MtxVecCPP.cpp must be included in the project #include "Math387.hpp" #include "Optimization.hpp" // Objective function double __fastcall Banana(TVec * Parameters, const double * Constants, const int Constants_Size, System::TObject* const * ObjConst, const int ObjConst_Size) { double* Pars = Parameters->PValues1D(0); return 100.0*IntPower(Pars[1]-IntPower(Pars[0],2),2)+IntPower(1.0-Pars[0],2); } // Analytical gradient and Hessian matrix of the objective function void __fastcall BananaGradHess(TRealFunction Fun, TVec * Paraneters, const double * Consts, const int Consts_Size, System::TObject* const * ObjConst, const int ObjConst_Size, TVec* Grad, TMtx* Hess) { double* Pars = Parameters->PValues1D(0); Grad->Values[0] = -400*(Pars[1]-IntPower(Pars[0],2))*Pars[0] - 2*(1-Pars[0]); Grad->Values[1] = 200*(Pars[1]-IntPower(Pars[0],2)); Hess->Values1D[0] = -400*Pars[1]+1200*IntPower(Pars[0],2)+2; Hess->Values1D[1] = -400*Pars[0]; Hess->Values1D[2] = -400*Pars[0]; Hess->Values1D[3] = 200; } void __fastcall Example; { double Pars[2]; double fmin; Matrix iHess; TOptStopReason StopReason; // initial estimates for x1 and x2 Pars[0] = 0; Pars[1] = 0; int iters = Marquardt(Banana,BananaGradHess,Pars,1,NULL,-1,NULL,-1,fmin, iHess, StopReason, 1000,1.0e-8,1.0e-3,NULL); // stop if Iters >1000 or Tolerance < 1e-8 }



Declaration
Function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: array of TSample; const Consts: array of TSample; const ObjConst: array of TObject; out FMin: TSample; IHess: TMtx; out StopReason: TOptStopReason; MaxIter: Integer; Tol: TSample; GradTol: TSample; Lambda0: TSample): Integer;


Declaration
Function Marquardt(Fun: TRealFunction; GradHess: TGradHess; var Pars: array of TSample; const Consts: array of TSample; const ObjConst: array of TObject; out FMin: TSample; out StopReason: TOptStopReason): Integer;

Copyright 2008 Dew Research
http://www.dewresearch.com