Weird empty sum behavior in JuMP

I am coding basic AC OPF model.

The following sum results in an error:
-(Vd[n]^2+Vq[n]^2)*sum(gs[sh] for sh in bus_shunts[n])

LoadError: MethodError: no method matching zero(::Type{MutableArithmetics.Zero}) Closest candidates are: zero(!Matched::Type{Union{}}, Any...)

However, the following example does not result in error:
-(Vd[n]^2)*sum(gs[sh] for sh in bus_shunts[n])
-(Vq[n]^2)*sum(gs[sh] for sh in bus_shunts[n])

Also for reactive power where there is one nonzero entry does not result in an error:
+(Vd[n]^2+Vq[n]^2)*sum(bs[sh] for sh in bus_shunts[n])

I am aware that setting init value to zero will avoid the error (;init=0), however I do not understand in which cases do I need to define init value. All other sums in JuMP for OPF formulation work ok without init value and putting init to all of them would make the code long and ugly. Any suggestions on why such behavior that it gives error when multiplying with a sum of two variables but no error when multiplied with single variable.

Thanks for reporting this. I can reproduce it:

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

I’ll open an issue to get this fixed: MethodError with empty summations · Issue #3736 · jump-dev/JuMP.jl · GitHub

I am aware that setting init value to zero will avoid the error (;init=0)

Yes, this will have to be the work-around for now.

Thanks for creating the issue. I’ve created other issues for MadNLP on GPU and ExaModels directly on Github this time. Not sure if these packages are related to you, but I think we will need to solve them. In particular, I am eager to see Fp32 GPU optimization working. Most of us do not have good Fp64 GPU as they are expensive.

1 Like

This should now work if you update your packages. (Although if the summation can be over zero elements, it’s probably always good practice to use init.)

Dropping links to the issues you opened at MadNLP CUDSS internal error (invalid argument: inertia correction) · Issue #333 · MadNLP/MadNLP.jl · GitHub and ExaModels convert_data Float32 support and missing documantation · Issue #86 · exanauts/ExaModels.jl · GitHub. I’m sure @sshin23 will be along to help you out.

(Note that the only packages which are “official” JuMP packages are those in the jump-dev GitHub repository: Repositories · GitHub. See also Introduction · JuMP and Introduction · JuMP)

1 Like