# Build JuMP variable from array of tuples

**URL:** <https://discourse.julialang.org/t/build-jump-variable-from-array-of-tuples/5638>\
**Category:** Optimization (Mathematical)\
**Created:** [August 30, 2017, 9:09pm UTC](https://discourse.julialang.org/t/build-jump-variable-from-array-of-tuples/5638 "2017-08-30T21:09:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbrunaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbrunaud/32/1737_2.png) [@bbrunaud](https://discourse.julialang.org/u/bbrunaud)\
**Post date:** [August 30, 2017, 9:09pm UTC](https://discourse.julialang.org/t/build-jump-variable-from-array-of-tuples/5638/1 "2017-08-30T21:09:54Z")

</div>

Hello

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

```julia
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.

thanks!

---

<div class="post-metadata">

**Author:** ![bbrunaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbrunaud/32/1737_2.png) [@bbrunaud](https://discourse.julialang.org/u/bbrunaud)\
**Post date:** [September 2, 2017, 3:02am UTC](https://discourse.julialang.org/t/build-jump-variable-from-array-of-tuples/5638/3 "2017-09-02T03:02:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [September 2, 2017, 3:52am UTC](https://discourse.julialang.org/t/build-jump-variable-from-array-of-tuples/5638/4 "2017-09-02T03:52:30Z")

</div>

You could use anonymous variables in a regular Julia `Dict`:

```julia
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]
x[a1,b1]

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [September 2, 2017, 8:33am UTC](https://discourse.julialang.org/t/build-jump-variable-from-array-of-tuples/5638/5 "2017-09-02T08:33:39Z")

</div>

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