2

While I am defining a linear programming variables, I have to consider

index_i = 1:3 index_j = J = [1:2, 1:5, 1:3]

I want to define a variable x indexed with both i and j such that i is {1,2,3} and j is in {1,2} if i is 1, {1,2,3,4,5} if i is 2 and {1,2,3} if i is 3.

I tried several syntaxes but non of them delivered it successfully. Any suggestion?

I wonder why this is not working

@variable(m, e[i for i in I, j for j in J[i]])

I m expecting a result like this

 e[1,1]
 e[1,2]
 e[1,3]
 e[2,1]
 e[2,2]
 e[2,3]
 e[2,4]
 e[2,5]
 e[3,1]
 e[3,2]
 e[3,3]
Ali Fradi
  • 110
  • 1
  • 10

1 Answers1

4

Assuming I=1:3 and J=[1:2, 1:5, 1:3]

you can do:

julia> @variable(m, e[i in I, j in J[i]])
JuMP.Containers.SparseAxisArray{VariableRef, 2, Tuple{Int64, Int64}} with 10 entries:
  [1, 1]  =  e[1,1]
  [1, 2]  =  e[1,2]
  [2, 1]  =  e[2,1]
  [2, 2]  =  e[2,2]
  [2, 3]  =  e[2,3]
  [2, 4]  =  e[2,4]
  [2, 5]  =  e[2,5]
  [3, 1]  =  e[3,1]
  [3, 2]  =  e[3,2]
  [3, 3]  =  e[3,3]
Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62