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 order of variables
VrblP.CoefficientsOrder := "x1[t];x1[t-1]";
// Get constraints of phase variable
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;
// Determine current time point
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 the x2 phase variable
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 order of coefficients
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 constraints of controlling variable
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 current moment of time
VarConstr.TimeMoment := i;
End For;
// Add a controlling dimension 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 controlling 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 target trajectory
ser[0] := 1;
ser[1] := 2;
ser[2] := 3;
ser[3] := 4;
TargetAdj.TargetTrajectory := Ser;
// Set target function
TargetAdj.CriterionFunction := "x1[t] + x2[t-1] - u[t] + i[t] + j[t]";
// Set number of iterations
TargetAdj.MaxIterationsCount := 25000;
// Set solution accuracy
TargetAdj.Tolerance := 0.00001;
// Get non-linear constraints of target function
Constraints := TargetAdj.Constraints;
// Add a non-linear constraint
Constraint := Constraints.Add;
// Set expression of non-linear constraint
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 satisfactory tolerance
TargetAdj.AutoAdjustSatisfactoryTolerance := 1.<font color="#008000">01</font><font color="#000000">;<br/> </font><font color="#008000">// Set number of constraints deleted during one iteration<br/> </font><font color="#000000"> TargetAdj.AutoAdjustRemoveCount := </font><font color="#008000">2</font><font color="#000000">;<br/> </font><font color="#008000">// Set handler to calculate values for controlling variables<br/> </font><font color="#000000"> MyCallBackC := </font><font color="#008080">New</font><font color="#000000"> MyCallbackCycle.Create;<br/> TargetAdj.CallbackCycle := MyCallBackC;<br/> </font><font color="#008000">// Calculate<br/> </font><font color="#000000"> Res := TargetAdj.Evaluate(T) </font><font color="#008080">As</font><font color="#000000"> ITargetResults;<br/> </font><font color="#008000">// If calculation is without errors, then display results to console<br/> </font><font color="#000000"> </font><font color="#008080">If</font><font color="#000000"> res.Status = </font><font color="#008000">0</font><font color="#000000"> </font><font color="#008080">Then</font><font color="#000000"><br/> </font><font color="#008000">// Display optimal value<br/> </font><font color="#000000"> Debug.WriteLine(</font><font color="#800000">"Optimal value:"</font><font color="#000000">);<br/> Debug.Indent;<br/> Debug.WriteLine(res.OptimalValue);<br/> Debug.Unindent;<br/> </font><font color="#008000">// Display values of controlling variables<br/> </font><font color="#000000"> </font><font color="#008080">For</font><font color="#000000"> j := </font><font color="#008000">1</font><font color="#000000"> </font><font color="#008080">To</font><font color="#000000"> VarsC.Count </font><font color="#008080">Do</font><font color="#000000"><br/> VrblC := VarsC.Item(j - </font><font color="#008000">1</font><font color="#000000">);<br/> Debug.WriteLine(</font><font color="#800000">"Values of controlling variable '"</font><font color="#000000"> + VrblC.Id + </font><font color="#800000">"':"</font><font color="#000000">);<br/> Debug.Indent;<br/> </font><font color="#008080">For</font><font color="#000000"> i := </font><font color="#008000">1</font><font color="#000000"> </font><font color="#008080">To</font><font color="#000000"> T </font><font color="#008080">Do</font><font color="#000000"><br/> Val := Res.VarValues(VrblC.Id)[i - </font><font color="#008000">1</font><font color="#000000">];<br/> Debug.WriteLine(i.ToString + </font><font color="#800000">": "</font><font color="#000000"> + Val.ToString);<br/> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">For</font><font color="#000000">;<br/> Debug.Unindent;<br/> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">For</font><font color="#000000">;<br/> </font><font color="#008000">// Display values of phase variables<br/> </font><font color="#000000"> </font><font color="#008080">For</font><font color="#000000"> j := </font><font color="#008000">1</font><font color="#000000"> </font><font color="#008080">To</font><font color="#000000"> VarsP.Count </font><font color="#008080">Do</font><font color="#000000"><br/> VrblP := VarsP.Item(j - </font><font color="#008000">1</font><font color="#000000">);<br/> Debug.WriteLine(</font><font color="#800000">"Values of phase variable '"</font><font color="#000000"> + VrblP.Id + </font><font color="#800000">"':"</font><font color="#000000">);<br/> Debug.Indent;<br/> </font><font color="#008080">For</font><font color="#000000"> i := </font><font color="#008000">1</font><font color="#000000"> </font><font color="#008080">To</font><font color="#000000"> T </font><font color="#008080">Do</font><font color="#000000"><br/> Val := Res.VarValues(VrblP.Id)[i - </font><font color="#008000">1</font><font color="#000000">];<br/> Debug.WriteLine(i.ToString + </font><font color="#800000">": "</font><font color="#000000"> + Val.ToString);<br/> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">For</font><font color="#000000">;<br/> Debug.Unindent;<br/> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">For</font><font color="#000000">;<br/> </font><font color="#008000">// Display progress of target function change during calculation<br/> </font><font color="#000000"> </font><font color="#008000">// of non-linear programming problem <br/> </font><font color="#000000"> Debug.WriteLine(</font><font color="#800000">"Progress of target function change during calculation"</font><font color="#000000"> +<br/> </font><font color="#800000">" of non-linear programming problem:"</font><font color="#000000">);<br/> Debug.Indent;<br/> </font><font color="#008080">For</font><font color="#000000"> i := </font><font color="#008000">0</font><font color="#000000"> </font><font color="#008080">To</font><font color="#000000"> Res.ObjValByIter.Length - </font><font color="#008000">1</font><font color="#000000"> </font><font color="#008080">Do</font><font color="#000000"><br/> Debug.WriteLine(Res.ObjValByIter[i]);<br/> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">For</font><font color="#000000">;<br/> Debug.Unindent;<br/> </font><font color="#008000">// Display progress of target function change during<br/> </font><font color="#000000"> </font><font color="#008000">// consecutive weakening of borders<br/> </font><font color="#000000"> Debug.WriteLine(</font><font color="#800000">"Progress of target function change during"</font><font color="#000000"> +<br/> </font><font color="#800000">" consecutive weakening of borders:"</font><font color="#000000">);<br/> Debug.Indent;<br/> </font><font color="#008080">For</font><font color="#000000"> i := </font><font color="#008000">0</font><font color="#000000"> </font><font color="#008080">To</font><font color="#000000"> Res.ObjValByAdjustIter.Length - </font><font color="#008000">1</font><font color="#000000"> </font><font color="#008080">Do</font><font color="#000000"><br/> Debug.WriteLine(Res.ObjValByAdjustIter[i]);<br/> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">For</font><font color="#000000">;<br/> Debug.Unindent;<br/> </font><font color="#008000">// Display optimal trajectory of target function<br/> </font><font color="#000000"> Debug.WriteLine(</font><font color="#800000">"Optimal trajectory of target function:"</font><font color="#000000">);<br/> Debug.Indent;<br/> </font><font color="#008080">For</font><font color="#000000"> i := </font><font color="#008000">0</font><font color="#000000"> </font><font color="#008080">To</font><font color="#000000"> Res.CriterionFunctionTrajectory.Length - </font><font color="#008000">1</font><font color="#000000"> </font><font color="#008080">Do</font><font color="#000000"><br/> Debug.WriteLine(Res.CriterionFunctionTrajectory[i]);<br/> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">For</font><font color="#000000">;<br/> Debug.Unindent;<br/> </font><font color="#008000">// If calculation is finished with error, then display its text<br/> </font><font color="#000000"> </font><font color="#008080">Else</font><font color="#000000"><br/> Debug.WriteLine(res.ErrorMsg);<br/> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">If</font><font color="#000000">;<br/> </font><font color="#008080">End</font><font color="#000000"> </font><font color="#008080">Sub</font><font color="#000000"> UserProc;</font>
After executing the example optimization problem parameters are set, the problem is calculated, results are displayed in the console.
See also: