Sudoku

48
1 Solving Sudoku using Solving Sudoku using Constraint Satisfaction Constraint Satisfaction Techniques Techniques Supervised by: Dr. Aliaa Supervised by: Dr. Aliaa Youssif Youssif Presented by: Mona Yassen Presented by: Mona Yassen Yara Ali Yara Ali 1

Transcript of Sudoku

Page 1: Sudoku

1

Solving Sudoku using Solving Sudoku using Constraint Satisfaction Constraint Satisfaction

TechniquesTechniques

Supervised by: Dr. Aliaa Supervised by: Dr. Aliaa YoussifYoussif

Presented by: Mona Yassen Presented by: Mona Yassen Yara AliYara Ali

1

Page 2: Sudoku

2

AgendaAgenda

ObjectiveObjective Constraint Satisfaction Problems (CSP)Constraint Satisfaction Problems (CSP) SudokuSudoku NP-Complete ProblemsNP-Complete Problems EvaluationEvaluation AnalysisAnalysis ConclusionConclusion

2

Page 3: Sudoku

3

ObjectiveObjective

This project:This project:1.1. Examines solving Sudoku puzzles as “ Examines solving Sudoku puzzles as “

Constraint Satisfaction Problems (CSP) “Constraint Satisfaction Problems (CSP) “

2.2. Determines if the symmetry of a puzzle Determines if the symmetry of a puzzle has a correlation with the time taken to has a correlation with the time taken to solve when using CSP.solve when using CSP.

3

Page 4: Sudoku

4

Constraint Satisfaction Constraint Satisfaction Problems (CSP)Problems (CSP)

It might be said that there are five basic tree It might be said that there are five basic tree search algorithms for the constraint satisfaction search algorithms for the constraint satisfaction problem (csp), namely, naive backtracking (BT), problem (csp), namely, naive backtracking (BT), backjumping (BJ), conflict-directed backjumping backjumping (BJ), conflict-directed backjumping (CBJ), backmarking (BM), and forward checking (CBJ), backmarking (BM), and forward checking (FC). (FC).

In In broad terms, BT, BJ, and CBJ describe broad terms, BT, BJ, and CBJ describe different styles of backward move different styles of backward move (backtracking), whereas BT, BM, and FC (backtracking), whereas BT, BM, and FC describe different styles of forward move describe different styles of forward move (labeling of variables).(labeling of variables).

4

Page 5: Sudoku

5

Constraint Satisfaction Problems Constraint Satisfaction Problems (CSP) – Cont.(CSP) – Cont.

5

Page 6: Sudoku

6

Constraint Satisfaction Constraint Satisfaction Problems (CSP) – Cont.Problems (CSP) – Cont.

a CSP is a problem composed of a finite set of variables a CSP is a problem composed of a finite set of variables each of which has a finite domain of values and a set of each of which has a finite domain of values and a set of constraints.constraints.

Each constraint is defined over some subset of the Each constraint is defined over some subset of the original set of variables and restricts the values.original set of variables and restricts the values.

These variables can simultaneously take. The task is to These variables can simultaneously take. The task is to find an assignment of a value for each variable such that find an assignment of a value for each variable such that the assignments satisfy all the constraints.the assignments satisfy all the constraints.

6

Page 7: Sudoku

7

Constraint Satisfaction Constraint Satisfaction Problems (CSP) – Cont.Problems (CSP) – Cont.

The idea: represent states as a vector of feature values. The idea: represent states as a vector of feature values. We haveWe have

■■k-features (or variables)k-features (or variables)■■Each feature takes a value. Domain of possible values for Each feature takes a value. Domain of possible values for

the variables:the variables: height = {short, average, tall}, height = {short, average, tall}, weight = {light, average, heavy}.weight = {light, average, heavy}.

In CSPs, the problem is to search for a set of values for In CSPs, the problem is to search for a set of values for the features (variables) so that the values satisfy some the features (variables) so that the values satisfy some conditions (constraints).conditions (constraints).

