Include: Boolean;
The Include property determines whether constraint is active.
Available values:
True. Default value. Constraint is active.
False. Constraint is not used.
To execute the example, add a link to the Cp system assembly.
Sub UserProc;
Var
TargetAdj: ICpTargetAdjustment;
T: Integer;
RetroX1, RetroX2, RetroU, RetroV: Array Of Double;
InitApproximation, Ser: Array Of Double;
i, j: Integer;
VarsP: ITargetPhaseVariablesArray;
VrblP: ITargetPhaseVariable;
VarConstrs: IVarTargetConstraintsArray;
VarConstr: IVarTargetConstraint;
VarsC: ITargetControlVariablesArray;
VrblC: ITargetControlVariable;
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];
Ser := New Double[T];
// Create an array of initial approximations
InitApproximation := New Double[T];
// Set initial approximations and variable values 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;
// 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 := -0.5- i / 100;
VarConstr.UpperBound := 0.5 + i / 100;
If (i = 2) Then
VarConstr.LowerBoundFixed := False;
End If;
// Specify the current moment of time
VarConstr.TimeMoment := i;
End For;
// Set frequency equation
VrblP.FunctionExpression := "0.3 * x1[t-1] + 0.1 * u[t-1] * x1[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 - i / 10;
VarConstr.UpperBound := 1 + i / 10;
If (i = 0) Then
VarConstr.UpperBoundFixed := False;
End If;
// Set the current moment of time
VarConstr.TimeMoment := i;
End For;
// Set criterion trajectory
For i := 0 To T - 1 Do
ser[i] := i;
End For;
TargetAdj.TargetTrajectory := Ser;
// Set criterion function
TargetAdj.CriterionFunction := "x1[t] + x1[t-1] / u[t]";
// Set number of iterations
TargetAdj.MaxIterationsCount := 25000;
// Set accuracy of solution
TargetAdj.Tolerance := 0.00001;
// 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("");
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;
// Display controlling variable constraints
Debug.WriteLine("");
Debug.WriteLine("Controlling variable constraints '" + VrblC.Id + "':");
Debug.Indent;
VarConstrs := VrblC.Constraints;
WriteConstraint(VarConstrs);
Debug.Unindent; Debug.Unindent;
End For;
// Display values of phase variables
For j := 1 To VarsP.Count Do
VrblP := VarsP.Item(j - 1);
Debug.WriteLine("");
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;
// Display phase variable constraints
Debug.WriteLine("");
Debug.WriteLine("Phase variable constraints '" + VrblP.Id + "':");
Debug.Indent;
VarConstrs := VrblP.Constraints;
WriteConstraint(VarConstrs);
Debug.Unindent;
End For;
// Display optimal trajectory of criterion function
Debug.WriteLine("");
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;
// Function for displaying status
Function StatusToStr(Status: TargetConstraintStatusType): String;
Var
s: String;
Begin
Select Case Status
Case TargetConstraintStatusType.Disabled: s := "Disabled";
Case TargetConstraintStatusType.NotReached: s := "Not reached";
Case TargetConstraintStatusType.Reached: s := "Reached";
End Select;
Return s;
End Function StatusToStr;
// Procedure for displaying variable constraints limits
Sub WriteConstraint(VarConstrs: IVarTargetConstraintsArray);
Var
i: Integer;
VarConstr: IVarTargetConstraint;
Begin
// Display values of lower constraint limit
Debug.WriteLine("Values of lower limit; Status");
Debug.Indent;
For i := 0 To VarConstrs.Count - 1 Do
VarConstr := VarConstrs.Item(i);
If VarConstr.Include Then
Debug.Write(VarConstr.LowerBound.ToString + "; " + #9);
Debug.WriteLine(StatusToStr(VarConstr.LowerConstraintStatus));
End If;
End For;
Debug.Unindent;
Debug.WriteLine("Values of Lagrange multiplier for lower limit");
Debug.Indent;
For i := 0 To VarConstrs.Count - 1 Do
VarConstr := VarConstrs.Item(i);
If VarConstr.Include Then
Debug.WriteLine(VarConstr.LowerBoundLagrangeMultiplier);
End If;
End For;
Debug.Unindent;
Debug.WriteLine("Values of upper limit; Status");
Debug.Indent;
For i := 0 To VarConstrs.Count - 1 Do
VarConstr := VarConstrs.Item(i);
If VarConstr.Include Then
Debug.Write(VarConstr.UpperBound.ToString + "; " + #9);
Debug.WriteLine(StatusToStr(VarConstr.UpperConstraintStatus));
End If;
End For;
Debug.Unindent;
Debug.WriteLine("Values of Lagrange multiplier for upper limit");
Debug.Indent;
For i := 0 To VarConstrs.Count - 1 Do
VarConstr := VarConstrs.Item(i);
If VarConstr.Include Then
Debug.WriteLine(VarConstr.UpperBoundLagrangeMultiplier);
End If;
End For;
Debug.Unindent;
End Sub WriteConstraint;
After executing the example optimization problem parameters are set, the problem is calculated, results are displayed in the console.
See also: