Documentation

CslibFrontier.Algorithms.WordRAM.Schoening.Trial

One trial of Schöning's algorithm on the randomized word RAM #

Cslib.Algorithms.WordRAM.Schoening.trialProgram is one trial of Schöning's k-SAT algorithm Schöning as a program of the randomized word RAM (Cslib.WordRAM.runRand). On the input encode φ, the number n of variables followed by the clauses of φ : CNF (Fin n), it

  1. draws a uniformly random assignment, one rand call per variable;
  2. performs 3 n steps of local search: each step scans for the first falsified clause and, if there is one, flips the variable of a uniformly chosen literal occurrence of it;
  3. halts with output 1 if the final assignment satisfies φ and 0 otherwise, leaving the final assignment in memory just after the input.

Cslib.Algorithms.WordRAM.Schoening.trialProgram_spec proves that the program halts on every path within trialTime n L = 36 n L + 66 n + 12 L + 19 instructions, where L is the input length, makes at most 4 n rand calls, reports correctly whether the assignment it leaves satisfies φ, and that the law of that assignment is exactly the mathematical trial Cslib.Complexity.Schoening.trial φ. Schöning's success bound for that trial (Cslib.Complexity.Schoening.trial_success_of_length_le) therefore applies to the program. Time is the instruction count of the word RAM with unit-cost rand (Cslib.WordRAM.stepRand); every instruction counts, including loop tests and jumps.

The trial body Cslib.Algorithms.WordRAM.Schoening.trialCore reads the clause region and the number of variables from registers, so the amplified algorithm reuses it with a different input layout. It writes memory only in the assignment: Cslib.Algorithms.WordRAM.Schoening.Frame records that the memory after the assignment is unchanged, which is where the amplified algorithm keeps its trial counter.

Main definitions #

Main results #

References #

Reading assignments #

The assignment left by a halting run: read from address base.

theorem Cslib.Algorithms.WordRAM.Schoening.assignAt_assignmentAt {n : ℕ} {mem : ℕ → ℕ} {base : ℕ} (h : ∀ i < n, mem (base + i) ≤ 1) :
AssignAt mem base (assignmentAt mem base n)

AssignAt for the assignment read back from bits.

theorem Cslib.Algorithms.WordRAM.Schoening.ScanResult.found_eq {n : ℕ} {σ : Fin n → Bool} {φ : Logic.PL.CNF (Fin n)} {mem : ℕ → ℕ} {e fnd cptr : ℕ} (h : ScanResult σ φ mem e fnd cptr) :

The found word reported by a clause scan is 0 when σ satisfies φ and 1 otherwise.

On the initial machine data for encode φ, the encoded clauses start at address 2.

The frame of a trial #

structure Cslib.Algorithms.WordRAM.Schoening.Frame {n : ℕ} (φ : Logic.PL.CNF (Fin n)) (C A : ℕ) (M : ℕ → ℕ) (x : WordRAM.Data 24) :

The registers and memory a trial relies on: the constants, the clauses of φ stored in [C, A), the assignment's address A, and n. The memory from address A + n on, just after the assignment, agrees with M; a trial never writes there, so callers can keep data such as a trial counter in it.

theorem Cslib.Algorithms.WordRAM.Schoening.Frame.congr {n : ℕ} {φ : Logic.PL.CNF (Fin n)} {C A : ℕ} {M : ℕ → ℕ} {x y : WordRAM.Data 24} (h : Frame φ C A M x) (hz : y.regs Reg.zero = x.regs Reg.zero) (ho : y.regs Reg.one = x.regs Reg.one) (hc : y.regs Reg.cBeg = x.regs Reg.cBeg) (ha : y.regs Reg.aBase = x.regs Reg.aBase) (hn : y.regs Reg.nReg = x.regs Reg.nReg) (hmem : ∀ (a : ℕ), a < A ∨ A + n ≤ a → y.mem a = x.mem a) :
Frame φ C A M y

The frame survives any change that keeps its registers and the memory outside the assignment [A, A + n).

theorem Cslib.Algorithms.WordRAM.Schoening.Frame.self {n : ℕ} {φ : Logic.PL.CNF (Fin n)} {C A : ℕ} {M : ℕ → ℕ} {x : WordRAM.Data 24} (h : Frame φ C A M x) :
Frame φ C A x.mem x

The frame with the memory after the assignment taken as it is.

theorem Cslib.Algorithms.WordRAM.Schoening.Frame.of_mem_lt {n : ℕ} {φ : Logic.PL.CNF (Fin n)} {C A : ℕ} {M : ℕ → ℕ} {x y : WordRAM.Data 24} (h : Frame φ C A M x) (hz : y.regs Reg.zero = x.regs Reg.zero) (ho : y.regs Reg.one = x.regs Reg.one) (hc : y.regs Reg.cBeg = x.regs Reg.cBeg) (ha : y.regs Reg.aBase = x.regs Reg.aBase) (hn : y.regs Reg.nReg = x.regs Reg.nReg) (hmem : ∀ a < A, y.mem a = x.mem a) :
Frame φ C A y.mem y

The frame survives any change that keeps its registers and the memory below A, with the memory after the assignment taken as it is.

Code that writes none of the frame registers keeps the frame registers.

Instances For
    theorem Cslib.Algorithms.WordRAM.Schoening.Frame.of_execWithin {n : ℕ} {φ : Logic.PL.CNF (Fin n)} {C A : ℕ} {M : ℕ → ℕ} {c : WordRAM.Code WordRAM.Basis.standard 24} {x y : WordRAM.Data 24} {w T : ℕ} (h : Frame φ C A M x) (hc : PreservesFrame c) (he : WordRAM.ExecWithin w c x y T) (hmem : ∀ (a : ℕ), a < A ∨ A + n ≤ a → y.mem a = x.mem a) :
    Frame φ C A M y

    A deterministic run of frame-preserving code that keeps the memory outside the assignment keeps the frame.

    The random initial assignment #

    theorem Cslib.Algorithms.WordRAM.Schoening.initAssign_law {n : ℕ} {φ : Logic.PL.CNF (Fin n)} {C A w : ℕ} {M : ℕ → ℕ} (hw : WordRAM.Fits w (A + 3 * n)) (hA : 1 ≤ A) {x : WordRAM.Data 24} (hx : Frame φ C A M x) :
    WordRAM.ExecRandLaw w initAssign x (fun (y : WordRAM.Data 24) => Frame φ C A M y ∧ ∀ i < n, y.mem (A + i) ≤ 1) (6 * n + 3) n (fun (y : WordRAM.Data 24) => assignmentAt y.mem A n) (Probability.PMF.uniformOfFintype (Fin n → Bool))

    The random initial assignment is uniform, takes 6 n + 3 instructions and n rand calls, and keeps the frame.

    Flipping a literal of a falsified clause #

    Read the literal at offset var of the clause at cPtr and flip its variable.

    One flip: choose a literal occurrence of the clause at cPtr uniformly and flip its variable; an empty clause changes nothing.

    theorem Cslib.Algorithms.WordRAM.Schoening.flip_law {n : ℕ} {φ : Logic.PL.CNF (Fin n)} {C A w : ℕ} {M : ℕ → ℕ} (hw : WordRAM.Fits w (A + 3 * n)) {x : WordRAM.Data 24} (hx : Frame φ C A M x) {σ : Fin n → Bool} (hσ : AssignAt x.mem A σ) {c : Logic.PL.Clause (Fin n)} (hcl : WordRAM.Block.HoldsList (x.regs Reg.cPtr) (encodeClause c) x.mem) (hend : x.regs Reg.cPtr + List.length c + 1 ≤ A) :
    WordRAM.ExecRandLaw w flip x (fun (y : WordRAM.Data 24) => Frame φ C A M y ∧ ∀ i < n, y.mem (A + i) ≤ 1) 12 1 (fun (y : WordRAM.Data 24) => assignmentAt y.mem A n) (Complexity.Schoening.flipStep c σ)

    One flip has the law of Cslib.Complexity.Schoening.flipStep.

    One step: scan for the first falsified clause and flip a literal of it, then count the step.

    noncomputable def Cslib.Algorithms.WordRAM.Schoening.stepLaw {n : ℕ} (φ : Logic.PL.CNF (Fin n)) (σ : Fin n → Bool) :
    PMF (Fin n → Bool)

    The law of one step of Cslib.Complexity.Schoening.walk.

    A satisfying assignment is never moved.

    One more step of the walk is a step followed by the walk.

    theorem Cslib.Algorithms.WordRAM.Schoening.stepBody_law {n : ℕ} {φ : Logic.PL.CNF (Fin n)} {C A w : ℕ} {M : ℕ → ℕ} (hw : WordRAM.Fits w (A + 3 * n)) (hA : 1 ≤ A) {x : WordRAM.Data 24} (hx : Frame φ C A M x) (hbits : ∀ i < n, x.mem (A + i) ≤ 1) (hcnt : WordRAM.Fits w (x.regs Reg.cnt + 1)) :
    WordRAM.ExecRandLaw w stepBody x (fun (y : WordRAM.Data 24) => (Frame φ C A M y ∧ ∀ i < n, y.mem (A + i) ≤ 1) ∧ y.regs Reg.cnt = x.regs Reg.cnt + 1 ∧ y.regs Reg.steps = x.regs Reg.steps) (12 * (encodeClauses φ).length + 18) 1 (fun (y : WordRAM.Data 24) => assignmentAt y.mem A n) (stepLaw φ (assignmentAt x.mem A n))

    One step has the law stepLaw φ σ, within 12 E + 18 instructions and one rand call.

    theorem Cslib.Algorithms.WordRAM.Schoening.walkLoop_law {n : ℕ} {φ : Logic.PL.CNF (Fin n)} {C A w : ℕ} {M : ℕ → ℕ} (hw : WordRAM.Fits w (A + 3 * n)) (hA : 1 ≤ A) (t : ℕ) (x : WordRAM.Data 24) :
    Frame φ C A M x → (∀ i < n, x.mem (A + i) ≤ 1) → x.regs Reg.cnt + t = x.regs Reg.steps → WordRAM.Fits w (x.regs Reg.steps) → WordRAM.ExecRandLaw w walkLoop x (fun (y : WordRAM.Data 24) => Frame φ C A M y ∧ ∀ i < n, y.mem (A + i) ≤ 1) (t * (12 * (encodeClauses φ).length + 20) + 2) t (fun (y : WordRAM.Data 24) => assignmentAt y.mem A n) (Complexity.Schoening.walk φ t (assignmentAt x.mem A n))

    The local search from t remaining steps has the law of walk φ t.

    One trial #

    The body of a trial: a uniform assignment, 3 n steps of local search, and the final check, which sets out to 1 exactly when the assignment satisfies the formula. The clauses are read from [cBeg, aBase), the assignment is stored from aBase, and n is read from nReg.

    The instruction bound of a trial body on n variables and a clause encoding of length E.

    theorem Cslib.Algorithms.WordRAM.Schoening.trialCore_law {n : ℕ} {φ : Logic.PL.CNF (Fin n)} {C A w : ℕ} {M : ℕ → ℕ} (hw : WordRAM.Fits w (A + 3 * n)) (hA : 1 ≤ A) {x : WordRAM.Data 24} (hx : Frame φ C A M x) :
    WordRAM.ExecRandLaw w trialCore x (fun (y : WordRAM.Data 24) => (Frame φ C A M y ∧ ∀ i < n, y.mem (A + i) ≤ 1) ∧ y.regs Reg.out = if Logic.PL.CNF.eval (assignmentAt y.mem A n) φ = true then 1 else 0) (trialCoreTime n (encodeClauses φ).length) (4 * n) (fun (y : WordRAM.Data 24) => assignmentAt y.mem A n) (Complexity.Schoening.trial φ)

    The body of a trial has the law of Cslib.Complexity.Schoening.trial, keeps the frame, and sets out to whether the assignment it leaves satisfies the formula.

    The program #

    Set up the registers of a trial on the input encode φ: the clauses start at address 2, and the assignment is stored just after the input.

    One trial of Schöning's algorithm on the input encode φ, as a structured program.

    One trial of Schöning's algorithm on the input encode φ, halting with out.

    The instruction bound of a trial on n variables and an input of length L.

    The data after the setup of a trial on the input encode φ.

    The setup establishes the frame of a trial on the input encode φ.

    One trial of Schöning's algorithm on the randomized word RAM. On the input encode φ of length L, at any word size w with L + 3 n + 1 < 2 ^ w, the program

    • halts on every path within trialTime n L = 36 n L + 66 n + 12 L + 19 instructions;
    • makes at most 4 n rand calls;
    • outputs 1 exactly when the assignment it leaves at address L + 1 satisfies φ;
    • leaves an assignment whose law is exactly Cslib.Complexity.Schoening.trial φ.