# Difficulty building JuMP SparseAxisArrays for network problems

**URL:** https://discourse.julialang.org/t/difficulty-building-jump-sparseaxisarrays-for-network-problems/27227
**Category:** Optimization (Mathematical)
**Created:** [August 6, 2019, 4:09pm UTC](https://discourse.julialang.org/t/difficulty-building-jump-sparseaxisarrays-for-network-problems/27227 "2019-08-06T16:09:10Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![pulsipher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pulsipher/32/10461_2.png) [@pulsipher](https://discourse.julialang.org/u/pulsipher)
#### Post date: [August 6, 2019, 4:09pm UTC](https://discourse.julialang.org/t/difficulty-building-jump-sparseaxisarrays-for-network-problems/27227/1 "2019-08-06T16:09:10Z")

</div>

I am trying to model a network flow problem via JuMP and I am having difficulty building the flow variables with the correct indexing using `@variable`. For example, let’s suppose I have a network with 2 flow variables x\_{12} and x\_{32}. Thus, I would like to build a `VariableRef` container named `x` that can be indexed at `[1, 2]` and `[3, 2]`. In other words, I would like to do the following using `@variable`:

```julia
julia> @variable(model, x[some_input], container = SparseAxisArray)
JuMP.Containers.SparseAxisArray{VariableRef,2,Tuple{Int64, Int64}} with 2 entries:
  [1, 2] = x[1, 2]
  [3, 2] = x[3, 2]

```

Is this behavior possible to achieve with JuMP in its current state? The closest I have come using `@variable` is by doing the following:

```julia
julia> @variable(model, x[[(1, 2), (3, 2)]], container = SparseAxisArray)
JuMP.Containers.SparseAxisArray{VariableRef,2,Tuple{Any}} with 2 entries:
  [(1, 2)] = x[(1, 2)]
  [(3, 2)] = x[(3, 2)]

```

However, having the nested tuples would be nice to avoid. The only way I can figure out how to get the desired container is to define it manually by doing the following:

```julia
using JuMP

model = Model()
idxs = [(1, 2), (3, 2)]
ns = [string("x[", idxs[i][1], ", ", idxs[i][2], "]") for i = 1:2]
var_dict = Dict(idxs[i] => @variable(model, base_name = ns[i]) for i = 1:2)
x = JuMP.Containers.SparseAxisArray(var_dict)

```

```julia
JuMP.Containers.SparseAxisArray{VariableRef,2,Tuple{Int64,Int64}} with 2 entries:
  [1, 2] = x[1, 2]
  [3, 2] = x[3, 2]

```

Can this be done with `@variable`? Thanks!

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [August 6, 2019, 4:51pm UTC](https://discourse.julialang.org/t/difficulty-building-jump-sparseaxisarrays-for-network-problems/27227/2 "2019-08-06T16:51:32Z")

</div>

You can go:

```nohighlight
julia> model = Model();

julia> S = [(1, 2), (3, 2)]
2-element Array{Tuple{Int64,Int64},1}:
 (1, 2)
 (3, 2)

julia> N = 3
3

julia> @variable(model, x[i=1:N, j=1:N; (i, j) in S])
JuMP.Containers.SparseAxisArray{VariableRef,2,Tuple{Any,Any}} with 2 entries:
  [1, 2] = x[1,2]
  [3, 2] = x[3,2]

```

But notice that the key is `Tuple{Any, Any}`. In addition, if `N` is large, this may take a long time to build. In general, your second or third option is preferred.
