Sudoku

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

description

AI applied on Sudoko

Transcript of Sudoku

Page 1: Sudoku

Solving Sudoku using Constraint Satisfaction Techniques

Supervised by: Dr. Aliaa Youssif

Presented by: Mona Yassin

Yara Ali

Page 2: Sudoku

Agenda

• Objective

• Constraint Satisfaction Problems (CSP)

• Sudoku

• NP-Complete Problems

• Evaluation

• Analysis

• Conclusion & Future Work

Page 3: Sudoku

Objective

• This project:

1. Examines solving Sudoku puzzles as “ Constraint Satisfaction Problems (CSP) “

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

Page 4: Sudoku

Constraint Satisfaction Problems (CSP)

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

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

Page 5: Sudoku

Constraint Satisfaction Problems (CSP) – Cont.

Page 6: Sudoku

Constraint Satisfaction Problems (CSP) – Cont.

• 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 constraints.

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

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

Page 7: Sudoku

Constraint Satisfaction Problems (CSP) – Cont.

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

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

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

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

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

Page 8: Sudoku

Constraint Satisfaction Problems (CSP) – Cont.

• CSP:– state is defined by variables Xi with values from

domain Di

– goal test is a set of constraints specifying allowable combinations of values for subsets of variables

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

• .–

Page 9: Sudoku

Example: Map-Coloring

• Variables WA, NT, Q, NSW, V, SA, T • Domains Di = {red,green,blue}• Constraints: adjacent regions must have different colors• e.g., WA ≠ NT, or (WA,NT) in {(red,green),(red,blue),(green,red),

(green,blue),(blue,red),(blue,green)}

••

Page 10: Sudoku

Example: Map-Coloring

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

Page 11: Sudoku

Sudoku

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

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

Page 12: Sudoku

Sudoku

• The only rule is that each row, column, and region must contain only one instance of each numeral.

Page 13: Sudoku

Sudoku

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

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

Page 14: Sudoku

NP- Complete problems

• The abbreviation NP refers to "nondeterministic polynomial time."

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

Page 15: Sudoku

NP- Complete problems Examples

• Games and puzzles• Bejeweled• Candy Crush Saga• FreeCell• Minesweeper Consistency Problem• Sudoku • Super Mario Bros• Tetris• Verbal arithmetic

Page 16: Sudoku

Proposed Solution

• Constraint satisfaction problems are defined by three items:

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

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

Page 17: Sudoku

Generation Strategy

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

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

Page 18: Sudoku

Solving Strategy

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

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

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

Page 19: Sudoku

Solving Strategy cont.

• During propagation, if any square's list is reduced to only one value, that value is assigned to that square. The puzzle is solved when every square has only one remaining possibility.

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

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

Page 20: Sudoku

Solving strategy cont.

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

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

Page 21: Sudoku

A Smart Backtracking Algorithm.

• The basic idea is as follows:• In the search tree of the backtracking algorithm whenever a node is

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

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

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

Page 22: Sudoku

Example: Map-Coloring

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

Page 23: Sudoku

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

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

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

• Backtracking search is the basic uninformed algorithm for CSPs

• Can solve n-queens for n ≈ 25

•••

–•

Page 24: Sudoku

Backtracking search

Page 25: Sudoku

Backtracking example

Page 26: Sudoku

Backtracking example

Page 27: Sudoku

Backtracking example

Page 28: Sudoku

Backtracking example

Page 29: Sudoku

a forward-looking backtracking search.

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

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

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

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

Page 30: Sudoku

Forward checking

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

Page 31: Sudoku

Forward checking

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

Page 32: Sudoku

Forward checking

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

Page 33: Sudoku

Forward checking

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

Page 34: Sudoku

Evaluation

• From each set of Easy Symmetric (ES), Easy Asymmetric (EA), Hard Symmetric (HS) and Hard Asymmetric (HA), a group of thirty randomly generated puzzles were used in the investigation.

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

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

Page 35: Sudoku

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

Page 36: Sudoku

Times for Solving Easy Sudokus

Page 37: Sudoku

Evaluation

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

Page 38: Sudoku

Times for Solving Hard Sudokus

Page 39: Sudoku

Evaluation

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

Page 40: Sudoku

Analysis

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

Page 41: Sudoku

Times for solving various Sudokus

Page 42: Sudoku

Analysis

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

Page 43: Sudoku

Conclusion and Future Work

• The motive of this project has been to model Sudoku as a CSP and to evaluate the correlation between the symmetry and time required to solve the puzzle.

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

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

Page 44: Sudoku

References

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

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

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

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

Page 45: Sudoku

References

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

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

Page 46: Sudoku

References

• http://en.wikipedia.org/wiki/List_of_NP-complete_problems

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

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

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

Page 47: Sudoku

Any Questions?

Page 48: Sudoku

Thank You !