A problem with JuMP Tutorial-linear problem-cannery problem

when I code like this , it leads a error

Positing screenshots of snippets makes it difficult to help

1 Like

At a minimum, you need to show us what data and x are. It looks like data might be some kind of nested Dict but I can’t really tell.

The Julia source code is here:
https://github.com/jump-dev/JuMP.jl/blob/master/docs/src/tutorials/linear/cannery.jl

If you’re trying to copy the tutorial, you probably made a typo somewhere. Just copy-paste from the Julia file.

If you’ve changed the data, then please provide a reproducible example.

1 Like

I can confirm that code produces an error

julia> includet("/tmp/cannery.jl")
ERROR: MethodError: no method matching getindex(::Base.KeySet{String, Dict{String, Any}}, ::Colon)
Stacktrace:
  [1] _getindex_recurse(data::Tuple{Base.KeySet{String, Dict{String, Any}}}, keys::Tuple{Colon}, condition::typeof(JuMP.Containers._is_range))
    @ JuMP.Containers ~/.julia/packages/JuMP/klrjG/src/Containers/DenseAxisArray.jl:250
  [2] _getindex_recurse(data::Tuple{Base.KeySet{String, Dict{String, Any}}, Base.KeySet{String, Dict{String, Any}}}, keys::Tuple{Int64, Colon}, condition::typeof(JuMP.Containers._is_range))
    @ JuMP.Containers ~/.julia/packages/JuMP/klrjG/src/Containers/DenseAxisArray.jl:249
  [3] getindex(::JuMP.Containers.DenseAxisArray{VariableRef, 2, Tuple{Base.KeySet{String, Dict{String, Any}}, Base.KeySet{String, Dict{String, Any}}}, Tuple{JuMP.Containers._AxisLookup{Dict{String, Int64}}, JuMP.Containers._AxisLookup{Dict{String, Int64}}}}, ::String, ::Function)
    @ JuMP.Containers ~/.julia/packages/JuMP/klrjG/src/Containers/DenseAxisArray.jl:270
  [4] macro expansion
    @ ~/.julia/packages/MutableArithmetics/6pKCK/src/rewrite.jl:279 [inlined]
  [5] macro expansion
    @ ~/.julia/packages/JuMP/klrjG/src/macros.jl:676 [inlined]
  [6] (::var"#3#4")(p::String)
    @ Main ~/.julia/packages/JuMP/klrjG/src/Containers/macro.jl:194
  [7] #38
    @ ~/.julia/packages/JuMP/klrjG/src/Containers/container.jl:105 [inlined]
  [8] iterate
    @ ./generator.jl:47 [inlined]
  [9] collect(itr::Base.Generator{JuMP.Containers.VectorizedProductIterator{Tuple{Base.KeySet{String, Dict{String, Any}}}}, JuMP.Containers.var"#38#39"{var"#3#4"}})
    @ Base ./array.jl:678
 [10] map(f::Function, A::JuMP.Containers.VectorizedProductIterator{Tuple{Base.KeySet{String, Dict{String, Any}}}})
    @ Base ./abstractarray.jl:2323
 [11] container(f::Function, indices::JuMP.Containers.VectorizedProductIterator{Tuple{Base.KeySet{String, Dict{String, Any}}}}, #unused#::Type{JuMP.Containers.DenseAxisArray})
    @ JuMP.Containers ~/.julia/packages/JuMP/klrjG/src/Containers/container.jl:105
 [12] container(f::Function, indices::JuMP.Containers.VectorizedProductIterator{Tuple{Base.KeySet{String, Dict{String, Any}}}})
    @ JuMP.Containers ~/.julia/packages/JuMP/klrjG/src/Containers/container.jl:66
 [13] top-level scope
    @ /tmp/cannery.jl:100
in expression starting at /tmp/cannery.jl:100

for anyone who can help


julia> x
2-dimensional DenseAxisArray{VariableRef,2,...} with index sets:
    Dimension 1, ["Seattle", "San-Diego"]
    Dimension 2, ["Chicago", "Topeka", "New-York"]
And data, a 2Ă—3 Matrix{VariableRef}:
 x[Seattle,Chicago]    x[Seattle,Topeka]    x[Seattle,New-York]
 x[San-Diego,Chicago]  x[San-Diego,Topeka]  x[San-Diego,New-York]

The code is attempting

julia> sum([x["Seattle", "Chicago"], x["Seattle", "Topeka"], x["Seattle", "New-York"]])

using what would work for integer indexing

julia> sum(x[1, :])

but this fails

julia> sum(x["Seattle", :])
ERROR: MethodError: no method matching getindex(::Base.KeySet{String, Dict{String, Any}}, ::Colon)
Stacktrace:
 [1] _getindex_recurse(data::Tuple{Base.KeySet{String, Dict{String, Any}}}, keys::Tuple{Colon}, condition::typeof(JuMP.Containers._is_range))
   @ JuMP.Containers ~/.julia/packages/JuMP/klrjG/src/Containers/DenseAxisArray.jl:250
 [2] _getindex_recurse(data::Tuple{Base.KeySet{String, Dict{String, Any}}, Base.KeySet{String, Dict{String, Any}}}, keys::Tuple{Int64, Colon}, condition::typeof(JuMP.Containers._is_range))
   @ JuMP.Containers ~/.julia/packages/JuMP/klrjG/src/Containers/DenseAxisArray.jl:249
 [3] getindex(::JuMP.Containers.DenseAxisArray{VariableRef, 2, Tuple{Base.KeySet{String, Dict{String, Any}}, Base.KeySet{String, Dict{String, Any}}}, Tuple{JuMP.Containers._AxisLookup{Dict{String, Int64}}, JuMP.Containers._AxisLookup{Dict{String, Int64}}}}, ::String, ::Function)
   @ JuMP.Containers ~/.julia/packages/JuMP/klrjG/src/Containers/DenseAxisArray.jl:270
 [4] top-level scope
   @ REPL[49]:1


What is import Pkg; Pkg.status()? Are you using an old version of JuMP?

perhaps that’s the problem, because I tried on Julia 1.6 and my JuMP is v0.21.10

my JuMP is v0.21.10

Yes, please update your packages. (JuMP is now 1.0! JuMP 1.0.0 is released | JuMP If you update, you won’t have problems like this breakage in future.)

Here’s the tutorial from JuMP v0.21:
https://jump.dev/JuMP.jl/v0.21/tutorials/Mixed-integer%20linear%20programs/cannery/

1 Like

Hi,very thanks for replying,i think the problem is that the P and M are a keysets,so x[p,:] makes a error, i find a method ,change the code P = keys(data[“plants”]) to P = collect(keys(data[“plants”])), so the problem addressed.

Hi, i tried to code Pkg.update(“JuMP”), but my JuMP version is still v0.21.10, so how can i update my JuMP to v1.0.0? very thanks!

You probably have some dependency that prevents you from updating JuMP, check this thread out.

2 Likes



i’m really confused

The error is saying that AnyMOD requires JuMP v0.21. So you cannot have both AnyMOD and JuMP 1.0.

Try removing AnyMOD first, then update.

1 Like

i removed both AnyMOD and JuMP,and reinstall JuMP, but still produced a error

maybe i didn’t remove old JuMP clearly?

This is now saying the same thing about powermodelsdistribution.

Given we only released JuMP 1.0 recently, it may take some time for packages which build on top of JuMP to update as well.

1 Like

very thanks for solving my problem , i remove PowerModelsDistribution and it works

2 Likes