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
------------------------------
Original Message:
Sent: Tue November 30, 2021 01:14 AM
From: Saumya Bhatnagar
Subject: Defining decision variables
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
Original Message:
Sent: Mon November 29, 2021 02:56 AM
From: Xavier Nodet
Subject: Defining decision variables
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
Original Message:
Sent: Mon November 29, 2021 02:15 AM
From: Saumya Bhatnagar
Subject: Defining decision variables
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
------------------------------
#DecisionOptimization