# How to write a sum constraint in JuMP with condition

**URL:** https://discourse.julialang.org/t/how-to-write-a-sum-constraint-in-jump-with-condition/56269
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [March 1, 2021, 5:49pm UTC](https://discourse.julialang.org/t/how-to-write-a-sum-constraint-in-jump-with-condition/56269 "2021-03-01T17:49:21Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![GraceMabele](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gracemabele/32/21360_2.png) [@GraceMabele](https://discourse.julialang.org/u/GraceMabele)
#### Post date: [March 1, 2021, 5:49pm UTC](https://discourse.julialang.org/t/how-to-write-a-sum-constraint-in-jump-with-condition/56269/1 "2021-03-01T17:49:21Z")

</div>

Hi,

I am having trouble writing the following constraint in JuMP

 ![2021-03-01](https://global.discourse-cdn.com/julialang/original/3X/d/6/d65fbf1b6abf1183b49040f765930accd5a70cae.png)

I currently have it written as below but it is not returning the desired output.

```julia
@constraints model begin
    constraint_1[n in n],
      sum(w[i,n,np] for i in Ij for np in 1:length(n) if n <= np <= (n+d) ) <= 1.0
 end

```

where np is n’

Could someone please assist with the correct syntax?

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [March 1, 2021, 5:56pm UTC](https://discourse.julialang.org/t/how-to-write-a-sum-constraint-in-jump-with-condition/56269/2 "2021-03-01T17:56:36Z")

</div>

A simple rule is that everything at the right of the ∀ (universal quantifier, \forall) should be in your `constraint_name[...]` clause. so:

```julia
@constraints model begin
    constraint_1[n in n, j in j],
      sum(w[i,n,np] for i in I[j] for np in n:(n+d)) <= 1.0
 end

```

Also, please stop using Sets with lowercase letters and iterate over them with the same lowercase letters, this is bad style.

---

<div class="post-metadata">

### Author: ![GraceMabele](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gracemabele/32/21360_2.png) [@GraceMabele](https://discourse.julialang.org/u/GraceMabele)
#### Post date: [March 1, 2021, 6:10pm UTC](https://discourse.julialang.org/t/how-to-write-a-sum-constraint-in-jump-with-condition/56269/3 "2021-03-01T18:10:21Z")

</div>

Noted and thank you. It worked.

---

<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 1, 2021, 7:03pm UTC](https://discourse.julialang.org/t/how-to-write-a-sum-constraint-in-jump-with-condition/56269/4 "2021-03-01T19:03:58Z")

</div>

To clarify why it’s bad style, if you have `[n in n, j in j]` instead of `[n in N, j in J]`, and you have something in the first constraint like `for np in 1:length(n)`, it is using `length(n)` and not `length(N)`.

---

<div class="post-metadata">

### Author: ![GraceMabele](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gracemabele/32/21360_2.png) [@GraceMabele](https://discourse.julialang.org/u/GraceMabele)
#### Post date: [March 1, 2021, 8:30pm UTC](https://discourse.julialang.org/t/how-to-write-a-sum-constraint-in-jump-with-condition/56269/5 "2021-03-01T20:30:11Z")

</div>

I see. i have made the changes and it helped with other errors I was encountering. Thank you
