gpt4 book ai didi

optimization - 在 Julia/JuMP 中使用 2 个迭代器进行循环

转载 作者:行者123 更新时间:2023-12-04 00:19:28 25 4
gpt4 key购买 nike

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

关于optimization - 在 Julia/JuMP 中使用 2 个迭代器进行循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61667415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com