Callback: ICallbackNLOptimization;
Callback: Prognoz.Platform.Interop.Stat.ICallbackNLOptimization;
The Callback property determines the user class used to calculate values of variables and criterion function.
If the Callback property is used, and if ISmBoxConstrainedOptimization.UseDerivatives = True, derivatives are calculated by means of the GetObjFunPartialDeriv and GetConstraintPartialDeriv methods determined in Callback, and if ISmBoxConstrainedOptimization.UseDerivatives = False, derivative values are obtained by function gradient calculation.
To get value of criterion function, use the ISmBoxConstrainedOptimization.OptimalFunctionValue property.
Add a link to the Stat system assembly.
The example uses the CallBackBCO user class, which description is given in ICallbackNLOptimization.GetConstraintPartialDeriv.
Sub UserProc;
Var
p1: SmBoxConstrainedOptimization;
CallBack : CallBackBCO;
ub, lb, init: Array[3] Of Double;
res, i: Integer;
Begin
p1 := New SmBoxConstrainedOptimization.Create;
ub[0] := 5; ub[1] := 2; ub[2] := 1;
lb[0] := -1; lb[1] := -1; lb[2] := -1;
// Definition area:
p1.Boundary.BoundaryUpper := ub;
p1.Boundary.BoundaryLower := lb;
// Coefficient order:
p1.CoefficientsOrder := "a;b;c";
// Criterion function:
p1.FunctionString := "a-4*b+2*c";
// Initial approximations:
init[0]:=0; init[1]:=-0.5; init[2]:=1;
p1.InitApproximation:=init;
// Use of derivatives:
p1.UseDerivatives := False;
// CallBack:
CallBack := New CallBackBCO.Create;
Callback.MAXCOUNT := 1000;
Callback.CallCount := 0;
p1.Callback := CallBack;
// Maximum number of iterations and precision:
p1.MaxIteration := 500;
p1.Tolerance := 0.00001;
res := p1.Execute;
If res=0 Then
Debug.WriteLine("=== Criterion function ===");
Debug.Indent;
Debug.WriteLine("CF = " + p1.FunctionString);
Debug.WriteLine(lb[0].ToString + " <= a <= " + ub[0].ToString);
Debug.WriteLine(lb[1].ToString + " <= b <= " + ub[1].ToString);
Debug.WriteLine(lb[2].ToString + " <= c <= " + ub[2].ToString);
Debug.WriteLine("Criterion function value: " + p1.OptimalFunctionValue.ToString);
Debug.WriteLine("Criterion function values by iterations:");
For i := 0 To p1.ObjValByIter.Length - 1 Do
Debug.WriteLine(i.ToString + ". " + p1.ObjValByIter[i].ToString);
End For;
Debug.Unindent;
Debug.WriteLine("=== Solution ===");
Debug.Indent;
For i := 0 To p1.Solution.Length - 1 Do
Debug.WriteLine(i.ToString + ". " + p1.Solution[i].ToString);
End For;
Debug.Unindent;
Debug.WriteLine("=== Criterion function gradient ===");
Debug.Indent;
For i := 0 To p1.FunctionGradient.Length - 1 Do
Debug.WriteLine(i.ToString + ". " + p1.FunctionGradient[i].ToString);
End For;
Debug.Unindent;
Else
Debug.WriteLine(p1.Errors);
End If;
End Sub UserProc;
After executing the example the console window displays the following:
Criterion function expression.
Criterion function values.
Solution.
Criterion function gradient.
The requirements and result of the Fore.NET example execution match with those in the Fore example.
Imports Prognoz.Platform.Interop.Stat;
…
Public Shared Sub Main(Params: StartParams);
Var
p1: SmBoxConstrainedOptimization;
CallBack : CallBackBCO;
ub, lb, init: Array[3] Of double;
res, i: Integer;
ObjValByIter, Solution, FuncGrad: System.Array;
opt: Array [16] Of double;
z: Array[3] Of double;
Begin
p1 := New SmBoxConstrainedOptimization.Create();
ub[0] := 5; ub[1] := 2; ub[2] := 1;
lb[0] := -1; lb[1] := -1; lb[2] := -1;
// Definition area:
p1.Boundary.BoundaryUpper := ub;
p1.Boundary.BoundaryLower := lb;
// Coefficient order:
p1.CoefficientsOrder := "a;b;c";
// Criterion function:
p1.FunctionString := "a-4*b+2*c";
// Initial approximations:
init[0]:=0; init[1]:=-0.5; init[2]:=1;
p1.InitApproximation:=init;
// Use of derivatives:
p1.UseDerivatives := False;
// CallBack:
CallBack := New CallBackBCO.Create();
Callback.MAXCOUNT := 1000;
Callback.CallCount := 0;
Callback._options := opt;
Callback.U := z;
p1.Callback := CallBack;
// Maximum number of iterations and precision:
p1.MaxIteration := 500;
p1.Tolerance := 0.00001;
res := p1.Execute();
If res=0 Then
System.Diagnostics.Debug.WriteLine("=== Criterion function ===");
System.Diagnostics.Debug.Indent();
System.Diagnostics.Debug.WriteLine("CF = " + p1.FunctionString);
System.Diagnostics.Debug.WriteLine(lb[0].ToString() + " <= a <= " + ub[0].ToString());
System.Diagnostics.Debug.WriteLine(lb[1].ToString() + " <= b <= " + ub[1].ToString());
System.Diagnostics.Debug.WriteLine(lb[2].ToString() + " <= c <= " + ub[2].ToString());
System.Diagnostics.Debug.WriteLine("Criterion function value: " + p1.OptimalFunctionValue.ToString());
System.Diagnostics.Debug.WriteLine("Criterion function values by iterations:");
ObjValByIter := p1.ObjValByIter;
For i := 0 To p1.ObjValByIter.Length - 1 Do
System.Diagnostics.Debug.WriteLine(i.ToString() + ". " + ObjValByIter[i].ToString());
End For;
System.Diagnostics.Debug.Unindent();
System.Diagnostics.Debug.WriteLine("=== Solution ===");
Solution := p1.Solution;
System.Diagnostics.Debug.Indent();
For i := 0 To p1.Solution.Length - 1 Do
System.Diagnostics.Debug.WriteLine(i.ToString() + ". " + Solution[i].ToString());
End For;
System.Diagnostics.Debug.Unindent();
System.Diagnostics.Debug.WriteLine("=== Criterion function gradient ===");
FuncGrad := p1.FunctionGradient;
System.Diagnostics.Debug.Indent();
For i := 0 To p1.FunctionGradient.Length - 1 Do
System.Diagnostics.Debug.WriteLine(i.ToString() + ". " + FuncGrad[i].ToString());
End For;
System.Diagnostics.Debug.Unindent();
Else
System.Diagnostics.Debug.WriteLine(p1.Errors);
End If;
End Sub;
See also: