Artificial Intelligence/Logic Algorithms
53 / 67

10/2021Artificial Intelligence

Logic Algorithms

Solving Boolean satisfiability with the GSAT and WalkSAT local-search algorithms, applied to Sudoku puzzles encoded as propositional clauses.

╌╌╌╌

Boolean satisfiability (SAT) asks whether a propositional formula can be made true by some assignment of its variables. It is NP-complete, so no known algorithm decides it in polynomial time; a polynomial solution would settle P versus NP. This project sidesteps the worst case with local search, trading completeness for speed, and uses it to solve Sudoku puzzles.

SAT solvers take conjunctive normal form,

a conjunction of clauses, each clause a disjunction of literals . A Sudoku board encodes as one Boolean variable per (row, column, digit) triple, true when cell holds digit ; the code names each variable by the three-digit string rcd and negates with a leading -. The Sudoku class writes the rules out as clauses to a .cnf file — each cell gets an at-least-one disjunction over its nine digits plus pairwise at-most-one clauses, each row, column, and block gets a clause per digit forcing it to appear, and every given cell contributes a unit clause. Smaller staged files (one_cell, rows, rows_and_cols, rules) build the encoding up piece by piece for testing. A completed board is a satisfying assignment.

The SAT solver is generic: it reads any CNF file, maps each variable to an index with a two-way dictionary, and starts from a random full assignment, flipping one variable at a time. GSAT and WalkSAT share a noise parameter (threshold ) and a flip budget (max_iterations ). On each step GSAT, with probability , flips a random variable, and otherwise scans all variables and flips the one whose flip leaves the most clauses satisfied — hill climbing that stalls on local optima where no single flip helps.

WalkSAT narrows the search. It first collects the currently unsatisfied clauses, picks one at random and, with probability , flips a random variable in it; otherwise it considers only that clause's variables and flips the one that leaves the most clauses satisfied. Restricting the greedy scan to a single broken clause is what makes each step cheap.

Algorithm:WalkSAT(Φ,p,n)\textsc{WalkSAT}(\Phi, p, n) — noisy local search for SAT
  1. 1
    input: a CNF formula Φ\Phi, noise probability pp, a flip budget nn
  2. 2
    assign each variable a random truth value
  3. 3
    repeat nn times
  4. 4
    if the assignment satisfies every clause then return it
  5. 5
    CC \gets a randomly chosen unsatisfied clause
  6. 6
    with probability pp do
  7. 7
    flip a random variable in CC
  8. 8
    otherwise
  9. 9
    flip the variable in CC that leaves the most clauses satisfied
  10. 10
    return failure

Neither algorithm is complete: on an unsatisfiable formula they simply exhaust the flip budget without reporting that no assignment exists. On satisfiable instances like a valid Sudoku they find an assignment quickly, which is the regime this project targets.

References

  1. Project repository
  2. Reference notes: Logical Agents and Propositional Logic
  3. Reference notes: Propositional Inference and Logical Agents
  4. Reference notes: NP-Completeness

╌╌ END ╌╌