■■i.e., a goal state specified as conditions on the vector of i.e., a goal state specified as conditions on the vector of feature values.feature values.

7

Page 8: Sudoku

8

Constraint Satisfaction Constraint Satisfaction Problems (CSP) – Cont.Problems (CSP) – Cont.

CSP:CSP: state is defined by variables state is defined by variables XX ii with values from with values from

domain domain DD ii goal test is a set of constraints specifying allowable goal test is a set of constraints specifying allowable

combinations of values for subsets of variablescombinations of values for subsets of variables

We are looking at a map of Australia showing We are looking at a map of Australia showing each of its states and territories, and that we are each of its states and territories, and that we are given the task of coloring each region either red, given the task of coloring each region either red, green, or blue in such a way that no neighboring green, or blue in such a way that no neighboring regions have the same colorregions have the same color

..8

Page 9: Sudoku

9

Example: Map-ColoringExample: Map-Coloring

Variables Variables WA, NT, Q, NSW, V, SA, TWA, NT, Q, NSW, V, SA, T Domains Domains DDii = {red,green,blue} = {red,green,blue} Constraints: adjacent regions must have different colorsConstraints: adjacent regions must have different colors

e.g., WA ≠ NT, or (WA,NT) in {(red,green),(red,blue),(green,red), e.g., WA ≠ NT, or (WA,NT) in {(red,green),(red,blue),(green,red), (green,blue),(blue,red),(blue,green)}(green,blue),(blue,red),(blue,green)}

9

Page 10: Sudoku

10

Example: Map-ColoringExample: Map-Coloring

Solutions are complete and consistent Solutions are complete and consistent assignments, e.g., WA = red, NT = green,Q = assignments, e.g., WA = red, NT = green,Q = red,NSW = green,V = red,SA = blue,T = greenred,NSW = green,V = red,SA = blue,T = green

10

Page 11: Sudoku

11

SudokuSudoku

Sudoku is a reasoning & logic puzzle, designed Sudoku is a reasoning & logic puzzle, designed by American Howard Garns, a retired architect & by American Howard Garns, a retired architect & freelance puzzle constructor.freelance puzzle constructor.

The objective is to fill a 9×9 grid with digits so The objective is to fill a 9×9 grid with digits so that each column, each row, and each of the that each column, each row, and each of the nine 3×3 sub-grids that compose the grid (also nine 3×3 sub-grids that compose the grid (also called "boxes", "blocks", "regions", or "sub-called "boxes", "blocks", "regions", or "sub-squares") contains all of the digits from 1 to 9. squares") contains all of the digits from 1 to 9.

11

Page 12: Sudoku

12

SudokuSudoku The only rule is that each row, column, and region must The only rule is that each row, column, and region must

contain only one instance of each numeral.contain only one instance of each numeral.

12

Page 13: Sudoku

13

SudokuSudoku

There have been many algorithms developed There have been many algorithms developed since then to generate and solve Sudoku, it still since then to generate and solve Sudoku, it still offers an exciting artificial intelligence challenge offers an exciting artificial intelligence challenge as there is no particular efficient way to solve all as there is no particular efficient way to solve all the puzzles. the puzzles.

Sudoku being puzzle with only one unique Sudoku being puzzle with only one unique solution, while solving Sudoku there is always a solution, while solving Sudoku there is always a finite number of integer entries until puzzle finite number of integer entries until puzzle completion. So this places Sudoku in the difficult completion. So this places Sudoku in the difficult to solve NP-complete class to solve NP-complete class

13

Page 14: Sudoku

14

NP- Complete problemsNP- Complete problems

The abbreviation The abbreviation NPNP refers to " refers to "nondeterministicnondeterministic polynomial time." polynomial time."

NPNP is the set of all decision problems for is the set of all decision problems for which the instances where the answer is which the instances where the answer is "yes" have efficiently verifiable proofs of "yes" have efficiently verifiable proofs of the fact that the answer is indeed "yes". the fact that the answer is indeed "yes".

14

Page 15: Sudoku

15

NP- Complete problems NP- Complete problems ExamplesExamples

Games and puzzlesGames and puzzles BejeweledBejeweled Candy Crush SagaCandy Crush Saga FreeCellFreeCell Minesweeper Consistency ProblemMinesweeper Consistency Problem Sudoku Sudoku Super Mario BrosSuper Mario Bros TetrisTetris Verbal arithmeticVerbal arithmetic

15

Page 16: Sudoku

16

Proposed SolutionProposed Solution

Constraint satisfaction problems are defined by three Constraint satisfaction problems are defined by three items: items:

1) a finite set of variables 1) a finite set of variables 2) a function that maps each variable to a finite domain 2) a function that maps each variable to a finite domain 3) a finite set of constraints. 3) a finite set of constraints.

For our puzzle the variables are the integers 1 – 9 and For our puzzle the variables are the integers 1 – 9 and the constraints on these variables can be considered a the constraints on these variables can be considered a 9*9 grid where no two same integers can be in the 9*9 grid where no two same integers can be in the same row, same column or the same sub-grid. same row, same column or the same sub-grid.

16

Page 17: Sudoku

17

Generation StrategyGeneration Strategy

The CSP algorithm inputs a value for an open The CSP algorithm inputs a value for an open location on the grid and checks it validity based location on the grid and checks it validity based on the given constraints. on the given constraints.

By repeatedly choosing a value for another By repeatedly choosing a value for another variable, it incrementally instantiate variables variable, it incrementally instantiate variables and extend a partial solution that specifies and extend a partial solution that specifies consistent values for some of the variables consistent values for some of the variables toward a complete solution using toward a complete solution using a smarta smart backtracking algorithmbacktracking algorithm . .

17

Page 18: Sudoku

18

Solving StrategySolving Strategy

A list of possible numbers for each cell of the 9x9 board A list of possible numbers for each cell of the 9x9 board is stored and the values for each square that conflict with is stored and the values for each square that conflict with the initial configuration of the game board are crossed the initial configuration of the game board are crossed out. out.

Then, for each square, S, in the board, algorithm will Then, for each square, S, in the board, algorithm will examine each value in its list of possible candidates, V.examine each value in its list of possible candidates, V.

If assignment of any one of those values, V[i], to that If assignment of any one of those values, V[i], to that square, S forces all possible candidates for another square, S forces all possible candidates for another square to be eliminated, and later cross V[i] out from the square to be eliminated, and later cross V[i] out from the list of possible candidates for S.list of possible candidates for S.

18

Page 19: Sudoku

19

Solving Strategy cont.Solving Strategy cont. During propagation, if any square's list is reduced to only During propagation, if any square's list is reduced to only

one value, that value is assigned to that square. The one value, that value is assigned to that square. The puzzle is solved when every square has only one puzzle is solved when every square has only one remaining possibility. remaining possibility.

If during propagation no more values can be eliminated, If during propagation no more values can be eliminated, meaning that there are no obvious moves, we employ a meaning that there are no obvious moves, we employ a forward-looking backtracking searchforward-looking backtracking search . .

To ensure the backtracking algorithm is complete, the To ensure the backtracking algorithm is complete, the solver maintains a list of every combination of decisions solver maintains a list of every combination of decisions that led to an error. that led to an error.

19

Page 20: Sudoku

20

Solving strategy cont.Solving strategy cont.

The algorithm then looks ahead before future The algorithm then looks ahead before future decisions to make sure that applying the next decisions to make sure that applying the next decision will not create a state where the current decision will not create a state where the current decision set matches one previous eliminated decision set matches one previous eliminated from the search space. from the search space.

In this way, the commutative property of In this way, the commutative property of constraint satisfaction problems reduces the constraint satisfaction problems reduces the number of decision paths that must be stored. number of decision paths that must be stored.

20

Page 21: Sudoku

21

A Smart Backtracking A Smart Backtracking Algorithm.Algorithm.

The basic idea is as follows:The basic idea is as follows: In the search tree of the backtracking algorithm In the search tree of the backtracking algorithm

whenever a node is visited a constraint propagation whenever a node is visited a constraint propagation algorithm is performed to attain a desired level of algorithm is performed to attain a desired level of consistency by removing inconsistent values from the consistency by removing inconsistent values from the domains of the as yet uninstantiated variables.domains of the as yet uninstantiated variables.

If in the process of constraint propagation at the node If in the process of constraint propagation at the node the domain of any variable becomes empty then the the domain of any variable becomes empty then the node is pruned.node is pruned.

The purpose of doing this is to detect a dead end as The purpose of doing this is to detect a dead end as early as possible This way potential thrashing can be early as possible This way potential thrashing can be reduced and the size of the search tree is reducedreduced and the size of the search tree is reduced

21

Page 22: Sudoku

22

Example: Map-ColoringExample: Map-Coloring

Solutions are complete and consistent Solutions are complete and consistent assignments, e.g., WA = red, NT = green,Q = assignments, e.g., WA = red, NT = green,Q = red,NSW = green,V = red,SA = blue,T = greenred,NSW = green,V = red,SA = blue,T = green

22

Page 23: Sudoku

23

Backtracking searchBacktracking search Variable assignments are commutative}, i.e.,Variable assignments are commutative}, i.e.,[ WA = red then NT = green ] same as [ NT = green then WA = red ][ WA = red then NT = green ] same as [ NT = green then WA = red ]

Only need to consider assignments to a single variable at each Only need to consider assignments to a single variable at each nodenode b = d and there are $d^n$ leavesb = d and there are $d^n$ leaves

Depth-first search for CSPs with single-variable assignments is Depth-first search for CSPs with single-variable assignments is called backtracking searchcalled backtracking search

Backtracking search is the basic uninformed algorithm for CSPsBacktracking search is the basic uninformed algorithm for CSPs

Can solve Can solve nn-queens for -queens for nn ≈ 25 ≈ 25

23

Page 24: Sudoku

24

Backtracking searchBacktracking search

24

Page 25: Sudoku

25

Backtracking exampleBacktracking example

25

Page 26: Sudoku

26

Backtracking exampleBacktracking example

26

Page 27: Sudoku

27

Backtracking exampleBacktracking example

27

Page 28: Sudoku

28

Backtracking exampleBacktracking example

28

Page 29: Sudoku

29

Forward-looking backtracking Forward-looking backtracking search.search.

This algorithm is able to back up or “undo” previously made This algorithm is able to back up or “undo” previously made decisions.decisions.

The goal of forward checking is to “fail early” by detecting The goal of forward checking is to “fail early” by detecting inconsistencies within the search tree as early as possible, inconsistencies within the search tree as early as possible, thus saving the exploration of fruitless alternatives. thus saving the exploration of fruitless alternatives.

Forward checking performs more work per node than the Forward checking performs more work per node than the algorithms presented so far, but attempts to visit as few nodes algorithms presented so far, but attempts to visit as few nodes as possible. as possible.

It is hope that this results in a net saving in consistency It is hope that this results in a net saving in consistency checks performed during the search process.checks performed during the search process.

29

Page 30: Sudoku

30

Forward checkingForward checking

Idea: Idea: Keep track of remaining legal values for unassigned variablesKeep track of remaining legal values for unassigned variables Terminate search when any variable has no legal valuesTerminate search when any variable has no legal values

30

Page 31: Sudoku

31

Forward checkingForward checking

Idea: Idea: Keep track of remaining legal values for unassigned variablesKeep track of remaining legal values for unassigned variables Terminate search when any variable has no legal valuesTerminate search when any variable has no legal values

31

Page 32: Sudoku

32

Forward checkingForward checking

Idea: Idea: Keep track of remaining legal values for unassigned variablesKeep track of remaining legal values for unassigned variables Terminate search when any variable has no legal valuesTerminate search when any variable has no legal values

32

Page 33: Sudoku

33

Forward checkingForward checking

Idea: Idea: Keep track of remaining legal values for unassigned variablesKeep track of remaining legal values for unassigned variables Terminate search when any variable has no legal valuesTerminate search when any variable has no legal values

33

Page 34: Sudoku

34

EvaluationEvaluation From each set of Easy Symmetric (ES), Easy From each set of Easy Symmetric (ES), Easy

Asymmetric (EA), Hard Symmetric (HS) and Hard Asymmetric (EA), Hard Symmetric (HS) and Hard Asymmetric (HA), a group of thirty randomly generated Asymmetric (HA), a group of thirty randomly generated puzzles were used in the investigation. puzzles were used in the investigation.

An empirical evaluation of the proposed hypothesis An empirical evaluation of the proposed hypothesis involves executing the algorithm on several test puzzles involves executing the algorithm on several test puzzles and recording the time taken to compute each solution. and recording the time taken to compute each solution.

The constraint-based backtracking approach is a simple The constraint-based backtracking approach is a simple approach to finding a solution to any Sudoku (provided a approach to finding a solution to any Sudoku (provided a solution exists). Even though the search tree could have solution exists). Even though the search tree could have up to 9^81 leaves, the algorithm can still find the solution up to 9^81 leaves, the algorithm can still find the solution relatively quickly. relatively quickly.

34

Page 35: Sudoku

35

Times for Solving 4 Times for Solving 4 different kind of different kind of Sudoku Puzzles in Sudoku Puzzles in 30 test runs 30 test runs

35

Page 36: Sudoku

36

Times for Solving Easy Times for Solving Easy Sudokus Sudokus

36Figure 2

Page 37: Sudoku

37

EvaluationEvaluation

Because so many clues are given, the Because so many clues are given, the number of incorrect decisions and number of incorrect decisions and consequently the time spent backtracking consequently the time spent backtracking by the solving algorithm is greatly by the solving algorithm is greatly reduced. For the easiest class of reduced. For the easiest class of Sudoku, those containing symmetry and Sudoku, those containing symmetry and more than 38 givens, solving is made more than 38 givens, solving is made easy because of the obvious easy because of the obvious assignments present at each step of the assignments present at each step of the search.search.

37

Page 38: Sudoku

38

Times for Solving Hard Times for Solving Hard Sudokus Sudokus

38Figure 3

Page 39: Sudoku

39

EvaluationEvaluation

When solving more difficult puzzles, When solving more difficult puzzles, symmetry plays less of a role because the symmetry plays less of a role because the algorithm is bound to perform quite a bit of algorithm is bound to perform quite a bit of decision-making and backtracking. Even decision-making and backtracking. Even so, Figure 3 shows the pattern that so, Figure 3 shows the pattern that symmetric puzzles are more easily solved. symmetric puzzles are more easily solved.

39

Page 40: Sudoku

40

AnalysisAnalysis

When starting grid cells are placed in a When starting grid cells are placed in a symmetric pattern, our data supports that symmetric pattern, our data supports that the number of steps taken by the solver is the number of steps taken by the solver is reduced substantially to make the average reduced substantially to make the average time to compute a solution approximately time to compute a solution approximately 80% faster for easy puzzles and 80% faster for easy puzzles and approximately 60% faster for hard approximately 60% faster for hard puzzles. puzzles.

40

Page 41: Sudoku

41

Times for solving various Times for solving various Sudokus Sudokus

41Figure 4

Page 42: Sudoku

42

AnalysisAnalysis

Puzzles that are symmetric have a much Puzzles that are symmetric have a much more evenly distributed set of givens. more evenly distributed set of givens. Givens in the same row, column, or region Givens in the same row, column, or region provide us with more constraints than provide us with more constraints than those that are not, and symmetric grids those that are not, and symmetric grids create these pairs of givens with a greater create these pairs of givens with a greater frequency. Therefore, symmetric puzzles frequency. Therefore, symmetric puzzles can be solved in less time when modeled can be solved in less time when modeled as a CSP. as a CSP.

42

Page 43: Sudoku

43

ConclusionConclusion The motive of this project has been to model Sudoku as The motive of this project has been to model Sudoku as

a CSP and to evaluate the correlation between the a CSP and to evaluate the correlation between the symmetry and time required to solve the puzzle.symmetry and time required to solve the puzzle.

As expected our experimental results indicate that As expected our experimental results indicate that symmetry indeed does have an effect on the solution symmetry indeed does have an effect on the solution time. time.

The reason asymmetric puzzles take substantially longer The reason asymmetric puzzles take substantially longer time to solve when compared to symmetric puzzles is time to solve when compared to symmetric puzzles is that, when modeled as a CSP there is more information that, when modeled as a CSP there is more information available in symmetric puzzles which makes the solving available in symmetric puzzles which makes the solving more efficient. more efficient.

43

Page 44: Sudoku

44

ReferencesReferences

http://www.inf.ucv.cl/~bcrawford/2009_1%20Paphttp://www.inf.ucv.cl/~bcrawford/2009_1%20Papers%20Tesis/teamsudoku.pdfers%20Tesis/teamsudoku.pdf

http://en.wikipedia.org/wiki/Sudokuhttp://en.wikipedia.org/wiki/Sudoku

http://www.cs.toronto.edu/~fbacchus/Papers/liu.http://www.cs.toronto.edu/~fbacchus/Papers/liu.pdfpdf

http://www0.cs.ucl.ac.uk/staff/a.moore/cspReviehttp://www0.cs.ucl.ac.uk/staff/a.moore/cspReview99.pdfw99.pdf

44

Page 45: Sudoku

45

ReferencesReferences

http://en.wikipedia.org/wiki/NP_%28complexityhttp://en.wikipedia.org/wiki/NP_%28complexity%29%29

http://www.google.com.eg/url?http://www.google.com.eg/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cadsa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCkQFjAA&url=http%3A=rja&uact=8&ved=0CCkQFjAA&url=http%3A%2F%2Faima.eecs.berkeley.edu%2Fslides-ppt%2F%2Faima.eecs.berkeley.edu%2Fslides-ppt%2Fm5-%2Fm5-csp.ppt&ei=esBOU4rCBMfB0QXe_4HwCA&usgcsp.ppt&ei=esBOU4rCBMfB0QXe_4HwCA&usg=AFQjCNFJd26O4Dwuft_EBiACC-=AFQjCNFJd26O4Dwuft_EBiACC-tbgWq5vw&sig2=fpPFY7CU3zYts-tbgWq5vw&sig2=fpPFY7CU3zYts-jWuqzNsQ&bvm=bv.64542518,bs.1,d.d2kjWuqzNsQ&bvm=bv.64542518,bs.1,d.d2k 45

Page 46: Sudoku

46

ReferencesReferences http://en.wikipedia.org/wiki/List_of_NP-http://en.wikipedia.org/wiki/List_of_NP-

complete_problemscomplete_problems

http://www.cs.toronto.edu/~hojjat/384w09/Lecturhttp://www.cs.toronto.edu/~hojjat/384w09/Lectures/Lecture-04-Backtracking-Search.pdfes/Lecture-04-Backtracking-Search.pdf

http://www.dcs.gla.ac.uk/~pat/cpM/papers/CI9%http://www.dcs.gla.ac.uk/~pat/cpM/papers/CI9%283%29.pdf283%29.pdf

http://www.dcs.gla.ac.uk/~pat/cpM/papers/CI9%http://www.dcs.gla.ac.uk/~pat/cpM/papers/CI9%283%29.pdf283%29.pdf

46

Page 47: Sudoku

47

Any Questions?Any Questions?

47

Page 48: Sudoku

48

Thank You !Thank You !

48