# Decision Optimization

View Only

## Defining decision variables

• #### 1.  Defining decision variables

Posted Mon November 29, 2021 02:16 AM
Edited by Saumya Bhatnagar Mon November 29, 2021 02:21 AM
Hi,

I am working on an optimization model using CPLEX and Java having a decision variable y[i][j][k] which denotes number of vehicles relocated from zone i to j at the end of time window k. However, relocation can only happen between adjacent zones. For this, I maintain a list in Java code that stores indices of zones adjacent to every zone.

One way to model this could be by setting variables for non-existing moves to 0. But I am not sure how exactly this could be modelled. Any insights on how to do this would be really helpful.

Thanks and regards,
Saumya

------------------------------
Saumya Bhatnagar
------------------------------

• #### 2.  RE: Defining decision variables

Community Leadership
Posted Mon November 29, 2021 02:56 AM
Hi Saumya,

It seems to me that the following pseudo-code should work:
``````for i ...
for j ...
if !is_adjacent(i,j) {
for k ...
cplex.add(y[i][j][k] <= 0);
}
​``````

------------------------------
Xavier
------------------------------

• #### 3.  RE: Defining decision variables

Posted Mon November 29, 2021 03:40 AM
Hi Xavier,

Thank you for the clarification.

Best regards,
Saumya

------------------------------
Saumya Bhatnagar
------------------------------

• #### 4.  RE: Defining decision variables

Posted Tue November 30, 2021 01:14 AM
Edited by Saumya Bhatnagar Tue November 30, 2021 01:42 AM
Hi Xavier,

One of the the constraints that uses this y[i][j][k] variable is as follows. Here, n[i][k] is another decision variable. Also, j can only take values for zones adjacent to zone i.
I have the following partial code in Java.

``````for(int i=0; i<s; i++) {
for(int j=0; j<s; j++) {
if(neighbouringZones.get(i).contains(j)) {
for(int k=0; k<t; k++) {
cplex.sum(n[i][k-1], cplex.sum(y[i][k-1]));
}

}

}
}``````

Since summation is over the second index j, I am unable to correctly formulate this constraint. Any insights on how to formulate this would be really helpful.

Thanks and regards,
Saumya

------------------------------
Saumya Bhatnagar
------------------------------

• #### 5.  RE: Defining decision variables

Community Leadership
Posted Tue November 30, 2021 03:53 AM
The way to formulate such a constraint is to use an intermediary object to hold the expression that you want to build, before you can put it in a constraint.  Such objects are of type IloNumExpr.  You will find examples of using such objects in the directory `cplex/examples/src/java` in your installation of CPLEX Optimization Studio, in particular `Diet.java`, `Etsp.java` and `FoodManufact.java`.

In this case, you can use such an object to hold the sum of the y variables that you're interested in. Here's what it could look like. Note that I'm not a Java programmer, and this may very well not even compile.
`for (int i=0; i<s; i++) {   for (int k=0; k<t; k++) {      sum_y = cplex.numExpr()      for (int j=0; j<s; j++) {         if (!neighbouringZones.get(i).contains(j)) {            continue;         }         sum_y = cplex.sum(sum_y, y[i][j][k-1])      }      cplex.add(cplex.eq(n[i][k],                         cplex.sum(n[i][k-1],                                   sum_y)                        )               );   }}`

------------------------------
Xavier
------------------------------

• #### 6.  RE: Defining decision variables

Posted Tue November 30, 2021 04:15 AM
Hi Xavier,

Thank you so much for the clarification.

Best regards,
Saumya

------------------------------
Saumya Bhatnagar
------------------------------