Problem Set 3: Sokuko Solver

Overview

In this assignment you will use the knowledge you gained in exercise 4 and exercise 5 that dealt with finite sets and use of the MiniSat solver to build a Sodoku solver.

Learning Objectives

Getting Started

You will need to take a few preliminary steps before you get started.

Problem Description

Your goal is to write a solver for a 4-by-4 soduko solver, which is a smaller version of the traditional 9-by-9 problems found in news papers, magazines, and puzzle books. A problem of size 4-by-4 consists of a grid with 4 rows and 4 columns and 4 quadrants. The goal is to fill in the grid such that each square contains one of the digits 1, 2, 3, or 4. The rules require that every row, every column, and every quadrant contain 4 different digits. A picture of such a grid, with some of the squares are filled in looks like this.

../images/EasySoduko.png

It is a good metaphor to address each square with a pair (row number,column number), as follows.

(0,0) (0,1) (0,2) (0,3)
(1,0) (1,1) (1,2) (1,3)
(2,0) (2,1) (2,2) (2,3)
(3,0) (3,1) (3,2) (3,3)

A though experiment

As an exercise in explanation we will use the following FiniteSet description. The actual problem will use a more complicated representation.
grid0 = universe (expand [4,4]) (\ xs -> True)
Which looks like this.
*FiniteSetExamples> grid0
(0,0)=True
(0,1)=True
(0,2)=True
(0,3)=True
(1,0)=True
(1,1)=True
(1,2)=True
(1,3)=True
(2,0)=True
(2,1)=True
(2,2)=True
(2,3)=True
(3,0)=True
(3,1)=True
(3,2)=True
(3,3)=True
Now we can write three functions with type: Int -> Relation -> Relation.
rowSquares  i grid = ...
colSquares  j grid = ...
quadSquares k grid = ...
and which behave as follows:

quadSquares 1 grid0 =
(0,2)=True
(0,3)=True
(1,2)=True
(1,3)=True
quadSquares 0 grid0 =
(0,0)=True
(0,1)=True
(1,0)=True
(1,1)=True
rowSquares 3 grid0 =
(3,0)=True
(3,1)=True
(3,2)=True
(3,3)=True
colSquares 2 grid0 =
(0,2)=True
(1,2)=True
(2,2)=True
(3,2)=True

I.e. each function computes a subset of the squares from the grid. The rowSqures picks out a row, colSquares picks out a column, and quadSquares picks out a quadrant.

Representing the actual board

The actual board is more complicated than the board of the thought experiment above. Each square can take on one of 4 possible values, rather than the present, not-present distinction in the thought experiment board. The key to the soduko solver board representation is to use a 3D relation. If the tuple (row,column,digit) is present then digit is present in the square (row,column). Note that more than one digit can be present in a square at the same time. The solution is to constrain the problem so that only one digit appears in any one square.

What to do

Extra Credit

Try one of the following for ectra credit.

What To Turn In

Back to the class web-page.
Back to the Course Schedule.