OptimalValue: Double;
The OptimalValue property returns the value corresponding to optimal solution of criterion function.
The value is available only after criterion problem calculation.
To execute the example, add a link to the Cp system assembly.
Sub UserProc;
Var
TargetAdj: ICpTargetAdjustment;
T: Integer;
RetroX1, RetroU: Array Of Double;
InitApproximation, Ser: Array Of Double;
i: Integer;
VarsP: ITargetPhaseVariablesArray;
VrblP: ITargetPhaseVariable;
VarConstrs: IVarTargetConstraintsArray;
VarConstr: IVarTargetConstraint;
VarsC: ITargetControlVariablesArray;
VrblC: ITargetControlVariable;
Constraints: ITargetConstraints;
Constraint: ITargetConstraint;
ConInfoArray: ITargetConstraintInfoArray;
ConInfo: ITargetConstraintInfo;
Res: ITargetResults;
Begin
TargetAdj := New TargetAdjustment.Create;
// Set period
T := 6;
// Create variables with retrospective
RetroX1 := New Double[T];
RetroU := New Double[T];
Ser := 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;
RetroU[i] := 0.9 + 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 * u[t-1] * x1[t-1] *u[t]";
// 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;
// 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] + u[t-1] - u[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 := "u[t] + x1[t]*0.001";
ConInfoArray := Constraint.Constraints;
For i := 0 To T - 1 Do
conInfo := ConInfoArray.Add;
conInfo.TimeMoment := i;
conInfo.LowerBound := -1.5555-i;
coninfo.UpperBound := 1.55555 + i;
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;
// 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 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;
// Display non-linear constraints
Debug.WriteLine("Value corresponding to optimal solution");
Debug.Indent;
For i := 0 To ConInfoArray.Count - 1 Do
conInfo := ConInfoArray.Item(i);
If conInfo.Include Then
Debug.WriteLine(conInfo.OptimalValue);
End If;
End For;
Debug.Unindent;
// Display values of lower constraint limit
Debug.WriteLine("Values of lower limit; Status");
Debug.Indent;
For i := 0 To ConInfoArray.Count - 1 Do
conInfo := ConInfoArray.Item(i);
If conInfo.Include Then
Debug.Write(conInfo.LowerBound.ToString + "; " + #9);
Debug.WriteLine(StatusToStr(conInfo.LowerConstraintStatus));
End If;
End For;
Debug.Unindent;
Debug.WriteLine("Values of Lagrange multiplier for lower limit");
Debug.Indent;
For i := 0 To ConInfoArray.Count - 1 Do
conInfo := ConInfoArray.Item(i);
If conInfo.Include Then
Debug.WriteLine(conInfo.LowerBoundLagrangeMultiplier);
End If;
End For;
Debug.Unindent;
Debug.WriteLine("Values of upper limit; Status");
Debug.Indent;
For i := 0 To ConInfoArray.Count - 1 Do
conInfo := ConInfoArray.Item(i);
If conInfo.Include Then
Debug.Write(conInfo.UpperBound.ToString + "; " + #9);
Debug.WriteLine(StatusToStr(conInfo.UpperConstraintStatus));
End If;
End For;
Debug.Unindent;
Debug.WriteLine("Values of Lagrange multiplier for upper limit");
Debug.Indent;
For i := 0 To ConInfoArray.Count - 1 Do
conInfo := ConInfoArray.Item(i);
If conInfo.Include Then
Debug.WriteLine(conInfo.UpperBoundLagrangeMultiplier);
End If;
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;
After executing the example optimization problem parameters are set, the problem is calculated, results are displayed in the console.
See also: