Chris Swan
Chris Swan

Reputation: 57

Nested iterations for Julia Jump Constraint

I'm attempting to try and nest iterations to condense my code a bit. I've got a big MIP that runs, but with really messy code. I'd like to condense it into vectors, etc.

my code is essentially as follows:

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
)

Essentially I want to condense all these constraints down to one line as further done in the program I have similar constraints that have 54 iterations.

I've tried:

@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
)

and a few other combinations, like

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

but it just won't groove for me - I'll run unto Load errors or duplicate iterator errors.

Help would be greatly appreciated!!! :-)

Upvotes: 1

Views: 506

Answers (2)

Chris Swan
Chris Swan

Reputation: 57

I somehow got it working:

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

Upvotes: 0

Oscar Dowson
Oscar Dowson

Reputation: 2574

This version worked for me:

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

Another, slightly more readable version is

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

Upvotes: 1

Related Questions