For loop with 2 iterators in Julia/JuMP

Viewed 468

I need to implement the following pseudocode to JuMP/Julia:

forall{i in M, j in Ni[i]}:  x[i] <= y[j];

I imagine something like:

for i in M and j in Ni[i]
    @constraint(model, x[i] <= y[j])
end

How do I properly implement the 2 iterators in the for loop?

2 Answers

I don't know if you want one iteration with both values, or the Cartesian product of the iterators, but here are example for both:

julia> M = 1:3; N = 4:6;

julia> for (m, n) in zip(M, N) # single iterator over both M and N
           @show m, n
       end
(m, n) = (1, 4)
(m, n) = (2, 5)
(m, n) = (3, 6)

julia> for m in M, n in N # Cartesian product
           @show m, n
       end
(m, n) = (1, 4)
(m, n) = (1, 5)
(m, n) = (1, 6)
(m, n) = (2, 4)
(m, n) = (2, 5)
(m, n) = (2, 6)
(m, n) = (3, 4)
(m, n) = (3, 5)
(m, n) = (3, 6)
Related