1

I'd like to define a binary variable x_ijk where i in I is an n-element Vector{Vector{Int64}}, k in K is an n-element Vectors, and j in J is a m-elemts vectors. Always the length of K is equal to the number of vectors in I. How to index over each elements of the nth vector in I with the nth elemnts in K pairwise?

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 with index every entries in every vectorI? What I'd like to have is like this:

# for a better explanation suppose K = [a,b]
# for each vector in I and associated elemnts  in K having a variable

# 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]

My last try was unsuccsful too:

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

1 Answers1

0

I believe you are looking for a way to flatten I so this could be:

julia> @variable(model, x[i in unique!(vcat(I...)), j in J, k in K] >= 0, Bin)
3-dimensional DenseAxisArray{VariableRef,3,...} with index sets:
    Dimension 1, [2, 6, 5, 4, 1, 3, 9]
    Dimension 2, [1, 2, 3]
    Dimension 3, [4, 5]
And data, a 7×3×2 Array{VariableRef, 3}:
[:, :, 4] =
 x[2,1,4]  x[2,2,4]  x[2,3,4]
 x[6,1,4]  x[6,2,4]  x[6,3,4]
 x[5,1,4]  x[5,2,4]  x[5,3,4]
 x[4,1,4]  x[4,2,4]  x[4,3,4]
 x[1,1,4]  x[1,2,4]  x[1,3,4]
 x[3,1,4]  x[3,2,4]  x[3,3,4]
 x[9,1,4]  x[9,2,4]  x[9,3,4]

[:, :, 5] =
 x[2,1,5]  x[2,2,5]  x[2,3,5]
 x[6,1,5]  x[6,2,5]  x[6,3,5]
 x[5,1,5]  x[5,2,5]  x[5,3,5]
 x[4,1,5]  x[4,2,5]  x[4,3,5]
 x[1,1,5]  x[1,2,5]  x[1,3,5]
 x[3,1,5]  x[3,2,5]  x[3,3,5]
 x[9,1,5]  x[9,2,5]  x[9,3,5]

Not since the values of I are repeated unique! was required.

Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
  • thanks. I need repeated numbers. Actually first I thought about flattening. But it gives incorrect result. For instance 'x[4,1,4]' is not valid. Since 4 from second list and should be matched with 5. But the repetition has a meaning in my model. Do you know anything that preserve them? –  May 19 '22 at 21:16
  • It is not clear to me what you exactly want. Maybe you could edit your question to a smaller MWE. Normally when you have 3 dimensions you make a cartesian product. What is that you want instead? – Przemyslaw Szufel May 19 '22 at 23:22