Peter wrote:
> Hey Guys,
>
> I have a set of binary variables y(i), where i= 1,2, ...,10. I wish to
> write a constraint to enforce that exactly two subsequent binary
> variables be equal to 1 and the rest be equal to zero (this might also
> include y(1) and y(10) ), i.e.:
>
> y(i) and y(i+1) = 1 and the rest equl to zero, for some i, except
> 1
> or
> y(1) and y(10)= 1 and the rest equal to zero,
>
> and exactly tow binary varialbes should be nonzero:
> sum(i, y (i) ) = 2
>
> Is there any way to do this using linear constraints?
>
>
If your solver supports SOS2 constraints, this is easy. Create one more
binary variable y(11) and put in the two constraints
y(1) + ... + y(11) = 2
y(1) = y(11)
and then declare y(1),...,y(11) to be a type 2 special ordered set (SOS2).
If your solver does not understand SOS2, try this:
y(1) + ... + y(10) = 2
y(1) <= y(10) + y(2)
y(2) <= y(1) + y(3)
y(3) <= y(2) + y(4)
...
y(9) <= y(8) + y(10)
y(10) <= y(9) + y(1)
/Paul