Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Ilog Cplex No solution exists error

    Posted 04/11/18 04:18 PM

    Originally posted by: beratpostalci


    I want to solve TSP while hasSubtour is true and adding new constraints to the model as below:

    
    
            cplex.addMinimize(expr);
            //System.out.println(cplex.getModel());
    
            Integer nOfVariables = 0;
            boolean hasSubtour = false;
    
            do {
    
                if( cplex.solve() ) {
    
                    if ( cplex.getStatus().equals(IloCplex.Status.Optimal) ) {
    
                     if(hasSubtour) {
                          for(int i = 0; i < n; i++) {
    
                              IloLinearNumExpr tempExpr = cplex.linearNumExpr();
    
                              for(int j = 0; j < n; j++) {
                                  if( i != j ) {
    
                                    tempExpr.addTerm(1.0,x[j][i]);
    
                                  }
                              }
                              cplex.addEq(tempExpr, 1);
                          }
                    }
                 }
    
            } while(hasSubtour);
    

     

    However, when hasSubtour is true, I'm getting an error for 
    cplex.getValue(x[i][j]) < 1 

    named

    Exception in thread "main" ilog.cplex.CpxException: CPLEX Error  1217: No solution exists.
    

    How can I add new constraints to the model without getting an exception?


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Ilog Cplex No solution exists error

    Posted 04/11/18 05:05 PM

    From the block of code you posted above, it's not clear where you are calling cplex.getValue(x[i][j]), and it's not clear how hasSubtour is being set to true. You get that exception when there is no solution and you are attempting to query a solution value for a variable. Can you provide more details?

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Ilog Cplex No solution exists error

    Posted 04/11/18 05:37 PM

    Originally posted by: beratpostalci


    I call cplex.getValue(x[i][j]) a few lines above the if( cplex.getValue(x[i][j]) == 1 ) statement. However at the first iteration it cannot query exactly same thing again and throw an exception.


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Ilog Cplex No solution exists error

    Posted 04/11/18 05:17 PM

    Originally posted by: beratpostalci


                            cplex.addMinimize(expr);
                            //System.out.println(cplex.getModel());
    
                            Integer nOfVariables = 0;
                            boolean hasSubtour = false;
               
                            do {
    
                                    if( cplex.solve() ) {
    
                                            if ( cplex.getStatus().equals(IloCplex.Status.Optimal) ) {
                                                 for(int i = 0; i < n; i++) {
                                                                                   for(int j = 0; j < n; j++) {
                                                                                           if( i != j ) {
    
                                                                                                   if( cplex.getValue(x[i][j]) > 0 ) {
    
                                                                                                           // Some operations here
    
                                                                                                    }
    
                                                                                            }
                                                                                    }
                                                                            }
                                                    hasSubtour = findSubTours(departureList, destinationList);
    
                                                    if(hasSubtour) {
    
                                                            if(nOfVariables > destinationList.size()) {
    
                                                                    for(int i = 0; i < n; i++) {
    
                                                                            IloLinearNumExpr tempExpr = cplex.linearNumExpr();
    
                                                                            for(int j = 0; j < n; j++) {
                                                                                    if( i != j ) {
                                                                                            if( cplex.getValue(x[i][j]) == 1 ) {
    
                                                                                                    tempExpr.addTerm(1.0,x[i][j]);
    
                                                                                            }
                                                                                    }
                                                                            }
    
                                                                            cplex.addEq(tempExpr, 1)
                                                                    }
    
                                                    }
    
                                                    System.out.println(cplex.getModel());
    
    
                                            }
                                    }
    
                            } while(hasSubtour);
    

     

    hasSubtour method controls if there are subtours and returns true or false. 

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Ilog Cplex No solution exists error

    Posted 04/11/18 05:37 PM

    That helps a little. Now, in your original post, you said that the exception was being raised when checking if cplex.getValue(x[i][j]) < 1. Which line of code exactly is triggering the exception (the exception output should tell you this)? I'm asking because I'm wondering if you're getting a failure on the very first solve (in which case there would be no solution and if you try to query a solution value below the code you've given us, it would make perfect sense). Also, if for some reason hasSubtour is true, but cplex.solve() returns false, it looks like you'll have an infinite loop....


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Ilog Cplex No solution exists error

    Posted 04/11/18 05:43 PM

    Originally posted by: beratpostalci


    I've edited the code to indicate where I call the  cplex.getValue(x[i][j]) Line 37 second call to cplex.getValue(x[i][j]) causes the exception


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Ilog Cplex No solution exists error

    Posted 04/11/18 05:50 PM

    Originally posted by: beratpostalci


    What I exactly want is to add new constraints after checking the solution variables and solving the problem again until there are no subtours. I also add the cplex.solve() to while condition to prevent infinite loop.


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Ilog Cplex No solution exists error

    Posted 04/11/18 06:48 PM

    Aha! I'm sorry this took me awhile to understand. After you modify the model by adding the first constraint in the loop, the solution is no longer present, so you get an exception. I would simply store the solution values in a temporary variable before you start adding constraints so that you don't have to query them again within the loop. For example, you could use the getValues method. Also, be aware that when checking floating point solution values you may have something that is within tolerances, and is not exactly what you expect. For example, it's safer to check that Math.abs(cplex.getValue(x[i][j]) - 1.0) < 1e-6 than cplex.getValue(x[i][j]) == 1.


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: Ilog Cplex No solution exists error

    Posted 04/12/18 03:59 PM

    Originally posted by: beratpostalci


    Thanks for your answer. I stored the solution values and construct constraints with them and it is working.


    #CPLEXOptimizers
    #DecisionOptimization