CS 531 Programming Assignment 1 - rk-pvt

advertisement
Submitted by:
Radha-Krishna Balla
Ganga Chari Muppidi
CS 531 Programming Assignment 3
SUDOKU Solver
Abstract:
The current paper studies the various implementations of SuDoKu game solver.
SuDoKu is a number placing puzzle based on a 9x9 grid containing several given
numbers. The object is to place numbers in the empty squares so that each row,
each column and each 3x3 box contains the numbers 1 to 9 only once.
Introduction:
A summary of the goals that we set to achieve and the results obtained, have been
discussed in this section.
The goal is to implement the SuDoKu game solver with algorithms using
backtracking, constraint propagation and various heuristics given below.
Heuristics:
1) Most Constrained Variable.
2) Random variable.
3) K-Cells constraint propagation
i) First, MRV, heuristic chooses the most constrained cell as next cells to be assigned
a Value.
ii) Second heuristic: the cells to be assigned the values are chosen randomly, unlike
one in MRV which chooses most restrained variables.
iii) Third heuristic algorithm: If the domains of k cells of a column, row or block are
all the same set of k values, then removes those values from the domains of all
other cells of that column, row or block
And here is how Constraint propagation is done in the implementation,
1. It remove from any cell's domain a value x if it is assigned to some other cell
in the same column, row, or block.
2. It assign to any cell a value x if it is the only value left in its domain.
3. It assign to any cell a value x if x is not in the domain of any other cell in that
row, column, or block.
The SuDoKu problem is a constraint satisfaction problem, with 3 kinds of constraints
(row, column, and block). The variables are cells and the values are numbers 1-9.
The algorithm is based on backtracking search plus constraint propagation. It
maintains all possible candidate values, i.e., the domain, for each cell after each
assignment. We implemented a constraint propagation, which removes candidate
values for a variable when it violates some constraints, given the currently assigned
values for other variables.
The search begins by storing all possible values for each of the empty spots. Then it
does constraint propagation. When the constraint propagation converges, then:


If no candidates left for some cell, then backtrack from the current state.
Else pick the most constrained cell, and assign it a consistent value (keep
other choices of values as options to backtrack to; there is no need to
backtrack over the cell choice since all cells need to be filled).
Pseudo Code:
1. MRV (Most Restrained Variable) Heuristic Algorithm:
This Heuristic chooses the most constrained cell as the next cell that is to be
assigned a value from one of its domain values.
game fillByMRV( game)
If(game complete) return game;
// returns the game if it is complete and consistent
cellArray = getMRV();
// Array, if more than one cell with same min domain
length
If(cellArray’s domainlength =0) return null;
// a back tracking condition
// Make assignments if the cell domains have only value
If or while(cellArray’s domainLength = 1)
For(all cells)
assignValueToCorrespondingCell();
reCalculateAllDomains()
// update the all row,col,block domains the cells w.r.t. current
assingment
cellArray = getMRV();
// if the cell domains have more than one values
For(each cellArray)
For(each each value of domain)
gameCellValue = cellArrayValue;
reCalculateAllDomains();
// update the all row,col,block domains the cells w.r.t. current
assingment
referenceToSolGame = fillByMRV(game.clone);
// clone, to store the state of game
If(referenceToSolutionGame != null)
Return referenceToSolGame;
Return referenceToSolGame;
// null if no solution
2. Random Heuristic Algorithm:
In the algorithm with this heuristic, the cells to be assigned the values are chosen
randomly from unassigned cells, unlike MRV which chooses most restrained
variables. Rest of the logic would be same.
game fillByRandom( game)
If(game complete) return game;
// returns the game if it is complete and consistent
cell = getRandomCell();
// Array, if more than one cell with same min domain
length
If(cellArray’s domainlength =0) return null;
// a back tracking condition
// Make assignments if the cell domains have only value
If or while(cell domainLength = 1)
assignValueToCorrespondingCell();
reCalculateAllDomains()
// update the all row,col,block domains the cells w.r.t. current
assingment
cellArray = getRanndomCell();
// if the cell domains have more than one values
For(each value in domain)
gameCellValue = cellArrayValue;
reCalculateAllDomains()
// update the all row,col,block domains the cells w.r.t. current
assingment
referenceToSolGame = fillByRandom(game.clone);
// clone, to store the state of game
If(referenceToSolutionGame != null)
Return referenceToSolGame;
Return referenceToSolGame;
// null if no solution
3. K-Cells Heuristic Algorithm:
In this heuristic algorithm, the K-Cell rule is implemented in reCalculateAllDomains()
method. When reCalculateAllDomains() is called it first update the all domains with
current assignments. Then look for the common values and same length
domains(first k=2 then k=3). If found any of such cells, the method removes these
common values from other row, cell, block cell’s domains. This constrained
propagation step heuristic can be used with both the heuristics above.
As its definition, If the domains of k cells of a column, row or block are all the same
set of k values, then it removes those values from the domains of all other cells of
that column, row or block
Experimental Setup:
The program has been written in C# (programming language), to solve the given
SuDoKu problem.
The program has been tested with 77 different input sets.
The inputs have been classified into 4 types:
 Easy – 23 input puzzles
 Medium – 21 input puzzles
 Hard – 18 input puzzles
 Evil – 15 input puzzles
