1. No, IloLPMatrix will not automatically remove 0's from your data. This is illustrated by the following example
import ilog.cplex.*;
import ilog.concert.*;
public final class Sparse {
public static void main(String[] args) throws IloException {
final IloCplex cplex = new IloCplex();
try {
IloNumVar x = cplex.boolVar("x");
IloNumVar y = cplex.boolVar("y");
IloNumVar z = cplex.boolVar("z");
IloLinearNumExpr expr = cplex.linearNumExpr();
expr.addTerm(1.0, x);
expr.addTerm(0.0, y);
expr.addTerm(1.0, z);
System.out.println(expr);
IloRange rng = cplex.range(1.0, expr, 2.0);
System.out.println(rng);
IloLPMatrix matrix = cplex.LPMatrix();
matrix.addRow(rng);
matrix.addRow(2.0, 3.0, new int[]{ 0, 1, 2 },
new double[]{ 0.0, 1.0, 2.0 });
System.out.println(matrix);
}
finally {
cplex.end();
}
}
}
which prints
(1.0*x + 0.0*y + 1.0*z)
IloRange : 1.0 <= (1.0*x + 0.0*y + 1.0*z) <= 2.0
IloLPMatrix {
IloRange : 1.0 <= (1.0*x + 0.0*y + 1.0*z) <= 2.0
IloRange : 2.0 <= (0.0*x + 1.0*y + 2.0*z) <= 3.0
}
2. I don't think there is an example shipped with CPLEX. But it should be rather obvious what to do: instead of storing a matrix in the file, only store the triples of non-zeros.
#CPLEXOptimizers#DecisionOptimization