我需要实现JuMP/Jjulia的以下伪代码:
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)
您想要
@constraint(model, [i = M, j = Ni[i]], x[i] <= y[j])
以下是相关文件:https://www.juliaopt.org/JuMP.jl/stable/constraints/#Constraint-集装箱-1