Results:
The results have been presented by taking the average of the number of backtracks
over the various inputs under each category.
Easy
MRV Heuristic
MRV with k-cells
Heuristic
No heuristic - Random
Avg. Filled-in
Numbers
Avg. Backtracks
34.69
1.3
Avg. Backtracks
0.87
Avg. Backtracks
1683.78
Medium
Hard
Evil
29.04
92.43
26.27
174.39
26.13
167.07
225.24
58212.3
3
935.89
76736.9
4
214.4
108150.2
7
1000
Avg. # of Backtracks
900
800
700
600
MRV
500
k-cells
400
300
200
100
0
34.69
29.04
26.27
Avg. Filled-in Numbers
26.13
Random
120000
Avg. # of Backtracks
100000
80000
60000
Random
40000
20000
0
34.69
29.04
26.27
26.13
Avg. Filled-in Numbers
Discussion of Results:
1) The primitive algorithm to solve the SuDoKu is by brute force. This algorithm
makes assignments and backtrackings to maximum extent. This is because it
does not expect or calculate how the current decision will affect the other
variables. Thus, to reduce the number of backtracks; we pass or propagate
some constraint information to next states, so it avoids the unnecessary
assignments or decisions.
2) The MRV (Most Restrained Variable) heuristic chooses the most constrained
variable to be assigned next. Before making any assignments, it checks if
there are any single domain cells. If so it makes all those assignments. Next it
makes the other assignments based on the MRV heuristic. After making an
assignment it recalculates the domains of all the row, column and block cells
w.r.t to the current assignment and then looks for the next assignment to be
made. This happens recursively. If it finds a solution at some time, it makes
successful backtracks to the first call made with the reference to the solution
object. The number of backtracks taken for each category of inputs have been
presented in the previous section. Even for hard and evil input types, the
program was talking only up to a maximum of 200 backtracks and solves
within a few seconds.
3) The Random method chooses the next cell to be assigned a value, randomly.
As expected, it is observed that this method uses much more backtrackings
than MRV. In our implementation it went up to a maximum of up to 341,669.
Where as MRV and K-cells took count below 1000.
4) The K-cells constraint propagation heuristic supposedly refines the game
solving program better by using an additional set of constraints at each step.
It has to produce lesser number of backtrackings than just the MRV heuristic
program. But with our program, it has been observed that the number of
backtrackings attained with this heuristic were marginally greater than those
obtained by the MRV heuristic. This might have been due to a defect in the
code of the implementation of this heuristic. This shall be rectified in the
future work.
5) The MRV algorithm with single domain assignments is able to solve some of
the easy and medium problems with zero backtrackings (i.e. when single
assignments are put in a loop, assigning and recalculating domain and
repeating the process if there exist some other cells with single domain
again).
6) All the problems (easy, medium, hard and evil) in the given file have been
solved by our program and within a maximum of 10 seconds for the hardest
problem. All the results (number of backtracks performed for each input)
have been printed to a text file.
7) Also we were able to observe from the experiment that not each and every
evil problem necessarily make more backtracks than a hard or medium
problem. In some cases, even a medium problem takes a lot of backtrackings
especially when the random heuristic is used. And at the same time, an evil
might take less backtrackings because of the fact that we are leaving most of
the values unassigned and hence the problem is more open and can have
multiple solutions. Hence sometimes it might converge faster than a problem
classified as hard.
Download