Dew MtxVec NET
TMtxOptimization Component
Summary
Interfaces the optimization routines.

Unit
MtxVecTools

Hierarchy
TMtxComponent <--TMtxOptimization

Subclasses
None

Description
The component can be used to find the minimum of function of several variables.

How to use TMtxOptimization component?
1) Drop a TMtxOptimization component on the form.
2) Define the number of variables and their initial values by accessing the VariableParameters vector.
3) Define any additional constant parameters by accessing ConstantParameters vector.
4) Define any additional constant pointer parameter by using the SetPointers method.
5) Define real function (must be of Optimization.TRealFunction type)
6) Define optimization method by accessing the OptimizationMethod property.
7) Depending on optimization method you'll have to (optionally) define the gradient calculation procedure (GradProcedure method) or gradient/Hessian matrix calculation procedure (GradHess method). If you don't specify the GradProcedure or GradHessProcedure then the numeric approximation will be used to calculate the gradient vector and Hessian matrix. In this case you must also specify which gradient aproximation method you will use - access the NumericGradMethod property.
8) Call the Recalculate method to find the minimum of function of several variables.

Results:
1) MinValue : RealFunction, evaluated at minimum.
2) VariableParameters : minimum position
3) Iterations: Number of iterations needed to reach specified (Tolerance property) minimum precision.
4) InverseHess : Inverse Hessian matrix (returned only by BFGS, ConjGrad and Marquardt methods)
5) StopReason: Why did the optimization algorithm stopped ?

Example 1

How to solve the optimization problem using TMtxOptimization component ?

Uses Math387, MtxVecTools; // define the real function to be minimized function BananaFunction(Pars: TVec; Consts: Array of TSample; PConsts: Array of TObject): TSample; begin Result := 100*Sqr(Pars[1]-Sqr(Pars[0]))+Sqr(1-Pars[0]); end; procedure Example(MtxOptim: TMtxOptimization); begin if Assigned(MtxOptim) then begin // define two variables and their initial values MtxOptim.VariableParameters.SetIt(false,[2,-1]); // use BFGS optimization method MtxOptim.OptimizationMethod := optBFGS; // tolerance for MinValue and gradient calculation // additional note : since we did not define the GradProc, // the internal numerical gradient approximation will be used MtxOptim.Tolerance := 2.0e-6; MtxOptim.GradTolerance := 2.0e-6; // function to be minimized MtxOptim.RealFunction := BananaFunction; // finally, calculate minimum MtxOptim.Recalculate; end; end;
#include "MtxVecCpp.h" #include "Math387.hpp" #include "MtxVecTools.hpp" // define the real function to be minimized 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); } void __fastcall(TMtxOptimization *MtxOptim); { if (MtxOptim != NULL) { // define two variables and their initial values MtxOptim->VariableParameters->SetIt(false,OPENARRAY(double,(2,-1))); // use BFGS optimization method MtxOptim->OptimizationMethod = optBFGS; // tolerance for MinValue and gradient calculation // additional note : since we did not define the GradProc, // the internal numerical gradient approximation will be used MtxOptim->Tolerance = 2.0e-6; MtxOptim->GradTolerance = 2.0e-6; // function to be minimized MtxOptim->RealFunction = Banana; // finally, calculate minimum MtxOptim->Recalculate(); } }
using Dew.Math; using Dew.Math.Units; namespace Dew.Examples { // define the real function to be minimized private double banana(TVec pars, double[] consts, object[] objConsts) { return 100*Math.Pow(pars[1]-Math.Pow(pars[0],2),2)+Math.Pow(1-pars[0],2); } private void Example(TMtxOptimization MtxOptim); { if (MtxOptim != null) { // define two variables and their initial values MtxOptim.VariableParameters.SetIt(false, new double[] {2,-1}); // use BFGS optimization method MtxOptim.OptimizationMethod = TOptMethod.optBFGS; // tolerance for MinValue and gradient calculation // additional note : since we did not define the GradProc, // the internal numerical gradient approximation will be used MtxOptim.Tolerance = 2.0e-6; MtxOptim.GradTolerance = 2.0e-6; // function to be minimized MtxOptim.RealFunction = Banana; // finally, calculate minimum MtxOptim.Recalculate(); } } }


Properties

 Name  Summary 
AutoUpdate  
ConstantParameters Access the ConstantParameters vector to set/read the additional constants used in minimized function. 
Dirty  
GradHessProcedure Defines the gradient vector and Hessian matrix calculation routine. 
GradProcedure Defines the gradient vector calculation routine. 
GradTolerance Sets the precision for numeric gradient and/or Hessian matrix calculation. 
Lambda0 Defines initial lambda step uses in Marquardt optimization algorithm. 
MaxIterations Defines the maximum number of iterations allowed for minimum search. 
NumericGradMethod Defines which gradient numerical approximation method will be used to to evaluate gradient. 
OptimizationMethod Defines which optimization method will be used to find the minimum of function of several variables. 
RealFunction Defines the function to be minimized. 
SoftSearch Defines the internal line search algorithm. 
StopReason Returns the reason why optimization algorithm was stopped. 
Tolerance Defines precision. 
VariableParameters Access the VariableParameters vector to set/read the variables in minimized function. 
Verbose If not nil then the optimization method uses it for logging each optimization step. 

Methods

 Name  Summary 
Recalculate Triggers TMtxOptimization recalculation. 
SetObjects Use SetObjects method to define any additional Object constant parameters in the RealFunction. 

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