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

Bool.(::DenseAxisArray) errors #2291

Closed
oxinabox opened this issue Jul 20, 2020 · 3 comments · Fixed by #2527
Closed

Bool.(::DenseAxisArray) errors #2291

oxinabox opened this issue Jul 20, 2020 · 3 comments · Fixed by #2527
Labels
Category: Containers Related to the Containers submodule

Comments

@oxinabox
Copy link

oxinabox commented Jul 20, 2020

I have been solving a problem with binary (Bin) variables.
To my annoyance, they come out as floating point values.
So I convert them by calling Bool.

I thought i could clean my code up by doing the convert at the same time as a did the value
Bool.(value.(my_bin_array_var))

But i got this error:

ERROR: MethodError: no method matching JuMP.Containers.DenseAxisArray(::BitArray{2}, ::Tuple{Tuple{Int64,Int64,Int64},Tuple{Symbol,Symbol,Symbol}}, ::Tuple{Dict{Int64,Int64},Dict{Symbol,Int64}})

Presumably this is because broadcasting Bool over a Array gives back a BitArray.
and DenseAxisArray is not prepared to handle that.

Here is a MWE without a solve in it::

julia> x = JuMP.Containers.DenseAxisArray([rand((0.0, 1.0)) for _ in 1:3, _ in 1:3], (1,2,3),(:a,:b, :c))
2-dimensional DenseAxisArray{Float64,2,...} with index sets:
    Dimension 1, (1, 2, 3)
    Dimension 2, (:a, :b, :c)
And data, a 3×3 Array{Float64,2}:
 1.0  0.0  0.0
 1.0  1.0  1.0
 1.0  0.0  0.0

julia> Bool.(x)
ERROR: MethodError: no method matching JuMP.Containers.DenseAxisArray(::BitArray{2}, ::Tuple{Tuple{Int64,Int64,Int64},Tuple
Symbol,Symbol,Symbol}}, ::Tuple{Dict{Int64,Int64},Dict{Symbol,Int64}})
Closest candidates are:
  JuMP.Containers.DenseAxisArray(::Array{T,N}, ::Ax, ::L) where {T, N, Ax, L<:Tuple{Vararg{Dict,N}}} at /Users/oxinabox/.ju
ia/packages/JuMP/YXK4e/src/Containers/DenseAxisArray.jl:13
  JuMP.Containers.DenseAxisArray(::Array{T,N}, ::Any...) where {T, N} at /Users/oxinabox/.julia/packages/JuMP/YXK4e/src/Con
ainers/DenseAxisArray.jl:53
Stacktrace:
 [1] broadcasted(::JuMP.Containers.DenseAxisArrayBroadcastStyle, ::Type{T} where T, ::JuMP.Containers.DenseAxisArray{Float6
,2,Tuple{Tuple{Int64,Int64,Int64},Tuple{Symbol,Symbol,Symbol}},Tuple{Dict{Int64,Int64},Dict{Symbol,Int64}}}) at /Users/oxin
box/.julia/packages/JuMP/YXK4e/src/Containers/DenseAxisArray.jl:253
 [2] broadcasted(::Type{T} where T, ::JuMP.Containers.DenseAxisArray{Float64,2,Tuple{Tuple{Int64,Int64,Int64},Tuple{Symbol,
ymbol,Symbol}},Tuple{Dict{Int64,Int64},Dict{Symbol,Int64}}}) at ./broadcast.jl:1232
 [3] top-level scope at REPL[10]:1
@odow
Copy link
Member

odow commented Jul 21, 2020

Bool.(value.(my_bin_array_var))

You shouldn't do this because solvers will often return values within a tolerance, instead of exact 0.0 and 1.0.

julia> Bool(1e-8)
ERROR: InexactError: Bool(1.0e-8)
Stacktrace:
 [1] Bool(::Float64) at ./float.jl:73
 [2] top-level scope at REPL[17]:1

The larger issue remains, however. We really need to think about how we manage the DenseAxisArray and SparseAxisArray. They don't play nice with the broader ecosystem.

@oxinabox
Copy link
Author

oxinabox commented Jul 21, 2020

You shouldn't do this because solvers will often return values within a tolerance, instead of exact 0.0 and 1.0.

I am not sure how that would work for a MIP.
But, I guess i should do . < 0.5 then?

Though issue will remain

@odow odow added the Category: Containers Related to the Containers submodule label Jul 21, 2020
@blegat
Copy link
Member

blegat commented Jul 22, 2020

It seems we just need to add a method that converts the BitArray to an Array

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 a pull request may close this issue.

3 participants