Due Tuesday, April 29 at 11:59 PM via Blackboard. PDF format required. Late assignments penalized 10% per day.
Assignment 2
Write a constraint satisfaction problem solver to solve sudoku puzzles
of varying size (see the link for the rules if you're unfamiliar with sudoku puzzles).
The goal is to obtain a performance comparison of various CSP strategies.
The input to the solver is a Sudoku board, and the output should be a complete solution, along with the
number of consistency checks utilized (for this assignment, this is equal to the number of
variable assignments your code makes).
Starter Code
We've supplied basic starter code for representing a board, and reading the puzzles from the input files
you'll use for the assignment.
Python Starter Code
The python starter code includes the following functions:
- To create a SudokuBoard object initialized with values from a file, use
sb = init_board("filename.extension")
- to create a SudokuBoard object initialized with specific values, use the command
sb = SudokuBoard(size, board_array)
- to create a new SudokuBoard which is the same as an existing board, but with one more number played, use the following
sbA = sb.set_value(row,col,value)
where #row and col are 0 indexed
C++ Starter Code
The C++ starter code includes the following key functions:
- To create a Board object of a certain size, use
Board * b = new Board(size);
- to a board with values from a file, use the command
Board * b = Board::fromFile(fileName)
- to change a Board to play a number at a given row and column, use the following
b.set_square_value(row,col,value)
where #row and col are 1 indexed(!).
- to get a square value, use:
int sq = b.get_square_value(row,col)
where where #row and col are 1 indexed.
- to represent a board as a string for output, use:
string boardStr = b.toString()
Input Puzzles
We've supplied specific input puzzles you'll use for this assignment (thanks to Stef
Schoenmackers of decide.com for these). Functions for reading the boards from files are included in the
sample code, but for completeness the files have the following format:
<board_size> - (board size will be 4, 9, 16, or 25)
<num_initial_values> - (The number of set cells in the initial state)
<row1> <col1> <value1> - value of the cell at row1, col1
<row2> <col2> <value2> - value of the cell at row2, col2
<row3> <col3> <value3> - value of the cell at row3, col3
... for a total of num_initial_values lines
Each row, col, and value is in the range [1, board_size].
This Example corresponds to the board:
When testing your code, it will help to try some easy test boards, which you can find
here. You want more boards? For further testing you may use these
boards. Note that for the larger problems, without powerful heuristics your program will not terminate
in a reasonable amount of time -- you should choose an upper bound to the number of consistency checks
you make so that the program terminates in, say, 10 minutes.
What to Turn In
Note: This assignment can be done in groups. Each student must turn in their own, individual homework write-up along with the code.
Turn in the following:
- Code
- (3 points) Your code
- (1 point) Describe who within your team performed which piece of the design/programming
effort.
- Write-up
- (1 point) Briefly explain your code's representation of each of the following, in 1-2 sentences each: the board, variables,
constraints, and the states.
- (1 point) How do you implement forward checking?
- (1 point) Make a table comparing the performance of the heuristics (in terms of number of consistency checks, i.e. variable assignments) for these test
problems: 4x4 9x9 16x16 25x25
The table should have the following format (numbers are random)
Problem | Backtracking | Forward Checking | MRV+MCV | MRV+MCV+LCV |
4x4 | 1234 | 567 | ... | ... |
9x9 | 23456 | 7890 | ... | ... |
16x16 | 789K | 12K | ... | ... |
25x25 | (> 1M) | 234K | ... | ... |
- (2 points) Comment on your results in about 100 words: what trends do you see,
in terms of scalability across the various heuristics? Are these as expected?
- (1/2 point each) Implement the following heuristics. For each one, describe how you implemented it and evaluate it within the table in problem 3 above.
- MRV + most constraining variable.
- MRV + most constraining variable + least constraining value.
If your program does not work, you may still discuss your methodology and what went wrong, and any possible ways to fix this. Considerable credit can still be earned even if the program doesn't work,
provided the writeup is of sufficient quality.