How to write a sum constraint in JuMP with condition


I am having trouble writing the following constraint in JuMP

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

@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

where np is n’

Could someone please assist with the correct syntax?

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

@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

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


Noted and thank you. It worked.

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


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