How can I access or index an OrderedDict?
I tried as below but returning keyerror
using DataStructures
price = OrderedDict(
"s1" => 0,
"s2" => 0,
"s3" => 0,
"s4" => 5
)
for s in 1:length(S)
price[s]
end
Returns Error
KeyError: key 1 not found
Stacktrace:
[1] getindex(::OrderedDict{String,Int64}, ::Int64) at C:\Users\glmab.julia\packages\OrderedCollections\cP9uu\src\ordered_dict.jl:380
[2] top-level scope at .\In[22]:2
[3] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
nilshg
April 7, 2021, 5:27am
2
You still need to use they keys of the ordered dict, so iterate over s in keys(price)
The full equation is as follows
sum((price[s]*d[s,n]) for n in 1:length(N) for s in S )
And it returns the error
KeyError: key 1 not found
Stacktrace:
[1] getindex at .\dict.jl:467 [inlined]
[2] lookup_index at C:\Users\glmab.julia\packages\JuMP\y5vgk\src\Containers\DenseAxisArray.jl:145 [inlined]
[3] _to_index_tuple at C:\Users\glmab.julia\packages\JuMP\y5vgk\src\Containers\DenseAxisArray.jl:154 [inlined] (repeats 2 times)
[4] to_index at C:\Users\glmab.julia\packages\JuMP\y5vgk\src\Containers\DenseAxisArray.jl:174 [inlined]
[5] getindex(::JuMP.Containers.DenseAxisArray{VariableRef,2,Tuple{Array{String,1},Array{UnitRange{Int64},1}},Tuple{Dict{String,Int64},Dict{UnitRange{Int64},Int64}}}, ::String, ::Int64) at C:\Users\glmab.julia\packages\JuMP\y5vgk\src\Containers\DenseAxisArray.jl:192
[6] (::var"#409 #412"{Int64})(::String) at .\none:0
[7] MappingRF at .\reduce.jl:93 [inlined]
[8] _foldl_impl(::Base.MappingRF{var"#409 #412"{Int64},Base.BottomRF{typeof(Base.add_sum)}}, ::Base._InitialValue, ::Base.KeySet{String,OrderedDict{String,Int64}}) at .\reduce.jl:58
[9] FlatteningRF at .\reduce.jl:119 [inlined]
[10] MappingRF at .\reduce.jl:93 [inlined]
[11] _foldl_impl(::Base.MappingRF{var"#410 #411",Base.FlatteningRF{Base.BottomRF{typeof(Base.add_sum)}}}, ::Base._InitialValue, ::UnitRange{Int64}) at .\reduce.jl:58
[12] foldl_impl at .\reduce.jl:48 [inlined]
[13] mapfoldl_impl at .\reduce.jl:44 [inlined]
[14] #mapfoldl #204 at .\reduce.jl:160 [inlined]
[15] mapfoldl at .\reduce.jl:160 [inlined]
[16] #mapreduce #208 at .\reduce.jl:287 [inlined]
[17] mapreduce at .\reduce.jl:287 [inlined]
[18] sum at .\reduce.jl:494 [inlined]
[19] sum(::Base.Iterators.Flatten{Base.Generator{UnitRange{Int64},var"#410 #411"}}) at .\reduce.jl:511
[20] top-level scope at In[25]:4
[21] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
With S and N being
S = ["s1","s2","s3","s4"]
N = [1:10]
d is a variable
using JuMP
m = Model()
@variables m begin
d[s in S, n in N] >= 0
end
nilshg
April 7, 2021, 6:15am
4
I don’t know how JuMP macros work, but the basic problem is the same - an OrderedDict
still has to be indexed by its keys, not by integers (unless you are using integers as keys, but in that case you might as well just use an array?)
julia> for s in keys(price)
@show s, price[s]
end
(s, price[s]) = ("s1", 0)
(s, price[s]) = ("s2", 0)
(s, price[s]) = ("s3", 0)
(s, price[s]) = ("s4", 5)