Using Constraint Processing to Model, Solve, and Support Interactive Solving

Transcription

Using Constraint Processing to Model, Solve, and Support Interactive Solving
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
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
1
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
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
2
Outline
• Background information: the game, CSPs
• Our contributions
– Algorithms
– Conjectures
– Interface: interactive solver
• Conclusions and future research
• Live demonstration
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
3
The game
• Number place
• Main properties
– NP-complete
– Well-formed Sudoku: has 1 solution
– Minimal Sudoku
[Yato 03]
[Simonis 05]
• 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
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
4
Constraint Satisfaction Problem (CSP)
V1
• Given P = (V, D, C)
V2
{d}
{c, d, e, f}
V
4
V3
– V: set of variables
{a, b, d}
{a, b, c}
– 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.
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
5
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
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
6
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:
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
7
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
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
8
Forward Checking (FC)
• Forward Checking on
the binary model
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
9
Maintaining Arc-Consistency (MAC)
• Arc Consistency on
the binary model
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
10
Generalized Arc-Consistency (GAC)
• Operates on the nonbinary model
• Subsumes AC
• Can be done in
polynomial time for
all-different
constraints [Régin 94]
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
11
Consistency Algorithms - SAC
• Shaving Arc-Consistency
– Binary
– Stronger than ArcConsistency
Michael Mepham Extreme #26
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
12
Consistency Algorithms - SGAC
• Shaving Generalized
Arc-Consistency
– Non-binary
– Subsumes SAC
Michael Mepham Extreme #26
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
13
Consistency relationships
Sets denote the values
removed by propagation
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
14
R(1,2)C
• Takes two constraints and
• Removes any value that does not appear in a
solution to both constraints
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
15
Consistency R(1,2)C
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
16
Conjecture 1: R(1,2)CSGAC
 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)CSGAC when the 2 relations
overlap over one variable
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
17
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
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
18
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
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
19
Buttons for constraint propagation
• Buttons allow
users to run
– AC
– GAC
• Over each
– Column
– Row
– Block
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
20
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
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
21
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
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
22
Web-based implementation
• XML files
• Solver
• Constructor
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
23
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
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
24
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
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
25
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?
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
26
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
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
27
Thank you for your attention
I would be glad
to answer your questions
Constraint Systems Laboratory
March 26, 2007
Reeson–Undergraduate Thesis
28