### 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:

• $\mathrm{\pi }$ and $\mathrm{\pi }$ be two distinct variables,

• ${C}_{1}\left({\mathrm{\pi  }}_{11},\beta ―,{\mathrm{\pi  }}_{1{n}_{1}}\right),\beta ―,{C}_{m}\left({\mathrm{\pi  }}_{m1},\beta ―,{\mathrm{\pi  }}_{m{n}_{m}}\right)$ be a set of $m$ constraints such that all constraints mention $\mathrm{\pi }$ and $\mathrm{\pi }$.

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

FigureΒ 3.7.78 shows five constraints and their respective forbidden regions (in pink) with respect to two given variables $\mathrm{\pi }$ and $\mathrm{\pi }$ and their domains. The first constraint requires that $\mathrm{\pi }$, $\mathrm{\pi }$ and $\mathrm{\pi }$ 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(\mathrm{\pi },\mathrm{\pi }\right)$ and $\left(\mathrm{\pi },\mathrm{\pi }\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 $\mathrm{\pi }$ and $\mathrm{\pi }$.

We illustrate the use of the sweep algorithm on a concrete example. Assume that we want to find out the minimum value of variable $\mathrm{\pi }$ 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}\mathrm{\pi }\beta 0..4,\mathrm{\pi }\beta 0..4,\mathrm{\pi }\beta 0..9,\mathrm{\pi }\beta 0..2,\mathrm{\pi }\beta 0..3\hfill & \\ \mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi \pi \pi \pi }\left(β©\mathrm{\pi },\mathrm{\pi },\mathrm{\pi }βͺ\right)\hfill & \hfill \left(\mathrm{A}\right)\\ |\mathrm{\pi }-\mathrm{\pi }|>2\hfill & \hfill \left(\mathrm{B}\right)\\ \mathrm{\pi }+2\mathrm{\pi }-1<\mathrm{\pi }\hfill & \hfill \left(\mathrm{C}\right)\\ \mathrm{\pi }+1<\mathrm{\pi }\beta ¨\mathrm{\pi }+2<\mathrm{\pi }\beta ¨\mathrm{\pi }+3<\mathrm{\pi }\beta ¨\mathrm{\pi }+1<\mathrm{\pi }\hfill & \hfill \left(\mathrm{D}\right)\\ \left(\mathrm{\pi }+\mathrm{\pi }\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 $\mathrm{\pi }$ 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 $\mathrm{\pi }$ 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 $\mathrm{\pi }$ is 4, since this is the first position of the sweep-line where the sweep-line status contains a value of $\mathrm{\pi }$ which is not forbidden (i.e.,Β $\mathrm{\pi }=4$, $\mathrm{\pi }=0$ is not covered by any forbidden region).

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(\mathrm{\pi },\mathrm{\pi }\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(\mathrm{\pi },\mathrm{\pi }\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(\mathrm{\pi },\mathrm{\pi }\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{\pi \pi \pi \pi \pi }$ and the $\mathrm{\pi \pi \pi \pi \pi }$ 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{\pi \pi \pi \pi \pi }$ and the $\mathrm{\pi \pi \pi \pi \pi }$ constraints).