Is JuMP.@variable slow when the number gets large?

WalterMadelim
Sat, 28 Mar 2026 05:41:27 +0000
Optimization (Mathematical)

I have a Unit commitment (24 hours, 5000 scenarios, IEEE118) problem, that has these two lines

@time JuMP.@variable(m, pA[i=_2(t,T,1), _0(i,t,S), agi])
@time JuMP.@variable(m, pf[i=_2(t,T,0), _0(i,t,S), 1:size(F,1)])

where

_3(i, t, s) = (s-1)*(i>t)+1 # return `s` or `1`
_0(i, t, S) = 1:_3(i, t, S) # S in this case is 5000
_2(t, T, p) = t-p:t+T

and F is the PTDF matrix, agi is a Vector{Int}.
The length of pA is approximately 24 * 5000 * 13 == 1560000,
whereas the length of pf is approximately 24 * 5000 * 186 == 22320000.

And their runtime performance is

adding pA variables...
  8.563364 seconds (36.34 M allocations: 1.792 GiB, 29.52% gc time, 2.12% compilation time)
adding pf variable...
146.426164 seconds (535.10 M allocations: 30.329 GiB, 39.43% gc time, 0.11% compilation time)

Note that the code are inside function bodies (and actually inside my custom package) so they had been compiled.

Do you think adding 1.5 million variables using 8.5 seconds is reasonable?

And also, 8.56 / 13 * 186 = 122.47 < 146.426 seconds.

My model is a Gurobi’s direct model.

13 posts - 2 participants