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

**URL:** https://discourse.julialang.org/t/how-to-write-a-constraint-in-jump-where-the-index-has-if-else-structure/21635
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [March 8, 2019, 2:27pm UTC](https://discourse.julialang.org/t/how-to-write-a-constraint-in-jump-where-the-index-has-if-else-structure/21635 "2019-03-08T14:27:11Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)
#### Post date: [March 8, 2019, 2:27pm UTC](https://discourse.julialang.org/t/how-to-write-a-constraint-in-jump-where-the-index-has-if-else-structure/21635/1 "2019-03-08T14:27:11Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![Rajat\_Sanyal](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@Rajat\_Sanyal](https://discourse.julialang.org/u/Rajat_Sanyal)
#### Post date: [March 8, 2019, 3:07pm UTC](https://discourse.julialang.org/t/how-to-write-a-constraint-in-jump-where-the-index-has-if-else-structure/21635/2 "2019-03-08T15:07:06Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [March 8, 2019, 3:15pm UTC](https://discourse.julialang.org/t/how-to-write-a-constraint-in-jump-where-the-index-has-if-else-structure/21635/3 "2019-03-08T15:15:04Z")

</div>

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

```julia
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)

```julia
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

```

---

<div class="post-metadata">

### Author: ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)
#### Post date: [March 8, 2019, 3:35pm UTC](https://discourse.julialang.org/t/how-to-write-a-constraint-in-jump-where-the-index-has-if-else-structure/21635/4 "2019-03-08T15:35:35Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)
#### Post date: [March 8, 2019, 3:42pm UTC](https://discourse.julialang.org/t/how-to-write-a-constraint-in-jump-where-the-index-has-if-else-structure/21635/5 "2019-03-08T15:42:20Z")

</div>

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