Key 1.0 not found

Hi,

I have an optimization model, which I have run for a long time on my personal machine. When I run the same model on a cluster, it gives some errors. The error is as follows:

ERROR: LoadError: KeyError: key 1.0 not found
Stacktrace:
  [1] getindex(#unused#::JuMP.Containers._AxisLookup{Dict{Int64, Int64}}, key::Float64)
    @ JuMP.Containers ~/.julia/packages/JuMP/b3hGi/src/Containers/DenseAxisArray.jl:18
  [2] _getindex_recurse(data::Tuple{JuMP.Containers._AxisLookup{Dict{Int64, Int64}}}, keys::Tuple{Float64}, condition::JuMP.Containers.var"#8#10")
    @ JuMP.Containers ~/.julia/packages/JuMP/b3hGi/src/Containers/DenseAxisArray.jl:250
  [3] _getindex_recurse(data::Tuple{JuMP.Containers._AxisLookup{Dict{Int64, Int64}}, JuMP.Containers._AxisLookup{Dict{Int64, Int64}}}, keys::Tuple{Int64, Float64}, condition::JuMP.Containers.var"#8#10")
    @ JuMP.Containers ~/.julia/packages/JuMP/b3hGi/src/Containers/DenseAxisArray.jl:249
  [4] to_index(A::JuMP.Containers.DenseAxisArray{VariableRef, 2, Tuple{Vector{Int64}, Vector{Int64}}, Tuple{JuMP.Containers._AxisLookup{Dict{Int64, Int64}}, JuMP.Containers._AxisLookup{Dict{Int64, Int64}}}}, idx::Tuple{Int64, Float64})
    @ JuMP.Containers ~/.julia/packages/JuMP/b3hGi/src/Containers/DenseAxisArray.jl:259
  [5] getindex(::JuMP.Containers.DenseAxisArray{VariableRef, 2, Tuple{Vector{Int64}, Vector{Int64}}, Tuple{JuMP.Containers._AxisLookup{Dict{Int64, Int64}}, JuMP.Containers._AxisLookup{Dict{Int64, Int64}}}}, ::Int64, ::Float64)
    @ JuMP.Containers ~/.julia/packages/JuMP/b3hGi/src/Containers/DenseAxisArray.jl:266
  [6] macro expansion
    @ ~/.julia/packages/MutableArithmetics/8xkW3/src/rewrite.jl:279 [inlined]
  [7] macro expansion
    @ ~/.julia/packages/JuMP/b3hGi/src/macros.jl:676 [inlined]
  [8] (::var"#119#281"{JuMP.Containers.DenseAxisArray{VariableRef, 2, Tuple{Vector{Int64}, Vector{Int64}}, Tuple{JuMP.Containers._AxisLookup{Dict{Int64, Int64}}, JuMP.Containers._AxisLookup{Dict{Int64, Int64}}}}, JuMP.Containers.DenseAxisArray{VariableRef, 2, Tuple{Vector{Int64}, Vector{Int64}}, Tuple{JuMP.Containers._AxisLookup{Dict{Int64, Int64}}, JuMP.Containers._AxisLookup{Dict{Int64, Int64}}}}, Model})(g::Int64, t::Int64)
    @ Main ~/.julia/packages/JuMP/b3hGi/src/Containers/macro.jl:194
  [9] #38
    @ ~/.julia/packages/JuMP/b3hGi/src/Containers/container.jl:105 [inlined]
 [10] iterate
    @ ./generator.jl:47 [inlined]
 [11] collect(itr::Base.Generator{JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, Vector{Int64}}}, JuMP.Containers.var"#38#39"{var"#119#281"{JuMP.Containers.DenseAxisArray{VariableRef, 2, Tuple{Vector{Int64}, Vector{Int64}}, Tuple{JuMP.Containers._AxisLookup{Dict{Int64, Int64}}, JuMP.Containers._AxisLookup{Dict{Int64, Int64}}}}, JuMP.Containers.DenseAxisArray{VariableRef, 2, Tuple{Vector{Int64}, Vector{Int64}}, Tuple{JuMP.Containers._AxisLookup{Dict{Int64, Int64}}, JuMP.Containers._AxisLookup{Dict{Int64, Int64}}}}, Model}}})
    @ Base ./array.jl:678
 [12] map(f::Function, A::JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, Vector{Int64}}})
    @ Base ./abstractarray.jl:2323
 [13] container(f::Function, indices::JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, Vector{Int64}}}, #unused#::Type{JuMP.Containers.DenseAxisArray})
    @ JuMP.Containers ~/.julia/packages/JuMP/b3hGi/src/Containers/container.jl:105
 [14] container(f::Function, indices::JuMP.Containers.VectorizedProductIterator{Tuple{Vector{Int64}, Vector{Int64}}})
    @ JuMP.Containers ~/.julia/packages/JuMP/b3hGi/src/Containers/container.jl:66
 [15] macro expansion
    @ ~/.julia/packages/JuMP/b3hGi/src/macros.jl:142 [inlined]
 [16] ExpansionModel()
    @ Main ~/RenewableOpt/WECC-model/ExpansionModel.jl:232
 [17] top-level scope
    @ ~/RenewableOpt/WECC-model/Run.jl:20
