Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Containers] convert axes to AbstractVector in DenseAxisArray #2713

Merged
merged 2 commits into from
Sep 28, 2021

Conversation

odow
Copy link
Member

@odow odow commented Sep 27, 2021

This PR fixes some confusing behavior with iterative non AbstractVector axes in DenseAxisArray.

Closes #2424

Note that now the axis is a vector of pairs, not a dictionary (which guarantees replicable iteration order), and you can access via tuples or pairs.

Before

julia> d = Dict("a" => 1, "b" => 2)
Dict{String,Int64} with 2 entries:
  "b" => 2
  "a" => 1

julia> model = Model();

julia> @variable(model, x[(k, v) in d])
1-dimensional DenseAxisArray{VariableRef,1,...} with index sets:
    Dimension 1, Dict("b" => 2,"a" => 1)
And data, a 2-element Array{VariableRef,1}:
 x[("b", 2)]
 x[("a", 1)]

julia> x[("a", 1)]
ERROR: KeyError: key ("a", 1) not found
Stacktrace:
 [1] getindex at ./dict.jl:467 [inlined]
 [2] lookup_index at /Users/oscar/.julia/packages/JuMP/qhoVb/src/Containers/DenseAxisArray.jl:140 [inlined]
 [3] _to_index_tuple at /Users/oscar/.julia/packages/JuMP/qhoVb/src/Containers/DenseAxisArray.jl:149 [inlined]
 [4] to_index at /Users/oscar/.julia/packages/JuMP/qhoVb/src/Containers/DenseAxisArray.jl:167 [inlined]
 [5] getindex(::JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{Dict{String,Int64}},Tuple{Dict{Pair{String,Int64},Int64}}}, ::Tuple{String,Int64}) at /Users/oscar/.julia/packages/JuMP/qhoVb/src/Containers/DenseAxisArray.jl:182
 [6] top-level scope at REPL[68]:1

julia> x["a" => 1]
x[("a", 1)]

Now

julia> using JuMP

julia> d = Dict("a" => 1, "b" => 2)
Dict{String, Int64} with 2 entries:
  "b" => 2
  "a" => 1

julia> model = Model();

julia> @variable(model, x[(k, v) in d])
1-dimensional DenseAxisArray{VariableRef,1,...} with index sets:
    Dimension 1, ["b" => 2, "a" => 1]
And data, a 2-element Vector{VariableRef}:
 x[("b", 2)]
 x[("a", 1)]

julia> x["a" => 1]
x[("a", 1)]

julia> x[("a", 1)]
x[("a", 1)]

@odow odow added the Category: Containers Related to the Containers submodule label Sep 27, 2021
@codecov
Copy link

codecov bot commented Sep 27, 2021

Codecov Report

Merging #2713 (f90a575) into master (4f0ecb4) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2713   +/-   ##
=======================================
  Coverage   93.62%   93.62%           
=======================================
  Files          44       44           
  Lines        5455     5460    +5     
=======================================
+ Hits         5107     5112    +5     
  Misses        348      348           
Impacted Files Coverage Δ
src/Containers/DenseAxisArray.jl 88.77% <100.00%> (+0.30%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4f0ecb4...f90a575. Read the comment docs.

@odow odow merged commit dae7b41 into master Sep 28, 2021
@odow odow deleted the od/DenseAxisArray_dict branch September 28, 2021 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Containers Related to the Containers submodule
Development

Successfully merging this pull request may close these issues.

Container with a dictionary as the dimension
1 participant