简体   繁体   中英

Extracting Boolean values from 2D array in CPLEX using Eclipse

I am solving a standard Vehicle Routing Problem with time windows using CPLEX(in concert with Java on Eclipse). As a start, I am modelling a single vehicle, which makes it a TSP I guess. I have used a 2D Boolean array to denote whether the vehicle travels from i to j. The problem is being solved and I receive the value of the objective function, but the values of the 2D array are not being read.

Here is the code for defining the array:

try {
            IloCplex cplex = new IloCplex();
            IloNumVar[][] x = new IloNumVar[n][];
            for(int i=0; i<n; i++){
                x[i] = cplex.boolVarArray(n);
            }

At the end I've written:

cplex.solve();

             System.out.print(cplex.getValue(obj));

             //decision matrix           
             for(int i=0; i<n; i++){
                    for(int j=0; j<n; j++){
                        System.out.print(cplex.getValue(x[i][j]) + " ");

                    }System.out.println();
             }   

             cplex.end();

            }
            catch (IloException e) {
                e.printStackTrace();
            }
            }
        }

The objective value is displayed which obviously means the problem is solved, but the following error message appears:

Total (root+branch&cut) =    0.02 sec. (2.23 ticks)
223.62126087689276ilog.cplex.IloCplex$UnknownObjectException: CPLEX Error: object is unknown to IloCplex
    at ilog.cplex.IloCplex.getValue(IloCplex.java:6495)
    at VRP01k.main(VRP01k.java:144)
IBM ILOG CPLEX Optimization Studio Preview Edition good for 18 more days.
The CPLEX Optimizers will solve problems up to 1000 variables and 1000 constraints.

Can someone please point out the problem? Is there a disparity between how I initialize the array and how I call its values? Thanks.

Here is the complete model. I have split the depot into destination and origin points.

try {
    IloCplex cplex = new IloCplex();
    IloNumVar[][] x = new IloNumVar[n][];
    for(int i=0; i<n; i++){
        x[i] = cplex.boolVarArray(n);
    }


    //variable definition
    IloNumVar[] w = cplex.numVarArray(n, 0, Double.MAX_VALUE); /*arrival time*/

    //objective function
    IloLinearNumExpr obj = cplex.linearNumExpr();
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(j!=i){obj.addTerm(c[i][j], x[i][j]);};
        }
    }
    cplex.addMinimize(obj);

    //path continuity
    for(int i=1; i<n-1; i++){
        IloLinearNumExpr expr = cplex.linearNumExpr();
        for(int j=1; j<n; j++){
            if(j!=i){expr.addTerm(1.0, x[i][j]);};
        }
        cplex.addEq(expr, 1.0);
    }

    //origin depot
    IloLinearNumExpr expr0 = cplex.linearNumExpr();
    for(int j=1; j<n-1; j++){
        expr0.addTerm(1.0, x[0][j]);

    }cplex.addEq(expr0, 1.0);

    //destination depot
    IloLinearNumExpr expr1 = cplex.linearNumExpr();
    for(int i=1; i<n-1; i++){
        expr1.addTerm(1.0, x[i][n-1]);

    }cplex.addEq(expr1, 1.0);

    for(int j=1; j<n-1; j++){
        IloLinearNumExpr expr2 = cplex.linearNumExpr();                         
        for(int i=0; i<n-1; i++){
            if(j!=i){

                if(i!=0 && i!=n-1){
                    expr2.addTerm(1.0, x[i][j]);
                    expr2.addTerm(-1.0, x[j][i]);}

                else if(i==0){expr2.addTerm(1.0, x[i][j]);}
                else if(i==n-1){expr2.addTerm(-1.0, x[j][i]);}
                }
            }
        }

    //time constraints
    for(int i=0; i<n; i++){
        IloLinearNumExpr expr3 = cplex.linearNumExpr();
        expr3.addTerm(1.0, w[i]);
        cplex.addGe(expr3, a[i]);
        cplex.addLe(expr3, b[i]);
    }

    //travel time constraints
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(j!=i){
        IloLinearNumExpr expr4 = cplex.linearNumExpr(); 
        expr4.addTerm(1.0, w[i]);
        expr4.addTerm(1000, x[i][j]);
        expr4.addTerm(-1.0, w[j]);
        cplex.addLe(expr4, 1000-s[i]-c[i][j]);
        }
        }
    }

    cplex.solve();

     System.out.print(cplex.getValue(obj));

     for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                System.out.print(cplex.getValue(x[i][j]) + " ");

            }System.out.println();
     }   

     cplex.end();

    }
    catch (IloException e) {
        e.printStackTrace();
    }
    }
}

The posted code does not show the model. However, I will guess you maybe have not included all of the variables in the model. For example, in the VRP the variable x[1][1] should be zero, but if you simply didn't add it to any constraint or the objective, then the model doesn't even know it exists. Thus, if you try to query the value after solving, it will throw an exception. The following code throws an exception when querying the value of x3 , which was not included in the model.

public static void main(String[] args) throws IloException {
    IloCplex cplex = new IloCplex();
    IloNumVar x1 = cplex.boolVar();
    IloNumVar x2 = cplex.boolVar();
    IloNumVar x3 = cplex.boolVar();
    cplex.addMinimize(cplex.prod(10, x1));
    cplex.addEq(cplex.sum(x1,x2),1);
    if (cplex.solve()) {
        System.out.println(cplex.getValue(x1));
        System.out.println(cplex.getValue(x2));
        System.out.println(cplex.getValue(x3));
    }
}

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