I'm trying to model a constraint that tells me that my binary variable (index in months) can't be 1 in more than 2 consecutive periods of time. Here is a picture of a smilar constraint. Thank you so much.
This is quite straightforward from the constraint formulation in your example (within 3 consecutive variables the following code snippet allows at most 2 non-zeros):
y: array(O,T) of mpvar
forall(i in O, t in T) y(i,t) is_binary
forall(i in O, t in T | t<=T.last-2) sum(h in t..t+2) y(i,h) <= 2
! Display what has been defined
Retrieving data ...