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

type unstability with container generation #2552

Closed
etiennedeg opened this issue Apr 6, 2021 · 3 comments · Fixed by #2553
Closed

type unstability with container generation #2552

etiennedeg opened this issue Apr 6, 2021 · 3 comments · Fixed by #2553
Labels
Category: Containers Related to the Containers submodule Type: Bug

Comments

@etiennedeg
Copy link

julia> model = Model(Gurobi.Optimizer)

julia> @variable(model, a[x in 1:10; x < 5], Bin)
JuMP.Containers.SparseAxisArray{VariableRef, 1, Tuple{Int64}} with 4 entries:

julia> @variable(model, b[x in 1:10; x > 15], Bin)
JuMP.Containers.SparseAxisArray{Any, 1, Tuple{Any}} with 0 entries

Here, I would expect the second example to return a JuMP.Containers.SparseAxisArray{VariableRef, 1, Tuple{Int64}}
I don't know if the current behaviour is expected, but it can cause type unstability if we don’t check the emptiness of the resulting container beforehand.

@odow
Copy link
Member

odow commented Apr 6, 2021

Thanks for opening an issue. Empty dictionaries seem to be well-typed, so maybe there is something we can do:

julia> Dict(x => x for x in 1:10 if x > 15)
Dict{Int64, Int64}()

Here is the backstory on Discourse: https://discourse.julialang.org/t/type-unstability-with-container-generation-in-jump/58433

@odow odow added Category: Containers Related to the Containers submodule Type: Bug labels Apr 6, 2021
@odow
Copy link
Member

odow commented Apr 6, 2021

Here's the problem:

if isempty(dict)
# If `dict` is empty, it was not able to determine the type
# of the key hence the type of `dict` is `Dict{Any, Any}`.
# This is an issue since `SparseAxisArray` needs the key
# type to be a tuple.
dict = Dict{_eltype_or_any(indices),Any}()
end

@blegat
Copy link
Member

blegat commented Apr 7, 2021

Dict(x => x for x in 1:10 if x > 15) is using some dark magic to work. See the _iterator_upper_bound function:
https://github.com/JuliaLang/julia/blob/8218480f059b7d2ba3388646497b76759248dd86/base/abstractdict.jl#L541
https://github.com/JuliaLang/julia/blob/8218480f059b7d2ba3388646497b76759248dd86/base/array.jl#L628-L656
We could use Base.@default_eltype as well.

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 Type: Bug
Development

Successfully merging a pull request may close this issue.

3 participants