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
| Name | Type | Description |
|---|---|---|
| AutoUpdate | Boolean | Automatic recalculation. |
| BlockAssign | Boolean | Block streaming of specific properties when storing only a "template". |
| ConstantParameters | TVec | set/read the additional constants used in minimized function. |
| Dirty | Boolean | Becomes true after any of the properties have changed. |
| EditorActive | Boolean | Returns True, if the component editor is displayed. |
| GradHessProcedure | TGradHess | Defines the gradient vector and Hessian matrix calculation routine. |
| GradProcedure | TGrad | Defines the gradient vector calculation routine. |
| GradTolerance | Double | The precision for numeric gradient and/or Hessian matrix calculation. |
| Lambda0 | Double | Initial lambda step used in Marquardt optimization algorithm. |
| MaxIterations | Int32 | maximum number of iterations allowed for minimum search. |
| NumericGradMethod | TNumericGradMethod | Defines which gradient numerical approximation method will be used to to evaluate gradient. |
| OptimizationMethod | TOptMethod | Optimization algorithm used for minimum search. |
| RealFunction | TRealFunction | Defines the function to be minimized. |
| Reference | TReferenceList | Stores a list of components that have to be notified, when this component is destroyed. |
| SoftSearch | Boolean | Internal line search algorithm. |
| StopReason | TOptStopReason | Stop reason for optimization algorithm. |
| Tolerance | Double | Precision. |
| VariableParameters | TVec | Set/read the variables in minimized function. |
| Verbose | TStrings | If not nil then the optimization method uses it for logging each optimization step. |
Methods
| Name | Description |
|---|---|
| AllowStreaming | Returns the negatated value of BlockAssign. |
| Assign | Assign values of all published properties from Source. |
| AssignTemplate | Assign values of "template-like" published properties from Source. |
| EditorClass | To be overriden in descendanr classses. |
| LoadFromStream | Load the component from Src stream. |
| LoadTemplateFromFile | Load a template from file. |
| LoadTemplateFromStream | Load a template from stream. |
| Recalculate | Triggers TMtxOptimization recalculation. |
| Reset | Reset is called after loading the data from stream or file. |
| SaveTemplateToFile | Save template to file. |
| SaveTemplateToStream | Save template to stream. |
| SaveToStream | Save the component to Dst stream. |
| SetObjects | Define any additional Object constant parameters in the RealFunction. |
Events
| Name | Description |
|---|---|
| GradHessProcedureEvent | Defines the gradient vector and Hessian matrix calculation routine. |
| GradProcedureEvent | Defines the gradient vector calculation routine. |
| RealFunctionEvent | Defines the function to be minimized. |