Integer linear programming constraint for checking if the number of consecutive 1's is greater than threshold

454 Views Asked by At

Suppose I have a list "y[x]" of N elements with the value of element constrained to be [0,1] .

e.g. y = [1,0,0,1,1,0,0,0,1,1,1,1,1,0,0,1,1,1,1,0,0]

and I want to construct a list "z[x]" from it such that it should have only consecutive 1's if the count of consecutive 1's in y is greater than 3(threshold).

e.g. z = [0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,1,1,1,1,0,0].

I need to formulate ILP equations out of these. I think I need some kind of sliding window approach but I am not able to come up with a solution.

1

There are 1 best solutions below

1
On

It seems that you want $z_i=1$ if and only if $y_i=1$ and $y_i$ is part of a string of at least four consecutive ones. One way to do this is to introduce an additional vector of binary variables, $w$, where $w_i$ signals that $y_i$ and the next three $y$ entries are all 1. You can do this with the following four inequalities: \begin{align*} 4w_{i} & \le y_{i}+y_{i+1}+y_{i+2}+y_{i+3}\\ w_{i} & \ge y_{i}+y_{i+1}+y_{i+2}+y_{i+3}-3\\ z_{i} & \le w_{i-3}+w_{i-2}+w_{i-1}+w_{i}\\ 4z_{i} & \ge w_{i-3}+w_{i-2}+w_{i-1}+w_{i} \end{align*} In your example, $w=[0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0]$. The first two inequalities hold for $i=1,\dots,N-3$ with $w_{N-2}=w_{N-1}=w_N=0$; the last two hold for $i=1,\dots,N$ with $w_{-2}=w_{-1}=w_0=0$.