## 5.186. length_first_sequence

Origin
Constraint

$\mathrm{\pi \pi \pi \pi \pi \pi }_\mathrm{\pi \pi \pi \pi \pi }_\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi }\left(\mathrm{\pi »\pi ΄\pi ½},\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\right)$

Arguments
 $\mathrm{\pi »\pi ΄\pi ½}$ $\mathrm{\pi \pi \pi \pi }$ $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }$ $\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi \pi }\left(\mathrm{\pi \pi \pi }-\mathrm{\pi \pi \pi \pi }\right)$
Restrictions
 $\mathrm{\pi »\pi ΄\pi ½}\beta ₯0$ $\mathrm{\pi »\pi ΄\pi ½}\beta €|\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }|$ $\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi }$$\left(\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi },\mathrm{\pi \pi \pi }\right)$
Purpose

$\mathrm{\pi »\pi ΄\pi ½}$ is the length of the maximum sequence of variables that take the same value that contains the first variable of the collection $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }$ (or 0 if the collection is empty).

Example
$\left(\begin{array}{c}3,β©\begin{array}{c}\mathrm{\pi \pi \pi }-4,\hfill \\ \mathrm{\pi \pi \pi }-4,\hfill \\ \mathrm{\pi \pi \pi }-4,\hfill \\ \mathrm{\pi \pi \pi }-5,\hfill \\ \mathrm{\pi \pi \pi }-5,\hfill \\ \mathrm{\pi \pi \pi }-4\hfill \end{array}βͺ\hfill \end{array}\right)$

The $\mathrm{\pi \pi \pi \pi \pi \pi }_\mathrm{\pi \pi \pi \pi \pi }_\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi }$ constraint holds since the sequence associated with the first value of the collection $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }=\beta ©4,4,4,5,5,4\beta ͺ$ spans over three consecutive variables.

Symmetry

All occurrences of two distinct values of $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }.\mathrm{\pi \pi \pi }$ can be swapped; all occurrences of a value of $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }.\mathrm{\pi \pi \pi }$ can be renamed to any unused value.

Reformulation

Without loss of generality let assume that the collection $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }=\beta ©{V}_{1},{V}_{2},...,{V}_{n}\beta ͺ$ has more than one variable. By introducing $2Β·n-1$ 0-1 variables, the $\mathrm{\pi \pi \pi \pi \pi \pi }_\mathrm{\pi \pi \pi \pi \pi }_\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi }$$\left(\mathrm{\pi »\pi ΄\pi ½},\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }\right)$ constraint can be expressed in term of $2Β·n-1$ reified constraints and one arithmetic constraint (i.e.,Β  a $\mathrm{\pi \pi \pi }_\mathrm{\pi \pi \pi }$ constraint). We first introduce $n-1$ variables that are respectively set to 1 if and only if two given consecutive variables of the collection $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }$ are equal:

Β Β Β ${B}_{1,2}\beta {V}_{1}={V}_{2}$,

Β Β Β ${B}_{2,3}\beta {V}_{2}={V}_{3}$,

Β Β Β $..................$

Β Β Β ${B}_{n-1,n}\beta {V}_{n-1}={V}_{n}$.

We then introduce $n$ variables ${A}_{1},{A}_{2},...,{A}_{n}$ that are respectively associated to the different sliding sequences starting on the first variable of the sequence ${V}_{1}{V}_{2}...{V}_{n}$. Variable ${A}_{i}$ is set to 1 if and only if ${V}_{1}={V}_{2}=...={V}_{i}$:

Β Β Β ${A}_{1}=1$,

Β Β Β ${A}_{2}\beta {B}_{1,2}\beta §{A}_{1},$

Β Β Β ${A}_{3}\beta {B}_{2,3}\beta §{A}_{2},$

Β Β Β $..................$

Β Β Β ${A}_{n}\beta {B}_{n-1,n}\beta §{A}_{n-1}$.

Finally we state the following arithmetic constraint:

Β Β Β $\mathrm{\pi »\pi ΄\pi ½}={A}_{1}+{A}_{2}+...+{A}_{n}$.

FigureΒ 5.186.1 depicts the automaton associated with the $\mathrm{\pi \pi \pi \pi \pi \pi }_\mathrm{\pi \pi \pi \pi \pi }_\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi }$ constraint. To each pair of consecutive variables $\left({\mathrm{\pi  \pi °\pi }}_{i},{\mathrm{\pi  \pi °\pi }}_{i+1}\right)$ of the collection $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }$ corresponds a signature variable ${\mathrm{\pi }}_{i}$. The following signature constraint links ${\mathrm{\pi  \pi °\pi }}_{i}$, ${\mathrm{\pi  \pi °\pi }}_{i+1}$ and ${\mathrm{\pi }}_{i}$: ${\mathrm{\pi  \pi °\pi }}_{i}={\mathrm{\pi  \pi °\pi }}_{i+1}\beta {\mathrm{\pi }}_{i}$.