-1

I have to write in Java (with Cplex) the variable x[i][j] that is the sum on k of x[i][j][k]. i,j and k are the indices of three sets. I have already declare x[i][j][k], but i would like to know the right expression. Thanks

  • Welcome to SO! Please also have a look at https://stackoverflow.com/help/how-to-ask and consider clarifying the question to show what you've tried, how far you've gotten, and more precisely where you're getting stuck. – WBT May 08 '19 at 17:12

1 Answers1

0

You appear to be confusing yourself regarding the type of x[i][j]. Is it a set with several elements, indexed by k, or is it a number representing their sum? You seem to start out this step in computation with the first answer and conclude with the second.

A solution to this is to use another variable to store the result, maybe something like:

sums[i][j] = sum(x[i][j]);

where sum(list) is a function that takes in a list, starts with a return value of 0, and iterates over the elements of the input list adding each one to the return value, then returns that value. You can check here for some ideas about how to implement that.

WBT
  • 2,249
  • 3
  • 28
  • 40
  • I'm sorry. What i mean is that for now i have a variable x[i][j][k] that is binary, according to the values of i,j and k.It's 1 if the vehicle k goes from i to j. Now i want to declare another variable that is the sum on k of the first one. Considering i and j like two nodes, in this way i know the total flow of the arc between them, because k is the index of the vehicles – Natalia Sacco May 08 '19 at 17:08
  • Then in the `sum` function, you just add one to the return value for each value that is true. `if(list[k]) { returnValue++;}` – WBT May 08 '19 at 17:10