Define variables on each elemnts of a vector of vectors

I’m trying to find a way to define a varible x_ijk where i in I is an n-elemnts vector of vectors, j in J and k in K are both vectors. In my case, always the length of K is equal to the number of vectors in I .

The question is related on how we can index over each vectors in I separatly with an orederd and separate combination with K.

For example:

I = [[2,6,5], [1,2,4,5,9]]
J = [1,2,3]
K = [4,5] # for a better explanation suppose K = [a,b]

How to have the variables x_ijk indexed with a pair entries in every vector I and associated ordered elements in K? I mean, the combination of each elemnts of the first vector of I and first elements in K. Then index over eache elements of the second vector in I and the second elements in K.

The desired output is like this:

# for a better explanation suppose K = [a,b]
# for each vector in I and associated elemnts  in K having a variable x_ijk where each elemnts of the nth vector in `I` is paired and indexed with nth element in `K`

# for first pair (i.e. I=[2,6,5] ,K = a)
x[2,1,a], x[2,2,a], x[2,3,a], x[6,1,a], .... x[5,3,a]  # in other words we cannot have x[2,1,b] or any other combination with `b`

# for second pair ( i.e. I=[1,2,4,5,9] ,K = b)
x[1,1,b], x[1,2,b],..., x[4,1,b], .... x[9,3,b]

Is this possible? I’ve tried many thing but neither were succesful? Like the following:

for idx in 1:length(K)
    @variable(model, x[i in I[idx], j in J, k in K] >= 0, Bin)
end

I would just pre-compute the set of indices that you want:

I = [[2, 6, 5], [1, 2, 4, 5, 9]]
J = [1, 2, 3]
K = [4,v5]
sets = [(i, j, k) for (ki, k) in enumerate(K) for j in J for i in I[ki]]
model = Model()
@variable(model, x[sets])
x[(2, 3, 5)]
1 Like

Thanks @odow dow Very Much!

1 Like