TMtxOptimization Class

Source: MtxVecTools.cs · Assembly: Dew.Math
ComponentTMtxComponentTMtxOptimization

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

Results:

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

Examples

csharp
using Dew.Math;
    using Dew.Math.Units;

    namespace Dew.Examples
    {
        // define the real function to be minimized
        private double Banana(TVec pars, TVec consts, params 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

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.
MaxIterationsInt32maximum 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.

Events

NameDescription
GradHessProcedureEventDefines the gradient vector and Hessian matrix calculation routine.
GradProcedureEventDefines the gradient vector calculation routine.
RealFunctionEventDefines the function to be minimized.