How to write a constraint in JuMP where the index has if-else structure

jump

#1

I have a little question in how to write a constraint in Jump language. The constraint is:
sum_{i} sum_{r=0}^{9} x[i,j,t-r] <= 1, for all t=1,…,12 and j=1,…,9
if t - r < 0, change by t - r + 9.
How can I use a if-else structure inside a sum() to incorporate the condition above?
Thanks!


#2

Could you explain a bit more? What is the range of i? Note that there is a term t-r=0 which is basically the zeroth index of the variable. Julia array starts from 1, so that would be an error.


#3

Option 1: use Julia’s inplace cond ? true : false syntax

for t in 1:12, j in 1:9
    @constraint(model, sum(x[i, j, t-r < 0 ? t-r : t-r+9] i in 1:N, r in 0:9) <= 1)
end

Option 2: make a list of the third indices (can be much more general)

for t in 1:12, j in 1:9
    third_index =Int[]
    for r in 0:9 
        if t - r < 0
            push!(third_index, t - r + 9)
        else
            push!(third_index, t - r)
        end
    end
    @constraint(model, sum(x[i, j, k] i in 1:N, k in third_index) <= 1)
end

#4

Thank you for your response.
The range of i is i=1,…,30.
The constraint is given by:

sum_{i=1}^{30} sum_{r = 0}^{9} x[i,j,t-r] <=1 for all j=1,…,9 and t= 1,…,12.
But, in my model, if t - r <=0, then chance the term t-r by t - r + 12.

Did you understand my problem?


#5

Wonderfull!
You solved my problem!!!
Best regards.