OptimalValue: Double;
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 this 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 restrospective
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 phase variable x1
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 controlling variable u
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 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 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, output results to console
If res.Status = 0 Then
// Output optimal value
Debug.WriteLine("Optimal value:");
Debug.Indent;
Debug.WriteLine(res.OptimalValue);
Debug.Unindent;
<font color="#008000">// Output optimal trajectory of criterion function<br/> </font> Debug.WriteLine(<font color="#800000">"Optimal trajectory of criterion function:"</font>);<br/> Debug.Indent;<br/> <font color="#008080">For</font> i := <font color="#008000">0</font> <font color="#008080">To</font> Res.CriterionFunctionTrajectory.Length - <font color="#008000">1</font> <font color="#008080">Do</font><br/> Debug.WriteLine(Res.CriterionFunctionTrajectory[i]);<br/> <font color="#008080">End</font> <font color="#008080">For</font>;<br/> Debug.Unindent;<br/> <font color="#008000">// Output non-linear constraints<br/> </font> Debug.WriteLine(<font color="#800000">"Value corresponding to optimal solution"</font>);<br/> Debug.Indent;<br/> <font color="#008080">For</font> i := <font color="#008000">0</font> <font color="#008080">To</font> ConInfoArray.Count - <font color="#008000">1</font> <font color="#008080">Do</font><br/> conInfo := ConInfoArray.Item(i);<br/> <font color="#008080">If</font> conInfo.Include <font color="#008080">Then</font><br/> Debug.WriteLine(conInfo.OptimalValue);<br/> <font color="#008080">End</font> <font color="#008080">If</font>;<br/> <font color="#008080">End</font> <font color="#008080">For</font>;<br/> Debug.Unindent;<br/> <font color="#008000">// Output values of lower constraint limit<br/> </font> Debug.WriteLine(<font color="#800000">"Lower limit values; Status"</font>);<br/> Debug.Indent;<br/> <font color="#008080">For</font> i := <font color="#008000">0</font> <font color="#008080">To</font> ConInfoArray.Count - <font color="#008000">1</font> <font color="#008080">Do</font><br/> conInfo := ConInfoArray.Item(i);<br/> <font color="#008080">If</font> conInfo.Include <font color="#008080">Then</font><br/> Debug.Write(conInfo.LowerBound.ToString + <font color="#800000">"; "</font> + #9);<br/> Debug.WriteLine(StatusToStr(conInfo.LowerConstraintStatus));<br/> <font color="#008080">End</font> <font color="#008080">If</font>;<br/> <font color="#008080">End</font> <font color="#008080">For</font>;<br/> Debug.Unindent;<br/> Debug.WriteLine(<font color="#800000">"Lagrange multiplier values for lower border"</font>);<br/> Debug.Indent;<br/> <font color="#008080">For</font> i := <font color="#008000">0</font> <font color="#008080">To</font> ConInfoArray.Count - <font color="#008000">1</font> <font color="#008080">Do</font><br/> conInfo := ConInfoArray.Item(i);<br/> <font color="#008080">If</font> conInfo.Include <font color="#008080">Then</font><br/> Debug.WriteLine(conInfo.LowerBoundLagrangeMultiplier);<br/> <font color="#008080">End</font> <font color="#008080">If</font>;<br/> <font color="#008080">End</font> <font color="#008080">For</font>;<br/> Debug.Unindent;<br/> Debug.WriteLine(<font color="#800000">"Upper limit values; Status"</font>);<br/> Debug.Indent;<br/> <font color="#008080">For</font> i := <font color="#008000">0</font> <font color="#008080">To</font> ConInfoArray.Count - <font color="#008000">1</font> <font color="#008080">Do</font><br/> conInfo := ConInfoArray.Item(i);<br/> <font color="#008080">If</font> conInfo.Include <font color="#008080">Then</font><br/> Debug.Write(conInfo.UpperBound.ToString + <font color="#800000">"; "</font> + #9);<br/> Debug.WriteLine(StatusToStr(conInfo.UpperConstraintStatus));<br/> <font color="#008080">End</font> <font color="#008080">If</font>;<br/> <font color="#008080">End</font> <font color="#008080">For</font>;<br/> Debug.Unindent;<br/> Debug.WriteLine(<font color="#800000">"Lagrange multiplier values for upper limit"</font>);<br/> Debug.Indent;<br/> <font color="#008080">For</font> i := <font color="#008000">0</font> <font color="#008080">To</font> ConInfoArray.Count - <font color="#008000">1</font> <font color="#008080">Do</font><br/> conInfo := ConInfoArray.Item(i);<br/> <font color="#008080">If</font> conInfo.Include <font color="#008080">Then</font><br/> Debug.WriteLine(conInfo.UpperBoundLagrangeMultiplier);<br/> <font color="#008080">End</font> <font color="#008080">If</font>;<br/> <font color="#008080">End</font> <font color="#008080">For</font>;<br/> Debug.Unindent;<br/> <font color="#008000">// If calculation is competed with error, output its text<br/> </font> <font color="#008080">Else</font><br/> Debug.WriteLine(res.ErrorMsg);<br/> <font color="#008080">End</font> <font color="#008080">If</font>;<br/> <font color="#008080">End</font> <font color="#008080">Sub</font> UserProc;<br/> <br/> <font color="#008000">// Function for status output<br/> </font><font color="#008080">Function</font> StatusToStr(Status: TargetConstraintStatusType): String;<br/> <font color="#008080">Var</font><br/> s: String;<br/> <font color="#008080">Begin</font><br/> <font color="#008080">Select</font> <font color="#008080">Case</font> Status<br/> <font color="#008080">Case</font> TargetConstraintStatusType.Disabled: s := <font color="#800000">"Disabled"</font>;<br/> <font color="#008080">Case</font> TargetConstraintStatusType.NotReached: s := <font color="#800000">"Not reached"</font>;<br/> <font color="#008080">Case</font> TargetConstraintStatusType.Reached: s := <font color="#800000">"Reached"</font>;<br/> <font color="#008080">End</font> <font color="#008080">Select</font>;<br/> <font color="#008080">Return</font> s;<br/> <font color="#008080">End</font> <font color="#008080">Function</font> StatusToStr;
After executing the example optimization problem parameters are set, the problem is calculated, results are displayed to the console.
The requirements and result of the Fore.NET example execution match with those in the Fore example.
Imports Prognoz.Platform.Interop.Cp;
…
Public Shared Sub Main(Params: StartParams);
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 restrospective
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 phase variable x1
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 controlling variable u
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 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 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.tastMinError;
// 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, output results to console
If res.Status = 0 Then
// Output optimal value
</font> System.Diagnostics.Debug.WriteLine(<font color="#800000">"Optimal value:"</font>);<br/> System.Diagnostics.Debug.Indent();<br/> System.Diagnostics.Debug.WriteLine(res.OptimalValue);<br/> System.Diagnostics.Debug.Unindent();<br/> <font color="#008000">// Output optimal trajectory of criterion function<br/> </font> System.Diagnostics.Debug.WriteLine(<font color="#800000">"Optimal trajectory of criterion function:"</font>);<br/> System.Diagnostics.Debug.Indent();<br/> <font color="#008080">For</font> i := <font color="#008000">0</font> <font color="#008080">To</font> Res.CriterionFunctionTrajectory.Length - <font color="#008000">1</font> <font color="#008080">Do</font><br/> System.Diagnostics.Debug.WriteLine(Res.CriterionFunctionTrajectory.GetValue(i));<br/> <font color="#008080">End</font> <font color="#008080">For</font>;<br/> System.Diagnostics.Debug.Unindent();<br/> <font color="#008000">// Output non-linear constraints<br/> </font> System.Diagnostics.Debug.WriteLine(<font color="#800000">"Value corresponding to optimal solution"</font>);<br/> System.Diagnostics.Debug.Indent();<br/> <font color="#008080">For</font> i := <font color="#008000">0</font> <font color="#008080">To</font> ConInfoArray.Count - <font color="#008000">1</font> <font color="#008080">Do</font><br/> conInfo := ConInfoArray.Item[i];<br/> <font color="#008080">If</font> conInfo.Include <font color="#008080">Then</font><br/> System.Diagnostics.Debug.WriteLine(conInfo.OptimalValue);<br/> <font color="#008080">End</font> <font color="#008080">If</font>;<br/> <font color="#008080">End</font> <font color="#008080">For</font>;<br/> System.Diagnostics.Debug.Unindent();<br/> <font color="#008000">// Output values of lower constraint border<br/> </font> System.Diagnostics.Debug.WriteLine(<font color="#800000">"Lower constraint border values; Status"</font>);<br/> System.Diagnostics.Debug.Indent();<br/> <font color="#008080">For</font> i := <font color="#008000">0</font> <font color="#008080">To</font> ConInfoArray.Count - <font color="#008000">1</font> <font color="#008080">Do</font><br/> conInfo := ConInfoArray.Item[i];<br/> <font color="#008080">If</font> conInfo.Include <font color="#008080">Then</font><br/> System.Diagnostics.Debug.Write(conInfo.LowerBound.ToString() + <font color="#800000">"; "</font> + char.ConvertFromUtf32(<font color="#008000">9</font>));<br/> System.Diagnostics.Debug.WriteLine(StatusToStr(conInfo.LowerConstraintStatus));<br/> <font color="#008080">End</font> <font color="#008080">If</font>;<br/> <font color="#008080">End</font> <font color="#008080">For</font>;<br/> System.Diagnostics.Debug.Unindent();<br/> System.Diagnostics.Debug.WriteLine(<font color="#800000">"Lagrange multiplier values for lower limit"</font>);<br/> System.Diagnostics.Debug.Indent();<br/> <font color="#008080">For</font> i := <font color="#008000">0</font> <font color="#008080">To</font> ConInfoArray.Count - <font color="#008000">1</font> <font color="#008080">Do</font><br/> conInfo := ConInfoArray.Item[i];<br/> <font color="#008080">If</font> conInfo.Include <font color="#008080">Then</font><br/> System.Diagnostics.Debug.WriteLine(conInfo.LowerBoundLagrangeMultiplier);<br/> <font color="#008080">End</font> <font color="#008080">If</font>;<br/> <font color="#008080">End</font> <font color="#008080">For</font>;<br/> System.Diagnostics.Debug.Unindent();<br/> System.Diagnostics.Debug.WriteLine(<font color="#800000">"Upper limit values; Status"</font>);<br/> System.Diagnostics.Debug.Indent();<br/> <font color="#008080">For</font> i := <font color="#008000">0</font> <font color="#008080">To</font> ConInfoArray.Count - <font color="#008000">1</font> <font color="#008080">Do</font><br/> conInfo := ConInfoArray.Item[i];<br/> <font color="#008080">If</font> conInfo.Include <font color="#008080">Then</font><br/> System.Diagnostics.Debug.Write(conInfo.UpperBound.ToString() + <font color="#800000">"; "</font> + char.ConvertFromUtf32(<font color="#008000">9</font>));<br/> System.Diagnostics.Debug.WriteLine(StatusToStr(conInfo.UpperConstraintStatus));<br/> <font color="#008080">End</font> <font color="#008080">If</font>;<br/> <font color="#008080">End</font> <font color="#008080">For</font>;<br/> System.Diagnostics.Debug.Unindent();<br/> System.Diagnostics.Debug.WriteLine(<font color="#800000">"Lagrange multiplier values for upper limit"</font>);<br/> System.Diagnostics.Debug.Indent();<br/> <font color="#008080">For</font> i := <font color="#008000">0</font> <font color="#008080">To</font> ConInfoArray.Count - <font color="#008000">1</font> <font color="#008080">Do</font><br/> conInfo := ConInfoArray.Item[i];<br/> <font color="#008080">If</font> conInfo.Include <font color="#008080">Then</font><br/> System.Diagnostics.Debug.WriteLine(conInfo.UpperBoundLagrangeMultiplier);<br/> <font color="#008080">End</font> <font color="#008080">If</font>;<br/> <font color="#008080">End</font> <font color="#008080">For</font>;<br/> System.Diagnostics.Debug.Unindent();<br/> <font color="#008000">// If calculation is completed with error, output its text<br/> </font> <font color="#008080">Else</font><br/> System.Diagnostics.Debug.WriteLine(res.ErrorMsg);<br/> <font color="#008080">End</font> <font color="#008080">If</font>;<br/> <font color="#008080">End</font> <font color="#008080">Sub</font>;<br/> <br/> <font color="#008000">// Function for status output<br/> </font><font color="#008080">Public</font> <font color="#008080">Shared</font> <font color="#008080">Function</font> StatusToStr(Status: TargetConstraintStatusType): String;<br/> <font color="#008080">Var</font><br/> s: String;<br/> <font color="#008080">Begin</font><br/> <font color="#008080">Select</font> <font color="#008080">Case</font> Status<br/> <font color="#008080">Case</font> TargetConstraintStatusType.tcstDisabled: s := <font color="#800000">"Disabled"</font>;<br/> <font color="#008080">Case</font> TargetConstraintStatusType.tcstNotReached: s := <font color="#800000">"Not reached"</font>;<br/> <font color="#008080">Case</font> TargetConstraintStatusType.tcstReached: s := <font color="#800000">"Reached"</font>;<br/> <font color="#008080">End</font> <font color="#008080">Select</font>;<br/> <font color="#008080">Return</font> s;<br/> <font color="#008080">End</font> <font color="#008080">Function</font> StatusToStr;
See also: