Originally posted by: EdKlotz
>
> Thank you Eumpfenbach for your suggestion. But there is nothing on the right of the typing box.
> Let me just try to repost everything again in here:
>
> Sorry, everyone! I have to
> Dear all,
> I am using CPLEX 12.0 Concert/C++ on Win 7 to solve Qudratic Assignment Problem, which has been transformed into a pure IP model in my case.
>
> Since I want to reduce the running time so I want to put two expression objects inside one block of 4-nested-for-loop structure. But I kept get infeasible msg after I ran .exe file at the end. Here are the codes:
>
> int tIndx = 0;
> int countT = 0;
> // add modified Transformed constrains after getting first feasible solution
> for (int i = 0; i < h; i++) {
> for (int j = i+1; j < h; j++) {
> for (int s = 0; s < l; s++) {
> for (int t = s + 1; t < l; t++) {
> if ( s >= m[i] - x_DQ*l/h && s <= m[i] + x_DQ*l/h && t >= m[j] - x_DQ*l/h && t <= m[j] + x_DQ*l/h) {
> //first expression
> IloExpr exprT(env);
> exprT = Z
tIndx - X[i][s] - X[j][t]; // Z and X are both IloBoolVar
> mod.add( exprT >= -1);
> exprT.end();
> //another expression
> IloExpr exprImP(env);
> exprImP = X[i][s] + X[j][t];
> mod.add( exprImP <= 1);
> exprImP.end();
> tIndx++;
> }
> }
> }
> }
> }
>
> Actually, I can split the above code into two similar 4-nested-for-loop piece of code, which worked as expected, but as I said,I wanted to do the above way because I can save half running time for 4-nested-for-loop
> Note that I defined a 1-d IloBoolArray for Z variable with a fixed size("numValid_Z") determined in in the following code already.
>
> IloInt numValid_Z=0;
> // count how many valid Z's we would define, so that we can define a 1-dim array for
> //IloBoolArray Z(env);
> for(int i=0; i<h; i++) {
> for(int j=i+1; j<h; j++) {
> for(int s=0; s<l; s++) {
> for(int t=s+1; t<l; t++) {
> if (s >= m[i] - x_DQ*l/h && s <= m[i] + x_DQ*l/h && t>= m[j] - x_DQ*l/h && t <= m[j] + x_DQ*l/h){
> //ZnumValid_Z = IloBoolVar(env);
> numValid_Z++;
> }
> }
> }
> }
> }
>
> // instead of defining a 4-dim array for Z's, we define only a 1-dim array for Z with size of total # possible Z's
>
> IloArray<IloBoolVar> Z(env, numValid_Z);
> for (int i = 0; i < numValid_Z; i++)
> { Z[i] = IloBoolVar(env);
> }
Regarding the infeasibility error message when you try to solve the model, that suggests that your code is generating constraints different from the ones you have intended. Try calling IloCplex::exportModel to write out a SAV file of the model. Read it into interactive CPLEX and solve it to the point of infeasibility. Then run CPLEX's conflict refiner to examine the cause of the infeasibility. That will give you a minimal subset of constraints causing the infeasibility. By examining those constraints, you hopefully will be able to determine why the constraints are not what you expected, and then correct your
code.
Regarding performance, I don't think you need to create and destroy those IloExprs with each constraint. Instead of
> IloExpr exprT(env);
> exprT = Z
tIndx - X[i][s] - X[j][t]; // Z and X are both IloBoolVar
> mod.add( exprT >= -1);
> exprT.end();
Why not do something replace those 4 lines with just
mod.add(Z[tIndx] - X[i][s] - X[j][t] >= -1);
Furthermore, you might get some additional improvement my creating an IloRangeArray or IloConstraintArray that builds up these constraints, then
add that range array to the model, e.g.
IloRangeArray rng(env);
for(int i=0; i<h; i++) {
for(int j=i+1; j<h; j++) {
for(int s=0; s<l; s++) {
for(int t=s+1; t<l; t++) {
...
rng.add((Z[tIndx] - X[i][s] - X[j][t] >= -1);
)
...
}
}
}
} // end depth 4 nested 4 loop
mod.add (rng);
This might help as well.
#CPLEXOptimizers#DecisionOptimization