Looping in expressions

Any preferred style of looping in expressions, objectives, or constraints that is syntactically superior and less error-prone? I see all the following works:

using JuMP

model = Model()

@variable(model, x[1:10] >= 0)
@variable(model, y[1:10] >= 0)
@variable(model, z[1:10] >= 0)

@expression(model, ex1, sum(sum(sum(x[i]+y[j]+z[k] for i in 1:3) for j in 1:2) for k in 1:3))

@expression(model, ex2, sum(x[i]+y[j]+z[k] for i in 1:3 for j in 1:2 for k in 1:3))

@expression(model, ex3, sum(x[i]+y[j]+z[k] for i in 1:3, j in 1:2, k in 1:3))

All of these return the same expression, so they must be equivalent. I remember not all of these worked a few years ago (I may be wrong), so I am curious if any of these can be chosen, or should I be careful while using one over the other. I would obviously prefer ex3 because of its readability, but any comments from JuMP developers would be appreciated. Thanks!

They are all equivalent. I’d use ex3.

2 Likes