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> @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> @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:
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)
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!