You are here: Symbol Reference > MtxVecTools Namespace > Classes > TMtxOptimization Class
MtxVec VCL
ContentsIndex
PreviousUpNext
TMtxOptimization Class

Interfaces the optimization routines.

MtxVecTools_TMtxOptimizationMtxVecTools_TMtxOptimization
Pascal
TMtxOptimization = class(TMtxComponent);

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 VariableParameters vector.
  • Define any additional constant parameters by accessing ConstantParameters vector.
  • Define any additional constant pointer parameter by using the SetObjects method.
  • Define real function (must be of TRealFunction type).
  • Define optimization method by accessing the OptimizationMethod property.
  • Depending on optimization method you'll have to (optionally) define the gradient calculation

procedure (GradProcedure method) or gradient/Hessian matrix calculation procedure (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 NumericGradMethod property.

  • Call the Recalculate method to find the minimum of function of several variables.

 

Results:

How to solve the optimization problem using TMtxOptimization component?

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;
#include "MtxExpr.hpp" #include "Math387.hpp" #include "MtxVecTools.hpp" // define the real function to be minimized double __fastcall Banana(TVec* const Parameters, TVec* const Constants, 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 Test(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(); } }
Examples on GitHub
Copyright (c) 1999-2025 by Dew Research. All rights reserved.
What do you think about this topic? Send feedback!