Why my work in ILOG CPLEX have no solution? - c++

i'm working on a production mix problem in ILOG CPLEX. But there are some problems that occur. My work is about sawmill production mix (production planning for lumber products that transform from log). I tried to solve my problem using 2 file .dat, one using dummy data and one using real data.
Data processing with dummy data show the solution, but when i'm using real data, result show no solution. So i've tried to change each of my dummy data with my real data (one by one to see what data that caused the problem). And it turned out the data are "LumberRecFac" and "DemandMaks". Then, i tried to change that two data with a several random numbers. And it shows that the maximum data for "LumberRecFac" is 0.5 and the maximum data for "DemandMaks" is 10.9.
Is there something that i missed/wrong in my .mod and .dat? Because i have to run my ILOG CPLEX .mod using my real data.
Here my .mod
//product mix
//i= (RST)
//d,l (Log)
//p (Cutting_Pattern)
int i=...;
int d=...;
int l=...;
int p=...;
range RST= 1..i; //Lumber Product(Raw Sawn Timber)
range Diameter_Log=1..d; //Diameter Log
range Panjang_Log=1..l; //Length log
range Cutting_Pattern=1..p; //Cutting Pattern
//set tuple
tuple DMdl{ //diameter and length
int d;
int l;
}
tuple DMdlp{ //diameter, length, and cutting pattern
int d;
int l;
int p;
}
tuple Cdpi{ //conversion factor (from log to lumber products)
int d;
int p;
int i;
}
//set
setof (DMdl) Log={<d,l> | d in Diameter_Log, l in Panjang_Log};
setof (DMdlp) Log_LCR={<d,l,p> | d in Diameter_Log, l in Panjang_Log, p in Cutting_Pattern};
setof (Cdpi) KonversiLog_LCR={<d,p,i> | d in Diameter_Log, p in Cutting_Pattern, i in RST};
//import data
int HargaJual[RST]=...; //Selling price of RST
int BiayaLogBaru[Log]=...; //Cost of new log
int BiayaLogLama[Log]=...; //Cost of old log
int BiayaProduksiLog=...; //Production cost
int BiayaSetup=...; //Setup cost
float LumberRecFac[KonversiLog_LCR]=...; //Conversion Factor (Lumber Recovery Ratio)
float DemandMaks[RST]=...; //Demand
int BM=...; //Big Constant (Big M)
int Tmax=...; //Max Production Time
int ProcessTime[Cutting_Pattern]=...; //Process time for one log
float SetupTime[Cutting_Pattern]=...; //Setup time
float TotalLogLamaSimpan[Log]=...; //Quantity of old log
//decision variable
dvar float+ Pi[RST]; //Quantity of lumber products
dvar float+ VF[RST]; //Quantity of selling lumber products
dvar boolean CPp[Cutting_Pattern];
dvar float TotalLogBaruBeli[Log]; //Quantity of new lod that have to buy
dvar float+ LogProduksi[Log_LCR]; //Total quantity of log to produce
dvar float+ LogBaruDipakai[Log]; //Total quantity of new log that processed
dvar float+ LogLamaDipakai[Log]; //Total quantity of old log that processed
dvar float+ TotalLogSimpan[Log]; //Total inventor of new log
//objective function
dexpr float sales=sum(i in RST) VF[i]*HargaJual[i];
dexpr float purchased=sum(d in Diameter_Log, l in Panjang_Log) (TotalLogBaruBeli[<d,l>]*BiayaLogBaru[<d,l>])+(LogLamaDipakai[<d,l>]*BiayaLogLama[<d,l>]);
dexpr float production=sum(d in Diameter_Log, l in Panjang_Log) LogProduksi[<d,l,p>]*BiayaProduksiLog;
dexpr float setup=sum(p in Cutting_Pattern) BiayaSetup*CPp[p];
maximize sales-(purchased+production+setup);
//constraint
subject to{
//constraint 1 : raw material inventory
forall (d in Diameter_Log, l in Panjang_Log)
TotalLogBaruBeli[<d,l>]==LogBaruDipakai[<d,l>]+TotalLogSimpan[<d,l>];
forall (d in Diameter_Log, l in Panjang_Log)
TotalLogLamaSimpan[<d,l>]>=LogLamaDipakai[<d,l>];
//constraint 2 : log processing
forall (l in Panjang_Log, i in RST)
sum(d in Diameter_Log, p in Cutting_Pattern) LogProduksi[<d,l,p>]*LumberRecFac[<d,p,i>]==Pi[i];
forall (d in Diameter_Log, l in Panjang_Log)
sum(p in Cutting_Pattern )LogProduksi[<d,l,p>]==LogLamaDipakai[<d,l>]+LogBaruDipakai[<d,l>];
forall (p in Cutting_Pattern)
sum(d in Diameter_Log, l in Panjang_Log) LogProduksi[<d,l,p>]<=BM*CPp[p];
forall (p in Cutting_Pattern)
sum(d in Diameter_Log, l in Panjang_Log) (LogProduksi[<d,l,p>]*ProcessTime[p])+(SetupTime[p]*CPp[p])<=Tmax;
//constraint 3 : production management and demand satisfaction
forall (i in RST)
Pi[i]>=VF[i];
forall (i in RST)
VF[i]==DemandMaks[i];
}
Here my dummy data
i = 3;
d = 2;
l = 2;
p = 1;
HargaJual= [1500 1400 1600];
BiayaLogLama=[100 250
200 400];
BiayaLogBaru=[150 250
300 450];
BiayaProduksiLog= 400;
BiayaSetup= 5;
LumberRecFac= [0.5 0.5 0.5
0.5 0.5 0.5];
DemandMaks= [9.8 10.9 10.8];
BM= 10000;
Tmax= 48;
ProcessTime= [1];
SetupTime= [0.1];
TotalLogLamaSimpan=[2 1
1 2];
Here my real data
i = 10;
d = 2;
l = 5;
p = 1;
HargaJual= [3114984
43347890
22956482
7775850
15380010
16984110
8703344
3500008
3288741
2525224
];
BiayaLogLama=[2328042 2834346 3035619 3044953 3199186
4446927 4872151 4924974 6006590 6637329];
BiayaLogBaru=[2360389 3291645 3212665 3231400 3453636
4456572 4884165 5057298 6261820 6733965];
BiayaProduksiLog= 17435453;
BiayaSetup= 5;
LumberRecFac= [0.0127 0.0145 0.0982 0.1353 0.4127 0.0001 0.2800 0.0107 0.1348 0.0166
0.6842 0.2105 0.0643 0.2359 0.4730 0.0497 0.3728 0.0187 0.2393 0.0434];
DemandMaks= [188.110
75.259
121.711
253.759
1206.444
5.248
180.735
0
164.510
0];
BM= 1000000;
Tmax= 480;
ProcessTime= [1];
SetupTime= [0.1];
TotalLogLamaSimpan=[0.030 2.770
3.776 5.530
2.993 3.880
1.678 2.970
1.588 0.140];

