ICpTargetAdjustment.Tolerance

Syntax

Tolerance: Double;

Description

The Tolerance property determines accuracy of solution.

Comments

The default value is 0.0001.

Example

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[
4Of 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/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008000">//&nbsp;Set&nbsp;number&nbsp;of constraints&nbsp;deleted&nbsp;during&nbsp;one&nbsp;iteration<br/> </font><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;TargetAdj.AutoAdjustRemoveCount&nbsp;:=&nbsp;</font><font color="#008000">2</font><font color="#000000">;<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008000">//&nbsp;Set&nbsp;handler&nbsp;to&nbsp;calculate&nbsp;values&nbsp;for controlling&nbsp;variables<br/> </font><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;MyCallBackC&nbsp;:=&nbsp;</font><font color="#008080">New</font><font color="#000000">&nbsp;MyCallbackCycle.Create;<br/> &nbsp;&nbsp;&nbsp;&nbsp;TargetAdj.CallbackCycle&nbsp;:=&nbsp;MyCallBackC;<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008000">//&nbsp;Calculate<br/> </font><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;Res&nbsp;:=&nbsp;TargetAdj.Evaluate(T)&nbsp;</font><font color="#008080">As</font><font color="#000000">&nbsp;ITargetResults;<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008000">//&nbsp;If&nbsp;calculation&nbsp;is without&nbsp;errors,&nbsp;then&nbsp;display&nbsp;results&nbsp;to&nbsp;console<br/> </font><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">If</font><font color="#000000">&nbsp;res.Status&nbsp;=&nbsp;</font><font color="#008000">0</font><font color="#000000">&nbsp;</font><font color="#008080">Then</font><font color="#000000"><br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008000">//&nbsp;Display&nbsp;optimal&nbsp;value<br/> </font><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;Debug.WriteLine(</font><font color="#800000">&quot;Optimal&nbsp;value:&quot;</font><font color="#000000">);<br/> &nbsp;&nbsp;&nbsp;&nbsp;Debug.Indent;<br/> &nbsp;&nbsp;&nbsp;&nbsp;Debug.WriteLine(res.OptimalValue);<br/> &nbsp;&nbsp;&nbsp;&nbsp;Debug.Unindent;<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008000">//&nbsp;Display&nbsp;values&nbsp;of controlling&nbsp;variables<br/> </font><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">For</font><font color="#000000">&nbsp;j&nbsp;:=&nbsp;</font><font color="#008000">1</font><font color="#000000">&nbsp;</font><font color="#008080">To</font><font color="#000000">&nbsp;VarsC.Count&nbsp;</font><font color="#008080">Do</font><font color="#000000"><br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VrblC&nbsp;:=&nbsp;VarsC.Item(j&nbsp;-&nbsp;</font><font color="#008000">1</font><font color="#000000">);<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.WriteLine(</font><font color="#800000">&quot;Values&nbsp;of controlling&nbsp;variable&nbsp;'&quot;</font><font color="#000000">&nbsp;+&nbsp;VrblC.Id&nbsp;+&nbsp;</font><font color="#800000">&quot;':&quot;</font><font color="#000000">);<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.Indent;<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">For</font><font color="#000000">&nbsp;i&nbsp;:=&nbsp;</font><font color="#008000">1</font><font color="#000000">&nbsp;</font><font color="#008080">To</font><font color="#000000">&nbsp;T&nbsp;</font><font color="#008080">Do</font><font color="#000000"><br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Val&nbsp;:=&nbsp;Res.VarValues(VrblC.Id)[i&nbsp;-&nbsp;</font><font color="#008000">1</font><font color="#000000">];<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.WriteLine(i.ToString&nbsp;+&nbsp;</font><font color="#800000">&quot;:&nbsp;&quot;</font><font color="#000000">&nbsp;+&nbsp;Val.ToString);<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">For</font><font color="#000000">;<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.Unindent;<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">For</font><font color="#000000">;<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008000">//&nbsp;Display&nbsp;values&nbsp;of phase&nbsp;variables<br/> </font><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">For</font><font color="#000000">&nbsp;j&nbsp;:=&nbsp;</font><font color="#008000">1</font><font color="#000000">&nbsp;</font><font color="#008080">To</font><font color="#000000">&nbsp;VarsP.Count&nbsp;</font><font color="#008080">Do</font><font color="#000000"><br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VrblP&nbsp;:=&nbsp;VarsP.Item(j&nbsp;-&nbsp;</font><font color="#008000">1</font><font color="#000000">);<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.WriteLine(</font><font color="#800000">&quot;Values&nbsp;of phase&nbsp;variable&nbsp;'&quot;</font><font color="#000000">&nbsp;+&nbsp;VrblP.Id&nbsp;+&nbsp;</font><font color="#800000">&quot;':&quot;</font><font color="#000000">);<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.Indent;<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">For</font><font color="#000000">&nbsp;i&nbsp;:=&nbsp;</font><font color="#008000">1</font><font color="#000000">&nbsp;</font><font color="#008080">To</font><font color="#000000">&nbsp;T&nbsp;</font><font color="#008080">Do</font><font color="#000000"><br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Val&nbsp;:=&nbsp;Res.VarValues(VrblP.Id)[i&nbsp;-&nbsp;</font><font color="#008000">1</font><font color="#000000">];<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.WriteLine(i.ToString&nbsp;+&nbsp;</font><font color="#800000">&quot;:&nbsp;&quot;</font><font color="#000000">&nbsp;+&nbsp;Val.ToString);<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">For</font><font color="#000000">;<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.Unindent;<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">For</font><font color="#000000">;<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008000">//&nbsp;Display&nbsp;progress&nbsp;of target&nbsp;function&nbsp;change&nbsp;during&nbsp;calculation<br/> </font><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008000">//&nbsp;of&nbsp;non-linear&nbsp;programming problem&nbsp;&nbsp;<br/> </font><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;Debug.WriteLine(</font><font color="#800000">&quot;Progress&nbsp;of target&nbsp;function&nbsp;change&nbsp;during&nbsp;calculation&quot;</font><font color="#000000">&nbsp;+<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#800000">&quot;&nbsp;of non-linear&nbsp;programming problem:&quot;</font><font color="#000000">);<br/> &nbsp;&nbsp;&nbsp;&nbsp;Debug.Indent;<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">For</font><font color="#000000">&nbsp;i&nbsp;:=&nbsp;</font><font color="#008000">0</font><font color="#000000">&nbsp;</font><font color="#008080">To</font><font color="#000000">&nbsp;Res.ObjValByIter.Length&nbsp;-&nbsp;</font><font color="#008000">1</font><font color="#000000">&nbsp;</font><font color="#008080">Do</font><font color="#000000"><br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.WriteLine(Res.ObjValByIter[i]);<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">For</font><font color="#000000">;<br/> &nbsp;&nbsp;&nbsp;&nbsp;Debug.Unindent;<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008000">//&nbsp;Display&nbsp;progress&nbsp;of target&nbsp;function&nbsp;change&nbsp;during<br/> </font><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008000">//&nbsp;consecutive&nbsp;weakening&nbsp;of borders<br/> </font><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;Debug.WriteLine(</font><font color="#800000">&quot;Progress&nbsp;of target&nbsp;function&nbsp;change&nbsp;during&quot;</font><font color="#000000">&nbsp;+<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#800000">&quot;&nbsp;consecutive&nbsp;weakening&nbsp;of borders:&quot;</font><font color="#000000">);<br/> &nbsp;&nbsp;&nbsp;&nbsp;Debug.Indent;<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">For</font><font color="#000000">&nbsp;i&nbsp;:=&nbsp;</font><font color="#008000">0</font><font color="#000000">&nbsp;</font><font color="#008080">To</font><font color="#000000">&nbsp;Res.ObjValByAdjustIter.Length&nbsp;-&nbsp;</font><font color="#008000">1</font><font color="#000000">&nbsp;</font><font color="#008080">Do</font><font color="#000000"><br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.WriteLine(Res.ObjValByAdjustIter[i]);<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">For</font><font color="#000000">;<br/> &nbsp;&nbsp;&nbsp;&nbsp;Debug.Unindent;<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008000">//&nbsp;Display&nbsp;optimal&nbsp;trajectory&nbsp;of target&nbsp;function<br/> </font><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;Debug.WriteLine(</font><font color="#800000">&quot;Optimal&nbsp;trajectory&nbsp;of target&nbsp;function:&quot;</font><font color="#000000">);<br/> &nbsp;&nbsp;&nbsp;&nbsp;Debug.Indent;<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">For</font><font color="#000000">&nbsp;i&nbsp;:=&nbsp;</font><font color="#008000">0</font><font color="#000000">&nbsp;</font><font color="#008080">To</font><font color="#000000">&nbsp;Res.CriterionFunctionTrajectory.Length&nbsp;-&nbsp;</font><font color="#008000">1</font><font color="#000000">&nbsp;</font><font color="#008080">Do</font><font color="#000000"><br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.WriteLine(Res.CriterionFunctionTrajectory[i]);<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">For</font><font color="#000000">;<br/> &nbsp;&nbsp;&nbsp;&nbsp;Debug.Unindent;<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008000">//&nbsp;If&nbsp;calculation&nbsp;is finished&nbsp;with&nbsp;error,&nbsp;then&nbsp;display&nbsp;its&nbsp;text<br/> </font><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">Else</font><font color="#000000"><br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.WriteLine(res.ErrorMsg);<br/> &nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">If</font><font color="#000000">;<br/> </font><font color="#008080">End</font><font color="#000000">&nbsp;</font><font color="#008080">Sub</font><font color="#000000">&nbsp;UserProc;</font>

After executing the example optimization problem parameters are set, the problem is calculated, results are displayed in the console.

See also:

ICpTargetAdjustment