Part IV · trunk C · constraint programming and SAT · spine

18

Global constraints and their filtering algorithms

One constraint that sees the whole structure beats many that each see a pair. Matching filters alldifferent; compulsory parts filter cumulative; the price is paid per call.

needs 17, 15~1¾ h slides + 3 h lablab: uv run co lab 18

18 · overview

The route

1 · why global

What a decomposition cannot see; strength versus cost.

2 · alldifferent

Hall sets, the value graph, Régin's matching-based filter.

3 · scheduling

Cumulative: timetable, edge-finding, disjunctive.

4 · the catalogue

element, table, circuit, gcc — and the one design question.

getting around

Words with a dotted underline are glossary terms: click one for its definition. G lists every term in the unit, N shows the notes. Optional extra: interactive figures, linked from the slides they support by a small ▸ chip; skip them freely.

section 1

Why global

Pairs cannot count.

18 · why global

What pairwise ≠ cannot see

$$x_1, x_2 \in \{1, 2\}, \quad x_3 \in \{1, 2, 3\}, \quad x_4 \in \{2, 3, 4\}$$
  • Pairwise ≠ (the decomposition, arc consistent): no variable is fixed, so nothing is pruned.
  • alldifferent (generalised arc consistent): $\{x_1, x_2\}$ use up $\{1, 2\}$, so $x_3 = 3$, so $x_4 = 4$.

$\{x_1, x_2\}$ is a Hall set: $k$ variables whose domains together hold exactly $k$ values. By Hall's theorem (unit 15), alldifferent is satisfiable iff no set of variables has fewer values than members.

the general fact

Arc consistency on a decomposition is at most as strong as generalised arc consistency on the global constraint, and often strictly weaker. For alldifferent, pairwise AC equals GAC only in the trivial case of fixed variables.

the design question

A stronger propagator removes more and costs more per call. It wins only if the nodes saved outweigh the time per node. That trade-off (strength vs cost) is the only design question in CP, and lab then shows both outcomes.

section 2

alldifferent

Unit 15, doing load-bearing work.

18 · alldifferent

Régin 1994: matching-based filtering

x₁x₂x₃x₄ 1234

The value graph of the example. Thick: a maximum matching. Dashed: edges in no maximum matching, removed. {x₁, x₂} ↔ {1, 2} is an alternating cycle, so those edges stay. The next slide works it out.

  1. Build the value graph and find a maximum matching (Hopcroft–Karp). If it doesn't cover every variable: fail.
  2. By Berge, an edge lies in some maximum matching iff it is matched, or on an even alternating cycle, or on an even alternating path from a free value.
  3. Orient matched edges value → variable and the rest variable → value. Cycles are SCCs, and paths are reachability to free values. Remove every other edge.

$O(m \sqrt n)$ for the matching and $O(m)$ for the rest: Régin's filter. Incrementally, the old matching is repaired rather than recomputed (incremental matching).

Lab step 2's test: on 80 random instances, the kept values equal brute-force supports exactly.

18 · alldifferent · example

Régin's filter, on the example

$x_1, x_2 \in \{1,2\}$, $x_3 \in \{1,2,3\}$, $x_4 \in \{2,3,4\}$. Ten edges in the value graph.

stepresult
1. maximum matching$x_1\!-\!1$, $x_2\!-\!2$, $x_3\!-\!3$, $x_4\!-\!4$: covers every variable, no free value
2. orientmatched: $1 \to x_1$, $2 \to x_2$, $3 \to x_3$, $4 \to x_4$; others: $x_1 \to 2$, $x_2 \to 1$, $x_3 \to 1$, $x_3 \to 2$, $x_4 \to 2$, $x_4 \to 3$
3. SCCs$\{x_1, x_2, 1, 2\}$ (cycle $x_1 \to 2 \to x_2 \to 1 \to x_1$); every other node alone
4. keepmatched edges, and edges inside an SCC
what is removed, and why
edgeHall set behind it
$x_3 = 1$, $x_3 = 2$$\{x_1, x_2\}$ own values $\{1, 2\}$
$x_4 = 2$$\{x_1, x_2\}$ own values $\{1, 2\}$
$x_4 = 3$$\{x_1, x_2, x_3\}$ own values $\{1, 2, 3\}$

Result: $x_3 = 3$, $x_4 = 4$, and $x_1, x_2$ keep $\{1, 2\}$: one matching, one SCC pass, all four removals at once.

Edge $x_3\!-\!1$ is not on any alternating cycle: from $x_3$ you reach 1 and 2 and their cycle, but the only arc into $x_3$ comes from value 3, and that cycle never reaches 3.

18 · alldifferent

Measured: when it pays, and when it doesn't

Lab then, your unit-17 engine:

modelnodespropagationss
queens 10, allpairwise11 430973 5501.44
alldifferent9 32645 3822.56
sudoku (Inkala)pairwise3 598823 4581.14
alldifferent1023 2720.18
pigeonhole 10 → 9pairwise725 75816.6 M25.9
alldifferent010.00

CP-SAT, queens 11 all solutions: 8.1 s pairwise, 2.1 s with AddAllDifferent.

a Hall set, by name

Two decisions down Inkala's solution path, pairwise ≠ at its fixpoint: alldifferent on row 7 removes 2, 4 and 7 from cell r7c1 (row 7, column 1; domain {2, 4, 5, 7}), leaving 5. The Hall set: r7c2, r7c4, r7c5, r7c6, whose domains hold only {2, 4, 7, 9}.

  • Queens: a Hall set rarely forms before a variable is fixed, so alldifferent saves 18% of nodes and costs a matching per call. Slower.
  • Sudoku: rows, columns and boxes create Hall sets constantly. 35× fewer nodes.
  • Pigeonhole: no perfect matching at the root. Pairwise ≠ can only discover this by enumeration.

section 3

Scheduling

Tasks, resources, time.

18 · scheduling

Cumulative, and timetable filtering

1234567 earliest: start 2 latest: start 3 compulsory [3, 5)

Duration 3, start ∈ {2, 3}: whatever it chooses, the task runs during [3, 5). Lab test: with capacity 1, a second task of duration 3 loses starts 1–4.

cumulative$(s, d, r, C)$: tasks with start variables $s_i$, durations $d_i$ and demands $r_i$ never use more than $C$ at once.

  • Compulsory part of task $i$: $[\max s_i,\ \min s_i + d_i)$ when non-empty.
  • Profile: the sum of demands of compulsory parts at each time. Above $C$ means fail.
  • Filter: remove a start $t$ of task $i$ if it would overload a time where the others' compulsory parts already sit.

Disjunctive scheduling (one machine) is cumulative with $C = 1$ and every $r_i = 1$. Lab step 4's job-shop uses one per machine.

18 · scheduling · example

A profile for three tasks

Capacity $C = 2$, every demand 1.

taskdurationstart incompulsory part
A4$\{0, 1, 2\}$$[2, 4)$: latest start 2, earliest end 4
B3$\{1, 2\}$$[2, 4)$: latest start 2, earliest end 4
C2$\{0, \dots, 5\}$empty: latest start 5 > earliest end 2
time0123456
profile0022000
filtering task C

C runs $[t, t+2)$. Times 2 and 3 are already full (2 of 2).

$t = 1$ covers time 2; $t = 2$ covers 2 and 3; $t = 3$ covers 3. All three removed. $t = 0$, 4, 5 miss the full times and stay.

C's starts: $\{0, 4, 5\}$.

and A, B?

Their own compulsory parts are what fill times 2 and 3, so each must subtract itself before checking. Doing that, neither loses a start: the profile without A is 1 at times 2–3, and A fits.

18 · scheduling

Stronger scheduling reasoning

  • Edge-finding (Carlier & Pinson 1989; $O(n \log n)$ with Θ-trees, Vilím 2004): if a task can't finish before a set $\Omega$ of tasks all end, given their total duration, it must end last, which raises its start.
  • Not-first / not-last: a task cannot be scheduled first (or last) among a set.
  • Energetic reasoning: the minimum energy each task must spend inside an interval, compared with $C \times$ interval length.
  • Time-table edge-finding (Vilím 2011), combining profile and edge-finding.

