Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Defining a variable in CPLEX Java

    Posted 07/11/19 04:55 PM

    Originally posted by: brown_rice


    Hi,

    I define the following sets outside of main function:

         public static final Integer[] _set1= {1};
         public static final Integer[] _set2= {1,2};

    Then, I convert them into TreeSet in the main function  as:

      List<Integer> set1_ = Arrays.asList(_set1);    
      Set<Integer> set1 = new TreeSet<Integer>(set1_);

    Additionally, I have "public static final int limit =3;"

    Now, I'd like to create my variable and then see how it looks.

       IloCplex cplex = new IloCplex();

       IloIntVar[][][] T = new IloIntVar[set1.size()][set2.size()][limit];

    At this point, I am little confused. If I was in OPL, I'd just do "dvar int+ T[set1][set2][1..time]" and it would work. 

    In Java, I plan to do something like;

            T[i][j][k] = cplex.intVar(0,Integer.MAX_VALUE, "T_"+ i+j+k);
              cplex.addEq(T[i][j][k], 0,"Constraint_"+ i+j+k);

    However, I am not sure how I should create the for loops. Should I rather create a class (like a tuple in OPL) and pass the class object into  IloIntVar[object]? A better question is that if I can create an object based variable in Java? What is the best way to do this? Please note that the example that I am providing is just a toy example. So I have some set containing string values. I am just trying to find a fixed way to go with all the variables. Thanks.

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Defining a variable in CPLEX Java

    Posted 07/12/19 04:26 PM

    Java lets you loop over collections. So you can write the following:

    for (i in set1) {
        for (j in set2) {
            for (int k = 0; k < limit; k++) {
                T[i][j][k] = cplex.intVar(...);
            }
        }
    }
    

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Defining a variable in CPLEX Java

    Posted 07/16/19 11:21 AM

    Originally posted by: brown_rice


    Hi,

    When I try your suggestion, I receive an error stating that i cannot be resolved a type. When I try 

    for(Integer i: set1)
    

    This time, I receive out of bound exception. Also, is it possible to create a variable in the form of  T[<i,j,k>] in Java? 

     

    When I create my loop in the way shown below, it works, but that's not how I want to create my variables and constraints, because then I lose the definition of the elements stored in my collection and end up with having only indices. 

     for(int i=0; i< set1.size();i++) {
        for(int j=0; j< set2.size();j++) {
           for(int k=0; k< time ; k++) {
              T[i][j][k] = cplex.intVar(0,Integer.MAX_VALUE, "T_"i+j+k);
              cplex.addEq(T[i][j][k], 0,"constraint"+ +j+k);
            }
        }
    }
    

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Defining a variable in CPLEX Java

    Posted 07/16/19 01:47 PM

    Sorry, there were typos in my answer. The first two for loops should start

    for (int i : _set1)  {
        for (int j : _set2) {
            ...
        }
    }
    

    As far as the index out of bounds exception, the problem is that Java uses zero-based indexing. So if _set1 contains one element, and you set the outer dimension of T to 1, the only legal index for the outer dimension is 0. You have three choices: change your indices to zero-based; add 1 to each dimension of T and leave T[0][...][...], T[...][0][...] and T[...][...][0] undefined (not recommended); or change the assignment to read T[i-1][j-1][k-1] = ...

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Defining a variable in CPLEX Java

    Posted 07/17/19 09:47 AM

    Originally posted by: brown_rice


    Paul, thanks for the suggestion. When I use the following loop, it works.

    for(int i=0; i< set1.size();i++) {
     for(int j=0; j< set2.size();j++) {
    

    However, it still creates everything based on indices. For instance, say I have a set containing three items namely A,B, and C. If I want to create an independent constraint just for item B, how am I going to do it? When I create my variables for these three items, Java will probably assign indices 0,1,2 for A,B,C, respectively.

     or another example, suppose I have a matrix showing the cost of going from location i to location . When I create the variables, each location will get an index number. However, the cost matrix (my parameter) has the names of locations in columns and rows. If I want to get the cost value, there is a mismatch. Hopefully, my example makes my question clearer. Do you mind helping me how to solve this issue?


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Defining a variable in CPLEX Java

    Posted 07/17/19 10:56 AM

    There are many ways to handle this. When I want to associate string labels with variables, I create a map (HashMap<String, IloNumVar>), then create a variable for each label and add the lable and variable to the map. You can use HashMap<String, IloRange> to associate labels with constraints in a similar way. If you need to go both ways (meaning take a variable or constraint and figure out the corresponding label), you can also create HashMap<IloNumVar, String> and/or HashMap<IloRange, String> and create the reverse mappings at the same time you create the original mappings.

    Another way to do it is to use arrays for the CPLEX objects (IloNumVar[], IloIntVar[], IloRange[] etc.) and use HashMap<String, Integer> and HashMap<Integer, String> to create mappings between indices and labels. So if you have variables in IloIntVar[] x and index mappings in HashMap<String, Integer> m, x[m.get("A")] will get the variable whose index corresponds to label "A".

    This is basic Java programming, not really specific to CPLEX, so you might be able to get more help on a Java programming forum.

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Defining a variable in CPLEX Java

    Posted 07/17/19 11:00 AM

    Originally posted by: brown_rice


    Great! Thanks for the answer. This should solve my issue hoping that creating several maps do not slow down the model. 


    #CPLEXOptimizers
    #DecisionOptimization