简体   繁体   中英

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...

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM