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

20

SAT and the CDCL engine

Unit propagation, conflict analysis, backjumping, activity, restarts — resolution with a very good heuristic, and a proof system with limits you can state precisely.

needs 17~2 h slides + 3½ h lablab: uv run co lab 20

20 · overview

The route

1 · from DPLL to CDCL

CNF, resolution, unit propagation, watched literals.

2 · learning

Implication graphs, first UIP, backjumping, VSIDS, restarts, LBD; why it stops.

3 · proofs and limits

DRAT; CDCL is resolution; pigeonhole's exponential lower bound.

4 · encodings

Tseitin, at-most-one, cardinality networks: why the smallest CNF is not the best.

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

From DPLL to CDCL

Branch, propagate, and remember why you failed.

20 · DPLL

CNF, resolution, and unit propagation

CNF: a conjunction of clauses, each a disjunction of literals. SAT is NP-complete (Cook 1971); 3-SAT already is.

resolution $$\frac{(C \lor x) \qquad (D \lor \lnot x)}{C \lor D}$$

Sound and complete for refutation: $F$ is unsatisfiable iff the empty clause can be derived.

DPLL (Davis, Logemann & Loveland 1962): branch on a variable, apply unit propagation (a clause with all literals false but one forces it), and backtrack chronologically on a conflict.

two watched literals (Chaff 2001)

Watch two non-false literals per clause. A clause needs attention only when a watched literal becomes false: find a replacement, or the clause is unit or conflicting.

Backtracking costs nothing: unassigning variables never invalidates a watch. No counters to restore.

Lab step 1's test: on 40 random formulas with random decisions, the watch loop reaches exactly the assignments and conflicts of naive propagation.

20 · DPLL · example

Watched literals, on four clauses

$c_1 = (\lnot x_1 \lor x_3)$, $\ c_2 = (\lnot x_2 \lor \lnot x_3 \lor x_4)$, $\ c_3 = (\lnot x_2 \lor \lnot x_4 \lor x_5)$, $\ c_4 = (\lnot x_4 \lor \lnot x_5)$. Each clause watches its first two literals. Decide $x_1$, then $x_2$.

just assignedvisit clauses watchingwhat happens
$x_1$ (decision, level 1)$\lnot x_1$: $c_1$no replacement, other watch $x_3$ free: unit, $x_3$
$x_3$$\lnot x_3$: $c_2$replacement $x_4$ is free: watch moves to $x_4$
$x_2$ (decision, level 2)$\lnot x_2$: $c_2$, $c_3$$c_2$: only $\lnot x_3$ left, false: unit, $x_4$. $c_3$: watch moves to $x_5$
$x_4$$\lnot x_4$: $c_3$, $c_4$$c_3$: unit, $x_5$. $c_4$: both literals false: conflict
what the watches saved

$x_1$ touched one clause, $x_3$ one. $c_4$ was not looked at until $x_4$ was set: before that it had two unassigned watches, so it could not be unit.

Undoing level 2 unassigns $x_2, x_4, x_5$ and moves no watch back: $c_2$ still watches $x_4$ and $\lnot x_2$, both unassigned again, which is a valid state.

section 2

Learning

A conflict is a lesson; write it down.

20 · learning

The implication graph and the first UIP

x₁x₃x₂ x₄x₅⊥ level 1level 2

Lab step 2's textbook example. Clauses (¬x₁∨x₃), (¬x₂∨¬x₃∨x₄), (¬x₂∨¬x₄∨x₅), (¬x₄∨¬x₅). Decisions x₁, then x₂.

Implication graph: a node per assigned literal (at its decision level, on the trail), with edges from the reason clause's other literals. A conflict is a clause with every literal false.

first UIP

A UIP is a current-level node on every path from the decision to the conflict. The first UIP is the one closest to the conflict.

Here x₄ is not a UIP: x₂ → x₅ bypasses it. The first UIP is x₂, and the learned clause is ¬x₂ ∨ ¬x₃: backjump to level 1 and assert ¬x₂. The next slide derives it.

Computing it is resolution: resolve the conflict clause with the reason of the latest current-level literal until one current-level literal remains.

20 · learning · example

The learned clause, by resolution, and checked

analysis: resolve backwards along the trail
clause so farlevel-2 literalsresolve with
$c_4 = (\lnot x_4 \lor \lnot x_5)$2reason of $x_5$: $c_3$
$(\lnot x_2 \lor \lnot x_4)$2reason of $x_4$: $c_2$
$(\lnot x_2 \lor \lnot x_3)$1stop

One level-2 literal left, $\lnot x_2$: the first UIP. The clause's other literal, $\lnot x_3$, is from level 1, so backjump to level 1. There the clause is unit and asserts $\lnot x_2$ (an asserting clause). Its LBD is 2: levels 1 and 2.

the RUP check a proof checker does

To accept $(\lnot x_2 \lor \lnot x_3)$ as a lemma, assume its negation, $x_2$ and $x_3$, and propagate over the clauses so far:

$c_2$ gives $x_4$. $c_3$ gives $x_5$. $c_4$ is now false: conflict, so the lemma is implied. ✓

No search, no heuristics: two unit propagations. That is why checking a DRAT proof is cheap even when finding it wasn't.

The backjump skipped nothing here, but with ten decisions between level 1 and the conflict it would skip all of them: non-chronological backjumping.

20 · learning

The rest of the engine

  • Non-chronological backjumping: jump to the second-highest level in the learned clause, where it becomes unit. It can skip many levels of irrelevant decisions.
  • VSIDS (Chaff): bump the activity of variables in each conflict, and grow the bump by 1/decay after every conflict, so recent conflicts weigh exponentially more. Branch on the most active.
  • Phase saving (Pipatsrisawat & Darwiche 2007): re-assign a variable its last value, so backjumps don't discard satisfied sub-problems.
  • Restarts (unit 19's Luby, or adaptive): cheap, because learned clauses and activities survive.
clause deletion by LBD (Glucose, 2009)

Literal block distance: the number of distinct decision levels in a learned clause. "Glue" clauses (LBD ≤ 2) link two blocks of propagation and are kept forever. Periodically delete the worse half of the rest, never a clause that is a current reason.

Lab then, random 3-SAT at the threshold:

nyour conflictsMiniSatyour sMiniSat s
150 unsat2 2363 0510.400.015
200 sat8 5887 7482.310.048

The same conflicts, 30–50× the time per conflict: the algorithm is right, the language is slow.

20 · learning · termination

Why CDCL stops, whatever it branches on

It is not obvious. VSIDS may branch on any variable, and a backjump can throw away every level, so the number of free variables goes back up. Counted level by level, it does go down.

the termination measure

For each decision level $\ell = 0, \dots, n$, let $f_\ell$ be the number of variables not assigned at any level $\le \ell$ on the trail. Read them as one number in base $n+1$, level 0 leading:

$$\mu = \sum_{\ell=0}^{n} f_\ell \,(n+1)^{\,n-\ell}$$

A level not yet open adds nothing, so its digit repeats the one before.

every step lowers $\mu$
  • Propagate at level $d$: $f_d - 1$.
  • Decide: $f_{d+1} - 1$.
  • Conflict at $d > 0$: the asserting clause adds a literal at some level $j < d$, so $f_j - 1$. The digits after $j$ go back up, but together they are worth less than one unit of digit $j$.
  • Conflict at level 0, or nothing left to decide: stop.

Each step lowers the first digit it changes. $\mu \ge 0$ and starts below $(n+1)^{n+1}$, so the run ends.

full proof in the notes (N)

where the choice of variable went

$\mu$ counts free variables per level, never which ones. VSIDS and phase saving pick which literal joins a level, not how many do. The heuristic owes one thing: an unassigned variable.

20 · learning · termination · example

The measure, on the running example

$n = 5$, so base 6
stepfree after levels 0–5$\mu$
start5 5 5 5 5 546 655
decide $x_1$5 4 4 4 4 445 100
propagate $x_3$5 3 3 3 3 343 545
decide $x_2$5 3 2 2 2 243 286
propagate $x_4$, $x_5$5 3 0 0 0 042 768
conflict, jump to 1, $\lnot x_2$5 2 2 2 2 241 990
decide $x_4$5 2 1 1 1 141 731
propagate $\lnot x_5$: all set5 2 0 0 0 041 472

Down on every row, including the jump: digits 2 to 5 go back up from 0 to 2, but digit 1 falls from 3 to 2.

level 0level 1level 2 beforeafter x₁ x₃ x₂ x₄ x₅ ⊥ x₁ x₃ ¬x₂ x₂ x₄ x₅ free afterfree after 5 3 0 μ = 42 768 5 2 2 μ = 41 990 learn (¬x₂ ∨ ¬x₃),jump to level 1

The jump of the implication graph slide. Under each level, the variables still free after it. Digit 1 falls from 3 to 2 and outweighs digit 2's rise: one unit of digit 1 is worth $6^4 = 1\,296$, more than all later digits can hold ($1\,295$). Decisions outlined; the shaded literal is the one the learned clause asserts; dashed cells are unassigned again.

20 · learning · termination

Back to level 0, and restarts

level 0level 1level 2 beforeafter x₁ x₃ x₂ x₄ x₅ ⊥ ¬x₂ x₁ x₃ x₂ x₄ x₅ free afterfree after 5 3 0 μ = 42 768 4 4 4 μ = 37 324 learn (¬x₂),jump to level 0

The example with $c_2$ shortened to $(\lnot x_2 \lor x_4)$. Same decisions, same conflict, but analysis ends at the unit learned clause $(\lnot x_2)$: no level-1 literal is involved, so the jump goes to level 0 and undoes $x_1$ and $x_3$ too. Shading as on the previous slide.

what went down

Five literals undone, and digits 1 to 5 jump back up to 4. But the leading digit, the variables not fixed at level 0, falls from 5 to 4: $42\,768 \to 37\,324$.

Level 0 is never undone, so that digit never rises, and at most $n$ jumps land there. Between two of them, the digit for level 1 only falls, and so on down.

restarts raise $\mu$

A restart cancels every level above 0, sending those digits back up. But a run without restarts ends within $(n+1)^{n+1}$ steps, and Luby cutoffs grow without bound: some run is given that many conflicts, and ends.

Or keep every learned clause: each one is new, and there are only $3^n$ clauses. The lab's solver deletes clauses, so it relies on Luby.

section 3

Proofs and limits

"Unsatisfiable" should come with evidence.

20 · proofs

DRAT: checkable unsatisfiability

A DRAT proof lists every learned clause (and every deletion) in order. A checker replays it: each lemma must be RUP, meaning that assigning its negation and unit-propagating over the clauses so far gives a conflict. It ends with the empty clause.

  • A first-UIP clause is always RUP: that's what analysis just showed.
  • RAT extends RUP to cover inprocessing techniques that add non-implied clauses (such as blocked clauses).
  • SAT competitions require verified proofs; drat-trim and the formally verified cake_lpr check them.

Lab then, proofs from your solver, checked by colib.drat (an independent RUP checker):

instancelemmascheck
3-SAT n = 120, unsat831accepted, 0.2 s
pigeonhole 7 into 6784accepted, 0.2 s

The tests also check 30 proofs, with aggressive deletions included. A solver bug that learns a wrong clause fails the check, even if the final answer happens to be right.

20 · limits

CDCL is resolution, and pigeonhole defeats resolution

Pipatsrisawat & Darwiche 2011; Atserias, Fichte & Thurley 2011

CDCL with restarts polynomially simulates general resolution, and every CDCL run yields a resolution refutation of similar size. So a resolution lower bound is a CDCL lower bound.

Haken 1985

Every resolution refutation of the pigeonhole formula $\mathrm{PHP}^{n+1}_n$ has size $2^{\Omega(n)}$.

Why, in one line: any refutation must at some point hold a clause about a third of all the pigeons at once, and building clauses that wide one resolution at a time takes exponentially many steps. sketch in the notes (N)

Counting ("$n+1$ objects don't fit in $n$ places") can't be expressed compactly in clause-by-clause reasoning. Unit 18's alldifferent does it in one matching.

Lab then, conflicts:

pigeonholeyoursMiniSatCaDiCaLKissat s
7 into 67842 4981 0440.004
9 into 841 36439 06331 7190.13
10 into 9—510 308267 0230.50
11 into 10—10.7 M2.2 M3.2

Better engineering shifts the curve; it doesn't change its shape. Stronger proof systems (cutting planes, extended resolution) have short pigeonhole proofs, and pseudo-Boolean solvers exploit that.

section 4

Encodings

The same constraint, very different CNFs.

20 · encodings

Tseitin, at-most-one, cardinality

  • Tseitin (1968): name every subformula with a fresh variable. Linear size, equisatisfiable. Arbitrary circuits become CNF.
  • At most one of $n$: pairwise ($\binom n2$ clauses), sequential counter (Sinz 2005, $3n - 4$ clauses, $n - 1$ variables), bitwise ($n \lceil\log n\rceil$ clauses), commander, product.
  • At most $k$: sequential counters, sorting and cardinality networks (Asín et al. 2011), totalizers.

Arc consistency is the property that matters: does unit propagation on the encoding prune as much as the constraint would? Pairwise and the sequential counter both have it.

Lab then, n-queens with your solver (clauses / seconds):

npairwisesequentialbitwise
40103 520 / 0.3318 292 / 3.8235 584 / 0.88
60352 880 / 0.9741 832 / 20.983 604 / 10.5

The largest CNF is the fastest. Pairwise propagates in one step. The counter needs chains of auxiliary variables, which also distract the branching heuristic. Fewer clauses is not the objective; propagation strength and a branching-friendly variable set are.

20 · encodings · example

Two encodings, written out

Tseitin on a two-gate circuit

Circuit $o = (a \land b) \lor c$, asserted true. Name the gates $g = a \land b$ and $o = g \lor c$:

gateclauses
$g \leftrightarrow a \land b$$(\lnot g \lor a)$, $(\lnot g \lor b)$, $(g \lor \lnot a \lor \lnot b)$
$o \leftrightarrow g \lor c$$(\lnot o \lor g \lor c)$, $(o \lor \lnot g)$, $(o \lor \lnot c)$
output$(o)$

7 clauses. The circuit has 5 satisfying inputs $(a,b,c)$, and each extends to exactly one value of $g, o$: same count, so equisatisfiable, and models map back.

at most one of four, two ways

Pairwise, 6 clauses: $(\lnot x_1 \lor \lnot x_2)$, $(\lnot x_1 \lor \lnot x_3)$, $(\lnot x_1 \lor \lnot x_4)$, $(\lnot x_2 \lor \lnot x_3)$, $(\lnot x_2 \lor \lnot x_4)$, $(\lnot x_3 \lor \lnot x_4)$.

Sequential counter, 8 clauses and 3 new variables $s_1, s_2, s_3$ ("one of the first $i$ is true"): $(\lnot x_1 \lor s_1)$, $(\lnot x_2 \lor s_2)$, $(\lnot s_1 \lor s_2)$, $(\lnot x_2 \lor \lnot s_1)$, $(\lnot x_3 \lor s_3)$, $(\lnot s_2 \lor s_3)$, $(\lnot x_3 \lor \lnot s_2)$, $(\lnot x_4 \lor \lnot s_3)$.

Set $x_1$ true. Pairwise: one round gives $\lnot x_2, \lnot x_3, \lnot x_4$. Counter: $s_1$, then $\lnot x_2$ and $s_2$, then $\lnot x_3$ and $s_3$, then $\lnot x_4$. Same result, after a chain of six propagations.

20 · lab

Lab 20 — what you build

about 3½ hours · uv run co lab 20
  1. Assignment and two-watched-literal propagation.
  2. First-UIP conflict analysis and backjumping with phase saving.
  3. The CDCL loop: VSIDS, Luby restarts.
  4. DRAT proofs and LBD-based deletion.
  5. Encodings: three at-most-one, at-most-k, pigeonhole, queens, colouring.
uv run co test 20
uv run co then 20     # MiniSat, CaDiCaL, Kissat · pigeonhole · encodings · DRAT checks

20 · checkpoint

Done when

checkpoint
  • All five lab steps are ✓.
  • Your solver emits a DRAT proof that an external checker accepts, and you can explain pigeonhole's hardness in terms of resolution lower bounds rather than "it just takes a long time".
  • You can compute the first UIP of a small implication graph by hand.
  • You can say what falls on every step of a CDCL run, even a jump to level 0, and why restarts need an argument of their own.

Reading

Marques-Silva, Lynce & Malik, Handbook of Satisfiability, ch. 4.
Eén & Sörensson, "An extensible SAT-solver" (2003).
Audemard & Simon, IJCAI 2009 (LBD).
Haken, TCS 39 (1985); Buss's survey (2012).

Next

21 lazy clause generation: CP propagators that explain themselves to this solver · 22 hardness and reductions.