Tolerance: Double;
The Tolerance property determines accuracy of solution.
The default value is 0.0001.
To execute the example, add a link to the Cp system assembly.
The example uses the MyCallBackCycle custom class. Implementation of this class is given in the example for ICallbackCycle.Execute.
Sub UserProc;
Var
TargetAdj: ICpTargetAdjustment;
T: Integer;
RetroX1, RetroX2, RetroU, RetroV: Array Of Double;
InitApproximation, ValuesI, ValuesJ: Array Of Double;
i, j: Integer;
VarsP: ITargetPhaseVariablesArray;
VrblP: ITargetPhaseVariable;
VarConstrs: IVarTargetConstraintsArray;
VarConstr: IVarTargetConstraint;
VarsC: ITargetControlVariablesArray;
VrblC: ITargetControlVariable;
Expls: INonLinearExplanatories;
Expl: INonLinearExplanatory;
Ser: Array[4] Of Double;
Constraints: ITargetConstraints;
Constraint: ITargetConstraint;
ConInfoArray: ITargetConstraintInfoArray;
ConInfo: ITargetConstraintInfo;
Res: ITargetResults;
Val: Double;
MyCallBackC: MyCallBackCycle;
Begin
TargetAdj := New TargetAdjustment.Create;
// Set period
T := 6;
// Create variables with retrospective
RetroX1 := New Double[T];
RetroX2 := New Double[T];
RetroU := New Double[T];
RetroV := New Double[T];
ValuesI := New Double[T];
ValuesJ := New Double[T];
// Create an array of initial approximations
InitApproximation := New Double[T];
// Set initial variable values
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;
ValuesI[i] := 0.6 + i / 8;
ValuesJ[i] := 0.7 + i / 6;
End For;
// Get phase variables
VarsP := TargetAdj.PhaseVariables;
// Add the x1 phase variable
VrblP := VarsP.Add("x1");
VrblP.Name := "x1";
// Set retrospective values
VrblP.Retrospective := RetroX1;
// Set variable order
VrblP.CoefficientsOrder := "x1[t];x1[t-1]";
// Get phase variable constraints
VarConstrs := VrblP.Constraints;
For i := 0 To T - 1 Do
// Create a new constraint
VarConstr := VarConstrs.Add;
// Set borders
VarConstr.LowerBound := -10 - i / 100;
VarConstr.UpperBound := 10 + i / 100;
// Specify the current moment of time
VarConstr.TimeMoment := i;
End For;
// Set frequency equation
VrblP.FunctionExpression := "0.3 * x1[t-1] + 0.1 * x2[t-1] + u[t-1] * x1[t-1] *x2[t-1]";
// Add phase variable x2
VrblP := VarsP.Add("x2");
VrblP.Name := "x2";
VrblP.Retrospective := RetroX2;
VrblP.CoefficientsOrder := "x2[t];x2[t-1]";
VarConstrs := VrblP.Constraints;
For i := 0 To T - 1 Do
VarConstr := VarConstrs.Add;
VarConstr.LowerBound := -100-i;
VarConstr.UpperBound := 100+i;
VarConstr.TimeMoment := i;
End For;
VrblP.FunctionExpression := "(-0.2) * x1[t-1] + 0.4 *x2[t-1] + (x1[t-1] * x2[t-1])/(v[t]+1)";
// Get controlling variables
VarsC := TargetAdj.ControlVariables;
// Add the u controlling variable
VrblC := VarsC.Add("u");
VrblC.Name := "u";
// Set retrospective values
VrblC.Retrospective := RetroU;
// Set coefficient order
VrblC.CoefficientsOrder := "u[t];u[t-1]";
// Set values of initial approximations
For i := 0 To T - 1 Do
InitApproximation[i] := 1.2 + (i + 1) / 100;
End For;
VrblC.InitApproximation := InitApproximation;
// Get controlling variable constraints
VarConstrs := VrblC.Constraints;
For i := 0 To T - 1 Do
// Add a constraint
VarConstr := VarConstrs.Add;
// Set constraint borders
VarConstr.LowerBound := 1;
VarConstr.UpperBound := 2;
// Set the current moment of time
VarConstr.TimeMoment := i;
End For;
// Add controlling variable v
VrblC := VarsC.Add("v");
VrblC.Name := "v";
VrblC.Retrospective := RetroV;
VrblC.CoefficientsOrder := "v[t];v[t-1]";
For i:=0 To T-1 Do
InitApproximation[i] := 1.5+ (i+1)/100;
End For;
VrblC.InitApproximation := InitApproximation;
VarConstrs := VrblC.Constraints;
For i := 0 To T - 1 Do
VarConstr := VarConstrs.Add;
VarConstr.LowerBound := 0.8;
VarConstr.UpperBound := 7;
VarConstr.TimeMoment := i;
End For;
// Get explanatory series
Expls := TargetAdj.Explanatories;
// Set explanatory series i
Expl := Expls.Add;
Expl.VariableName := "i";
Expl.CoefficientsOrder := "i[t]";
Expl.Series := ValuesI;
// Set explanatory series j
Expl := Expls.Add;
Expl.VariableName := "j";
Expl.CoefficientsOrder := "j[t]";
Expl.Series := ValuesJ;
// Set criterion trajectory
ser[0] := 1;
ser[1] := 2;
ser[2] := 3;
ser[3] := 4;
TargetAdj.TargetTrajectory := Ser;
// Set criterion function
TargetAdj.CriterionFunction := "x1[t] + x2[t-1] - u[t] + i[t] + j[t]";
// Set number of iterations
TargetAdj.MaxIterationsCount := 25000;
// Set accuracy of solution
TargetAdj.Tolerance := 0.00001;
// Get non-linear constraints of criterion function
Constraints := TargetAdj.Constraints;
// Add a non-linear constraint
Constraint := Constraints.Add;
// Set non-linear constraint expression
Constraint.Expression := "v[t] + u[t] + x1[t]*0.001";
ConInfoArray := Constraint.Constraints;
For i := 0 To T - 1 Do
conInfo := ConInfoArray.Add;
conInfo.TimeMoment := i;
conInfo.LowerBound := -10.5555;
coninfo.UpperBound := 10.55555;
coninfo.LowerBoundFixed := False;
coninfo.UpperBoundFixed := False;
End For;
// Set problem type
TargetAdj.AutoSearchType := TargetAutoSearchType.MinError;
// Set number of cycles
TargetAdj.AutoAdjustMaxIter := 10;
// Set allowed accuracy
TargetAdj.AutoAdjustSatisfactoryTolerance := 1.01;
// Set number of constraints removed in one iteration
TargetAdj.AutoAdjustRemoveCount := 2;
// Set handler for calculating controlling variable values
MyCallBackC := New MyCallbackCycle.Create;
TargetAdj.CallbackCycle := MyCallBackC;
// Execute calculation
Res := TargetAdj.Evaluate(T) As ITargetResults;
// If calculation is executed without errors, display results in the console
If res.Status = 0 Then
// Display optimal value
Debug.WriteLine("Optimal value:");
Debug.Indent;
Debug.WriteLine(res.OptimalValue);
Debug.Unindent;
// Display controlling variable values
For j := 1 To VarsC.Count Do
VrblC := VarsC.Item(j - 1);
Debug.WriteLine("Controlling variable values '" + VrblC.Id + "':");
Debug.Indent;
For i := 1 To T Do
Val := Res.VarValues(VrblC.Id)[i - 1];
Debug.WriteLine(i.ToString + ": " + Val.ToString);
End For;
Debug.Unindent;
End For;
// Display phase variable values
For j := 1 To VarsP.Count Do
VrblP := VarsP.Item(j - 1);
Debug.WriteLine("Phase variable values '" + VrblP.Id + "':");
Debug.Indent;
For i := 1 To T Do
Val := Res.VarValues(VrblP.Id)[i - 1];
Debug.WriteLine(i.ToString + ": " + Val.ToString);
End For;
Debug.Unindent;
End For;
// Display progress of criterion function change during calculation
// non-linear programming tasks
Debug.WriteLine("Progress of criterion function change during calculation" +
" non-linear programming tasks:");
Debug.Indent;
For i := 0 To Res.ObjValByIter.Length - 1 Do
Debug.WriteLine(Res.ObjValByIter[i]);
End For;
Debug.Unindent;
// Display progress of criterion function change during
// consecutive loosening of limits
Debug.WriteLine("Progress of criterion function change during" +
" consecutive loosening of limits:");
Debug.Indent;
For i := 0 To Res.ObjValByAdjustIter.Length - 1 Do
Debug.WriteLine(Res.ObjValByAdjustIter[i]);
End For;
Debug.Unindent;
// Display optimal criterion function trajectory
Debug.WriteLine("Optimal trajectory of criterion function:");
Debug.Indent;
For i := 0 To Res.CriterionFunctionTrajectory.Length - 1 Do
Debug.WriteLine(Res.CriterionFunctionTrajectory[i]);
End For;
Debug.Unindent;
// If calculation is completed with error, display its text
Else
Debug.WriteLine(res.ErrorMsg);
End If;
End Sub UserProc;
After executing the example optimization problem parameters are set, the problem is calculated, results are displayed in the console.
See also: