Stasik
Stasik

Reputation: 2614

Cplex NullPointerException on big Arrays

I use cplex Java API.

Following code is used:

//init cplex
IloCplex cplex = new IloCplex();
cplex.setParam(IloCplex.IntParam.Threads, 1);
//is commodity k,l routed over i and j
//x ijkl
IloIntVar[] x = cplex.boolVarArray(inst.getSize()*inst.getSize()*inst.getSize()*inst.getSize());
for (int i = 0; i < x.length; i++) {
    x[i] = cplex.boolVar();
}

//is node a hub
IloIntVar[] y = cplex.boolVarArray(inst.getSize());
for (int i = 0; i < y.length; i++) {
    y[i] = cplex.boolVar();
}

//=== FITTNESS FUNCTION ===
IloLinearNumExpr expr = cplex.linearNumExpr();
//first big sum
for(int k=0;k<inst.getSize();k++){
    for(int i=0;i<inst.getSize();i++) {
        for(int j=0;j<inst.getSize();j++) {
            for(int l=0;l<inst.getSize();l++) {
                expr.addTerm(c[i][j][k][l], x[Static.quadToLinear(i, j, k, l, inst.getSize())]);
            }
        }
    }
}
//second sum
for(int i=0;i<inst.getSize();i++) {
    expr.addTerm(inst.getFixed(i), y[i]);
}
//minimise it
cplex.addMinimize(expr);

So I just use two boolean vectors x and y. This snippet works fine for smaller instances where inst.getSize() is, for instance, 25. However, for an instance of size 40 it crashes in the last line.

Exception in thread "main" java.lang.NullPointerException
at ilog.cplex.CpxNumVar.unmark(CpxNumVar.java:296)
at ilog.cplex.CpxLinearExpr.unmarkVars(CpxLinearExpr.java:402)
at ilog.cplex.CpxLinearExpr.removeDuplicates(CpxLinearExpr.java:515)
at ilog.cplex.CpxLinearExpr.removeDuplicates(CpxLinearExpr.java:489)
at ilog.cplex.CpxObjective.setExpr(CpxObjective.java:108)
at ilog.cplex.CpxObjective.<init>(CpxObjective.java:362)
at ilog.cplex.IloCplexModeler.objective(IloCplexModeler.java:706)
at ilog.cplex.IloCplexModeler.addObjective(IloCplexModeler.java:768)
at ilog.cplex.IloCplexModeler.addMinimize(IloCplexModeler.java:790)
at ExactSolver.main(ExactSolver.java:69)

Have you got any ideas? I need to get it working...

Upvotes: 0

Views: 1228

Answers (2)

Stasik
Stasik

Reputation: 2614

Well after some trial and errors i found out that this is a kind of a memory issue.

Adding sufficient heapspace to JVM e.g. -Xms512M -Xmx750M solves the problem and lets the program to run as expected.

Upvotes: 1

willem
willem

Reputation: 2717

You create and add Boolean Variables to the model by calling

IloIntVar[] x = cplex.boolVarArray
    (inst.getSize()*inst.getSize()*inst.getSize()*inst.getSize());

you then add new variables to the formulation by calling

x[i] = cplex.boolVar();

but the old variables are still in the formulation, though you don't have references to them anymore. This might cause problems, and it is definitely not something you will want. I am not sure how to do this properly in Java, but in .net I would do either

IIntvar[] x = new IIntvar[size];
for (int i = 0; i < x.length; i++) {
    x[i] = cplex.boolVar();
}

or

IIntVar[] x=  cplex.BoolVarArray(size);
//No for loop!

(IIntVar is the .net variant of java IloIntVar). Same for y. Try commenting out the first two for loops, and see what happens to the error.

Upvotes: 0

Related Questions