jump-dev / JuMP.jl

Modeling language for Mathematical Optimization (linear, mixed-integer, conic, semidefinite, nonlinear)
http://jump.dev/JuMP.jl/
Other
2.17k stars 390 forks source link

MethodError with empty summations #3736

Closed odow closed 2 months ago

odow commented 2 months ago

Reported on discourse: https://discourse.julialang.org/t/weird-empty-sum-behavior-in-jump/113386

Not sure if we should fix this in JuMP or MA.

julia> using JuMP

julia> model = Model();

julia> @variable(model, x);

julia> @expression(model, -x^2 * sum(x for i in 1:0))
ERROR: MethodError: no method matching zero(::Type{MutableArithmetics.Zero})

Closest candidates are:
  zero(::Type{Union{}}, Any...)
   @ Base number.jl:310
  zero(::Type{Pkg.Resolve.FieldValue})
   @ Pkg ~/.julia/juliaup/julia-1.10.2+0.x64.apple.darwin14/share/julia/stdlib/v1.10/Pkg/src/Resolve/fieldvalues.jl:38
  zero(::Type{Missing})
   @ Base missing.jl:104
  ...

Stacktrace:
  [1] _instantiate_zero(::Type{MutableArithmetics.Zero})
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:28
  [2] promote_operation_fallback(op::typeof(*), ::Type{QuadExpr}, ::Type{MutableArithmetics.Zero})
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:51
  [3] promote_operation(::typeof(*), ::Type, ::Type)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:113
  [4] mutability(::Type, ::Function, ::Type, ::Type)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:273
  [5] mutability(::QuadExpr, ::Function, ::QuadExpr, ::MutableArithmetics.Zero)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:281
  [6] operate!!(::typeof(*), ::QuadExpr, ::MutableArithmetics.Zero)
    @ MutableArithmetics ~/.julia/dev/MutableArithmetics/src/interface.jl:616
  [7] macro expansion
    @ ~/.julia/dev/MutableArithmetics/src/rewrite.jl:321 [inlined]
  [8] macro expansion
    @ ~/.julia/dev/JuMP/src/macros.jl:257 [inlined]
  [9] macro expansion
    @ ~/.julia/dev/JuMP/src/macros/@expression.jl:86 [inlined]
 [10] macro expansion
    @ ~/.julia/dev/JuMP/src/macros.jl:393 [inlined]
 [11] top-level scope
    @ ./REPL[37]:1
jd-foster commented 2 months ago

+ 1 for the fix. We came across this issue as well while building a model.