繁体   English   中英

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

[英]For loop with 2 iterators in Julia/JuMP

我需要为 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)

你要

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

以下是相关文档: https://www.juliaopt.org/JuMP.jl/stable/constraints/#Constraint-containers-1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM