MILP: formulation of sums with barriers

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.


Many thanks in advance!

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)


See this documentation page for more

1 Like

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

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.

1 Like

It works, thanks!