Solving the Sudoku With the Differe
Solving the Sudoku With the Differe
INFORMATYKA
Abstract: In this paper, we present the application of the Differential Evolution (DE) al-
gorithm to solving the combinatorial problem. The advantage of the DE algorithm is its
capability of avoiding so-called „local minima” within the considered search space. Thanks
to the special operator of the adaptive mutation, it is possible to direct the searching pro-
cess within the solution space. The DE algorithm applies the selection operator that selects
from the child population only the offspring with the greater value of the fitness function in
comparison to their parents. An algorithm applied to a combinatorial optimization problem:
Sudoku puzzle is presented. Sudoku consists of a nine by nine grid, divided into nine three
by three boxes. Each of the eighty-one squares should be filled in with a number between
one and nine. In this article we show, that the mutation schema has significant impact on the
quality of created solution.
1. Introduction
This paper presents the Differential Evolution algorithm for solving a combinatorial
problem - the Sudoku puzzle. Sudoku is a Japanese logical game which achieved
international popularity in 2005. A classical Sudoku puzzle consists of 9 by 9 matrix
partitioned into nine 3 by 3 sub-matrices. The rules for completing Sudoku are very
simple:
5
Urszula Boryczka, Przemysław Juszczuk
3
4 3 5 7 6
6 9 2 4
8 9 6 2
1 2 4 7
1 6 3
3 2 8 1 5
5
These are then used to fulfill remaining empty cells. Also set of start squares
is determined in such way, that it provides a unique solution to the Sudoku. Rules
of this game are very simple and the objective is clear. But in the elementary 9x9
puzzle version of the game there are about 6.671x1021 valid grids, and only one
appropriate solution for some given cells is satisfied. Moreover the general case of
the problem has been proven to be the NP-Complete [21], which makes it one of the
most interesting problems, that can be solved by approximate algorithms.
When generating these puzzles it is important that the set of start squares ensure
a unique solution, so in general, puzzles are created by calculating a valid solution,
and then removing a subset of the grid values, leaving just enough that the configu-
ration has a unique solution. In general a puzzle require at least 17 start squares to
maintain its uniqueness, although this problem remains formally unsolved [15].
The standard Sudoku puzzle is 9 by 9, but any n by n grid can be made into
Sudoku puzzle. For example rather often found in literature problem consist of 16 by
16 cells. In 16 by 16 puzzle the rows, columns and boxes are filled with the numbers
1 to 16. Such a puzzle will be referred to as size four in reference to the value of n.
The standard Sudoku will be referred to as a size three puzzle. Similarly a 4 by 4
puzzle will be called a size two puzzle.
Main objective of this paper is to test if the Differential Evolution is an efficient
method for solving combinatorial problems - especially the Sudoku puzzle. Proposed
algorithm will be evaluated on different sets of example problems and different diffi-
culty puzzle levels. Also authors will try to evaluate difficulty of puzzles depending
6
Solving the sudoku with the differential evolution
on number of iterations needed to solve the problem and then estimate the puzzle
rating.
The paper is structured as follows: first, authors discuss some related works in
section 2 and give description of the differential evolution algorithm in section 3. In
section 4 authors describe the proposed method - especially an evaluation function
and a mutation schema. Next section provides experimental results and finally we
end with some conclusions.
2. Related Works
Mathematical foundations of sudoku are well discused in [2,3]. For example, one
of aims is to compute the number of valid Sudoku grids. The sudoku puzzle were
deeply studied as a constraint programming problem [16,6]. Other proposed method
is a transformation of the sudoku puzzle to the SAT problem [8,20].
Sudoku is a combinatorial optimization problem [7], It is obviously related to the
ancient magic square problem (Latin square), where different size of squares must be
filled in way that the sum of each column and row are equal to 45. The magic square
problem has been solved by GAs [1].
Evolutionary methods are not popular methods for solving Sudoku puzzle. There
are only a few articles which presents this approach: genetic algoritms were so far
used to generate optimal solutions [10]. In [4] authors focus on using the genetic
algorithm for generating new instances of the Sudoku puzzle. Other population based
methods are ant systems [15] and the Particle Swarm Optimization [12,11,13].
7
Urszula Boryczka, Przemysław Juszczuk
(a) Compute the fitness function for every genotype in the population
{ f (X 1,g ), f (X 2,g ), ..., f (X n,g )}
(b) Create the population of trial genotypes Vg based on Pg
(c) Make crossover of genotypes from the population Pg and Vg to create popu-
lation Ug
(d) Choose the genotypes with the highest fitness function from the population
Ug and Pg for the next population
(e) generation = generation + 1, go to step a.
The DE algorithm begins with the initialization of the population P(0) which
consist of nX individuals. Mutation is a process that adds randomly-generated values
to the selected genes. Each increment moves selected individuals towards the global
optimum. Note that the mutation is used to every individual in the population. Trial
individual ui (t) is created as follows:
Individual xi (t) is called the target vector. (xi2 (t) − xi3 (t)) is a differential vector cre-
ated from the two random individuals xi2 (t) and xi3 (t).
Crossover uses both the genotype from the population Pg and the trial genotype
(population Vg ). This operator will be described deeply in next section. After the
crossover process the offspring is compared with its parent. Next, the better one of
these individuals is added to the new population. The last step of the algorithm is the
increment of the generation counter t. The best individual from the last generation is
the result of the DE algorithm.
Sudoku is a combinatorial problem, where each row, column and also each of nine
3 by 3 small square must have number digit from set {1, 2...9} exactly once. Our
method assumes, that the single individual genotype is the one dimension vector,
where every gene holds value from set {1, 2, . . . , 9}. The array is divided into nine
sub blocks (building blocks) where each block consists of nine digits.
Proposed method of transforming Sudoku square to linear vector implies some
conditions. First we assume that every digit may occur only once in every of 9 geno-
type parts (each part is 9 genes long and corresponds to small Sudoku square).
Two mutation schemas are presented in this paper. First mutation schema applied
for our algorithm is based on standard swap operator used in combinatorial problems.
The mutations are applied only inside a sub block. We are using the mutation strategy
8
Solving the sudoku with the differential evolution
M5,1 M5,2 M5,3 M5,4 M5,5 M5,6 M5,7 M5,8 M5,9 [M11,M12,M13,M14,M15,M16,M17,M18,M19,M21,M22,...,M99]
M6,1 M6,2 M6,3 M6,4 M6,5 M6,6 M6,7 M6,8 M6,9
that is common in the combinatorial optimization - swap mutation (Fig. 3). Of course
some genes in genotype are given from the beginning of the algorithm. This positions
are related to the Sudoku puzzle. As it was said in section 1 each puzzle requires at
least 17 start squares to maintain its uniqueness. Mutation schema allows to swap this
position, but strong penalty function is used while evaluating each individual.
1 6 4 7 2 5 3 9 8
1 6 4 9 2 5 3 7 8
Fig. 3. Swap mutation
Other mutation schema is close to approach used in [14] and it is reffered as the
geometric operator [14,13].
x11 x12 . . . x1p y11 y12 . . . y1p
x21 x22 . . . x2p y21 y22 . . . y2p
di f f vector =
...
−
... ... ... ... ... ... ...
xn1 xn2 . . . xnp yn1 yn2 . . . ynp
9
Urszula Boryczka, Przemysław Juszczuk
The first interpretation of the mutation schema is close to the general one used in
the Differential Evolution algorithm for the continuous optimization. Unfortunately,
as we show in the next section, this approach gives worse effect than the second
option.
The crossover operation is applied so that it exchanges whole sub blocks of nine
numbers between individuals. Thus the crossover point cannot be inside a building
block.
The core of this algorithm is the fitness function. It is required that the each row
xi and column x j must be equal to set A = {1, 2, ..., 9}. So we can calculate number
of missing digits in each row xi and column x j set:
f1 = |A − xi |
f2 = |A − x j |
f it(x) = f1 + f2 + f3 (1)
The fitness function is computed for each offspring from the population Ug , and the
comparison of the computed fitness function with the value of the fitness function
of the parent (from the population Pg ) follows. The genotype with the higher fitness
function value is transferred to the next population. Our proposed method is based on
two simple rules which allows properly evaluate population:
– rule 1 is derived from the set theory. It is required that the each row and column
of Sudoku must be equal to set A = {1, 2, ..., 9}.
– second rule is linked with mutation operator. If mutation affects on cell that is
considered as constant value in puzzle - the fitness function is strongly decreased
(this case explicitly rejects individual as unsatisfactory).
A selection schema used in this algorithm guarantee that individuals promoted to the
next generation are not worse than individuals in actual generation.
{
U i,t when f (U i,t ) ≤ f (X i,t ),
X i,t+1 =
X i,t else.
10
Solving the sudoku with the differential evolution
5. Experimental results
The aim of this research work is to determine if the Differential Evolution algorithm
is capable to solve an example of the difficult combinatorial problem - the Sudoku
puzzle. For all experiments described in this section we assume the following set-
tings:
– size of the population P = 200,
– crossover CR = 0.5,
– swap mutation,
– the length of the gene was 81,
– the maximum number of iterations was set to 40000.
For our experiments we used a group of Sudoku puzzle divided in dependence of
their difficulty. Every puzzle was run 100 times. In the Table 1 we put informa-
tion about the given number of cells at the beginning of the algorithm, minimum,
maximum and average number of iterations needed to achive solution, median and
standard deviation. We also used the Differential Evolution to generate Sudoku puz-
zle from scratch. Afterwards newly generated puzzle is solved by our algorithm and
rated. Unfortunately first mutation schema based on geometric operators provides far
worse solutions and the algorithm is capable to solve within a reasonable time only
easy puzzle (less than 20000 iterations), results shown below concern only the Dif-
ferential Evolution algorithm with the swap mutation. Also, as a comparison, in the
Table 2 we present results for solving Sudoku puzzle with the Genetic Algorithm
[10]. We used the same Sudoku problems, and the same number of individuals in the
population. As it may be seen, it is difficult to clearly rate newly generated puzzle.
Four sets generated from the scrach, had 30 cells filled. It is rather unclear, if the
newly generated puzzles should be assigned to the „easy” or „medium” classes. The
Differential Evolution seems to be far more effective in solving the Sudoku problems.
Most likely, cause of this is the selection schema. In the Differential Evolution only
the best adapted individuals are moved to the next generation. In the Genetic Algo-
rithm we used the proportional selection, which allows worse adapted individuals to
move into the next generation. Premature convergence in the Sudoku is not the prob-
lem, and the more greedy schema is better. Interesting is fact, that both algorithms
seems to be sometimes insensitive to the Sudoku difficulty level. It may be seen in
the GA results, where average number of iterations needed to find solution i similar
for the Medium and Hard problems. We also present distribution of population in
subsequent iterations for selected „easy”, „medium” and „hard” puzzle.
Two interesting population features may be seen at Figures 4, 5 and 6. In all three
cases, at the first step of the algorithm significant improvement of fitness function can
11
Urszula Boryczka, Przemysław Juszczuk
Fitness
40
30
20
10
fitness
40
30
20
10
600 1200 1800 2400 3000 3600 4200 4800 5400 6000 6600 iteration
fitness
40
30
20
10
12
Solving the sudoku with the differential evolution
Table 1. The comparison of how effectively DE finds solutions for the Sudoku puzzles with different
difficulty ratings.
be observed. But further, solutions have been improved very slowly. Second feature
of the algorithm is clearly seen at Fig. 6. Solutions for the puzzle have been found in
about 17 thousands iterations, but from 11th thousand iteration no improvements are
reported. Minimal fitness value of population remains at 2 (0 is optimal solution).
After the analysis of all test sets, the following conclusions may be reached:
– The Differential evolution is capable to solve a very difficult Sudoku puzzles. For
example „hard” instance in 15 thousand iterations.
– The Proper mutation schema is very difficult to implement even with strong the-
oretic analysis of geometric operators.
– The Simple swap mutation brings very good results.
– The Algorithm is far better with finding solutions close to the optimal solution
(for example the fitness function equal 2).
To summarize it should be observed as well that the Differential Evolution and other
evolutionary algorithms are capable not only to find solutions for Sudoku puzzle but
also to generate new puzzles from scratch. The new mutation schema seems to be
very effective tool for solving dicrete problems. Our next goal is to adapt similar mu-
tation into the continuous optimization problem. One of the most difficult things, will
13
Urszula Boryczka, Przemysław Juszczuk
Table 2. The comparison of how effectively GA finds solutions for the Sudoku puzzles with different
difficulty ratings.
be descrption of the mutation schema, which will allow to modify only small part of
the genotype even in the continuous optimization problems. The key issue should be
the proper representation of the individual. We try to show, that appropriate problem
transformation is one of the main problems for the described mutation schema.
References
14
Solving the sudoku with the differential evolution
15
Urszula Boryczka, Przemysław Juszczuk
16