## 5.187. length_last_sequence

Origin
Constraint

$\mathrm{\pi \pi \pi \pi \pi \pi }_\mathrm{\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 last 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}1,β©\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 }_\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi }$ constraint holds since the sequence associated with the last value of the collection $\mathrm{\pi  \pi °\pi \pi Έ\pi °\pi ±\pi »\pi ΄\pi }=\beta ©4,4,4,5,5,4\beta ͺ$ spans over one single variable.

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 }_\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}_{n-1,n}\beta {V}_{n-1}={V}_{n}$,

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

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

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

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

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

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

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

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

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

Finally we state the following arithmetic constraint:

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

FigureΒ 5.187.1 depicts the automaton associated with the $\mathrm{\pi \pi \pi \pi \pi \pi }_\mathrm{\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}$.