我需要对JuMP / Julia实现以下伪代码:
forall{i in M, j in Ni[i]}: x[i] <= y[j];
我想像是:
for i in M and j in Ni[i]
@constraint(model, x[i] <= y[j])
end
如何在for循环中正确实现2个迭代器?
我不知道您是否要同时使用两个值或迭代器的笛卡尔积,但是这里是两个示例:
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)