Constraints: ILoConstraints;
The Constraints property returns linear optimization constraints.
The property is read-only.
Add a link to the Cp system assembly.
Sub UserProc;
Var
ZCU: LinearOptimization;
Vrbl: ILoVariable;
VarS: ILoVariables;
ConstrS: ILoConstraints;
Constr: ILoConstraint;
AddendS: ILoAddends;
Addend: ILoAddend;
EqS: ILoEquations;
Eq: ILoEquation;
ObjFun: ILoCriterionFunction;
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"));
//variable coefficient
Addend.Coeff := 1;
//lag
Addend.Lag := i;
End For;
Constr := ConstrS.Add;
Constr.LowerBound := 2;
Constr.UpperBound := 1e20;
AddendS := Constr.Addends;
Addend := AddendS.Add(Vars.FindById("x1"));
//variable coefficent
Addend.Coeff := 1;
//lag
Addend.Lag := 7;
//====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"));
//variable coefficient
Addend.Coeff := 1;
//lag
Addend.Lag := i;
End For;
Constr := ConstrS.Add;
Constr.LowerBound := 2;
Constr.UpperBound := 1e20;
AddendS := Constr.Addends;
Addend := AddendS.Add(Vars.FindById("x2"));
//variable coefficient
Addend.Coeff := 1;
//lag
Addend.Lag := 7;
//====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"));
//variable coefficient
Addend.Coeff := 1;
//lag
Addend.Lag := i;
End For;
Constr := ConstrS.Add;
Constr.LowerBound := 2;
Constr.UpperBound := 1e20;
AddendS := Constr.Addends;
Addend := AddendS.Add(Vars.FindById("x3"));
//variable coefficient
Addend.Coeff := 1;
//lag
Addend.Lag := 7;
//====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"));
//variable coefficient
Addend.Coeff := 1;
//lag
Addend.Lag := i;
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"));
//variable coefficient
Addend.Coeff := 1;
//lag
Addend.Lag := i;
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"));
//variable coefficient
Addend.Coeff := 1;
//lag
Addend.Lag := i;
End For;
// LIST OF EQUATIONS
EqS := ZCU.Equations;
// Equation 1
Eq := EqS.Add;
AddendS := Eq.Addends;
//left part
Addend := Addends.Add(Vars.FindById("x1"));
//variable coefficient
Addend.Coeff := 1;
//lag
Addend.Lag := 0;
//1st addend
Addend := Addends.Add(Vars.FindById("x1"));
//variable coefficient
Addend.Coeff := 0.5;
//lag
Addend.Lag := 1;
//2nd addend
Addend := Addends.Add(Vars.FindById("x2"));
//variable coefficient
Addend.Coeff := -0.3;
//lag
Addend.Lag := 1;
//3rd addend
Addend := Addends.Add(Vars.FindById("x3"));
//variable coefficient
Addend.Coeff := 0.1;
//lag
Addend.Lag := 1;
//4th addend
Addend := Addends.Add(Vars.FindById("x4"));
//variable coefficient
Addend.Coeff := 1;
//lag
Addend.Lag := 1;
// Equation 2
Eq := EqS.Add;
AddendS := Eq.Addends;
//left part
Addend := Addends.Add(Vars.FindById("x2"));
//variable coefficient
Addend.Coeff := 1;
//lag
Addend.Lag := 0;
//1st addend
Addend := Addends.Add(Vars.FindById("x1"));
//variable coefficient
Addend.Coeff := -0.2;
//lag
Addend.Lag := 1;
//2nd addend
Addend := Addends.Add(Vars.FindById("x2"));
//variable coefficient
Addend.Coeff := 0.1;
//lag
Addend.Lag := 1;
//3rd addend
Addend := Addends.Add(Vars.FindById("x5"));
//variable coefficient
Addend.Coeff := 1;
//lag
Addend.Lag := 1;
// Equation 3
Eq := EqS.Add;
AddendS := Eq.Addends;
//left part
Addend := Addends.Add(Vars.FindById("x3"));
//variable coefficient
Addend.Coeff := 1;
//lag
Addend.Lag := 0;
//1st addend
Addend := Addends.Add(Vars.FindById("x3"));
//variable coefficient
Addend.Coeff := 0.1;
//lag
Addend.Lag := 1;
//2nd addend
Addend := Addends.Add(Vars.FindById("x6"));
//variable coefficient
Addend.Coeff := 1;
//lag
Addend.Lag := 1;
// 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: