### 3.7.239. Sweep

A constraint for which the filtering algorithm may use a sweep algorithm. A sweep algorithm [PreparataShamos85] solves a problem by moving an imaginary object (usually a line, a plane or sometime a point). The object does not move continuously, but only at particular points where we actually do something. A sweep algorithm uses the following two data structures:

• A data structure called the sweep status, which contains information related to the current position of the object that moves,

• A data structure named the event point series, which holds the events to process.

The algorithm initialises the sweep status for the initial position of the imaginary object. Then the object jumps from one event to the next event; each event is handled by updating the status of the sweep.

A first typical application reported in [BeldiceanuCarlsson01sweep] of the idea of sweep within the context of constraint programming is to aggregate several constraints that have two variables in common in order to perform more deduction. Let:

• $𝚇$ and $𝚈$ be two distinct variables,

• ${C}_{1}\left({𝚅}_{11},...,{𝚅}_{1{n}_{1}}\right),...,{C}_{m}\left({𝚅}_{m1},...,{𝚅}_{m{n}_{m}}\right)$ be a set of $m$ constraints such that all constraints mention $𝚇$ and $𝚈$.

The sweep algorithm tries to adjust the minimum value of $𝚇$ wrt. the conjunction of the previous constraints by moving a sweep -line from the minimum value of $𝚇$ to its maximum value. It accumulates within the sweep -line status the values to be currently removed from the domain of $𝚈$. If, for the current position $\Delta$ of the sweep -line, all values of $𝚈$ have to be removed, then the algorithm removes value $\Delta$ from the domain of $𝚇$. The events to process correspond to the starts and ends of forbidden two -dimensional regions wrt. constraints ${C}_{1},...,{C}_{m}$ and variables $𝚇$ and $𝚈$. Forbidden regions are a way to represent constraints ${C}_{1},...,{C}_{m}$ that is suited for this sweep algorithm. A forbidden region of the constraint ${C}_{i}$ wrt. the variables $𝚇$ and $𝚈$ is an ordered pair $\left(\left[{F}_{x}^{-},{F}_{x}^{+}\right],\left[{F}_{y}^{-},{F}_{y}^{+}\right]\right)$ of intervals such that: $\forall x\in \left[{F}_{x}^{-},{F}_{x}^{+}\right],\forall y\in \left[{F}_{y}^{-},{F}_{y}^{+}\right]:{C}_{i}\left({𝚅}_{i1},...,{𝚅}_{i{n}_{i}}\right)$ has no solution in which $𝚇=x$ and $𝚈=y$.

Figure 3.7.66 shows five constraints and their respective forbidden regions (in pink) wrt. two given variables $𝚇$ and $𝚈$ and their domains. The first constraint requires that $𝚇$, $𝚈$ and $𝚁$ be pairwise distinct. Constraints (B,C) are usual arithmetic constraints.Within the context of continuous variables, Chabertet al. [ChabertBeldiceanu10] shows how to compute a forbidden region that contains a given unfeasible point for numerical constraints with arbitrary mathematical expressions. Constraint (D) can be interpreted as requiring that two rectangles of respective origins $\left(𝚇,𝚈\right)$ and $\left(𝚃,𝚄\right)$ and sizes $\left(2,4\right)$ and $\left(3,2\right)$ do not overlap. Finally, constraint (E) is a parity constraint of the sum of $𝚇$ and $𝚈$.

We illustrate the use of the sweep algorithm on a concrete example. Assume that we want to find out the minimum value of variable $𝚇$ wrt. the conjunction of the five constraints that were introduced by Figure 3.7.66, that is versus the following conjunction of constraints:

$\left\{\begin{array}{cc}𝚇\in 0..4,𝚈\in 0..4,𝚁\in 0..9,𝚃\in 0..2,𝚄\in 0..3\hfill & \\ \mathrm{𝚊𝚕𝚕𝚍𝚒𝚏𝚏𝚎𝚛𝚎𝚗𝚝}\left(〈𝚇,𝚈,𝚁〉\right)\hfill & \hfill \left(\mathrm{A}\right)\\ |𝚇-𝚈|>2\hfill & \hfill \left(\mathrm{B}\right)\\ 𝚇+2𝚈-1<𝚂\hfill & \hfill \left(\mathrm{C}\right)\\ 𝚇+1<𝚃\vee 𝚃+2<𝚇\vee 𝚈+3<𝚄\vee 𝚄+1<𝚈\hfill & \hfill \left(\mathrm{D}\right)\\ \left(𝚇+𝚈\right)\mathrm{mod}2=2\hfill & \hfill \left(\mathrm{E}\right)\end{array}\right\$

Figure 3.7.67 shows the content of the sweep -line status (i.e., the forbidden values for $𝚈$ according the current position of the sweep -line) for different positions of the sweep -line. More precisely, the sweep -line status can be viewed as an array (see the rightmost part of Figure 3.7.67) which records for each possible value of $𝚈$ the number of forbidden regions that currently intersect the sweep -line (see the leftmost part of Figure 3.7.67 where these forbidden regions are coloured in red). The smallest possible value of $𝚇$ is 4, since this is the first position of the sweep -line where the sweep -line status contains a value of $𝚈$ which is not forbidden (i.e., $𝚇=4$, $𝚈=0$ is not covered by any forbidden region).

A second typical application reported in [BeldiceanuCarlssonPoderSadekTruchet07] and in [CarlssonBeldiceanuMartin08] of the idea of sweep within the context of multi -dimensional  placement problems (see for instance the $\mathrm{𝚍𝚒𝚏𝚏𝚗}$ and the $\mathrm{𝚐𝚎𝚘𝚜𝚝}$ constraints) for filtering each coordinate of the origin of an object $o$ to place is as follows. To adjust the minimum (respectively maximum) value of a coordinate of the origin we perform a recursive traversal of the  placement space in increasing (respectively decreasing) lexicographic order and skips infeasible points that are located in a multi -dimensional forbidden set. Each multi -dimensional forbidden set is computed from a constraint where object $o$ occurs (for instance a non -overlapping constraint in the context of the $\mathrm{𝚍𝚒𝚏𝚏𝚗}$ and the $\mathrm{𝚐𝚎𝚘𝚜𝚝}$ constraints).