Optimization.Simplex Method

Overload List

#SignatureDescription
1function 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.
2function 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.
3function 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.

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.

#NameTypeDescription
1FuncTRealFunction
2ParsDouble
3ConstsTDoubleArray
4ObjConstTObjectArray
5FMinDouble
6StopReasonTOptStopReason
7FloatPrecisionTMtxFloatPrecision
8MaxIterInteger
9ToleranceDoublescalar

Returns: Int32

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

#NameDescription
1FuncReal function (must be of TRealFunction type) to be minimized.
2ParsStores the initial estimates for parameters (minimum estimate). After the call to routine returns adjusted calculated values (minimum position).
3ConstsAdditional Fun constant parameteres (can be/is usually nil).
4ObjConstAdditional Fun constant parameteres (can be/is usually nil).
5FMinReturns function value at minimum.
6StopReasonReturns reason why minimum search stopped (see TOptStopReason).
7MaxIterMaximum allowed numer of minimum search iterations.
8ToleranceDesired Pars - minimum position tolerance.
9FloatPrecisionSpecifies the floating point precision to be used by the routine.
10VerboseIf 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.

Remarks:

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.

Examples
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;
See Also: TRealFunction

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.

#NameTypeDescription
1FuncTRealFunction
2ParsTDoubleArray
3ConstsTDoubleArray
4ObjConstTObjectArray
5LBTDoubleArray
6UBTDoubleArray
7FMinDouble
8StopReasonTOptStopReason
9FloatPrecisionTMtxFloatPrecision
10MaxIterInteger
11ToleranceDoublescalar
12VerboseTStrings

Returns: Int32

Remarks:

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.