Empty sums in expressions

When you create an expression that is an empty sum, it evaluates to Val{false}() instead of 0:

model = Model()
@expression(model, sum(1 for i in 1:0))
julia> Val{false}()

A similar problem was posted in the GitHub issue Problem with Empty Summations and closed here, but it seems to work only for constraints, not for expressions.

Something even weirder happens when you try to create a collection of such expressions:

model = Model()
@expression(model, [i in 1:2], sum(1 for j in 1:0))
julia> ERROR: Collection of expressions with @expression must be linear.
  For quadratic expressions, use your own array.

Is this a or am I doing something wrong?

The first case is arguably a bug. I’ve opened an issue: https://github.com/JuliaOpt/JuMP.jl/issues/2120

One issue is that:

julia> sum(1 for i = 1:0)
ERROR: ArgumentError: reducing over an empty collection is not allowed
 [1] _empty_reduce_error() at ./reduce.jl:216
 [2] mapreduce_empty_iter(::Function, ::Function, ::Base.Generator{UnitRange{Int64},getfield(Main, Symbol("##5#6"))}, ::Base.EltypeUnknown) at ./reduce.jl:261
 [3] mapfoldl_impl at ./reduce.jl:57 [inlined]
 [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{UnitRange{Int64},getfield(Main, Symbol("##5#6"))}) at ./reduce.jl:416
 [10] top-level scope at none:0

But it seems like we agreed that empty sums should be 0.0.

The collection case is fixed (in the sense that it now returns a vector of Val{false}()) in the latest release of JuMP.



In the latest relase of JuMP, i.e. version 0.20.1, it actually throws the error I posted above. The result you posted is returned in version 0.20.0, the current development version.