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

Problem with dot broadcasting with sums of empty sets #2195

Closed
pseudocubic opened this issue Mar 6, 2020 · 6 comments
Closed

Problem with dot broadcasting with sums of empty sets #2195

pseudocubic opened this issue Mar 6, 2020 · 6 comments

Comments

@pseudocubic
Copy link

pseudocubic commented Mar 6, 2020

The following code

m = Model()
v1 = @variable(m)
v2 = @variable(m)

@constraint(m, sum(a for a in [[v1, v2]]) .== sum(a for a in [ones(2)]) + sum(a for a in [])*1.0)

gives the error

ERROR: ArgumentError: reducing over an empty collection is not allowed
Stacktrace:
 [1] _empty_reduce_error() at ./reduce.jl:212
 [2] mapreduce_empty_iter(::Function, ::Function, ::Base.Generator{Array{Any,1},var"#17#18"}, ::Base.EltypeUnknown) at ./reduce.jl:257
 [3] mapfoldl_impl(::Function, ::Function, ::NamedTuple{(),Tuple{}}, ::Base.Generator{Array{Any,1},var"#17#18"}) at ./reduce.jl:57
 [4] #mapfoldl#186 at ./reduce.jl:72 [inlined]
 [5] mapfoldl at ./reduce.jl:72 [inlined]
 [6] #mapreduce#194 at ./reduce.jl:200 [inlined]
 [7] mapreduce at ./reduce.jl:200 [inlined]
 [8] sum at ./reduce.jl:395 [inlined]
 [9] sum(::Base.Generator{Array{Any,1},var"#17#18"}) at ./reduce.jl:412
 [10] top-level scope at .julia/packages/MutableArithmetics/Oy9aO/src/rewrite.jl:224
 [11] top-level scope at .julia/packages/JuMP/CZ8vV/src/macros.jl:380

as does

@constraint(m, sum(a for a in [[v1, v2]]) .== sum(a for a in [ones(2)]) + sum(a for a in []).*1.0)

whereas the following two constraints give no errors

@constraint(m, sum(a for a in [[v1,v2]]) .== sum(a for a in [ones(2)]) + sum(a.*1.0 for a in []))
@constraint(m, sum(a for a in [v1,v2]) == sum(a for a in ones(2)) + sum(a for a in [])*1.0)

The first example was working previous to JuMP v0.21

@odow
Copy link
Member

odow commented Mar 6, 2020

This is not JuMP-specific:

julia> sum(a for a in [])
ERROR: ArgumentError: reducing over an empty collection is not allowed
Stacktrace:
 [1] _empty_reduce_error() at ./reduce.jl:216
 [2] mapreduce_empty_iter(::Function, ::Function, ::Base.Generator{Array{Any,1},getfield(Main, Symbol("##11#12"))}, ::Base.EltypeUnknown) at ./reduce.jl:261
 [3] mapfoldl_impl(::Function, ::Function, ::NamedTuple{(),Tuple{}}, ::Base.Generator{Array{Any,1},getfield(Main, Symbol("##11#12"))}) at ./reduce.jl:57
 [4] #mapfoldl#187 at ./reduce.jl:72 [inlined]
 [5] mapfoldl at ./reduce.jl:72 [inlined]
 [6] #mapreduce#191 at ./reduce.jl:205 [inlined]
 [7] mapreduce at ./reduce.jl:205 [inlined]
 [8] sum at ./reduce.jl:399 [inlined]
 [9] sum(::Base.Generator{Array{Any,1},getfield(Main, Symbol("##11#12"))}) at ./reduce.jl:416
 [10] top-level scope at none:0

Previously, JuMP allowed things like this because it did not follow Base Julia correctly.

I think this is a case of "won't fix."

@odow
Copy link
Member

odow commented Mar 6, 2020

The cases that do work are slightly funky due to the way we re-write the summation. I don't think they should be relied upon to keep working in the future.

@mlubin
Copy link
Member

mlubin commented Mar 6, 2020

Could we make the default value of an empty sum within a macro equal to MutableArithmetics.Zero()?

@odow
Copy link
Member

