Indexing variables by vectors not working in JuMPv1.2.x

Hello, a bug appeared in my package when I updated JuMP to v1.2.1 from v1.1.1.

Here is a minimum (non) working example:

using GLPK
using JuMP

paths = [
    [1, 2, 15, 3, 20],
    [1, 9, 16, 20],
    [1, 2, 20]
]

model = Model(GLPK.Optimizer)

@variable(model, y[p in paths], Bin)

@objective(
    model,
    Min,
    sum(y[p] for p in paths)
)

The variable definition works, but the expression sum(y[p] for p in paths) raises a KeyError: key 1 not found, which was not the case before.

Did I miss something ?

1 Like

Hmm. This may have broken things Allow `_AxisLookup` to fallback to Dict `getindex` by nickrobinson251 · Pull Request #3028 · jump-dev/JuMP.jl · GitHub

Let me take a look.

1 Like

I have a fix: [Containers] fix DenseAxisArray with Vector key by odow · Pull Request #3064 · jump-dev/JuMP.jl · GitHub

It might take a bit to get a new release, but you can fix your code by adding this function to your script, just after using JuMP. Once we update JuMP, you can remove it.

function Base.getindex(
    x::JuMP.Containers._AxisLookup{Dict{K,Int}},
    key::K,
) where {K<:AbstractVector}
    return x.data[key]
end
1 Like

Thanks for the quick answer, your workaround works perfectly !

1 Like

Just to follow up: JuMP v1.3.0 has been released with a fix for this bug.

1 Like