Documentation

CslibFrontier.Algorithms.Lean.CDCL.VSIDS

VSIDS: variable activities for the CDCL solver #

MiniSat's branching heuristic, variable state independent decaying sum. Every variable has an activity. After each conflict, the variables of the clauses met by the conflict analysis are bumped: the current increment is added to their activity. These are all the variables of the conflict clause and of the reasons resolved on, including variables assigned at decision level 0. MiniSat bumps a variable only when it is assigned above level 0 (level(var(q)) > 0 in analyze), so this rule is a variant of MiniSat's. The increment then becomes inc + inc / 19, with integer division: at most the factor 1 / 0.95 (MiniSat's var_decay = 0.95), so older bumps weigh exponentially less. The solver decides an unassigned variable of greatest activity.

Activities and the increment are natural numbers, so the kernel can run the solver. The decay step sets the increment to inc + inc / 19, with integer division, in place of MiniSat's floating-point inc / 0.95. As in MiniSat, when an activity reaches a limit (here 2 ^ 60, tested by a shift so that no large literal is built at run time) all activities and the increment are scaled down (here by 2 ^ 20, with truncation). The increment starts at 2 ^ 40. An activity is then below the limit plus the increment, and the increment at most 20 / 19 times the limit, so every value stays below 2 ^ 62: compiled code keeps them as unboxed machine integers, with no arbitrary-precision arithmetic. The truncations can make the increment grow less than by 1 / 0.95 and turn different activities into ties (an activity more than about 2 ^ 60 times smaller than the increment becomes 0), so decisions can differ from those of floating-point activities.

The same structure, indexed by clause number and decaying by 1 / 0.999, holds MiniSat's clause activities.

The variable order (VarOrder, MiniSat's order_heap) finds the decision variable without scanning the variables for the greatest activity: a binary max-heap of variables, ordered by activity and then by variable (Activity.keyOrd), with the position of each variable in it (MiniSat's indices). As in MiniSat, bumping a variable in the heap moves it up (VarOrder.increase), a variable undone by a backjump is inserted unless it is in the heap (VarOrder.addAll, MiniSat's insertVarOrder), and popping removes assigned variables from the top until it finds an unassigned one (VarOrder.pop, MiniSat's pickBranchLit). The sifting functions compute Batteries' heapifyUp and heapifyDown (VarOrder.siftUp_fst, VarOrder.siftDown_fst), updating the positions as they swap, so that Batteries' proofs apply. Scaling the activities down can turn different activities into ties, which the order breaks by variable, so the heap is then rebuilt (VarOrder.rebuild); scaling is rare, once every few hundred conflicts.

The invariant (VarOrder.Inv) asks that the heap be well formed in the order of the current activities, that the positions be consistent with it (VarOrder.PosOK), that every variable have room in the position array, and that every unassigned variable admitted by the solver be in the heap. Bumping a variable raises its activity alone, so moving it up restores the heap (Batteries.BinaryHeap.topDown_heapifyUp_of_isGE). Under the invariant, the variable popped has the greatest activity among the unassigned variables (VarOrder.pop_spec), and among ties the greatest variable.

The choice of variable does not affect correctness: the solver only needs an unassigned variable of the database, or the fact that there is none. Activity.pick scans for one of greatest activity (Activity.pick_some, Activity.pick_none, Activity.pick_max); the solver uses it, with every activity 0, when VSIDS is off. With VSIDS, the order runs out only when every admitted variable is assigned (VarOrder.pop_none).

Main definitions #

Main results #

Variable activities: a score for each variable, by index, and the current increment.

  • score : Array ℕ

    The activity of each variable; variables past the end have activity 0.

  • inc : ℕ

    The amount added to the activity of a bumped variable.

The initial activities of the variables below n: all 0, with increment 2 ^ 40.

@[inline]

The activity of the variable v.

Bumping v: its activity grows by the increment; if it reaches 2 ^ 60, every activity and the increment are divided by 2 ^ 20. The activities are an array indexed by variable: for v beyond its end, Array.modify leaves it unchanged, so bumping v does nothing and its activity stays 0. The solver's variables are all within the array (WExec.init sizes it by the largest variable of the formula).

After a conflict: bump each variable met by the analysis once, then set the increment to inc + inc / decay, with integer division (by default decay = 19): at most the factor 1 + 1 / decay (1 / 0.95 by default), and less by the truncation.

Bumping each index of is, which has no repetitions, then growing the increment as Activity.bump does. For the activities of clauses, whose numbers grow with the run: removing repetitions in linear time would cost as much as the greatest number.

The activities with room for the indices below n, the new ones 0.

The decision variable: a variable of vs unassigned in asg, of greatest activity, the first one among ties.

theorem Cslib.Algorithms.Lean.CDCL.Activity.pick_some {asg : Assign} {M : Trail} {A : Activity} (hasg : asg.Agrees M) {vs : List ℕ} {x : ℕ} (h : A.pick asg vs = some x) :
x ∈ vs ∧ x ∉ M.vars
theorem Cslib.Algorithms.Lean.CDCL.Activity.pick_none {asg : Assign} {M : Trail} {A : Activity} (hasg : asg.Agrees M) {vs : List ℕ} (h : A.pick asg vs = none) (x : ℕ) :
x ∈ vs → x ∈ M.vars
theorem Cslib.Algorithms.Lean.CDCL.Activity.pick_max {asg : Assign} {M : Trail} {A : Activity} (hasg : asg.Agrees M) {vs : List ℕ} {x : ℕ} (h : A.pick asg vs = some x) {y : ℕ} (hy : y ∈ vs) (hyM : y ∉ M.vars) :
A.get y ≤ A.get x

The chosen variable has the greatest activity among the unassigned variables of vs.

theorem Cslib.Algorithms.Lean.CDCL.Activity.get_bumpVar_le {A : Activity} {u v : ℕ} (huv : u ≠ v) :
(A.bumpVar v).get u ≤ A.get u

Bumping another variable does not raise the activity of u.

theorem Cslib.Algorithms.Lean.CDCL.Activity.get_bump_le {A : Activity} {vs : List ℕ} {d v : ℕ} (hv : v ∉ vs) :
(A.bump vs d).get v ≤ A.get v

Bumping does not raise the activity of the variables not bumped.

The variable order #

An entry of the variable order: a variable and the activity it is keyed by. (variable is a keyword, so the field is var.)

  • activity : ℕ

    The activity the entry is keyed by.

  • var : ℕ

    The variable.

Instances For

    An entry as a pair, activity first.

    @[instance_reducible]

    Entries compare by activity, then by variable: the lexicographic order on toPair, written out so that comparing builds no pairs at run time.

    @[instance_reducible]

    The order of the variables by the activities A: by activity, then by variable, the order of their entries OrderKey.mk (A.get v) v. A total order, so that the variable of the variable order with the greatest activity, the greatest variable among ties, is well defined.

    @[inline]

    Whether a comes before b in the order of the activities A: a lower activity, or the same and a smaller variable.

    theorem Cslib.Algorithms.Lean.CDCL.Activity.below_iff (A : Activity) (a b : ℕ) :
    A.below a b = true ↔ A.get a < A.get b ∨ A.get a = A.get b ∧ a < b

    In the order of the activities A, a is at least b when b has a lower activity, or the same and a variable at most a.

    Positions #

    Positions consistent with a heap array a (MiniSat's indices): p holds at each element of a its index plus one, and 0 at every other variable.

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.PosOK.lt_size {a p : Array ℕ} (h : PosOK a p) {v : ℕ} (hv : v ∈ a) :
    v < p.size
    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.PosOK.inj {a p : Array ℕ} (h : PosOK a p) {i j : ℕ} (hi : i < a.size) (hj : j < a.size) (he : a[i] = a[j]) :
    i = j

    Empty positions for the empty heap.

    The positions after swapping the elements x at i and y at j.

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.PosOK.swap {a p : Array ℕ} (h : PosOK a p) {i j : ℕ} (hi : i < a.size) (hj : j < a.size) :
    PosOK (a.swap i j hi hj) (swapPos p a[i] a[j] i j)
    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.PosOK.push {a p : Array ℕ} (h : PosOK a p) {v : ℕ} (hv : v < p.size) (h0 : p.getD v 0 = 0) :
    PosOK (a.push v) (p.setIfInBounds v (a.size + 1))

    Sifting, with positions #

    heapifyUpF in the order of the activities A, updating (a, p) in place without allocating intermediate pairs.

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.siftUp_fst (A : Activity) (fuel : ℕ) {n : ℕ} (a : Vector ℕ n) (p : Array ℕ) (i : Fin n) :
    (siftUp A fuel (a, p) i).1 = Batteries.BinaryHeap.heapifyUpF fuel a i
    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.siftUp_size (A : Activity) (fuel : ℕ) {n : ℕ} (a : Vector ℕ n) (p : Array ℕ) (i : Fin n) :
    (siftUp A fuel (a, p) i).2.size = p.size
    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.siftUp_pos (A : Activity) (fuel : ℕ) {n : ℕ} (a : Vector ℕ n) (p : Array ℕ) (i : Fin n) :
    PosOK a.toArray p → PosOK (siftUp A fuel (a, p) i).1.toArray (siftUp A fuel (a, p) i).2
    @[inline]

    The larger child, maxChildF in the order of the activities A.

    heapifyDownF in the order of the activities A, updating (a, p) in place without allocating intermediate pairs.

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.siftDown_fst (A : Activity) (fuel : ℕ) {n : ℕ} (a : Vector ℕ n) (p : Array ℕ) (i : Fin n) :
    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.siftDown_size (A : Activity) (fuel : ℕ) {n : ℕ} (a : Vector ℕ n) (p : Array ℕ) (i : Fin n) :
    (siftDown A fuel (a, p) i).2.size = p.size
    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.siftDown_pos (A : Activity) (fuel : ℕ) {n : ℕ} (a : Vector ℕ n) (p : Array ℕ) (i : Fin n) :
    PosOK a.toArray p → PosOK (siftDown A fuel (a, p) i).1.toArray (siftDown A fuel (a, p) i).2

    The heap #

    The variable order, MiniSat's order_heap: a binary max-heap of variables in the order of their activities (Activity.keyOrd), with the position of each variable in it. A variable's position is its index plus one, 0 when it is not in the heap (MiniSat's indices). Bumping a variable in the heap moves it up (VarOrder.increase, MiniSat's decrease).

    • heap : Array ℕ

      The variables, as a binary heap.

    • pos : Array ℕ

      The position of each variable in heap, plus one; 0 for variables not in it.

    The empty order, with room for the variables below n.

    The heap is well formed in the order of the activities A: as a Batteries heap, every variable is at least its children.

    Inserting v with the activities A, if it is not in the heap and has a position.

    Moving v up after its activity grew, the new activities being A, if it is in the heap: MiniSat's decrease.

    Removing the top of the heap, in the order of the activities A: the last variable takes its place and is moved down, as popMaxF does.

    Inserting the variables vs not in the heap, with the activities A: MiniSat's insertVarOrder on each.

    Rebuilding the heap with the activities A, after they were scaled down: scaling can turn different activities into ties, which the order breaks by variable.

    What the operations keep #

    theorem Cslib.Algorithms.Lean.CDCL.Activity.isGE_raise {A A' : Activity} {x y : ℕ} (hx : A.get x ≤ A'.get x) (hy : A'.get y = A.get y) (h : (compare x y).isGE = true) :

    A comparison x ≥ y survives activities raised at x and unchanged at y.

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.WF.congr {A A' : Activity} {H : VarOrder} (hwf : H.WF A) (h : ∀ w ∈ H.heap, A'.get w = A.get w) :
    H.WF A'

    A heap stays well formed when the activities of its variables do not change.

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.insert_spec (A : Activity) (H : VarOrder) (v : ℕ) (hp : PosOK H.heap H.pos) :
    (H.WF A → (insert A H v).WF A) ∧ PosOK (insert A H v).heap (insert A H v).pos ∧ (insert A H v).pos.size = H.pos.size ∧ ∀ (w : ℕ), w ∈ (insert A H v).heap ↔ w ∈ H.heap ∨ w = v ∧ v < H.pos.size

    What inserting keeps: a well-formed heap stays well formed, the positions consistent and the room the same; v is added if it has room.

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.increase_spec {A A' : Activity} {H : VarOrder} {v : ℕ} (hp : PosOK H.heap H.pos) (hwf : H.WF A) (hle : ∀ (u : ℕ), A.get u ≤ A'.get u) (heq : ∀ (u : ℕ), u ≠ v → A'.get u = A.get u) :
    (increase A' H v).WF A' ∧ PosOK (increase A' H v).heap (increase A' H v).pos ∧ (increase A' H v).pos.size = H.pos.size ∧ ∀ (w : ℕ), w ∈ (increase A' H v).heap ↔ w ∈ H.heap

    What moving v up keeps, after its activity alone grew from A to A': the heap stays well formed, the positions consistent, the room and the variables the same.

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.removeTop_spec (A : Activity) (H : VarOrder) (h : 0 < H.heap.size) (hp : PosOK H.heap H.pos) :
    (H.WF A → (removeTop A H h).WF A) ∧ PosOK (removeTop A H h).heap (removeTop A H h).pos ∧ (removeTop A H h).pos.size = H.pos.size ∧ ∀ (w : ℕ), w ∈ (removeTop A H h).heap ↔ w ∈ H.heap ∧ w ≠ H.heap[0]

    What removing the top keeps: a well-formed heap stays well formed, the positions consistent and the room the same, and the variables left are the others.

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.addAll_spec (A : Activity) (H : VarOrder) (vs : List ℕ) :
    PosOK H.heap H.pos → (H.WF A → (addAll A H vs).WF A) ∧ PosOK (addAll A H vs).heap (addAll A H vs).pos ∧ (addAll A H vs).pos.size = H.pos.size ∧ ∀ (w : ℕ), w ∈ (addAll A H vs).heap ↔ w ∈ H.heap ∨ w ∈ vs ∧ w < H.pos.size

    What inserting the variables vs keeps: a well-formed heap stays well formed, the positions consistent and the room the same; the variables of vs with room are added.

    What rebuilding keeps: the heap is well formed in the order of the new activities, the positions consistent, the room and the variables the same.

    Bumping #

    @[inline]

    Bumping v in the activities A and the order H: Activity.bumpVar, and the variable is moved up in the heap, or the heap is rebuilt if the activities were scaled down.

    What an operation on the order keeps, with the activities A' after it: a well-formed heap, consistent positions, the room and the variables of the order H before.

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.Kept.trans {H H' H'' : VarOrder} {A' A'' : Activity} (h : H.Kept A' H') (h' : H'.Kept A'' H'') :
    H.Kept A'' H''
    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.bumpVar_spec {A : Activity} {H : VarOrder} (v : ℕ) (hwf : H.WF A) (hp : PosOK H.heap H.pos) :
    H.Kept (bumpVar A H v).1 (bumpVar A H v).2
    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.bumpAll_spec (vs : List ℕ) {A : Activity} {H : VarOrder} :
    H.WF A → PosOK H.heap H.pos → H.Kept (bumpAll vs A H).1 (bumpAll vs A H).2

    After a conflict: bump each variable met by the analysis once, in the activities and in the order, then grow the increment, as Activity.bump does.

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.bump_fst (A : Activity) (H : VarOrder) (vs : List ℕ) (decay : ℕ) :
    (bump A H vs decay).1 = A.bump vs decay
    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.bump_spec {A : Activity} {H : VarOrder} (vs : List ℕ) (decay : ℕ) (hwf : H.WF A) (hp : PosOK H.heap H.pos) :
    H.Kept (bump A H vs decay).1 (bump A H vs decay).2

    Bumping variables listed once each: VarOrder.bump without removing repetitions, which costs an array as large as the greatest variable. The solver bumps the variables met by an analysis, which lists each once (Cslib.Algorithms.Lean.CDCL.Learned.seen_nodup).

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.bumpDistinct_eq (A : Activity) (H : VarOrder) {vs : List ℕ} (h : vs.Nodup) (decay : ℕ) :
    bumpDistinct A H vs decay = bump A H vs decay

    The invariant and popping #

    structure Cslib.Algorithms.Lean.CDCL.VarOrder.Inv (H : VarOrder) (A : Activity) (V : List ℕ) (M : Trail) (occ : ℕ → Bool) :

    The invariant of the variable order over the variables V, with the activities A, the trail M and the variables occ admits: a well-formed heap with consistent positions, room for every variable of V, and every variable of V admitted and not on the trail in the heap.

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.inv_addAll_empty (A : Activity) (V : List ℕ) (M : Trail) (occ : ℕ → Bool) {n : ℕ} (hn : ∀ v ∈ V, v < n) :
    (addAll A (empty n) V).Inv A V M occ

    All the variables of V inserted in an empty order with room for them: an order satisfying the invariant whatever the trail.

    The result of popping: the variable found, if any, and the order left.

    Popping the decision variable, as MiniSat's pickBranchLit: the top of the heap is removed until it is an unassigned variable admitted by occ. none if the heap runs out, or the fuel does; with more fuel than variables in the heap, the heap runs out (VarOrder.pop_none).

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.le_top {A : Activity} {H : VarOrder} (hwf : H.WF A) (h : 0 < H.heap.size) {y : ℕ} (hy : y ∈ H.heap) :
    A.get y ≤ A.get H.heap[0]

    The top of a well-formed heap has the greatest activity.

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.pop_spec {A : Activity} {asg : Assign} {M : Trail} {V : List ℕ} {occ : ℕ → Bool} (hasg : asg.Agrees M) (fuel : ℕ) (H : VarOrder) :
    H.WF A → PosOK H.heap H.pos → (∀ v ∈ V, v ∉ M.vars → occ v = true → v ∈ H.heap) → (pop A asg occ fuel H).order.WF A ∧ PosOK (pop A asg occ fuel H).order.heap (pop A asg occ fuel H).order.pos ∧ (pop A asg occ fuel H).order.pos.size = H.pos.size ∧ (∀ (x : ℕ), (pop A asg occ fuel H).var = some x → x ∉ M.vars ∧ occ x = true ∧ ∀ y ∈ V, y ∉ M.vars → occ y = true → A.get y ≤ A.get x) ∧ ∀ v ∈ V, v ∉ M.vars → occ v = true → (pop A asg occ fuel H).var ≠ some v → v ∈ (pop A asg occ fuel H).order.heap

    What popping finds: the order stays well formed, with consistent positions and the same room; a variable found is unassigned, admitted, and of greatest activity among the unassigned admitted variables of V; and every other unassigned admitted variable of V stays in the heap.

    theorem Cslib.Algorithms.Lean.CDCL.VarOrder.pop_none (A : Activity) (asg : Assign) (occ : ℕ → Bool) (fuel : ℕ) (H : VarOrder) :
    H.heap.size < fuel → (pop A asg occ fuel H).var = none → (pop A asg occ fuel H).order.heap.size = 0

    Popping with more fuel than variables finds none only by emptying the heap.