Abstract. Harmony Search (HS) Algorithm Was Applied To Solving Sudoku Puz
Abstract. Harmony Search (HS) Algorithm Was Applied To Solving Sudoku Puz
Abstract. Harmony search (HS) algorithm was applied to solving Sudoku puz-
zle. The HS is an evolutionary algorithm which mimics musicians’ behaviors
such as random play, memory-based play, and pitch-adjusted play when they
perform improvisation. Sudoku puzzles in this study were formulated as an op-
timization problem with number-uniqueness penalties. HS could successfully
solve the optimization problem after 285 function evaluations, taking 9 sec-
onds. Also, sensitivity analysis of HS parameters was performed to obtain a
better idea of algorithm parameter values.
1 Introduction
Sudoku, which is Japanese term meaning “singular number,” has gathered popular-
ity in Japan, the UK, and the USA. The Sudoku puzzle consists of 9 × 9 grid and 3 ×
3 blocks for all the 81 cells. Each puzzle, which has a unique solution, has some
cells that have already been filled in. The objective of the puzzle is to fill in the re-
maining cells with the numbers 1 through 9 so that the following three rules are
satisfied:
• Each horizontal row should contain the numbers 1 - 9, without repeating any.
• Each vertical column should contain the numbers 1 - 9, without repeating any.
• Each 3 × 3 block should contain the numbers 1 - 9, without repeating any.
In recent years researchers have started to apply various methods such as graph
theory [1], artificial intelligence [2], and genetic algorithm [3] to solve the Sudoku
puzzle.
Eppstein [1] used the transformation from a directed or undirected graph to an
unlabeled digraph to solve the puzzle. Although it was successful to the undirected
case, the method is not successful to a directed one because the latter is NP-complete
[4]. Caine and Cohen [2] proposed an artificial intelligent model named MITS
(Mixed Initiative Tutoring System for Sudoku), in which the tutor takes the initiative
to interact when the student lacks knowledge and makes moves that have low utility.
Nicolau and Ryan [3] developed a system named GAuGE (Genetic Algorithm using
Grammatical Evolution) for Sudoku, which uses a position independent representa-
1
tion. Each phenotype variable is encoded as a genotype string along with an associ-
ated phenotype position to learn linear relationships between variables.
Recently, a musicians’ behavior-inspired evolutionary algorithm, harmony search
(HS), has been developed [5] and applied to various optimization problems such as
structural design [6], water network design [7], dam scheduling [8], traffic routing
[9], satellite heat pipe design [10], hydrologic parameter calibration [11], and music
composition [12].
From its success in various applications, HS in this study tackles the board game
Sudoku, which can be formulated as an optimization problem with minimal viola-
tions of the above-mentioned three rules.
The objective of the Sudoku problem is to fill in the cells with the numbers 1 through
9 only once while satisfying the above-mentioned three rules. In other words, the
problem can be formulated as an optimization problem as follows:
9 9 9 9 9
Minimize Z x ij 45 xij 45 x lm 45 (1)
i 1 j 1 j 1 i 1 k 1 (l ,m )Bk
where xij = cell at row i and column j , which has integer value from 1 to 9; and
Bk = set of coordinates for block k .
The first term in Equation 1 represents the penalty function for each horizontal
row; the second term for each vertical column; and the third term for each block. It
should be noted that, although the sum of each row, each column, or each block
equals 45, it does not guarantee that the numbers 1 through 9 are used exactly once.
However, any violation of the uniqueness affects other row, column, or block which
contains the wrong value jointly.
To this penalty-included optimization problem, HS was applied, which originally
came from the behavioral phenomenon of musicians when they together perform
improvisation [5]. HS basically mimics musician’s behaviors such as memory con-
sideration, pitch adjustment, and random consideration, but it also includes problem-
specific features for some applications.
For the first step of the HS algorithm, solution vectors are randomly generated as
many as HMS (harmony memory size), then they are stored in HM (harmony mem-
ory) as follows:
x11
1 1
x12 1
x19
1
x21 x122 x129
Z (x1 ) (2a)
1
x91 x192 x199
2
x11
2 2
x12 2
x19
2 2 2
x 21 x 22 x29 Z (x 2 )
(2b)
2
x91
2 2
x92 x99
...
x11
HMS HMS
x12 HMS
x19
HMS HMS HMS
x21 x22 x29 Z (x HMS )
(2c)
HMS
x91
HMS HMS
x92 x99
where xijn = cell at row i and column j in nth vector stored in HM; and Z (x n ) =
function value for n th vector in HM.
For the next step, a new harmony in Equation 3 is improvised using one of the
following three mechanisms: random selection, memory consideration, and pitch
adjustment.
x11NEW NEW
x12 NEW
x19
NEW NEW NEW
x29
21
x x22
x NEW (3)
NEW
x91
NEW NEW
x92 x99
Random Selection. For xijNEW , random value is chosen out of value range
( 1 xijNEW 9 ) with a probability of (1-HMCR). HMCR (0 ≤ HMCR ≤ 1) stands
for harmony memory considering rate.
Memory Consideration. Instead of the random selection, the value can be chosen
from any values stored in HM with a probability of HMCR.
Pitch Adjustment. Once one pitch is obtained in memory consideration rather than
random selection, the obtained value may further move to neighboring values with a
probability of HMCR × PAR while the original value obtained in memory considera-
3
tion does not move with a probability of HMCR × (1-PAR). PAR (0 ≤ PAR ≤ 1)
stands for pitch adjusting rate. Here, xijNEW in the right hand side is the value origi-
nally obtained in memory consideration; and is the amount of increment (
equals one if xijNEW is not upper limit (9) or lower limit (1). Otherwise, equals
zero).
If the new harmony vector x NEW is better than the worst harmony in the HM in
terms of objective function value, Z (x NEW ) , the new harmony is included in the HM
and the existing worst harmony is excluded from the HM.
If the HS model reaches MaxImp (maximum number of improvisations), compu-
tation is terminated. Otherwise, another new harmony is improvised by considering
one of three mechanisms.
3 Applications
The HS model was applied to the Sudoku puzzle proposed by Nicolau and Ryan [3]
as shown in Figure 1.
4
The HS model found the optimal solution without any violation after 285 function
evaluations using HMS = 50, HMCR = 0.7, and PAR = 0.1. Figure 2 shows the his-
tory of reaching global optimum.
(a) (b)
(c) (d)
Fig. 2. Intermediate and Final Solutions of Test Sudoku Puzzle
While the green-colored cell (light-dark color in black & white) in Figure 2 means
that there is no violation, the magenta-colored cell (dark color in black & white)
indicates that there is at least one violation horizontally, vertically, or block-wise.
Figure 2 (a) is the solution at 13 improvisations, which has a penalty of 21; Figure 2
(b) is the solution at 121 improvisations, which has a penalty of 5; Figure 2 (c) is the
solution at 231 improvisations, which has a penalty of 2; and Figure 2 (d) is the
solution at 285 improvisations, which has a penalty of 0.
This HS model further performed sensitivity analysis of algorithm parameters
(HMS = {1, 2, 10, 50}, HMCR = {0.5, 0.7, 0.9}, PAR = {0.01, 0.1, 0.5}). Table 1
shows the analysis results. When only one vector is considered in the HM (HMS = 1),
like simulated annealing or tabu search algorithm, the HS found global optimum
except in one case (HMCR = 0.9, PAR = 0.1, Z = 6); When two vectors are consid-
ered in the HM (HMS = 2), partially similar to genetic algorithm, the HS also found
global optimum except in two cases (HMCR = 0.7, PAR = 0.01, Z = 15; HMCR =
0.7, PAR = 0.1, Z = 27). However, when more than two vectors were considered in
the HM (HMS = 10 or 50), there was no rule violation for the Sudoku example.
The HS computation was performed on Intel Celeron 1.8GHz CPU. The comput-
ing time ranged 4 - 38 seconds for HMS = 1 in order to arrive at the global optimum;
for HMS = 2, it ranged 3 - 20 seconds; for HMS = 10, it ranged 3 - 8 seconds; and
for HMS = 50, it ranged 7 - 12 seconds.
5
Table 1. Results of Sensitivity Analysis with HS Parameters
6
The HS model developed in this study was further applied to another Sudoku
problem classified as “hard” as shown in Figure 3 [13]. When applied to the problem,
the HS model was entrapped in one of local optima with a penalty of 14 after 1,064
function evaluations as shown in Figure 4.
4 Conclusions
7
unique global solution. The total searching space for this case is 941 = 1.33 × 1039 if
integer programming formulation is considered. The proposed HS model found the
global optimum without any row, column or block violation after 285 function
evaluations, taking 9 seconds on Intel Celeron 1.8 GHz Processor.
When sensitivity analysis of algorithm parameters was performed, the HS could
reach the global optimum 33 times out of 36 runs, taking 3 - 38 seconds (median for
33 successful cases is 8 seconds).
However, it failed to find the global optimum for hard level case with 26 given
values, which has the searching space of 955 = 3.04 × 1052. The HS model was in-
stead entrapped in one of local optima with the penalty of 14 after 1,064 function
evaluations.
For study in the future, the HS model should consider additional problem-specific
heuristics in order to efficiently solve a harder puzzle.
References
1. Eppstein, D.: Nonrepetitive Paths and Cycles in Graphs with Application to Sudoku. ACM
Computing Research Repository. cs.DS/0507053 (2005)
2. Caine, A., Cohen, R.: MITS: A Mixed-Initiative Intelligent Tutoring System for Sudoku.
Lecture Notes in Artificial Intelligence. 4013 (2006) 550-561
3. Nicolau, M., Ryan, C.: Solving Sudoku with the GAuGE System. Lecture Notes in Com-
puter Science. 3905 (2006) 213-224
4. Yato, T., Seta, T.: Complexity and Completeness of Finding Another Solution and its Ap-
plication to Puzzles. IEICE Transactions on Fundamentals of Electronics, Communications
and Computer Sciences. 86 (2003) 1052-1060
5. Geem, Z. W., Kim, J. H., Loganathan, G. V.: A New Heuristic Optimization Algorithm:
Harmony Search. Simulation. 76(2) (2001) 60-68
6. Lee K. S., Geem, Z. W.: A New Structural Optimization Method Based on the Harmony
Search Algorithm. Computers and Structures. 82(9-10) (2004) 781-798
7. Geem, Z. W.: Optimal Cost Design of Water Distribution Networks using Harmony Search.
Engineering Optimization. 38(3) (2006) 259-280
8. Geem, Z. W.: Optimal Scheduling of Multiple Dam System Using Harmony Search Algo-
rithm. Lecture Notes in Computer Science. 4507 (2007) 316-323
9. Geem, Z. W., Lee, K. S., Park, Y.: Application of Harmony Search to Vehicle Routing.
American Journal of Applied Sciences. 2(12) (2005) 1552-1557
10. Geem, Z. W., Hwangbo, H.: Application of Harmony Search to Multi-Objective Optimiza-
tion for Satellite Heat Pipe Design. Proceedings of 2006 US-Korea Conference on Science,
Technology, & Entrepreneurship (UKC 2006). (2006) CD-ROM
11. Kim, J. H., Geem, Z. W., Kim, E. S.: Parameter Estimation of the Nonlinear Muskingum
Model Using Harmony Search. Journal of the American Water Resources Association.
37(5) (2001) 1131-1138
12. Geem, Z. W., Choi, J. –Y.: Music Composition Using Harmony Search Algorithm. Lec-
ture Notes in Computer Science. 4448 (2007) 593-600
13. Web Sudoku. http://www.websudoku.com/ (Jan. 19, 2007)