Problem summing multiple dims in NLobjective

I’m having trouble using matrices in an NLobjective

model = Model()

@variable(model, 0 .<= x[1:100, 1:100] .<= 1)
@NLexpression(model,x_con[ii=1:100, jj=1:100], x[ii,jj] * abs(x[ii,jj]))

This works

@NLobjective(model, Min, sum(x_con[ii] for ii in 1:100*100))

while this gives

julia> @NLobjective(model, Min, sum(x_con[ii,jj] for ii in 1:100 for jj in 1:100))
ERROR: UndefVarError: jj not defined
 [1] macro expansion
   @ ~/.julia/packages/JuMP/gVq7V/src/macros.jl:1834 [inlined]
 [2] top-level scope
   @ REPL[6]:1

I appreciate these are doing the same thing, but it could be helpful for matrix sums to work.

1 Like

Oops. This is a pretty bad bug that I likely introduced in the recent nonlinear changes.

For a work-around, use sum(x_con[ii,jj] for ii in 1:100, jj in 1:100).

I’ll get it fixed ASAP: Fix parsing of nonlinear expressions with :flatten nodes by odow · Pull Request #3116 · jump-dev/JuMP.jl · GitHub


Fixed in JuMP v1.4.0.

which will be available once this PR is merged: New version: JuMP v1.4.0 by JuliaRegistrator · Pull Request #71272 · JuliaRegistries/General · GitHub</s.

1 Like

That was super quick, thank you. I’ll update now.

1 Like