5

I declare an array of binary variables as $y(i), i = 1, ..., N$

I would like to model the following:

If $y(i-1) + y(i) = 1$ then $y(k) = 0$ for $k < i$ and $y(m) = 1$ for $m \geq i$

To make the question clear, here is an example:

Suppose I have the following 10 binary variables.

$$y(1), y(2), y(3), y(4), y(5), y(6), y(7), y(8), y(9), y(10)$$

The following is true: $y(i-1) \leq y(i)$ for $i > 2 $

The optimiser is supposed to set the values of the variables in a pattern like the following: $(y(1), y(2), y(3), y(4), y(5), y(6), y(7), y(8), y(9), y(10)) = ( 0,0,0,0,0,1,1,1,1,1)$

I know that I will get a pattern like the one mentioned, but I don't know when the first $1$ will appear. I need to determine the variable $y(i)$ that gets first the value $1$ and then set all variables to its right to $1$ and all variables to its left to $0$. So I need to determine when $y(i) + y(i-1) = 1$, knowing that this implies $y(i) = 1$ and $y(i-1)=0$.

Clement
  • 2,252
  • 7
  • 13

2 Answers2

7

You could simply write $$y(i) - y(i - 1) \ge 0, \qquad i=2,...,N$$

RobPratt
  • 32,006
  • 1
  • 44
  • 84
Claudio Contardo
  • 1,574
  • 6
  • 13
2

New answer based on modified question. If you have constraints $y_i \ge y_{i-1}$, the value of $y_i-y_{i-1}$ indicates whether $y_i+y_{i-1}=1$, and this can happen only once.

RobPratt
  • 32,006
  • 1
  • 44
  • 84
  • Hi Rob! You are right. The discussion sofar, before the modification, led me to that answer as well. Thank you all, especially Claudio and you! – Clement Mar 12 '20 at 16:39