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.
- 1input: a CNF formula , noise probability , a flip budget
- 2assign each variable a random truth value
- 3repeat times
- 4if the assignment satisfies every clause then return it
- 5a randomly chosen unsatisfied clause
- 6with probability do
- 7flip a random variable in
- 8otherwise
- 9flip the variable in that leaves the most clauses satisfied
- 10return 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
- Project repository
- Reference notes: Logical Agents and Propositional Logic
- Reference notes: Propositional Inference and Logical Agents
- Reference notes: NP-Completeness
╌╌ END ╌╌