in expression starting at /home/fkucuksayacigil/RenewableOpt/WECC-model/Run.jl:20

I feel that packages installed on my machine and the cluster are different versions, that is why errors show up. When I go to line 232 of ExpansionModel.jl, I realized that I am using Array() function. I feel that this is something changed from the version of Julia on my machine to the version of Julia on the cluster.

Could you please help me with this?

Kind regards

Please provide a reproducible example. This looks like a mistake in your code, where you are using 1.0 to index an array instead of 1.

What is your local machine? What versions of Julia? What versions of the packages?

2 Likes

Thank you so much for your reply. I am sorry for the delay, but I delayed the response to figure out the error by myself. It was an easy one.

I was using Julia 1.3 and I now updated to Julia 1.6.2.

Below is a reproducible error:

numbers = rand(10)

for n in Array(3 - 2.0 : 8)
     println(numbers(n))
end

Above loop did not give any error and printed numbers in numbers indexed at {1,2,3,…,8}. When I updated Julia to 1.6.2., it gives error because Array(3 - 2.0 : 8) produces float numbers and they can not be used for indexing. To correct that, I changed it to

round.(Int, Array(3 - 2.0 : 8))

I know that this is a simple one, but I was also surprised about how it was previously indexing with float numbers.

Kind regards,

1 Like

I don’t think that’s right. First of all you can’t index with round brackets:

julia> numbers = rand(10);

julia> for n in Array(3 - 2.0 : 8)
            println(numbers(n))
       end
ERROR: MethodError: objects of type Array{Float64,1} are not callable
Use square brackets [] for indexing an Array.
Stacktrace:
 [1] top-level scope at .\REPL[2]:2

but fixing that:

julia> for n in Array(3 - 2.0 : 8)
            println(numbers[n])
       end
ERROR: ArgumentError: invalid index: 1.0 of type Float64
Stacktrace:
 [1] to_index(::Float64) at .\indices.jl:273
 [2] to_index(::Array{Float64,1}, ::Float64) at .\indices.jl:250
 [3] to_indices at .\indices.jl:301 [inlined]
 [4] to_indices at .\indices.jl:298 [inlined]
 [5] getindex(::Array{Float64,1}, ::Float64) at .\abstractarray.jl:981
 [6] top-level scope at .\REPL[3]:2

julia> versioninfo()
Julia Version 1.3.1

So Array(3 - 2.0 : 8) produces floats in 1.3 and 1.6, and indexing with floats isn’t allowed in either Julia version.

Can’t you just write

for n in (3 - 2):8
     println(numbers[n])
end

?
Where does the number 2.0 come from?

Notice also that you shouldn’t use Array on the range.

1 Like

Thank you. Yes, I typed it wrong. It should be square bracket. However, it is interesting that Julia 1.3 does not allow indexing with float (I know that this is supposed to be, but it was working in my case). Anyway, maybe I messed up something.

2.0 comes from an external file. I do not know in advance if it is integer or float. You are right, I realized that Array() is not a good idea to create range. I do not know either whether I had a specific reason to Array() in this context.

I do not know any language besides Lua that allows indexing the primitive “vector” with floating point numbers. And such design decision is kinda strange to me until today.

Then I think you should convert or round that single number to an Int before using it.

2 Likes

Julia did allow indexing with integer valued floats once, but that’s ancient history, maybe around version 0.1 or so. Matlab and Octave still allow it.

1 Like

In Matlab the literal ‘2’ is actually a float, so they basically have to.

Haha, actually, it’s even worse: it’s a 1x1 matrix of floats…

4 Likes

Oh, does it allows only integer floating values, or any floating point values, in the indexing?

As far as I understand and observe, it allows neither of them. Index should be integer, not integer valued float.

The float indices must be integer-valued in Matlab.