Sudoku: Daa Case Study
Sudoku: Daa Case Study
Sudoku: Daa Case Study
Topic : SUDOKU
Abstract :
In this bachelor thesis three different Sudoku solving algorithms are studied. The study
is primarily concerned with solving ability, but also includes the following: difficulty rating,
puzzle generation ability, and suitability for parallelizing. These aspects are studied for
individual algorithms but are also compared between the different algorithms. The evaluated
algorithms are backtrack, rule-based and Boltzmann machines. Measurements are carried out
by measuring the solving time on a database of 17-clue puzzles, with easier versions used for
the Boltzmann machine. Results are presented as solving time distributions for every algorithm,
but relations between the algorithms are also shown. We conclude that the rule-based algorithm
is by far the most efficient algorithm when it comes to solving Sudoku puzzles. It is also shown
that some correlation in difficulty rating exists between the backtrack and rule-based
algorithms. Parallelization is applicable to all algorithms to a varying extent, with clear
implementations for search-based solutions. Generation is shown to be suitable to implement
using deterministic algorithms such as backtrack and rule-based.
Introduction :
Sudoku is a game that under recent years has gained popularity. Many newspapers
today contain Sudoku puzzles and there are even competitions devoted to Sudoku solving. It is
therefore of interest to study how to solve, generate and rate such puzzles by the help of
computer algorithms. This thesis explores these concepts for three chosen algorithms.
3. Algorithm or code:
Code:
boltzmann(puzzle):
temperature = T_0
i=0
//encode puzzle
nodes <- {0}
for each square in puzzle
nodes[same row as square][square] = -10
nodes[same column as square][square] = -10
nodes[sharing the same node][square] = -20
9
CHAPTER 2. BACKGROUND
//iterate until a valid solution is found
while(checkSudoku(nodes) != VALID)
//update the state of all nodes
for each node in nodes
node.offset = calculateOffset(nodes, node)
probability = 1/(1 + exp(temperature * node.offset))
node.active = rand() < probability
//perform temperature decline
i++
temperature = T_0 * exp(TEMP_DECLINE * i)
return nodes
checkSudoku(nodes):
//begin by building the Sudoku grid
grid = {0}
for each node in nodes:
//check if this node should be used
//for the current square
if(node.offset > nodes[same square])
grid.add(node)
//check constraints on grid
if unique rows &&
unique columns &&
unique subsquares
return true
return false
calculateOffset(nodes, selected):
offset = 0
//iterates over all nodes and calculates the summed weights
//many negative connections implies a large negative offset
for each node in nodes
offset += nodes[node][selected]
return offset;
4. Analysis:
In this section multiple results are presented together with a discussion about how the
results could be interpreted. Section 4.1 is devoted to presenting how different algorithms
perform. Section 4.2 show how the algorithms performs relative to the each other and discusses
different aspect of comparison. Section 4.3 explores the idea of difficulty rating and the concept
of some puzzles being inherently difficult. Section 4.4 compares the algorithms by how well
they are suited for generation and parallelizing. 4.1 Time distributions To get an idea of how
each algorithm performs it is suitable to plot solving times in a histogram. Another way of
displaying the performance is to sort the solving times and plot puzzle index versus solving
time. Both of these are of interest however since they can reveal different things about the
algorithms performance. 4.1.1 Rule-based solver The rule-based solver was by far the fastest
algorithm in the study with a mean solving time of 0.02 seconds. Variation in solving time was
also small with a standard deviation of 0.02 seconds. It solved all 49151 17-clue puzzles that
was in the puzzle database used for testing and none of the puzzles resulted in a unstable
measurement of solving time. Figure 4.1 is a histogram on a logarithmic scale that shows how
the rule-based solver performed over all test puzzles. It is observable that there is a quite small
time interval at which most puzzles are solved. This is probably due to the use of logic rules
with a polynomial time complexity. When the solver instead starts to use guessing the time
complexity is changed to exponential time and it is therefore reasonable to believe that the
solving time will then increase substantially. As will be seen in section 4.1.2 the backtrack
algorithm have a similar behavior which is also taken as a reason to believe that the rule-based
solver starts to use guessing ANALYSIS after the peak. Guessing might of course be used
sparingly at or even before the peak, but the peak is thought to decrease as a result of a more
frequent use of guessing solvers time distribution among all 49151 puzzles. The bars represent
half the time interval compared to figure 4.1. All puzzles was solved and none had an unstable
measurement in running time. The confidence level for the measured solving times was 95 %
at an interval of 0.05 seconds. Another way to visualize the result is shown in figure 4.3. The
figure have plotted the puzzle indices sorted after solving time against their solving times. Note
that the y-axis is a logarithmic scale of the solving time. As in figure 4.2, only a few puzzles
had relatively high solving times. This picture also more clearly illustrates the idea explored
above. Namely that the algorithm will increase its solving times fast at a certain point. That
point is as mentioned thought to be the point where the solver starts to rely more upon guessing
then the logical rules. From that statement, it can be concluded that only a small portion of all
Sudoku puzzles are difficult, in the sense that the logic rules that the rule-based solver uses is
not enough.
Puzzle difficulty :
Both the backtrack solver and the rule-based solver were executed on the same set of puzzles.
One interesting aspect to study is to see if some of those puzzles are difficult for both algorithms
or if they are independent when it comes to whichpuzzles they perform well at. Even if the
rule-based solver uses backtrack searchas a last resort it is not clear if the most difficult puzzles
correlate between thetwo algorithms. The reason for this is because a puzzle can be very hard
for thebacktrack algorithm, but still trivial for the rule-based solver. This has to do withthe
naked tuple rule in the rule-based solver that quickly can reduce the number ofcandidates in
each square.
To test for independence the statistical method described in section 3.4.1 is used. The
measurements shows that about 20% of the worst 10% puzzles are common forboth algorithms.
This means that some puzzles are inherently difficult regardlessof which of the two algorithms
are used. If that would not have been the case only10% of the worst puzzles for one algorithm
shall have been among the 10% worstpuzzles for the other algorithm. The statistical test also
confirms this with a highconfidence level, higher than 99.9%.While there is interest to correlate
results of the Boltzmann machine solverwith others, there are difficulties with doing this.
Considering the large variance inrunning time for individual puzzles there is little room for
statistical significance in the results.
Conclusion :
Three different Sudoku solvers have been studied; backtrack search, rule-basedsolver and
Boltzmann machines. All solvers were tested with statistically significant results being
produced. They have shown to be dissimilar to each other interms of performance and general
behaviorBacktrack search and rule-based solvers are deterministic and form executiontime
distributions that are precise with relatively low variance. Their executiontime was also shown
to have rather low variance when sampling the same puzzlerepeatedly, which is believed to
result from the highly deterministic behavior. Comparing the two algorithms leads to the
conclusion that rule-based performs betteroverall. There were some exceptions at certain
puzzles, but overall solution timeswere significantly lower.
The Boltzmann machine solver was not capable of solving harder puzzles withless clues
within a reasonable time frame. A suitable number of clues was foundto be 46 with a 20 second
execution time limit, resulting in vastly worse generacapabilities than the other solvers. Due to
stochastic behavior, which is a centralpart of the Boltzmann solver, there was a relatively large
variance when samplingthe execution time of a single puzzle. Another important aspect of the
Boltzmann isthe method of temperature descent, in this case selected to be simulated
annealingwith a single descent. This affected the resulting distribution times in a way
thatmakes the probability of puzzles being solved under a certain critical temperaturelimit high.
The critical temperature was found to be about 0.5% of the startingtemperature, with no puzzles
being solved after this interval.