your model is not feasible. If you label your constraints then CPLEX will give you some relaxation and conflicts that will help you understand why.
For instance:
forall (d in Diameter_Log, l in Panjang_Log)
ct1:TotalLogLamaSimpan[<d,l>]>=LogLamaDipakai[<d,l>];
//constraint 2 : log processing
forall (l in Panjang_Log, i in RST)
ct2:sum(d in Diameter_Log, p in Cutting_Pattern) LogProduksi[<d,l,p>]*LumberRecFac[<d,p,i>]==Pi[i];
will display a conflict with ct2 and if you remove that then you get a feasible solution

Related

How to get all solutions in CPLEX to a MIP

UPDATE: Now I get the count of solutions, but when I try to get them out, it just gives me identical schedules. I have added the code suggested by Alex below. As an example, it gives me that there is 4 different solutions, but when I write the X matrices they are identical. Can anyone help me with this? I want the four different solutions.
I am making a schedule for a Single round robin tournament.
It is modelled as a MIP in CPLEX and in my solution pool there is at the moment four solutions with the same optimal objective value.
I want to get each of these four solutions, so they can be printed and examined indivudally. Is that possible?
// Create Parameters:
{string} G1 = ...; // Set of teams in first group
{string} G2 = ...; // Set of teams in second group
{string} Teams = G1 union G2;
tuple Match {string team1; string team2;}
{Match} Matches_G1 = {<t1,t2>| ordered t1,t2 in G1};
{Match} Matches_G2 = {<t1,t2>| ordered t1,t2 in G2};
{Match} MD1 = ...;
{Match} MD2 = ...;
{Match} MD3 = ...;
{Match} M = Matches_G1 union Matches_G2; //All matches for the two groups
{Match} matchForTeam[t in Teams] = {m| m in M : m.team1 == t || m.team2 == t}; //List of all teams
{string} S =...; //Set of stadiums
{string} T = ...; //Set of kick off times
{string} D = ...; //Set of kick off days
int K[D][S][T] = ...; //Predetermined schedule between stadium and kickoff time
float VT[M][T] = ...; //Value of match if played on Matchday M at Time T according to TV distribution
// Decision Variables:
dvar int X[M][S][T] in 0..1; // if match M is played at time T
dvar int Dist; //Object function for distribution
//////////// OBJECTIVE FUNCTION ///////////////
maximize
Dist;
//////////// CONSTRAINTS ///////////////
subject to{
Dist == sum(m in M, s in S, t in T) (VT[m][t])*X[m][s][t];
//A match can only be played one time
forall(m in M)
sum(s in S, t in T) X[m][s][t] == 1;
//Simultaneous Kickoff on matchday 3
sum(s in S)X[<"A1", "A4">][s]["22.00"] == sum(s in S)X[<"A2", "A3">][s]["22.00"];
//only one match on possible kick off times at matchday 1
forall(t in T : t != "18.00")
sum(s in S, m in MD1) X[m][s][t]==1;
//only one match on possible kick off times at matchday 2
forall(t in T : t != "18.00")
sum(s in S, m in MD2) X[m][s][t]==1;
//two matches per possible kick off times at matchday 3
forall(t in T : t in {"18.00", "22.00"})
sum(s in S, m in MD3) X[m][s][t]==2;
//One match per stadium on matchday 1
forall(s in S)
sum(m in MD1, t in T: t != "18.00") X[m][s][t] == 1;
//One match per stadium on matchday 2
forall(s in S)
sum(m in MD2, t in T: t != "18.00") X[m][s][t] == 1;
//one match per stadium on matchday 3
forall(s in S)
sum(m in MD3, t in T: t in {"18.00", "22.00"}) X[m][s][t] == 1;
//Each team can play at most two matches per stadium
forall(i in Teams, s in S)
sum(t in T, m in matchForTeam[i]) X[m][s][t] <= 2;
//Each team can play at most two matches per kickoff time
forall(i in Teams, t in T)
sum(s in S, m in matchForTeam[i]) X[m][s][t] <= 2;
forall(s in S, t in T, m in MD1)
X[m][s][t] <= K["1"][s][t];
forall(s in S, t in T, m in MD2)
X[m][s][t] <= K["2"][s][t];
forall(s in S, t in T, m in MD3)
X[m][s][t] <= K["3"][s][t];
}
execute{
writeln("schedule: ", X);
var cd = new IloOplOutputFile("resbi2.txt");
for(var m in M)
for(var s in S)
for(var t in T)
cd.writeln(thisOplModel.X[m][s][t]);
cd.close();
}
main{
cplex.solnpoolintensity=4;
cplex.solnpoolagap=0;
thisOplModel.generate();
cplex.solve();
if (cplex.populate()) {
var nsolns = cplex.solnPoolNsolns;
writeln("number of solutions: ", nsolns);
writeln("average object value: ", cplex.getSolnPoolMeanObjValue());
writeln();
for (var s=0; s<nsolns; s++) {
thisOplModel.setPoolSolution(s);
var cd = new IloOplOutputFile("resAB" +s+".txt");
cd.writeln(thisOplModel.X);
cd.close();
thisOplModel.postProcess();
}
}
}
yes in scripting you can loop into all solutions from a solution pool.
See https://github.com/AlexFleischerParis/zooopl/blob/master/zooseveral.mod
int nbKids=300;
float costBus40=500;
float costBus30=400;
dvar int+ nbBus40;
dvar int+ nbBus30;
//minimize
//costBus40*nbBus40 +nbBus30*costBus30;
subject to
{
40*nbBus40+nbBus30*30>=nbKids;
}
execute
{
writeln("nbBus40 = ",nbBus40," and nbBus30 = ",nbBus30," and the cost is ",costBus40*nbBus40 +nbBus30*costBus30);
}
main {
cplex.solnpoolintensity=4;
thisOplModel.generate();
cplex.solve();
if (cplex.populate()) {
var nsolns = cplex.solnPoolNsolns;
writeln("Number of solutions found = ",nsolns);
writeln();
for (var s=0; s<nsolns; s++) {
thisOplModel.setPoolSolution(s);
thisOplModel.postProcess();
}
}
}

