TMtxOptimization Class

ComponentTMtxComponentTMtxOptimization

type TMtxOptimization = class(TMtxComponent);

Interfaces the optimization routines.

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

How to use TMtxOptimization component?

  • Drop a TMtxOptimization component on the form.
  • Define the number of variables and their initial values by accessing the TMtxOptimization.VariableParameters vector.
  • Define any additional constant parameters by accessing TMtxOptimization.ConstantParameters vector.
  • Define any additional constant pointer parameter by using the TMtxOptimization.SetObjects method.
  • Define real function (must be of TRealFunction type).
  • Define optimization method by accessing the TMtxOptimization.OptimizationMethod property.
  • Depending on optimization method you'll have to (optionally) define the gradient calculation procedure (TMtxOptimization.GradProcedure method) or gradient/Hessian matrix calculation procedure (TMtxOptimization.GradHessProcedure 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 TMtxOptimization.NumericGradMethod property.
  • Call the TMtxOptimization.Recalculate method to find the minimum of function of several variables.

Results:

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

Examples

delphi
Uses Math387, MtxVecTools;

// define the real function to be minimized
function BananaFunction(const Pars: TVec; const Consts: TVec; const PConsts: Array of TObject): double;
begin
    BananaFunction := 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;

Properties

NameTypeDescription
AutoUpdateBooleanAutomatic recalculation.
BlockAssignBooleanBlock streaming of specific properties when storing only a "template".
ConstantParametersTVecset/read the additional constants used in minimized function.
DirtyBooleanBecomes true after any of the properties have changed.
EditorActiveBooleanReturns True, if the component editor is displayed.
GradHessProcedureTGradHessDefines the gradient vector and Hessian matrix calculation routine.
GradProcedureTGradDefines the gradient vector calculation routine.
GradToleranceDoubleThe precision for numeric gradient and/or Hessian matrix calculation.
Lambda0DoubleInitial lambda step used in Marquardt optimization algorithm.
MaxIterationsIntegermaximum number of iterations allowed for minimum search.
NumericGradMethodTNumericGradMethodDefines which gradient numerical approximation method will be used to to evaluate gradient.
OptimizationMethodTOptMethodOptimization algorithm used for minimum search.
RealFunctionTRealFunctionDefines the function to be minimized.
ReferenceTReferenceListStores a list of components that have to be notified, when this component is destroyed.
SoftSearchBooleanInternal line search algorithm.
StopReasonTOptStopReasonStop reason for optimization algorithm.
ToleranceDoublePrecision.
VariableParametersTVecSet/read the variables in minimized function.
VerboseTStringsIf not nil then the optimization method uses it for logging each optimization step.

Methods

NameDescription
AllowStreamingReturns the negatated value of BlockAssign.
AssignAssign values of all published properties from Source.
AssignTemplateAssign values of "template-like" published properties from Source.
EditorClassTo be overriden in descendanr classses.
LoadFromStreamLoad the component from Src stream.
LoadTemplateFromFileLoad a template from file.
LoadTemplateFromStreamLoad a template from stream.
RecalculateTriggers TMtxOptimization recalculation.
ResetReset is called after loading the data from stream or file.
SaveTemplateToFileSave template to file.
SaveTemplateToStreamSave template to stream.
SaveToStreamSave the component to Dst stream.
SetObjectsDefine any additional Object constant parameters in the RealFunction.