JuMP reports an error when attempting to sum the empty DenseAxisArray as the objective function

Minimal Reproducible Example

using JuMP
set = Symbol[]
m = Model()
@variable(m, x[i in set])
@objective(m, Min, sum(x; init=0))

In this case, optimization is not impossible; rather, an unexpected error occurs when the program executes at @objective.

ERROR: LoadError: MethodError: no method matching _rewrite_generic_generator(::Expr, ::Symbol, ::Symbol, ::Symbol)
The function `_rewrite_generic_generator` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  _rewrite_generic_generator(::Expr, ::Symbol, ::Expr, ::Any)      
   @ MutableArithmetics .julia\packages\MutableArithmetics\VMS0x\src\rewrite_generic.jl:295
  _rewrite_generic_generator(::Expr, ::Symbol, ::Expr)
   @ MutableArithmetics .julia\packages\MutableArithmetics\VMS0x\src\rewrite_generic.jl:295

Does anyone know how to resolve this?

> versioninfo()
Julia Version 1.12.1
Commit ba1e628ee4 (2025-10-17 13:02 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)

you can use the generator syntax:

using JuMP

set = Symbol[]
m = Model()

@variable(m, x[i in set])
@objective(m, Min, sum(x[i] for i in set; init=0.0))
1 Like

Hi @karei, thanks for this bug report. I’m fixing this now :smile:

1 Like

Wow this works, thank you!

Thank you!

I know that you’ve simplified your example, but did you consider just dropping the init?

This works:

@objective(m, Min, sum(x))

Your example actually exposed two bugs:

I added it precisely because the sum function threw an error when init wasn’t included.

When running @objective(m, Min, sum(x)) on my computer, I got:

ERROR: ArgumentError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer

:thinking:

1 Like

What version of Julia are you using?

As yet another work-around, you can do:

@objective(m, Min, sum(x.data))

The underlying issue is that most mathematicians consider sum of the empty set to be 0, but Julia is a bit more picky. In JuMP we have tried to override the behaviour so that you get 0, but we miss some edge cases :cry:

My version is

versioninfo()
Julia Version 1.12.1
Commit ba1e628ee4 (2025-10-17 13:02 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)

This works!

Thank you for your kind help. Now I can bypass this bug using langestefan and your method and continue executing the code. Don’t worry. :wink:

1 Like