### 3.7.252. 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},\cdots ,{𝚅}_{1{n}_{1}}\right),\cdots ,{C}_{m}\left({𝚅}_{m1},\cdots ,{𝚅}_{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 $𝚇$ with respect to 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 with respect to constraints ${C}_{1},\cdots ,{C}_{m}$ and variables $𝚇$ and $𝚈$. Forbidden regions are a way to represent constraints ${C}_{1},\cdots ,{C}_{m}$ that is suited for this sweep algorithm. A forbidden region of the constraint ${C}_{i}$ with respect to 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},\cdots ,{𝚅}_{i{n}_{i}}\right)$ has no solution in which $𝚇=x$ and $𝚈=y$.

Figure 3.7.78 shows five constraints and their respective forbidden regions (in pink) with respect to 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 $𝚈$.

##### Figure 3.7.78. Examples of forbidden regions (in pink) according to the two variables $𝚇$ and $𝚈$ ($𝚇\in \left[0,4\right]$, $𝚈\in \left[0,4\right]$) for five constraints We illustrate the use of the sweep algorithm on a concrete example. Assume that we want to find out the minimum value of variable $𝚇$ with respect to the conjunction of the five constraints that were introduced by Figure 3.7.78, 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.79 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.79) 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.79 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).

##### Figure 3.7.79. Sweep-line status while sweeping through the potential values of variable $𝚇$ (i.e., values from 0 to 4) until a potentially feasible point $𝚇=4$, $𝚈=4$ wrt the five constraints (A), (B), (C), (D) and (E) is found; the sweep-line status (i.e., the rightmost column) records for a potential value $v$ of variable $𝚇$ and for each potential value $w$ of variable $𝚈$ how many constraints are violated when both $𝚇=v$ and $𝚈=w$. A second similar application of the idea of sweep in the context of the cardinality operator [VanHentenryckDeville91], where all constraints have at least two variables in common, is reported in [BeldiceanuCarlssonSoftSweep01]. As before, each constraint $C$ of the cardinality operator is defined by its forbidden regions with respect to a pair of variables $\left(𝚇,𝚈\right)$ that occur in every constraint. In addition to that, a constraint $C$ is also defined by its safe regions, where a safe region is the set of assignments to the pair $\left(𝚇,𝚈\right)$ located in a rectangle such that the constraint always holds, no matter which values are taken by the other variables of $C$. Then the extended sweep algorithm filters the pair of variables $\left(𝚇,𝚈\right)$ right from the beginning according to the minimum and maximum number of constraints of the cardinality operator that have to hold.

A third 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).