CalcWithNonLinConstraints: Boolean;
The CalcWithNonLinConstraints property determines whether to use non-linear constraints in calculation.
Available values:
True. Default value. The calculation uses non-linear constraints set by the ICpTargetAdjustment.Constraints property.
False. Non-linear constraints are not used in the calculation.
Add a link to the Cp system assembly.
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;
Ser: Array[4] Of Double;
Res: ITargetResults;
Val: Double;
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];
// 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;
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 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 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 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;
// 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]";
// Set number of iterations
TargetAdj.MaxIterationsCount := 25000;
// Set accuracy of solution
TargetAdj.Tolerance := 0.00001;
// Specify that do not use non-linear constraints in calculation
TargetAdj.CalcWithNonLinConstraints := False;
// 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;
// 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 values of phase variables
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 optimal trajectory of criterion function
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: