I have an IloCplex
object and I want to get the coefficients of the IloObjective
with the Cplex Java api.
With this code I get the IloObjective
and IloNumExpr
but I can't access the coefficients.
IloCplex cplex = new IloCplex();
cplex.importModel("model.lp");
IloObjective obj = cplex.getObjective();
IloNumExpr expr = obj.getExpr();
How can I get the coefficients of IloObjective
or IloNumExpr
?
Since your model.lp
file contains a linear model, expr
should be an instance of IloLinearNumExpr
, and you should be able to iterate over it using the linearIterator
method:
if (expr instanceof IloLinearNumExpr) {
IloLinearNumExpr lexpr = (IloLinearNumExpr) expr;
IloLinearNumExprIterator it = lexpr.linearIterator();
while (it.hasNext()) {
IloNumVar var = it.nextNumVar();
double coeff = it.getValue();
System.out.println(var + " " + coeff);
}
}