Build JuMP variable from array of tuples



Let’s say that I have an array of tuples containing all the indices I need for a variable

A = [(:a1,:b1),(:a2,:b2)]

Now, I want to define a JuMP variable such that I can access x[:a1,:b1] for example. How do I accomplish that?. I tried @variable(m, x[i in A] but the indices of the JuMPDict are Tuples. I end up with one dimension index when I actually want two.



Thank you!. It works!.
Why does it work and doesn’t give four indices?? (a1,b1),(a2,b1),(a2,b1),(a2,b2)


You could use anonymous variables in a regular Julia Dict:

julia> using JuMP

julia> m = Model();

julia> A = [(:a1,:b1),(:a2,:b2)];

julia> x = Dict((i, j) => @variable(m, basename = "x[$(string(i)),$(string(j))]") for (i, j) in A)
Dict{Tuple{Symbol,Symbol},JuMP.Variable} with 2 entries:
  (:a2, :b2) => x[a2,b2]
  (:a1, :b1) => x[a1,b1]

julia> x[:a1, :b1]


Oops, it actually does give 4, sorry! @tkoolen’s approach is the right way. I will take back my post.