Initializer element is not a compile-time constant when multiplying constants, C++

I have this code here
//GLOBAL
// Power Demand in St John's per year [GJ/yr]
double P_year = 14367865E9;
double s_year = 365*24*60*60;// Seconds in a year [s]
// Power Demand in St John's per second [GJ/yr]
double P_s = P_year*(1/s_year);
//electric permitivitty of free space [s4A2/m3kg]
double epsilon = 8.85418782E-12;
int main(){
// Energy Required for 6hrs demand [J]
double E = P_s * 60*60*6;
// Voltage Difference accross capacitor [V]
double V = 230E3;
// Capacitance
double C = 2*E/pow(V,2);
printf("%f %f \n", E, C);
}
I get the following error:
Capacitor.c:11:20: error: initializer element is not a compile-time constant
double P_s = P_year*(1/s_year);
I do not understand why it won't just multiply it.
Thank you guys for your help.

Formulating diagonal constraint for N Queen problem C++

I'm having a problem with the N Queens problem. I try to implement a constraint that the queens cannot be placed in the diagonals. The program is embedded in CPLEX.
Here is what I have for the column constraint, that max. one queen is placed per column:
for (rr = 0; rr < ROWS; rr++)
{
IloExpr constraint2(env);
for (rr=0;rr<ROWS;rr++)
constraint2 += x[rr][cc];
mod.add(constraint2 == 1);
constraint2.end();
}
I tried to implement this for the upper left diagonal:
for (d = rr - 1; d < (ROWS - 2); d++)
{
IloExpr constraint3(env);
for (d = cc - 1; d < (ROWS - 2); d++)
constraint3 += x[d][d];
mod.add(constraint3 >= 1);
constraint3.end();
}
But this does not work. Can somebody help me with this?
Within CPLEX you can rely on MIP.
With OPL you could write
int Dim=400;
range Bord=1..Dim;
dvar boolean x[Bord][Bord];
subject to
{
forall(i in Bord) sum(j in Bord) x[i][j]==1;
forall(i in Bord) sum(j in Bord) x[j][i]==1;
forall(k in 2..2*Dim) sum(i,j in Bord:i+j==k) x[i][j]<=1;
forall(k in 1-Dim..Dim-1) sum(i,j in Bord:i-j==k) x[i][j]<=1;
}
But with Dim=400 it takes 15 s on my laptop whereas with CPOptimizer within CPLEX , in less than 1 s
using CP;
int Dim=400;
range Bord=1..Dim;
dvar int queen[Bord] in Bord;
dvar int d1[Bord];
dvar int d2[Bord];
constraints {
forall(ind in Bord) {
d1[ind]== queen[ind]+ind;
d2[ind ]== queen[ind]-ind;
};
allDifferent(queen); // One queen max per line
allDifferent(d1); // One queen max per diagonal 1
allDifferent(d2); // One queen max per diagonal 2
};

