Implement the ac-3 constraint satisfaction algorithm for


QUESTION ONE

Instructions

In this assignment, you will implement inference algorithms for the popular puzzle game Sudoku.

A skeleton file homework4.py containing empty definitions for each question has been provided. Since portions of this assignment will be graded automatically, none of the names or function signatures in this file should be modified. However, you are free to introduce additional variables or functions if needed.

You may import definitions from any standard Python library, and are encouraged to do so in case you find yourself reinventing the wheel. If you are unsure where to start, consider taking a look at the data structures and functions defined in the collections, copy, and itertools modules.

You will find that in addition to a problem specification, most programming questions also include one or two examples from the Python interpreter. In addition to performing your own testing, you are strongly encouraged to verify that your code gives the expected output for these examples before submitting.

It is highly recommended that you follow the Python style guidelines set forth in PEP 8, which was written in part by the creator of Python. However, your code will not be graded for style.

Once you have completed the assignment, you should submit your file on Eniac using the following turnin command, where the flags -c and -p stand for "course" and "project", respectively.

turnin -c cis521 -p hw4 homework4.py

You may submit as many times as you would like before the deadline, but only the last submission will be saved. To view a detailed listing of the contents of your most recent submission, you can use the following command, where the flag -v stands for "verbose".

turnin -c cis521 -p hw4 -v

Sudoku

In the game of Sudoku, you are given a partially-filled 9×99×9 grid, grouped into a 3×33×3 grid of 3×33×3 blocks. The objective is to fill each square with a digit from 1 to 9, subject to the requirement that each row, column, and block must contain each digit exactly once.

In this section, you will implement the AC-3 constraint satisfaction algorithm for Sudoku, along with two extensions that will combine to form a complete and efficient solver.

A number of puzzles have been made available on the course website for testing, including:

- An easy-difficulty puzzle: easy.txt.
- Four medium-difficulty puzzles: medium1.txt, medium2.txt, medium3.txt, and medium4.txt.
- Two hard-difficulty puzzles: hard1.txt and hard2.txt.

1. In this section, we will view a Sudoku puzzle not from the perspective of its grid layout, but more abstractly as a collection of cells. Accordingly, we will represent it internally as a dictionary mapping from cells, i.e. (row, column) pairs, to sets of possible values.

2. You are instructed to write a function sudoku_cells() that returns the list of all cells in a Sudoku puzzle as (row, column) pairs. The line CELLS = sudoku_cells() in the Sudoku class then creates a class-level constant Sudoku.CELLS that can be used wherever the full list of cells is needed. Although the functionsudoku_cells() could still be called each time in its place, that approach results in a large amount of repeated computation and is therefore highly inefficient. The ordering of the cells within the list is not important, as long as they are all present.

3. >>> sudoku_cells()

4. [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), ..., (8, 5), (8, 6), (8, 7), (8, 8)]

5. Write a function sudoku_arcs() that returns the list of all arcs between cells in a Sudoku puzzle corresponding to inequality constraints. In other words, each arc should be a pair of cells whose values cannot be equal in a solved puzzle. The arcs should be represented a two- tuples of cells, where cells themselves are (row, column) pairs. The line ARCS = sudoku_arcs() in the Sudoku class then creates a class-level constant Sudoku.ARCS that can be used wherever the full list of arcs is needed. The ordering of the arcs within the list is not important, as long as they are all present.

6. In the Sudoku class, write a method remove_inconsistent_values(self, cell1, cell2) that removes any value in the set of possibilities for cell1 for which there are no values in the set of possibilities for cell2 satisfying the corresponding inequality constraint. Each cell argument will be a (row, column) pair. If any values were removed, return True; otherwise, return False.

7. In the Sudoku class, write a method infer_ac3(self) that runs the AC-3 algorithm on the current board to narrow down each cell's set of values as much as possible. Although this will not be powerful enough to solve all Sudoku problems, it will produce a solution for easy- difficulty puzzles such as the one shown below. By "solution", we mean that there will be exactly one element in each cell's set of possible values, and that no inequality constraints will be violated.

8. Consider the outcome of running AC-3 on the medium-difficulty puzzle shown below. Although it is able to determine the values of some cells, it is unable to make significant headway on the rest.

9. However, if we consider the possible placements of the digit 7 in the upper-right block, we observe that the 7 in the third row and the 7 in the final column rule out all but one square, meaning we can safely place a 7 in the indicated cell despite AC-3 being unable to make such an inference.

10. In the Sudoku class, write a method infer_improved(self) that runs this improved version of AC-3, usinginfer_ac3(self) as a subroutine (perhaps multiple times).

11. Although the previous inference algorithm is an improvement over the ordinary AC-3 algorithm, it is still not powerful enough to solve all Sudoku puzzles. In the Sudoku class, write a methodinfer_with_guessing(self) that calls infer_improved(self) as a subroutine, picks an arbitrary value for a cell with multiple possibilities if one remains, and repeats. You should implement a backtracking search which reverts erroneous decisions if they result in unsolvable puzzles. For efficiency, the improved inference algorithm should be called once after each guess is made.

Attachment:- assignment.rar

Request for Solution File

Ask an Expert for Answer!!
Computer Engineering: Implement the ac-3 constraint satisfaction algorithm for
Reference No:- TGS02898996

Expected delivery within 24 Hours