Originally posted by: JorisK
According to the documentation (Cplex 12.8, java interface), IloLPMatrix.getRows(int start, int num, double[] lb, double[] ub, int[][] ind, double[][] val) returns rows in a sparse matrix representation. Apparently this is not always the case. Take for instance this example:
public static void main(String[] args) throws IloException {
IloCplex cplex = new IloCplex();
//PROBLEM DEFINITION
int numVars=50;
int numConstraints=6;
int objCoefficients[]={4, 100, 3, 7, 12, 3, 8, 5, 6, 10, 12, 8, 13, 8, 3, 7, 5, 10, 6, 5, 15, 6, 34, 12, 4, 6, 100, 5, 3, 34, 34, 5, 3, 9, 4, 6, 5, 4, 6, 13, 20, 8, 9, 3, 4, 3, 50, 8, 5, 50};
int[][] coefficientMatrix={{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 13, 0, 2, 13, 15, 0, 0, 0, 0, 10, 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0},
{0, 0, 0, 0, 0, 13, 0, 0, 9, 0, 0, 1, 0, 9, 0, 11, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 8, 0, 4, 13, 0, 0, 0, 4, 13, 11, 0, 0, 10, 0, 0, 0, 0},
{0, 0, 17, 15, 0, 0, 0, 11, 0, 10, 7, 0, 0, 0, 0, 1, 0, 8, 0, 0, 0, 0, 0, 0, 15, 0, 0, 20, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 19, 0, 1, 0, 0, 1},
{0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 7, 3, 0, 0, 4, 1, 0, 11, 6, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 20, 0, 7, 0, 0, 0, 15, 8, 16, 0, 13, 0, 0},
{15, 0, 0, 0, 8, 5, 0, 10, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 18, 0, 0, 0, 0, 8, 0, 0, 0, 0, 6, 0, 0, 6, 13, 3, 16, 14, 6, 0, 0, 0, 0, 0, 0, 18, 0, 16, 0, 0, 0, 0},
{17, 0, 17, 0, 0, 20, 12, 0, 9, 0, 0, 12, 0, 0, 0, 3, 0, 0, 0, 10, 0, 18, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 3, 0, 0, 0, 0, 18, 0}};
int[] rhs={20, 29, 30, 29, 36, 30};
//CPLEX MODEL
//Variables
IloNumVar[] x = new IloNumVar[numVars];
for (int i = 0; i < numVars; i++)
x[i] = cplex.boolVar("x_" + i);
// Objective
cplex.addMaximize(cplex.scalProd(objCoefficients, x));
//Constraints
IloLPMatrix lpMatrix=cplex.LPMatrix();
for(int k=0; k<numConstraints; k++)
lpMatrix.addRow(
cplex.eq(
cplex.scalProd(coefficientMatrix[k],x),
rhs[k],
"C"+k)
);
//Result of LPMatrix.getRows method
int[][] ind=new int[numConstraints][]; //SPARSE table!
double[][] coefficients=new double[numConstraints][]; //SPARSE table!
double[] lb=new double[numConstraints];
double[] ub=new double[numConstraints];
lpMatrix.getRows(0, numConstraints, lb, ub, ind, coefficients); //populate arrays
System.out.println("ind[0]: "+ Arrays.toString(ind[0]));
System.out.println("coefficients[0]: "+ Arrays.toString(coefficients[0]));
}
The output of this little code snippet is:
ind[0]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
coefficients[0]: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 17.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 12.0, 0.0, 0.0, 0.0, 13.0, 0.0, 2.0, 13.0, 15.0, 0.0, 0.0, 0.0, 0.0, 10.0, 13.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.0, 0.0]
As opposed to:
ind[0]: [10, 14, 24, 28, 30, 31, 32, 37, 38, 48]
coefficients[0]: [1.0, 17.0, 12.0, 13.0, 2.0, 13.0, 15.0, 10.0, 13.0, 3.0]
As it turns out, a sparse representation is only returned after the LPMatrix has been added to the cplex model. So instead of IloLPMatrix lpMatrix=cplex.LPMatrix();, one should invoke IloLPMatrix lpMatrix=cplex.addLPMatrix(); to get the sparse representation! This is not mentioned in the documentation, and took quite some time to troubleshoot. Please explicitly state this in the documentation (or have the method correctly return a sparse representation at all times).
#CPLEXOptimizers#DecisionOptimization