How to add new constraints in the scheduling problem at CPLEX?

I am a beginner at CPLEX and I am struggling to add more constraints in my project. The script works well when I have multiple origin and destination, and just one product
I would like to do it with more products demand in each destination, and I do not know how to write the Constraints.
{string} Forest = {"A","B","C","D","E"};
{string} Destination = {"D1" , "D2"};
{string} Products = {"Pinus","Eucalyptus"};
float Demand [Destination][Products]= [[3,1],[4,5]];
float Distance [Forest][Destination]=[[21,52],[42,12],[25,15],[52,31],[9,42]];
float Stock [Forest][Products]= [[0.94,0],[0,8.62],[0,1.21],[2.6,0],[8.77,0]];`
//Decision Variables
dvar float+ Delivered [Forest][Destination];
//Função Objetivo
minimize
sum (u in Forest, c in Destination) Distance[u][c] * Delivered[u][c];
//Constraints
subject to {
forall (u in Forest)
sum (c in Destination)
Delivered[u][c] <= Stock [u];
forall (c in Destination)
sum (u in Forest)
Delivered[u][c] >= Demand[c];
}
I have cross-posted this question.
You shave to also expand your decision variable by Products (like you did for Demand and Stock) so that you can know how much of each product is delivered.
Then you can replicate each constraint for each product by adding a "forall (p in Products)".
dvar float+ Delivered [Forest][Destination][Products];
forall (p in Products)
forall (u in Forest)
sum (c in Destination)
Delivered[u][c][p] <= Stock[u][p];
you could try something like
{string} Forest = {"A","B","C","D","E"};
{string} Destination = {"D1" , "D2"};
{string} Products = {"Pinus","Eucalyptus"};
float Demand [Destination][Products]= [[3,1],[4,5]];
float Distance [Forest][Destination]=[[21,52],[42,12],[25,15],[52,31],[9,42]];
float Stock [Forest][Products]= [[0.94,0],[0,8.62],[0,1.21],[2.6,0],[8.77,0]];
//Decision Variables
dvar float+ Delivered [Products][Forest][Destination];
//Função Objetivo
minimize sum (p in Products,u in Forest, c in Destination) Distance[u][c] * Delivered[p][u][c];
//Constraints
subject to {
forall (u in Forest,p in Products) sum (c in Destination) Delivered[p][u][c] <= Stock [u][p];
forall (p in Products,c in Destination) sum (u in Forest) Delivered[p][u][c] >= Demand[c][p];
}

CPLEX dvar as condition

I have this problem while trying to find a solution for a resource constraint scheduling problem. Whenever I put dvar as a condition in a forall loop or if condition I have the error that states "Decision variable (or expression) "S" not allowed".
range activity = 1..16;
dvar float+ S[activity];
dvar float+ rd[jobs];
forall (i in activity)
forall (t in T:S[i]<=t<=S[i]+D[i]) //boolean b
b[i][t]==1;
forall (t in T)
forall (k in R)
sum (i in activity)b[i][t]*V[i][k]<=Rk[k];//human resources constraint
forall (j in jobs)
forall (t in T:rd[j]<=t<=S[maxact[j]])//boolean y
y[j][t]==1;
This is a very common mistake. The code you are writing is building the model for cplex. The cplex variables in your (dvars, like your S) usually do not have a value until the model has been solved, so you cannot use their value during the model building process. You need to restructure your problem and you will probably need to use additional bool or int variables or indicator constraints inside your model.
This will work:
range activity = 1..16;
dvar float+ S[activity];
range jobs=1..3;
range T=1..3;
range R=1..4;
dvar boolean b[activity][T];
dvar boolean y[jobs][T];
int D[activity];
int Rk[R]; int V[activity][R]; int maxact[j in jobs]=1;
dvar float+ rd[jobs];
subject to {
forall (i in activity)
forall (t in T) //boolean b
((S[i]<=t) &&(t<=S[i]+D[i])) => (b[i][t]==1);
forall (t in T) forall (k in R)
sum (i in activity)b[i][t]*V[i][k]<=Rk[k];//human resources constraint
forall (j in jobs) forall (t in T)//boolean y
((rd[j]<=t) &&
(t<=S[maxact[j]])) => (y[j][t]==1);
}