Lab then, ft06 (optimum 55), 100 000 nodes:

machines as55 found at nodeproof
pairwise disjunctions588not finished
cumulative, timetable116not finished
CP-SAT, pairwise0.035 s
CP-SAT, AddNoOverlap0.018 s

CP-SAT's no-overlap runs edge-finding, not-first/not-last and precedence reasoning, all explained as clauses and learned from (unit 21).

18 · scheduling · example

One edge-finding deduction

One machine (capacity 1). Times as est = earliest start, lct = latest completion.

taskdurationestlct
A205
B305
C3110
edge-finding

$\Omega = \{A, B\}$: $\text{est}_\Omega = 0$, $\text{lct}_\Omega = 5$, total duration 5.

Add C: $\text{est}_{\Omega \cup C} + d_{\Omega \cup C} = 0 + 8 = 8 > 5 = \text{lct}_\Omega$. C cannot fit before either task ends, so C ends after all of $\Omega$.

A and B need all of $[0, 5)$, so C starts at 5 or later.

timetable, on the same tasks

A (start 0–3, duration 2): compulsory part empty. B (start 0–2, duration 3): compulsory $[2, 3)$. C: empty.

So timetable only knows time 2 is taken. C loses starts 1 and 2: C ≥ 3. (A loses starts 1 and 2 too.)

Edge-finding reasons about the total duration of a set of tasks inside a window, which no single time point shows. Here it moves C's earliest start from 3 to 5.

section 4

The catalogue

400 constraints, one question.

18 · catalogue

A few globals worth knowing

constraintmeansfiltering
element$(i, T, v)$$v = T[i]$GAC by supports, $O(|D_i|)$
table$(x, \mathcal T)$$x \in \mathcal T$STR, compact tables, MDDs
gcc$(x, [l_a, u_a])$value $a$ used $l_a$..$u_a$ timesflow (Régin 1996), unit 14
regular$(x, \mathcal A)$$x$ spells a word of automaton $\mathcal A$layered graph reachability (Pesant 2004)
circuit$(\text{succ})$successors form one Hamiltonian cyclealldifferent + subtour pruning
cumulative / no-overlapresource capacitytimetable, edge-finding, energy
the pattern

Each strong filter is an algorithm from Parts I–III wearing a CP interface: matching (alldifferent), flow (gcc), DP over an automaton (regular), shortest paths and assignment bounds (circuit, the TSP relaxations of unit 09). Global constraints are where the traditions first cooperate.

Beldiceanu, Carlsson & Rampon's Global Constraint Catalog lists 400+ constraints with semantics, decompositions and filtering algorithms.

18 · lab

Lab 18 — what you build

about 3 hours · uv run co lab 18
  1. The value graph and its maximum matching (your unit-15 Hopcroft–Karp).
  2. Régin's alldifferent, and the Hall set behind every removal.
  3. Cumulative with timetable filtering.
  4. Global and decomposed models (queens, sudoku, pigeonhole, job-shop) and minimisation.
uv run co test 18
CO_MINE=15,17 uv run co test 18   # on your own matching and engine
uv run co then 18                 # node counts, a named Hall set, CP-SAT's globals

18 · checkpoint

Done when

checkpoint
  • All four lab steps are ✓.
  • On one instance your decomposed and global models give the same answer and differ by orders of magnitude in node count, and you can name the Hall set responsible.
  • You can explain why alldifferent is slower on queens and 6× faster on sudoku.

Reading

Régin, AAAI 1994.
van Hoeve, "The alldifferent constraint: a survey" (2001).
Baptiste, Le Pape & Nuijten, Constraint-Based Scheduling, ch. 2–3.
Global Constraint Catalog (sofdem.github.io/gccat).

Next

19 search: heuristics, restarts, LNS on job-shop · 21 these propagators, explaining themselves.