Overload List
Overload 1: function Simplex(Func: TRealFunction; var Pars: Double; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; out StopReason: TOptStopReason; const FloatPrecision: TMtxFloatPrecision; MaxIter: Integer; Tolerance: Double): Integer;
Minimizes function of several variables by using Simplex optimization method with no algorithm step log.
| # | Name | Type | Description |
|---|---|---|---|
| 1 | Func | TRealFunction | |
| 2 | Pars | Double | |
| 3 | Consts | TDoubleArray | |
| 4 | ObjConst | TObjectArray | |
| 5 | FMin | Double | |
| 6 | StopReason | TOptStopReason | |
| 7 | FloatPrecision | TMtxFloatPrecision | |
| 8 | MaxIter | Integer | |
| 9 | Tolerance | Double | scalar |
Returns: Int32
Uses MtxVec, Math387, Optimization;
function Banana(const Pars: TVec; const Consts: TVec; Const OConsts: Array of TObject): double;
begin
Banana := 100*Sqr(Pars[1]-Sqr(Pars[0]))+Sqr(1-Pars[0]);
end;
procedure Example;
var stop: TOptStopReason;
fmin: double;
Pars: Array[0..1] of double;
begin
// Initial estimates for pars
Pars[0] := 0.2;
Pars[1] := 0.3;
// define lower and upper bounds
// 0<=pars[0]<=0.5
// pars[1]<=0.7
Simplex(Banana,Pars,[],[],[0,-INF],[0.5,0.7],fMin,stop,mvDouble);
end;
Overload 2: function Simplex(Func: TRealFunction; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; out FMin: Double; out StopReason: TOptStopReason; const FloatPrecision: TMtxFloatPrecision; MaxIter: Integer; Tolerance: Double; const Verbose: TStrings): Integer;
Minimizes the function of several variables by using the Nelder-Mead (Simplex) optimization method.
| # | Name | Description |
|---|---|---|
| 1 | Func | Real function (must be of TRealFunction type) to be minimized. |
| 2 | Pars | Stores the initial estimates for parameters (minimum estimate). After the call to routine returns adjusted calculated values (minimum position). |
| 3 | Consts | Additional Fun constant parameteres (can be/is usually nil). |
| 4 | ObjConst | Additional Fun constant parameteres (can be/is usually nil). |
| 5 | FMin | Returns function value at minimum. |
| 6 | StopReason | Returns reason why minimum search stopped (see TOptStopReason). |
| 7 | MaxIter | Maximum allowed numer of minimum search iterations. |
| 8 | Tolerance | Desired Pars - minimum position tolerance. |
| 9 | FloatPrecision | Specifies the floating point precision to be used by the routine. |
| 10 | Verbose | If assigned, stores Fun, evaluated at each iteration step. Optionally, you can also pass TOptControl object to the Verbose parameter. This allows the optimization procedure to be interrupted from another thread and optionally also allows logging and iteration count monitoring. |
Returns: Int32 - the number of iterations required to reach the solution(minimum) within given tolerance.
What it computes. Minimizes a real function of several
variables with the gradient-free Nelder-Mead (downhill simplex) method - reflection, expansion, contraction and shrink steps on a simplex of n+1 vertices. No gradient or Hessian is required. The standard test ('Banana'/Rosenbrock) objective is
f(x_0,x_1) = 100 (x_1 - x_0^2)^2 + (1 - x_0)^2
with the global minimum f=0 at (x_0,x_1)=(1,1).
Domain. Pars holds the n starting coordinates (any
finite reals); MaxIter > 0; Tolerance ≥ 0 is the simplex-size convergence threshold.
Defined behaviour. On return Pars holds the best vertex,
FMin its objective value, and StopReason is TOptStopReason.OptResConverged when the simplex shrank below Tolerance, TOptStopReason.OptResMaxIter when the iteration cap was hit, or TOptStopReason.optNANValue if the objective returned NaN. The bounded overload (with LB/UB) maps each parameter through a smooth transform so the returned point always satisfies LB_i <= Pars_i <= UB_i; use +/-inf for a one-sided or absent bound.
Minimizes the function of several variables by using the Nelder-Mead (Simplex) optimization method. The advantage of Simplex method is it does not require gradient or Hessian.
Uses MtxVec, Math387, Optimization;
function Banana(const Pars: TVec; const Consts: TVec; Const OConsts: Array of TObject): double;
begin
Banana := 100*Sqr(Pars[1]-Sqr(Pars[0]))+Sqr(1-Pars[0]);
end;
procedure Example;
var Iters : integer;
Pars : Array [0..1] of double;
StopReason : TOptStopReason;
begin
// initial estimates for x1 and x2
Pars[0] := 0;
Pars[1] := 0;
Iters := Simplex(Banana,Pars,[],[],FMin,StopReason,mvDouble,1000);
// stop if Iters >1000 or Tolerance < 1e-8
// Returns Pars = [1,1] and FMin = 0, meaning x1=1, x2=1 and minimum value is 0
end;
Overload 3: function Simplex(Func: TRealFunction; var Pars: TDoubleArray; const Consts: TDoubleArray; const ObjConst: TObjectArray; const LB: TDoubleArray; const UB: TDoubleArray; out FMin: Double; out StopReason: TOptStopReason; const FloatPrecision: TMtxFloatPrecision; MaxIter: Integer; Tolerance: Double; const Verbose: TStrings): Integer;
Minimize function of several variables by using Simplex method with lower and/or upper bounds for parameters.
| # | Name | Type | Description |
|---|---|---|---|
| 1 | Func | TRealFunction | |
| 2 | Pars | TDoubleArray | |
| 3 | Consts | TDoubleArray | |
| 4 | ObjConst | TObjectArray | |
| 5 | LB | TDoubleArray | |
| 6 | UB | TDoubleArray | |
| 7 | FMin | Double | |
| 8 | StopReason | TOptStopReason | |
| 9 | FloatPrecision | TMtxFloatPrecision | |
| 10 | MaxIter | Integer | |
| 11 | Tolerance | Double | scalar |
| 12 | Verbose | TStrings |
Returns: Int32
This version supports lower and upper bound(s) for function parameters Pars. Lower and upper bounds are defined in LB and UP arrays respectively. Depending on lower and/or upper bound for parameter, there are several possibilities for LB and UB:
- Lower and upper bound: For each parameter both LB and UB should be set to specific value.
- Upper bound only: In this case, LB is set to -INF, UB to specific value.
- Lower bound only: In this case, LB should be set to specific value, UB to +INF.
- No bounds: In this case, use non-bounded Simplex version or set LB and UB to -INF and +INF respecively.