How to add variables with the same name in JuMP?

Hello,

I am trying to reproduce a model from a mosel file in JuMP. The mosel file uses a loop to iteratively define variables at specific indices. However, in JuMP, it seems I can’t find how to add variables with the same name but at different indices.

    forall(j in 2..N-2, p in j+1..N-1, q in p+1..N) do
        create(y(j,p,q));  y(j,p,q) is_binary;  
        create(y(p,j,q));  y(p,j,q) is_binary;  
        create(y(q,j,p));  y(q,j,p) is_binary; 
        y(j,p,q)+y(p,j,q)+y(q,j,p)=1
    end-do

The last line creates a constraint, that I can do in another @constraint macro. I tried this:

    idxs = [(j,p,q) for j in TAXA[1:end-2], p in TAXA[1:end-1], q in TAXA if p>j && q>p]
    @variable(model, y[(j,p,q) in idxs], Bin)
    @variable(model, y[(p,j,q) in idxs], Bin)
    @variable(model, y[(q,j,p) in idxs], Bin)

but it says y is already registered.

How about:

y1 = @variable(model, [(j,p,q) in idxs], Bin)
y2 = @variable(model, [(p,j,q) in idxs], Bin)
y = [y1; y2]
1 Like

This does not work, this throws:

ERROR: `Base.size` is not implemented for `SparseAxisArray` because although it is a subtype of `AbstractArray`, it is conceptually closer to a dictionary with `N`-dimensional keys. If you encounter this error and you didn't call `size` explicitly, it is because you called a method that is unsupported for `SparseAxisArray`s. Consult the JuMP documentation for a list of supported operations.

You cannot create multiple variables with the same name.

Do something like:

idxs = []
for j in TAXA[1:end-2], p in TAXA[1:end-1], q in TAXA
    if p > j && q > p
        push!(idxs, (j, p, q))
        push!(idxs, (p, j, q))
        push!(idxs, (q, j, p))
    end
end
@variable(model, y[idxs], Bin)
2 Likes