Using java cplex to solve TSP, getValues of variables gives an error -


i use cplex solve travelling salesman problem (tsp). given if x[i][j]=1, path goes city i city j, otherwise, ther no path between these cities. corresponding matrix:

ilonumvar[][] x = new ilonumvar[n][]; for(int = 0; < n; i++){     x[i] = cplex.boolvararray(n); } 

when cplex finishes solving, want values of x this:

if(cplex.solve()){     double[][] var = new double[n][n];     for(int = 0; i<x.length; i++){         for(int j = 0 ; j < x[i].length; j ++){             var[i][j] = cplex.getvalue(x[i][j]);             if(var[i][j] ==1){                 system.out.print(i + "  "+ j);             }                }        } } 

but gives error. appreciate can give advice. error in :

 var[i][j] = cplex.getvalue(x[i][j]); 

the explain :

exception in thread "main"  ilog.cplex.ilocplex$unknownobjectexception: cplex error: object unknown ilocplex @ ilog.cplex.ilocplex.getvalue(ilocplex.java:6495) 

the entire code following:

import java.io.ioexception; import ilog.concert.*; import ilog.cplex.ilocplex;  public class tsp { public static void solveme(int n) throws iloexception, ioexception{      //random data     double[] xpos = new double[n];     double[] ypos = new double[n];     (int = 0; < n; ++){         xpos[i] = math.random()*100;         ypos[i] = math.random()*100;     }      double[][] c = new double[n][n];     (int = 0 ; < n; i++){         (int j = 0 ; j < n; j++)         c[i][j] = math.sqrt(math.pow(xpos[i]-xpos[j], 2)+ math.pow(ypos[i]-ypos[j],2));     }      //model      ilocplex cplex = new ilocplex();      //variables     ilonumvar[][] x = new ilonumvar[n][];      for(int = 0; < n; i++){         x[i] = cplex.boolvararray(n);     }      ilonumvar[] u = cplex.numvararray(n, 0, double.max_value);      //objective     ilolinearnumexpr obj = cplex.linearnumexpr();     for(int =0 ; <n ; i++){         (int j = 0; j< n ;j++){             if(j != i){                 obj.addterm(c[i][j], x[i][j]);             }         }     }     cplex.addminimize(obj);      //constraints     for(int j = 0; j < n; j++){         ilolinearnumexpr expr = cplex.linearnumexpr();         for(int = 0; i< n ; i++){             if(i!=j){                 expr.addterm(1.0, x[i][j]);             }         }         cplex.addeq(expr, 1.0);     }      for(int = 0; < n; i++){         ilolinearnumexpr expr = cplex.linearnumexpr();         for(int j = 0; j< n ; j++){             if(j!=i){                 expr.addterm(1.0, x[i][j]);             }         }         cplex.addeq(expr, 1.0);     }       for(int = 1; < n; i++){         for(int j = 1; j < n; j++){             if(i != j){                 ilolinearnumexpr expr = cplex.linearnumexpr();                 expr.addterm(1.0, u[i]);                 expr.addterm(-1.0, u[j]);                 expr.addterm(n-1, x[i][j]);                 cplex.addle(expr, n-2);             }         }     }      //solve mode     if(cplex.solve()){         system.out.println();         system.out.println("solution status = "+ cplex.getstatus());         system.out.println();         system.out.println("cost = " + cplex.getobjvalue());         for(int = 0; i<x.length; i++){             for(int j = 0 ; j < x[i].length; j ++){                     system.out.print(cplex.getvalue(x[i][j]));                                       }            }        }      //end     cplex.end();     } } 

looking @ this page @ ibm knowledge center, argument getvalue() must used construct model solving, in example here. since var n x n, should initialize x ilonumvar[][] x = new ilonumvar[n][n];. @ point before calling solve(), each element of x should added model.

in setup, diagonal elements of x never referenced model. notice setup loops have condition if(i != j). fix issue, either add else clause @ least 1 of loops, or if meaningless (as suspect is), printout consistently input:

for(int = 0; i<x.length; i++) {     for(int j = 0 ; j < x[i].length; j ++) {         if(i != j)             system.out.print(cplex.getvalue(x[i][j]));         else             system.out.print("-");     }    } 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -