-
-
Notifications
You must be signed in to change notification settings - Fork 393
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
Comments
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." |
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. |
Could we make the default value of an empty sum within a macro equal to |
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 |
I've changed my mind. This is a fixable bug. I've opened an issue: jump-dev/MutableArithmetics.jl#44 |
Closing in favor of the MutableArithmetics issue. |
The following code
gives the error
as does
whereas the following two constraints give no errors
The first example was working previous to JuMP v0.21
The text was updated successfully, but these errors were encountered: