Artificial Intelligence Spring Homework 4: CSPs Solution





As usual, the standard honor code and academic honesty policy applies. We run ​automated checks for plagiarismto ensure only original work is given credit. Submissions isomorphic to (1) those that exist anywhere online, (2) those submitted by classmates, or (3) those submitted by students in prior semesters, will be considered plagiarism.


You will submit one zip file, named ​, which contains files:

  • pdf​,
  • py​or ​ ​[Both are included in the starter code. Update the one you wish to use and ​delete the otherbefore submitting]
  • txt ​or​.pdf

You can submit as many times as you like before the deadline. Only your most recent submission will be graded. The written submission can be handwritten and scanned (PDF scanning mobile apps typically work well), or typed, but it must be legible in order to receive credit.


 Consider the Sudoku puzzle below. Each variable is named by its row and its column (see Programming “Introduction” for an example). Recall each variable must be assigned a value from 1 to 9 subject to the constraint that no two cells in the same row, column, or box may contain the same value.

  1. List the names of the variables in the blue circle and their corresponding initial value domains.
  2. Reduce the domain for the four unassigned variables in question 1 by enforcing arc constraints using the entire puzzle board. List the new domains.
  3. Assume we have to choose one of the four unassigned variables in question 2 to explore further. Using the minimum remaining value heuristic, which variable or variables should we explore next?
  4. Assume we choose A4 to explore next and assume that A6, B5, C5 are the last three remaining variables waiting to be assigned. Using least constraining value rule, which value of A4 should be tried first?



II.What To Submit

III.Backtracking Algorithm

IV.Important Information

V.Before You Submit

  1. Introduction

The objective of Sudoku is to ll a 9×9 grid with the numbers 1-9 so that each column, row, and 3×3 sub-grid (or box) contains one of each digit. You may try out the game here: ​ Sudoku has 81 ​variables, i.e. 81 tiles. The variables are named by ​rowand ​column, and are ​valuedfrom 1 to 9 subject to the constraints that no two cells in the same row, column, or box may be the same.

Frame your problem in terms of ​variables, ​domains, and ​constraints. We suggest representing a Sudoku board with a Python dictionary, where each key is a variable name based on location, and value of the tile placed there. Using variable names ​Al… ​A9… I1… I9, the board above has:

  • sudoku_dict[“​B1“] = ​9, and
  • sudoku_dict[“​E9“] = ​8.

We give value ​zeroto a tile that has not yet been filled.

  1. What To Submit

Your program will be executed as follows:

$ python <input_string>

In the starter zip, ​sudokus_start.txt​, contains hundreds of sample unsolved Sudoku boards, and

sudokus_finish.txt​the corresponding solutions. Each board is represented as a single line of text, starting from the top-left corner of the board, and listed left-to-right, top-to-bottom.

The first board in ​sudokus_start.txt​is represented as the string:


Which is equivalent to:

0 0 3 0 2 0 6 0 0

9 0 0 3 0 5 0 0 1

0 0 1 8 0 6 4 0 0

0 0 8 1 0 2 9 0 0

7 0 0 0 0 0 0 0 8

0 0 6 7 0 8 2 0 0

0 0 2 6 0 9 5 0 0

8 0 0 2 0 3 0 0 9

0 0 5 0 1 0 3 0 0

Your program will generate ​output.txt​, containing a single line of text representing the finished Sudoku board. E.g.:


Test your program using ​sudokus_finish.txt​, which contains the solved versions of all of the same puzzles.

Besides your driver (and any other python code dependency), submit a README.txtwith your results and observations, including the:

  • number AND line numbers of boards you could solve from txt,
  • running time, and
  • any other relevant information.
  • Backtracking Algorithm

Implement ​backtrackingsearch using the ​minimum remaining value​heuristic. Pick your own order of values to try for each variable, and apply ​forward checking​to reduce variables domains.

  • Test your program on ​txt​.
  • Report the puzzles you can solve now, running time, observations.
  1. Important Information
  1. Test-Run Your Code

 Test, test, test. Make sure you produce an output file with the ​exact format of the example given.

  1. Grading Submissions

We test your final program on ​20 boards. Each board is worth ​5 pointsif solved, and zero otherwise.

These boards are similar to those in your starter zip, so if you solve all those, you’ll get full credit.

  1. Time Limit

No brute-force! Your program should solve puzzles in ​well under a minuteper board. Programs with much longer running times will be killed.

  1. Just for fun

Try your code on the world’s hardest Sudokus! There’s nothing to submit here, just for fun. For example:





error: Content is protected !!