Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Generating constraints in Java

    Posted 04/08/18 04:14 PM

    Originally posted by: beratpostalci


    Hi all,

     

    I'm learning Cplex Java API to solve TSP. I am able to generate obj (paths between cities with random weights) like below:

     

    randValue x12 + randValue x13 + randValue x14 + ...

     

    with this code:

    for(int i = 0; i < n; i++) {
            
            x[i] = cplex.numVarArray(n, 0.0, Double.MAX_VALUE);
            for(int j = 0; j < n; j++) {
                    if(i != j) {
                            if (i + 1 > 10 && j + 1 < 10) {
                                    x[i][j].setName("x" + (i + 1) + "0" + (j + 1));
                            } else {
                                    x[i][j].setName("x" + (i + 1) + (j + 1));
                            }                                     
                            expr.addTerm(50 + r.nextInt(951), x[i][j]);
                    } 
            }
    
        
    }
    

    Then, I want to add a constraint for 5 cities like below:

     

    x12+x13+x14+x15 = 1

     

    with this code:

     

    for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                    if(i != j) {
                            if(j >= n - 1) {
                                    cplex.addEq(cplex.sum(x[i][j],0), 1);
                            } else {
                                    cplex.addEq(cplex.sum(x[i][j],x[i][j + 1]), 1);
                            }
                            j++;
                    }
            }
    }
    

     

    However when I write System.out.println( cplex.toString() ); It gives output as below:

     

    IloModel  {
    IloMinimize  : (738.0*x12 + 968.0*x13 + 268.0*x14 + 732.0*x15 + 431.0*x21 + 173.0*x23 + 675.0*x24 + 174.0*x25 + 371.0*x31 + 831.0*x32 + 236.0*x34 + 279.0*x35 + 293.0*x41 + 119.0*x42 + 673.0*x43 + 465.0*x45 + 125.0*x51 + 433.0*x52 + 875.0*x53 + 561.0*x54)
    IloRange  : 1.0 <= (1.0*x12 + 1.0*x13) <= 1.0
    IloRange  : 1.0 <= (1.0*x14 + 1.0*x15) <= 1.0
    IloRange  : 1.0 <= (1.0*x21 + 1.0*[0.0..infinity]) <= 1.0
    IloRange  : 1.0 <= (1.0*x23 + 1.0*x24) <= 1.0
    IloRange  : 1.0 <= (1.0*x25) <= 1.0
    IloRange  : 1.0 <= (1.0*x31 + 1.0*x32) <= 1.0
    IloRange  : 1.0 <= (1.0*x34 + 1.0*x35) <= 1.0
    IloRange  : 1.0 <= (1.0*x41 + 1.0*x42) <= 1.0
    IloRange  : 1.0 <= (1.0*x43 + 1.0*[0.0..infinity]) <= 1.0
    IloRange  : 1.0 <= (1.0*x45) <= 1.0
    IloRange  : 1.0 <= (1.0*x51 + 1.0*x52) <= 1.0
    IloRange  : 1.0 <= (1.0*x53 + 1.0*x54) <= 1.0

    }

     

    How can I generate constraint  x12 + x13 + x14 + x15 = 1   ?

     

     

     

     

     

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Generating constraints in Java

    Posted 04/08/18 04:53 PM

    It's a bit hard to see what is going on here, particularly since your code for assigning names looks buggy to me. (One symptom: The "1.0*[0.0..infinity])" suggests that at least one variable did not get a name.) If you're looking for suggestions on how to add up a bunch of variables in a constraint, there are multiple ways to do it.

    One approach is as follows.

    • Create a vector to hold the variables: IloNumVar[] temp = new IloNumVar[<number of terms>];
    • Plug each variable you want to add into a slot in temp (using a loop, or nested loops).
    • Use cplex.addEq(cplex.sum(temp), ...) to create the constraint.

    A variation on that approach is to make temp an instance of ArrayList<IloNumVar>. That saves you having to count the number of terms up front and keep track of an index; you just use the ArrayList.add() method to populate temp. You then need to use ArrayList.toArray() to export it to IloNumVar[] and feed that to cplex.sum().

    A different approach is as follows.

    • Create an empty expression: IloLinearNumExpr temp = cplex.linearNumExpr();
    • Inside a loop, or nested loop, add your variables to the expression: temp.addTerm(1.0, <next variable>);
    • Use cplex.addEq(temp, ...) to create the constraint.

    There are other variations, but that should be enough to get you rolling.

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Generating constraints in Java

    Posted 04/09/18 05:12 AM

    Originally posted by: beratpostalci


    Thanks for your answer. Since I eliminate main diagonal in matrix with i != j statement their names are not assigned. I'll try out your suggestions.


    #CPLEXOptimizers
    #DecisionOptimization