OptimalValue: Double;
The OptimalValue property returns optimal value of criterion function.
The property is read-only.
Add a link to the Cp system assembly.
Sub UserProc;
Var
ZCU: LinearOptimization;
VarS: ILoVariables;
Vrbl: ILoVariable;
ConstrS: ILoConstraints;
Constr: ILoConstraint;
EqS: ILoEquations;
Eq: ILoEquation;
AddendS: ILoAddends; // List of addends
Addend: ILoAddend; // Addend
ObjFun: ILoCriterionFunction; // Criterion function
Res: ILoResults;
Retro: Array[1] Of Double;
I, J: Integer;
s: String;
Val: Double;
ci: ICultureInfo;
TimeInterval: Integer;
Begin
ZCU := New LinearOptimization.Create;
// Variables
Retro := New Double[1];
Retro[0] := 1;
// List of variables
Vars := ZCU.Variables;
For I := 1 To 6 Do
s := "x" + i.ToString;
Vrbl := Vars.Add(s);
Vrbl.Retrospective := Retro;
End For;
//List of constraints
ConstrS := ZCU.Constraints;
//====x1
For i := 1 To 6 Do
Constr := ConstrS.Add;
Constr.LowerBound := 0;
Constr.UpperBound := 1e20;
AddendS := Constr.Addends;
Addend := AddendS.Add(Vars.FindById("x1"));
Addend.Coeff := 1; //variable coefficient
Addend.Lag := i; //lag
End For;
Constr := ConstrS.Add;
Constr.LowerBound := 2;
Constr.UpperBound := 1e20;
AddendS := Constr.Addends;
Addend := AddendS.Add(Vars.FindById("x1"));
Addend.Coeff := 1; //variable coefficient
Addend.Lag := 7; //lag
//====x2
For i := 1 To 6 Do
Constr := ConstrS.Add;
Constr.LowerBound := 0;
Constr.UpperBound := 1e20;
AddendS := Constr.Addends;
Addend := AddendS.Add(Vars.FindById("x2"));
Addend.Coeff := 1; //variable coefficient
Addend.Lag := i; //lag
End For;
Constr := ConstrS.Add;
Constr.LowerBound := 2;
Constr.UpperBound := 1e20;
AddendS := Constr.Addends;
Addend := AddendS.Add(Vars.FindById("x2"));
Addend.Coeff := 1; //variable coefficient
Addend.Lag := 7; //lag
//====x3
For i := 1 To 6 Do
Constr := ConstrS.Add;
Constr.LowerBound := 0;
Constr.UpperBound := 1e20;
AddendS := Constr.Addends;
Addend := AddendS.Add(Vars.FindById("x3"));
Addend.Coeff := 1; //variable coefficient
Addend.Lag := i; //lag
End For;
Constr := ConstrS.Add;
Constr.LowerBound := 2;
Constr.UpperBound := 1e20;
AddendS := Constr.Addends;
Addend := AddendS.Add(Vars.FindById("x3"));
Addend.Coeff := 1; //variable coefficient
Addend.Lag := 7; //lag
//====x4
For i := 1 To 7 Do
Constr := ConstrS.Add;
Constr.LowerBound := 0;
Constr.UpperBound := 1e20;
AddendS := Constr.Addends;
Addend := AddendS.Add(Vars.FindById("x4"));
Addend.Coeff := 1; //variable coefficient
Addend.Lag := i; //lag
End For;
//====x5
For i := 1 To 7 Do
Constr := ConstrS.Add;
Constr.LowerBound := 0;
Constr.UpperBound := 1e20;
AddendS := Constr.Addends;
Addend := AddendS.Add(Vars.FindById("x5"));
Addend.Coeff := 1; //variable coefficient
Addend.Lag := i; //lag
End For;
//====x6
For i := 1 To 7 Do
Constr := ConstrS.Add;
Constr.LowerBound := 0;
Constr.UpperBound := 1e20;
AddendS := Constr.Addends;
Addend := AddendS.Add(Vars.FindById("x6"));
Addend.Coeff := 1; //variable coefficient
Addend.Lag := i; //lag
End For;
// List of equations
EqS := ZCU.Equations;
// Equation 1
Eq := EqS.Add;
AddendS := Eq.Addends;
Addend := Addends.Add(Vars.FindById("x1")); //left part
Addend.Coeff := 1; //variable coefficient
Addend.Lag := 0; //lag
Addend := Addends.Add(Vars.FindById("x1")); //1st addend
Addend.Coeff := 0.5; //variable coefficient
Addend.Lag := 1; //lag
Addend := Addends.Add(Vars.FindById("x2")); //2nd addend
Addend.Coeff := -0.3; //variable coefficient
Addend.Lag := 1; //lag
Addend := Addends.Add(Vars.FindById("x3")); //3rd addend
Addend.Coeff := 0.1; //variable coefficient
Addend.Lag := 1; //lag
Addend := Addends.Add(Vars.FindById("x4")); //4th addend
Addend.Coeff := 1; //variable coefficient
Addend.Lag := 1; //lag
// Equation 2
Eq := EqS.Add;
AddendS := Eq.Addends;
Addend := Addends.Add(Vars.FindById("x2")); //left part
Addend.Coeff := 1; //variable coefficient
Addend.Lag := 0; //lag
Addend := Addends.Add(Vars.FindById("x1")); //1st addend
Addend.Coeff := -0.2; //variable coefficient
Addend.Lag := 1; //lag
Addend := Addends.Add(Vars.FindById("x2")); //2nd addend
Addend.Coeff := 0.1; //variable coefficient
Addend.Lag := 1; //lag
Addend := Addends.Add(Vars.FindById("x5")); //3rd addend
Addend.Coeff := 1; //0.1;//variable coefficient
Addend.Lag := 1; //lag
// Equation 3
Eq := EqS.Add;
AddendS := Eq.Addends;
Addend := Addends.Add(Vars.FindById("x3")); //left part
Addend.Coeff := 1; //variable coefficient
Addend.Lag := 0; //lag
Addend := Addends.Add(Vars.FindById("x3")); //1st addend
Addend.Coeff := 0.1; //variable coefficient
Addend.Lag := 1; //lag
Addend := Addends.Add(Vars.FindById("x6")); //2nd addend
Addend.Coeff := 1; //variable coefficient
Addend.Lag := 1; //lag
// Criterion function
ObjFun := ZCU.CriterionFunction;
//=== x4
For i := 1 To 7 Do
Addend := ObjFun.Addends.Add(Vars.FindById("x4"));
Addend.Coeff := 1;
Addend.Lag := 7 - i;
End For;
//=== x5
For i := 1 To 7 Do
Addend := ObjFun.Addends.Add(Vars.FindById("x5"));
Addend.Coeff := 1;
Addend.Lag := 7 - i;
End For;
//=== x6
For i := 1 To 7 Do
Addend := ObjFun.Addends.Add(Vars.FindById("x6"));
Addend.Coeff := 1;
Addend.Lag := 7 - i;
End For;
TimeInterval := 7;
ZCU.Extremum := ExtremumType.Minimum;
Res := ZCU.Evaluate(TimeInterval) As ILoResults;
s := "Optimal value: " + Res.OptimalValue.ToString;
Debug.WriteLine(s);
s := Res.ErrorMsg + " (Code: " + Res.Status.ToString + ")";
Debug.WriteLine(s);
s := " ";
For j := 1 To Vars.Count Do
Vrbl := Vars.Item(j - 1);
s := s + Vrbl.Id + ", ";
End For;
Debug.WriteLine(s);
ci := CultureInfo.Current;
For i := 1 To TimeInterval Do
s := "t=" + i.ToString + ", ";
For j := 1 To Vars.Count Do
Vrbl := Vars.Item(j - 1);
Val := Res.VarValues(Vrbl.Id)[i - 1];
s := s + ci.FormatDoublePrec(Val, 6) + ", ";
End For;
Debug.WriteLine(s);
End For;
End Sub UserProc;
After executing the example the console window displays
calculation results.
See also: