CallbackCycle: ICallbackCycle;
CallbackCycle: Prognoz.Platform.Interop.Cp.ICallbackCycle;
The CallbackCycle property determines the handler that is used to calculate controlling variables.
The variable is a controlling variable if the INonLoVariable.ControlVariable property is set to True.
To execute the example, add a link to the Cp system assembly. This example also uses the MyCallbackCycle class described in ICallbackCycle.Execute.
Sub UserProc;
Var
Optima: NonLinearOptimization;
Vars: INonLoVariables; //variables list
Vrbl: INonLoVariable; //variable
VarConstr: IVarConstraint; //variable constraint
VarConstrs: IVarConstraints; //list of variable constraints
Constraints: INonLoConstraints; //constraint
Constraint: INonLoConstraint; //constraints list
RetroX1: Array Of double;
RetroX2: Array Of double;
RetroU: Array Of double;
RetroV: Array Of double;
InitApproximation: Array Of double;
Funstions: Array[2] Of string;
s: string;
T, i: integer;
MyCallBackC: MyCallBackCycle;
Res: INonLoResults;
Begin
T := 4;
Optima := New NonLinearOptimization.Create;
// Variables with retrospective
RetroX1 := New double[T];
RetroX2 := New double[T];
RetroU := New double[T];
RetroV := New double[T];
InitApproximation := New Double[T];
// Values of variables with retrospective
For i := 0 To T - 1 Do
RetroX1[i] := 0.8 + i / 5;
RetroX2[i] := 0.85 + i / 4;
RetroU[i] := 0.9 + i / 10;
RetroV[i] := 0.95 + i / 10;
End For;
// Variables and constraints
Vars := Optima.Variables;
// First variable
Vrbl := Vars.Add("x1");
Vrbl.Retrospective := RetroX1;
Vrbl.CoefficientsOrder := "x1[t];x1[t-1]";
VarConstrs := Vrbl.Constraints;
For i := 0 To T - 1 Do
VarConstr := VarConstrs.Add;
VarConstr.LowerBound := -10 - i / 100;
VarConstr.UpperBound := 10 + i / 100;
VarConstr.Lag := i + 1;
End For;
// Second variable
Vrbl := Vars.Add("x2");
Vrbl.Retrospective := RetroX2;
Vrbl.CoefficientsOrder := "x2[t];x2[t-1]";
VarConstrs := Vrbl.Constraints;
For i := 0 To T - 1 Do
VarConstr := VarConstrs.Add;
VarConstr.LowerBound := -100 - i;
VarConstr.UpperBound := 100 + i;
VarConstr.Lag := i + 1;
End For;
// First controlling variable
For i := 0 To T - 1 Do
InitApproximation[i] := 1.2 + (i + 1) / 100;
End For;
Vars := Optima.Variables;
Vrbl := Vars.Add("u");
Vrbl.Retrospective := RetroU;
Vrbl.ControlVariable := True;
Vrbl.CoefficientsOrder := "u[t];u[t-1]";
Vrbl.InitApproximation := InitApproximation;
VarConstrs := Vrbl.Constraints;
For i := 0 To T - 1 Do
VarConstr := VarConstrs.Add;
VarConstr.LowerBound := 1;
VarConstr.UpperBound := 2;
VarConstr.Lag := i;
End For;
// Second controlling variable
For i := 0 To T - 1 Do
InitApproximation[i] := 1.5 + (i + 1) / 100;
End For;
Vars := Optima.Variables;
Vrbl := Vars.Add("v");
Vrbl.Retrospective := RetroV;
Vrbl.ControlVariable := True;
Vrbl.CoefficientsOrder := "v[t];v[t-1]";
Vrbl.InitApproximation := InitApproximation;
VarConstrs := Vrbl.Constraints;
For i := 0 To T - 1 Do
VarConstr := VarConstrs.Add;
VarConstr.LowerBound := 0.8;
VarConstr.UpperBound := 7;
VarConstr.Lag := i;
End For;
// Non-linear optimization equations
Funstions := New string[2];
Funstions[0] := "0.3 * x1[t-1] + 0.1 * x2[t-1] + u[t-1] * x1[t-1] *x2[t-1]";
Funstions[1] := "(-0.2) * x1[t-1] + 0.4 *x2[t-1] + (x1[t-1] * x2[t-1])/(v[t]+1)";
Optima.Equations := Funstions;
// Criterion function
Optima.CriterionFunction := "x1[t] + x2[t-1] - u[t]";
// Extremum type
Optima.Extremum := ExtremumType.Minimum;
// Maximum number of iterations
Optima.MaxIterationsCount := 150;
// Solution accuracy
Optima.Tolerance := 0.0001;
// Solutions search method
Optima.MethodType := CpNonLinearMethodType.SequentialQP;
// Number of intervals
Optima.NodesCount := 2;
// Non-linear optimization constraints
Constraints := Optima.Constraints;
Constraint := Constraints.Add;
Constraint.UpperBound := "4";
Constraint.Expression := "v[t] + u[t] + x1[t]*0.001";
Constraint.LowerBound := "2";
// The handler to calculate the controlling variables values
MyCallBackC := New MyCallbackCycle.Create;
Optima.CallbackCycle := MyCallBackC;
Res := Optima.Evaluate(T) As INonLoResults;
If res.Status = 0 Then
s := Optimal value: " + Res.OptimalValue.ToString;
Else
s := Error : + Res.ErrorMsg;
End If;
Debug.WriteLine(s);
End Sub UserProc;
Result: non-linear optimization is calculated. Custom handler is used to calculate controlling variables.
The example uses the MyCallbackCycle class described in ICallbackCycle.Execute.
Imports Prognoz.Platform.Interop.Cp;
…
[STAThread]
Public Shared Sub Main(Params: StartParams);
Var
Optima: NonLinearOptimization;
Vars: INonLoVariables; //variables list
Vrbl: INonLoVariable; //variable
VarConstr: IVarConstraint; //variable constraint
VarConstrs: IVarConstraints; //list of variable constraints
Constraints: INonLoConstraints; //constraint
Constraint: INonLoConstraint; //constraints list
RetroX1: Array Of double;
RetroX2: Array Of double;
RetroU: Array Of double;
RetroV: Array Of double;
InitApproximation: Array Of double;
Funstions: Array[2] Of string;
s: string;
T, i: integer;
MyCallBackC: MyCallBackCycle;
Res: INonLoResults;
Begin
T := 4;
Optima := New NonLinearOptimization.Create();
// Variables with retrospective
RetroX1 := New double[T];
RetroX2 := New double[T];
RetroU := New double[T];
RetroV := New double[T];
InitApproximation := New Double[T];
// Values of variables with retrospective
For i := 0 To T - 1 Do
RetroX1[i] := 0.8 + i / 5;
RetroX2[i] := 0.85 + i / 4;
RetroU[i] := 0.9 + i / 10;
RetroV[i] := 0.95 + i / 10;
End For;
// Variables and constraints
Vars := Optima.Variables;
// First variable
Vrbl := Vars.Add("x1");
Vrbl.Retrospective := RetroX1;
Vrbl.CoefficientsOrder := "x1[t];x1[t-1]";
VarConstrs := Vrbl.Constraints;
For i := 0 To T - 1 Do
VarConstr := VarConstrs.Add();
VarConstr.LowerBound := -10 - i / 100;
VarConstr.UpperBound := 10 + i / 100;
VarConstr.Lag := i + 1;
End For;
// Second variable
Vrbl := Vars.Add("x2");
Vrbl.Retrospective := RetroX2;
Vrbl.CoefficientsOrder := "x2[t];x2[t-1]";
VarConstrs := Vrbl.Constraints;
For i := 0 To T - 1 Do
VarConstr := VarConstrs.Add();
VarConstr.LowerBound := -100 - i;
VarConstr.UpperBound := 100 + i;
VarConstr.Lag := i + 1;
End For;
// First controlling variable
For i := 0 To T - 1 Do
InitApproximation[i] := 1.2 + (i + 1) / 100;
End For;
Vars := Optima.Variables;
Vrbl := Vars.Add("u");
Vrbl.Retrospective := RetroU;
Vrbl.ControlVariable := True;
Vrbl.CoefficientsOrder := "u[t];u[t-1]";
Vrbl.InitApproximation := InitApproximation;
VarConstrs := Vrbl.Constraints;
For i := 0 To T - 1 Do
VarConstr := VarConstrs.Add();
VarConstr.LowerBound := 1;
VarConstr.UpperBound := 2;
VarConstr.Lag := i;
End For;
//Second controlling variable For i := 0 To T - 1 Do
InitApproximation[i] := 1.5 + (i + 1) / 100;
End For;
Vars := Optima.Variables;
Vrbl := Vars.Add("v");
Vrbl.Retrospective := RetroV;
Vrbl.ControlVariable := True;
Vrbl.CoefficientsOrder := "v[t];v[t-1]";
Vrbl.InitApproximation := InitApproximation;
VarConstrs := Vrbl.Constraints;
For i := 0 To T - 1 Do
VarConstr := VarConstrs.Add();
VarConstr.LowerBound := 0.8;
VarConstr.UpperBound := 7;
VarConstr.Lag := i;
End For;
//Non-linear optimization equations Funstions := New string[2];
Funstions[0] := "0.3 * x1[t-1] + 0.1 * x2[t-1] + u[t-1] * x1[t-1] *x2[t-1]";
Funstions[1] := "(-0.2) * x1[t-1] + 0.4 *x2[t-1] + (x1[t-1] * x2[t-1])/(v[t]+1)";
Optima.Equations := Funstions;
//Criterion function Optima.CriterionFunction := "x1[t] + x2[t-1] - u[t]";
//Extremum type Optima.Extremum := ExtremumType.tetMinimum;
//Maximum number of iterations Optima.MaxIterationsCount := 150;
//Accuracy of solution Optima.Tolerance := 0.0001;
//Solutions search method Optima.MethodType := CpNonLinearMethodType.cnlmtSequentialQP;
//Number of intervalsOptima.NodesCount:=2; //Non-linear optimization constraintsConstraints:=Optima.Constraints; Constraint:=Constraints.Add(); Constraint.UpperBound:="4"; Constraint.Expression:="v[t]+u[t]+x1[t]*0.001"; Constraint.LowerBound:="2"; //Handler for calculation of values of controlling variablesMyCallBackC:=NewMyCallbackCycle.Create(); Optima.CallbackCycle:=MyCallBackC; Res:=Optima.Evaluate(T)AsINonLoResults; Ifres.Status=0Then s:="Optimalvalue:"+Res.OptimalValue.ToString(); Else s:="Error:"+Res.ErrorMsg; EndIf; System.Diagnostics.Debug.WriteLine(s); EndSub;
Result: non-linear optimization is calculated. Custom handler is used to calculate controlling variables.
See also: