# MILP: formulation of sums with barriers

**URL:** <https://discourse.julialang.org/t/milp-formulation-of-sums-with-barriers/89067>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [October 21, 2022, 2:47pm UTC](https://discourse.julialang.org/t/milp-formulation-of-sums-with-barriers/89067 "2022-10-21T14:47:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MeryKay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/merykay/32/43753_2.png) [@MeryKay](https://discourse.julialang.org/u/MeryKay)\
**Post date:** [October 21, 2022, 2:47pm UTC](https://discourse.julialang.org/t/milp-formulation-of-sums-with-barriers/89067/1 "2022-10-21T14:47:58Z")

</div>

I want to program a sum over an indexed variable, whereby the index has a barrier just for this constraint.

`sum(X[t] for t=1:T)`

is clear to me, but how can I integrate the barrier of t\<= R\_j as shown in the screenshot.

![grafik](https://global.discourse-cdn.com/julialang/original/3X/e/e/ee14f6176e2b310308db9f1449e2da6679d756f2.png)

Many thanks in advance!

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [October 21, 2022, 2:53pm UTC](https://discourse.julialang.org/t/milp-formulation-of-sums-with-barriers/89067/2 "2022-10-21T14:53:30Z")

</div>

I think you are asking how to add a conditional (a filter) in a “comprehension”. Does this work for you: `sum(x[i] for i in 1:10 if i<5)`

Generally `EXPRESSION for INDEX in RANGE if PREDICATE`.

See [this documentation page for more](https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions)

---

<div class="post-metadata">

**Author:** ![MeryKay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/merykay/32/43753_2.png) [@MeryKay](https://discourse.julialang.org/u/MeryKay)\
**Post date:** [October 21, 2022, 3:04pm UTC](https://discourse.julialang.org/t/milp-formulation-of-sums-with-barriers/89067/3 "2022-10-21T15:04:29Z")

</div>

**That makes totally sense! Thank you.**

While working with indices I always get back this:

`sum(x[i] for i in 1:10 if i<5)`

`MethodError: no method matching getindex(::VariableRef, ::Int64)`

Do you have any idea what might be missing?

JuMP is already added as a Pkg

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [October 21, 2022, 3:10pm UTC](https://discourse.julialang.org/t/milp-formulation-of-sums-with-barriers/89067/4 "2022-10-21T15:10:43Z")

</div>

`x` needs to be an indexable collection datastructure to permit `[]` syntax. `getindex` is the function being called when you use `[]`. Presumably, `x` is not something that permits indexing. I do not know much about JuMP, so I do not know how they set up collections of variables. Any chance it is just a capitalization issue? Your original code used capital `X`, while the example I gave uses a lower `x` which might be some other unrelated variable you have defined.

Also, I would suggest experimenting with these expressions in the REPL. Just typing `x` to see what prints out and whether it is the object you expect it to be.

---

<div class="post-metadata">

**Author:** ![MeryKay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/merykay/32/43753_2.png) [@MeryKay](https://discourse.julialang.org/u/MeryKay)\
**Post date:** [October 21, 2022, 5:17pm UTC](https://discourse.julialang.org/t/milp-formulation-of-sums-with-barriers/89067/5 "2022-10-21T17:17:48Z")

</div>

It works, thanks!
