Julia/JuMP 中带有 2 个迭代器的 For 循环

bak*_*kun 3 optimization mathematical-optimization linear-programming julia julia-jump

我需要为 JuMP/Julia 实现以下伪代码:

forall{i in M, j in Ni[i]}:  x[i] <= y[j];
Run Code Online (Sandbox Code Playgroud)

我想象这样的事情:

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

如何在 for 循环中正确实现 2 个迭代器?

fre*_*kre 6

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)
Run Code Online (Sandbox Code Playgroud)