280 likes | 324 Views
Using Constraint Processing to Model, Solve, and Support Interactive Solving of Sudoku Puzzles Christopher G. Reeson Constraint Systems Laboratory Department of Computer Science & Engineering University of Nebraska-Lincoln Research supported by a UCARE grant. Goals.
E N D
Using Constraint Processing to Model, Solve, and Support Interactive Solving of Sudoku Puzzles Christopher G. Reeson Constraint Systems Laboratory Department of Computer Science & Engineering University of Nebraska-Lincoln Research supported by a UCARE grant Reeson–Undergraduate Thesis
Goals • How to use CP techniques on Sudoku puzzles? • Study & compare the effectiveness of various constraint propagation algorithms • How can we use CP to interactively support and guide human players? • Is Sudoku useful to illustrate and teach CP? • … How to deconstruct Sudoku solving to • Demystify the puzzle • Prevent students from losing time playing it Reeson–Undergraduate Thesis
Outline • Background information: the game, CSPs • Our contributions • Algorithms • Conjectures • Interface: interactive solver • Conclusions and future research • Live demonstration Reeson–Undergraduate Thesis
The game • Number place • Main properties • NP-complete [Yato 03] • Well-formed Sudoku: has 1 solution [Simonis 05] • Minimal Sudoku • In a 9x9 Sudoku: smallest known number of givens is 17 [Royle] • Symmetrical puzzles • Many axes of symmetry • Position of the givens, not their values • Often makes the puzzle non-minimal • Level of difficulties • Varied ranking systems exist • Mimimality and difficulty not related #15 on Royle’s web site Reeson–Undergraduate Thesis
V1 V2 {c, d, e, f} {d} V4 V3 {a, b, d} {a, b, c} Constraint Satisfaction Problem (CSP) • GivenP = (V, D, C) • V: set of variables • D: set of their domains • C: set of constraints (relations) restricting the acceptable combination of values for variables • Solution is a consistent assignment of values to variables • Query: find 1 solution, all solutions, etc. Reeson–Undergraduate Thesis
Sudoku as a CSP (9x9 puzzles) • Variables are the cells • Domains are sets {1,…,9} • Two models • Binary constraints: 810 all-different binary constraint between variables • Non-binary constraints: 27 all-different 9-ary constraints Reeson–Undergraduate Thesis
Solving Sudoku as a CSP • Search • Builds solutions by enumerating consistent combinations • Constraint propagation • Removes values that do not appear in a solution • Example, arc-consistency: Reeson–Undergraduate Thesis
Search • Backtrack search • Systematically enumerate solutions by instantiating one variable after another • Backtracks when a constraint is broken • Is sound and complete (guaranteed to find solution if one exists) • Look-ahead • Cleans up domain of ‘future’ variables, during search, given current instantiations • Forward Checking (FC): weak form of arc-consistency • Maintaining Arc-Consistency (MAC): arc-consistency Reeson–Undergraduate Thesis
Forward Checking (FC) • Forward Checking on the binary model Reeson–Undergraduate Thesis
Maintaining Arc-Consistency (MAC) • Arc Consistency on the binary model Reeson–Undergraduate Thesis
Generalized Arc-Consistency (GAC) • Operates on the non-binary model • Subsumes AC • Can be done in polynomial time for all-different constraints [Régin 94] Reeson–Undergraduate Thesis
Consistency Algorithms - SAC • Shaving Arc-Consistency • Binary • Stronger than Arc-Consistency Michael Mepham Extreme #26 Reeson–Undergraduate Thesis
Consistency Algorithms - SGAC • Shaving Generalized Arc-Consistency • Non-binary • Subsumes SAC Michael Mepham Extreme #26 Reeson–Undergraduate Thesis
Consistency relationships Sets denote the values removed by propagation Reeson–Undergraduate Thesis
R(1,2)C • Takes two constraints and • Removes any value that does not appear in a solution to both constraints Reeson–Undergraduate Thesis
Consistency R(1,2)C Reeson–Undergraduate Thesis
Conjecture 1: R(1,2)CSGAC • R(1,2)C is at least as strong as SGAC • Is SGAC at least as strong as R(1,2)C? • We proved R(1,2)CSGAC when the 2 relations overlap over one variable Reeson–Undergraduate Thesis
Conjecture 2: SGAC solves.. … every: • Well-formed • 9x9 Sudoku? • Proof still open, but empirical evidence is strong • Warning • Sudoku is NP-Complete • SGAC is polynomial • When a 9x9 Sudoku has multiple solutions, SGAC does not solve it • Proof may exploit the fact that all non-binary constraints are 1-tight Reeson–Undergraduate Thesis
Interactive interface • Loading instances • Buttons for propagation • On individual constraints • For consistency algorithms: AC, GAC, SAC, SGAC • For interactive look-ahead • Finding solutions • Hint panel • Highlighting error • Manual domain filtering • Manual assignments • Do/Undo buttons supporting every functionality Reeson–Undergraduate Thesis
Buttons for constraint propagation • Buttons allow users to run • AC • GAC • Over each • Column • Row • Block Reeson–Undergraduate Thesis
Finding solutions • Find the number of solutions at any point in the game • Creates a copy of the CSP displayed on the board • Runs consistency algorithms to speed up search • Runs exhaustive search to find all solutions • Found Sudokus that are not well-formed on the web and in… the LA Times Reeson–Undergraduate Thesis
Hint panel • Two types of hints: Singleton & Vital • Eight Levels (6 implemented) • FC, AC, Single GAC, GAC, Single SAC, SAC, Single SGAC, SGAC • Highlight a cell with a hint • Option to move to the next hint • Display the number of hints • Preliminary tests show that this new strategy can teach people to play Sudoku effectively and… get them quickly bored with the game Reeson–Undergraduate Thesis
Web-based implementation • XML files • Solver • Constructor Reeson–Undergraduate Thesis
The way humans play • ‘Cross-hash,’ sweep through lines, columns, and blocks • Pencil in possible positions of values • Generally, look for patterns, some intricate, and give them funny names: • Naked single, locked pairs, swordfish, medusa, etc. • ‘Identified’ dozens of strategies • Many fall under a unique constraint propagation technique • But humans do not seem to be able to carry simple inference (i.e., propagation) in a systematic way for more than a few steps Reeson–Undergraduate Thesis
Impact • Inspired a PhD student @ USC • to use propagation to infer the applicable constraints for automatically modeling various types of Sudoku puzzles • Collaborating with an MS student @ USC on Constructor • Inputting, storing instances • Generalization to other Sudoku variations Reeson–Undergraduate Thesis
Conclusions: CP solving • Propagation is useful for • effectively guide humans & train them in Sudoku solving • SAC (often), SGAC (always) can solve any well-formed Sudoku, also noticed by Simonis • Sudoku good illustration of the power of SAC and SGAC • otherwise thought to be more expensive than effective • No need for extra CP modeling as Simonis did: • Use the simple constraint model on row, columns, blocks • Stick to general purpose propagation algorithms • But… Who wants to play Sudoku any more when they know that SGAC can do it? Reeson–Undergraduate Thesis
Finally • Future work: • Use the interface to generate Sudoku puzzles • Investigate theoretically the relationship between • Sudoku size • And the consistency level necessary for solving it • Live demonstration Reeson–Undergraduate Thesis
Thank you for your attention I would be glad to answer your questions Reeson–Undergraduate Thesis