Julia 跳跃约束的嵌套迭代

Nested iterations for Julia Jump Constraint

我正在尝试尝试嵌套迭代以稍微压缩我的代码。我有一个 运行 的大 MIP,但代码非常混乱。我想把它压缩成向量等

我的代码基本上如下:

using JuMP
using Gurobi
model = Model(with_optimizer(Gurobi.Optimizer))

@variable(model, x[1:11, 1:17, 1:54], Bin)

I = [(1:6),(7:11),(1:6),(7:11)]
K = [(51:54), (1:4), (1:50),(5:54)]
RHS = [4,4,0,0]

@constraints(model, begin
 [i in I[1]], sum(x[i,17,k] for k in K[1]) == RHS[1]
 [i in I[2]], sum(x[i,17,k] for k in K[2]) == RHS[2]
 [i in I[3]], sum(x[i,17,k] for k in K[3]) == RHS[4]
 [i in I[4]], sum(x[i,17,k] for k in K[4]) == RHS[4]
end
)

基本上我想将所有这些约束压缩到一行,就像在程序中进一步完成的一样,我有类似的约束,有 54 次迭代。

我试过:

@constraint(model, 
    for (a,b,c) in zip(I, K, RHS)
        [i in a], sum(x[i,17,k] for k in b) == c
    end
)

和其他一些组合,例如

@constraint(model, [(a,b,c) in zip(I, K, RHS), i in a], sum(x[i,17,k] for k in b) == c)

但它不适合我 - 我会 运行 加载错误或重复迭代器错误。

不胜感激!!! :-)

我以某种方式让它工作:

@constraint(model, [(a,b,c) in zip(I,K,RHS), i in a], 
    sum(x[i,17,k] for k in b) == c)

这个版本对我有用:

@constraint(
    model, 
    [(a, b, c) in zip(I, K, RHS), i in a], 
    sum(x[i, 17, k] for k in b) == c
)

另一个更易读的版本是

for (a, b, c) in zip(I, K, RHS)
   @constraint(model, [i in a], sum(x[i, 17, k] for k in b) == c)
end