Callback: 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; if ISmBoxConstrainedOptimization.UseDerivatives = False, function gradient is calculated to get derivative values.
To get value of criterion function, use the ISmBoxConstrainedOptimization.OptimalFunctionValue property.
To execute the example, add a link to the Stat system assembly.
The example uses the CallBackBCO custom class described 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.
See also: