Artificial Intelligence
Homework 3 |
|
Our final application of search is solving constraint satisfaction problems. In this assignment, you will implement the AC-3 algorithm and BackTracking search to solve Sudoku puzzles. This is a programming assignment and you may work with a partner if you choose. Click here to download the starter code. You will notice that the code is arranged in packages:
SudokuIf you have never played a game of Sudoku, I recommend going online (https://www.websudoku.com/) and playing a few games first. I also recommend reading section 6.2.6 in the textbook which talks specifically about solving Sudoku puzzles using CSPs.
The starting Sudoku board is blank with the exception of a few cells that have been filled in. Below is an example of an initial Sudoku board and the corresponding solution. Representing a Constraint Satisfaction ProblemA CSP consists of 3 components: the variables, the domain for each variable, and the set of constraints. In Sudoku, each cell in the 9x9 grid represents a variable with domain {1, 2,..., 8, 9}. The values for some variables have been specified by the original problem and are fixed. As for the constraints, each pair of cells in the same row, the same column, and the same 3x3 box are involved in a difference constraint -- i.e., they must take on different values. Thus, there are 9*72 row constraints, 9*72 column constraints, and 9*72 box constraints for a total of 1,944 constraints. The csp package contains the code for encoding Sudoku as a CSP. In particular,
AC-3 and BackTracking SearchThe search package is where you will write your AC-3 and BackTracking algorithms. The As your textbook states, the easiest Sudoku puzzles can be solved using AC-3 alone. Harder puzzles, however, require actual search. As such, your
The AC-3 AlgorithmPseudocode for the AC-3 algorithm can be found in your textbook and on the course webpage. There are some simplifications that can be made when applying AC-3 to Sudoku. In particular, when considering the constraint (Xi, Xj)
BackTracking with AC-3Pseudocode for BackTracking can be found in your textbook and on the course webpage. Since BackTracking is nothing more than DFS (with some additions), I highly recommend creating a Your BackTracking search should use the minimum-remaining-values heuristic to choose which variable to assign a value to next. You do not need to use the least-constraining-value heuristic...just go ahead and check each value in the domain from 1 to 9. Finally, you should use AC-3 during the inference step to speed up the search process. Running Your CodeThe main() method is in Sudoku.java. You can pass a puzzle to this class via the command line. This class will read in the puzzle from file, call the SudokuPanel class, which then calls your solve() method to solve the puzzle. The solved puzzle is then graphically displayed on the screen. Note that the bolded values are those specified by the puzzle. (If your solve() method returns null, then the SudokuPanel class will simply display the original, unsolved puzzle). There are various Sudoku puzzles in the "puzzles" directory ranging from easy to evil. Your code should be able to solve all puzzles (even the evil puzzle) in a matter of seconds. If you would like to create your own Sudoku puzzle files, feel free. Each line in the file specifies a row index, column index, and value. Grading and Submission |