Solution: Array;
Solution: System.Array;
The Solution property returns the found solution.
To get the value of criterion function by iterations, use the ISmBoxConstrainedOptimization.ObjValByIter property.
Add a link to the Stat system assembly.
Sub UserProc;
Var
p1: SmBoxConstrainedOptimization;
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;
// Coefficients order
p1.CoefficientsOrder := "a;b;c";
// Criterion function:
p1.FunctionString := "a-4*b+2*c";
// Initial approximations
init[0]:=100; init[1]:=-50; init[2]:=800;
p1.InitApproximation:=init;
// Use of variables
p1.UseDerivatives := False;
// Maximum number of iterations and accuracy
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;
Debug.WriteLine("=== Actually used initial approximations ===");
Debug.Indent;
For i := 0 To p1.InitApproximationActual.Length - 1 Do
Debug.WriteLine(i.ToString + ". " + p1.InitApproximationActual[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 value.
Solution.
Criterion function gradient.
Actually used initial approximations.
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;
ub, lb, init: Array[3] Of Double;
res, i: Integer;
ObjValByIter, Sol, FuncGrad, InitApproxActual: System.Array;
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;
// Coefficients order
p1.CoefficientsOrder := "a;b;c";
// Criterion function
p1.FunctionString := "a-4*b+2*c";
// Initial approximations
init[0] := 100; init[1] := -50; init[2] := 800;
p1.InitApproximation := init;
// Use of variables
p1.UseDerivatives := False;
// Maximum number of iterations and accuracy
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 ===");
Sol := p1.Solution;
System.Diagnostics.Debug.Indent();
For i := 0 To p1.Solution.Length - 1 Do
System.Diagnostics.Debug.WriteLine(i.ToString() + ". " + Sol[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();
System.Diagnostics.Debug.WriteLine("=== Actually used initial approximations ===");
InitApproxActual := p1.InitApproximationActual;
System.Diagnostics.Debug.Indent();
For i := 0 To InitApproxActual.Length - 1 Do
System.Diagnostics.Debug.WriteLine(i.ToString() + ". " + InitApproxActual[i].ToString());
End For;
System.Diagnostics.Debug.Unindent();
Else
System.Diagnostics.Debug.WriteLine(p1.Errors);
End If;
End Sub;
See also: