# How to model this constraint using integer linear programming?

Suppose we have $S=\{1,2,\ldots,n\}$, a binary variable $x$ and an integer $p$. I would like to model the following constraint using integer linear programming:

If $x = 1$, then there must exists a unique $i\in S$ such that: $x_j=1$ for all $j\in\{i,i+1,\ldots,i+p-1\}$.

How can I do this?

• Is $x$ a scalar or a vector? – Rodrigo de Azevedo Apr 11 '17 at 9:07
• $x$ is a scalar – Zir Apr 12 '17 at 1:24

Assuming $x_j$ are binary.
The additional binary variable $y_i$ equals one if and only if when all $j$ are 1.
$$\sum\limits_{j=i}^{i+p-1}x_j - p+1 \leq y_i \leq \frac{1}{p}\sum\limits_{j=i}^{i+p-1}x_j$$
Then to have a unique $y_i$ equals one you add $$x \leq \sum_{i=1}^n y_i \leq (n-1)(1-x)+1$$