## 5.186. length_first_sequence

Origin
Constraint

$\mathrm{𝚕𝚎𝚗𝚐𝚝𝚑}_\mathrm{𝚏𝚒𝚛𝚜𝚝}_\mathrm{𝚜𝚎𝚚𝚞𝚎𝚗𝚌𝚎}\left(\mathrm{𝙻𝙴𝙽},\mathrm{𝚅𝙰𝚁𝙸𝙰𝙱𝙻𝙴𝚂}\right)$

Arguments
 $\mathrm{𝙻𝙴𝙽}$ $\mathrm{𝚍𝚟𝚊𝚛}$ $\mathrm{𝚅𝙰𝚁𝙸𝙰𝙱𝙻𝙴𝚂}$ $\mathrm{𝚌𝚘𝚕𝚕𝚎𝚌𝚝𝚒𝚘𝚗}\left(\mathrm{𝚟𝚊𝚛}-\mathrm{𝚍𝚟𝚊𝚛}\right)$
Restrictions
 $\mathrm{𝙻𝙴𝙽}\ge 0$ $\mathrm{𝙻𝙴𝙽}\le |\mathrm{𝚅𝙰𝚁𝙸𝙰𝙱𝙻𝙴𝚂}|$ $\mathrm{𝚛𝚎𝚚𝚞𝚒𝚛𝚎𝚍}$$\left(\mathrm{𝚅𝙰𝚁𝙸𝙰𝙱𝙻𝙴𝚂},\mathrm{𝚟𝚊𝚛}\right)$
Purpose

$\mathrm{𝙻𝙴𝙽}$ is the length of the maximum sequence of variables that take the same value that contains the first variable of the collection $\mathrm{𝚅𝙰𝚁𝙸𝙰𝙱𝙻𝙴𝚂}$ (or 0 if the collection is empty).

Example
$\left(\begin{array}{c}3,〈\begin{array}{c}\mathrm{𝚟𝚊𝚛}-4,\hfill \\ \mathrm{𝚟𝚊𝚛}-4,\hfill \\ \mathrm{𝚟𝚊𝚛}-4,\hfill \\ \mathrm{𝚟𝚊𝚛}-5,\hfill \\ \mathrm{𝚟𝚊𝚛}-5,\hfill \\ \mathrm{𝚟𝚊𝚛}-4\hfill \end{array}〉\hfill \end{array}\right)$

The $\mathrm{𝚕𝚎𝚗𝚐𝚝𝚑}_\mathrm{𝚏𝚒𝚛𝚜𝚝}_\mathrm{𝚜𝚎𝚚𝚞𝚎𝚗𝚌𝚎}$ constraint holds since the sequence associated with the first value of the collection $\mathrm{𝚅𝙰𝚁𝙸𝙰𝙱𝙻𝙴𝚂}=〈4,4,4,5,5,4〉$ spans over three consecutive variables.

Symmetry

All occurrences of two distinct values of $\mathrm{𝚅𝙰𝚁𝙸𝙰𝙱𝙻𝙴𝚂}.\mathrm{𝚟𝚊𝚛}$ can be swapped; all occurrences of a value of $\mathrm{𝚅𝙰𝚁𝙸𝙰𝙱𝙻𝙴𝚂}.\mathrm{𝚟𝚊𝚛}$ can be renamed to any unused value.

Reformulation

Without loss of generality let assume that the collection $\mathrm{𝚅𝙰𝚁𝙸𝙰𝙱𝙻𝙴𝚂}=〈{V}_{1},{V}_{2},...,{V}_{n}〉$ has more than one variable. By introducing $2·n-1$ 0-1 variables, the $\mathrm{𝚕𝚎𝚗𝚐𝚝𝚑}_\mathrm{𝚏𝚒𝚛𝚜𝚝}_\mathrm{𝚜𝚎𝚚𝚞𝚎𝚗𝚌𝚎}$$\left(\mathrm{𝙻𝙴𝙽},\mathrm{𝚅𝙰𝚁𝙸𝙰𝙱𝙻𝙴𝚂}\right)$ constraint can be expressed in term of $2·n-1$ reified constraints and one arithmetic constraint (i.e.,  a $\mathrm{𝚜𝚞𝚖}_\mathrm{𝚌𝚝𝚛}$ 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{𝚅𝙰𝚁𝙸𝙰𝙱𝙻𝙴𝚂}$ are equal:

${B}_{1,2}⇔{V}_{1}={V}_{2}$,

${B}_{2,3}⇔{V}_{2}={V}_{3}$,

$..................$

${B}_{n-1,n}⇔{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}⇔{B}_{1,2}\wedge {A}_{1},$

${A}_{3}⇔{B}_{2,3}\wedge {A}_{2},$

$..................$

${A}_{n}⇔{B}_{n-1,n}\wedge {A}_{n-1}$.

Finally we state the following arithmetic constraint:

$\mathrm{𝙻𝙴𝙽}={A}_{1}+{A}_{2}+...+{A}_{n}$.

Keywords
Automaton

Figure 5.186.1 depicts the automaton associated with the $\mathrm{𝚕𝚎𝚗𝚐𝚝𝚑}_\mathrm{𝚏𝚒𝚛𝚜𝚝}_\mathrm{𝚜𝚎𝚚𝚞𝚎𝚗𝚌𝚎}$ constraint. To each pair of consecutive variables $\left({\mathrm{𝚅𝙰𝚁}}_{i},{\mathrm{𝚅𝙰𝚁}}_{i+1}\right)$ of the collection $\mathrm{𝚅𝙰𝚁𝙸𝙰𝙱𝙻𝙴𝚂}$ corresponds a signature variable ${𝚂}_{i}$. The following signature constraint links ${\mathrm{𝚅𝙰𝚁}}_{i}$, ${\mathrm{𝚅𝙰𝚁}}_{i+1}$ and ${𝚂}_{i}$: ${\mathrm{𝚅𝙰𝚁}}_{i}={\mathrm{𝚅𝙰𝚁}}_{i+1}⇔{𝚂}_{i}$.