odow commented Mar 6, 2020

Actually, maybe this is a broadcasting issue in MutableArithmetics:

julia> @macroexpand @constraint(
           model,
           0 .== 1.0 * sum(a for a in [])
       )
quote
    #= /Users/oscar/.julia/packages/JuMP/CZ8vV/src/macros.jl:44 =#
    (JuMP._valid_model)(model, :model)
    #= /Users/oscar/.julia/packages/JuMP/CZ8vV/src/macros.jl:45 =#
    begin
        #= /Users/oscar/.julia/packages/JuMP/CZ8vV/src/macros.jl:380 =#
        #125###475 = begin
                #= /Users/oscar/.julia/packages/MutableArithmetics/Oy9aO/src/rewrite.jl:223 =#
                let
                    #= /Users/oscar/.julia/packages/MutableArithmetics/Oy9aO/src/rewrite.jl:224 =#
                    begin
                        begin
                            #126###478 = (JuMP.MutableArithmetics).Zero()
                            #127###477 = (JuMP.MutableArithmetics).operate!((JuMP.MutableArithmetics).add_mul, #126###478, 0)
                        end
                        #128###476 = (JuMP.MutableArithmetics).broadcast!((JuMP.MutableArithmetics).sub_mul, #127###477, 1.0 * sum((a for a = [])))
                    end
                    #= /Users/oscar/.julia/packages/MutableArithmetics/Oy9aO/src/rewrite.jl:225 =#
                    #128###476
                end
            end
        #= /Users/oscar/.julia/packages/JuMP/CZ8vV/src/macros.jl:381 =#
        (JuMP.add_constraint).(model, (JuMP.build_constraint).(getfield(JuMP, Symbol("#_error#70")){Symbol}(Core.Box(Any[:model, :(0 .== 1.0 * sum((a for a = [])))]), :constraint), (JuMP._functionize)(#125###475), (JuMP.Ref)(MathOptInterface.EqualTo{Float64}(0.0))))
    end
end

In addition, this is almost certainly a bug:

julia> @constraint(
           model,
           1.0 * sum(a for a in []) .== 0
       )
ERROR: MethodError: no method matching -(::MutableArithmetics.Zero, ::Int64)
Closest candidates are:
  -(::Complex{Bool}, ::Real) at complex.jl:298
  -(::Missing, ::Number) at missing.jl:97
  -(::Base.CoreLogging.LogLevel, ::Integer) at logging.jl:107
  ...
Stacktrace:
 [1] sub_mul(::MutableArithmetics.Zero, ::Int64) at /Users/oscar/.julia/packages/MutableArithmetics/Oy9aO/src/MutableArithmetics.jl:31
 [2] _broadcast_getindex_evalf at ./broadcast.jl:578 [inlined]
 [3] _broadcast_getindex at ./broadcast.jl:551 [inlined]
 [4] getindex at ./broadcast.jl:511 [inlined]
 [5] copy at ./broadcast.jl:763 [inlined]
 [6] materialize at ./broadcast.jl:753 [inlined]
 [7] broadcast(::typeof(MutableArithmetics.sub_mul), ::MutableArithmetics.Zero, ::Int64) at ./broadcast.jl:707
 [8] broadcast_fallback!(::MutableArithmetics.NotMutable, ::Function, ::MutableArithmetics.Zero, ::Int64) at /Users/oscar/.julia/packages/MutableArithmetics/Oy9aO/src/broadcast.jl:85
 [9] broadcast!(::Function, ::MutableArithmetics.Zero, ::Int64) at /Users/oscar/.julia/packages/MutableArithmetics/Oy9aO/src/broadcast.jl:77
 [10] top-level scope at /Users/oscar/.julia/packages/MutableArithmetics/Oy9aO/src/rewrite.jl:224

@odow
Copy link
Member

odow commented Mar 6, 2020

I've changed my mind. This is a fixable bug.

I've opened an issue: jump-dev/MutableArithmetics.jl#44

@odow
Copy link
Member

odow commented Dec 6, 2020

Closing in favor of the MutableArithmetics issue.

@odow odow closed this as completed Dec 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants