Вы находитесь на странице: 1из 4

3rd International Conference on Engineering and ICT (ICEI2012) Melaka, Malaysia 4 6 April 2012

SOLVING KNIGHTS TOUR PROBLEM USING FIREFLY ALGORITHM


Mohd Muzafar Ismail1,a, Amar Faiz Zainal Abidin2,3,b, Sigit Widiyanto4,5,c, M. H Misran1,d, Maizatul Alice1,e, Nur Anis Nordin3,f, Ezreen Farina Shair1,2,g, Seri Mastura Mustaza2,6,h, M. N. Shah Zainudin1,i
Faculty of Electronic and Computer Engineering, Universiti Teknikal Malaysia Melaka, 76100 Durian Tunggal, Hang Tuah Jaya, Melaka, Malaysia. a muzafar@utem.edu.my,dharris@utem.edu.my, emaizatul@utem.edu.my, gezreen1987@yahoo.com, i noorazlan@utem.edu.my Faculty of Electrical Engineering, Faculty of Computer Science and Information System, Universiti Teknologi Malaysia, 81310 UTM Skudai, Johor, Malaysia. b amarfaiz@fke.utm.my, fnuranisnordin@gmail.com
3 2 1

Laboratoire Electronique, Informatique et Image, Universit de Bourgogne, Le2i, 12 rue de la fonderie, 71200 Le Creusot, France. Informatics Department, Gunadarma University, Pondok Cina, Depok 16424, West Java, Indonesia. c sigit.first@gmail.com Faculty of Engineering and Built Environment, Universiti Kebangsaan Malaysia, 43600 UKM Bangi, Selangor, Malaysia. h seri.mastura@gmail.com
AbstractThe knights tour problem is an ancient
6 5

puzzle where the objective of the puzzle is to construct a series of legal moves made by a knight so that it visits every square of a chessboard exactly once. This paper proposes a model using Firefly Algorithm to solve the problem. Each firefly represents a possible solution of the problem. Each dimensions of the search space represents nth move taken by the knight. The numerical value for each space can be in the range of 0 to 7 according to the 8 possible moves that can be taken by the knight. The fitness of a firefly is then calculated based on the number of legal moves it has taken. The proposed model has been tested using 8x8 chessboard setup. Result obtained shows that the proposed model has a potential to be applied to solve the general knights tour problem. Keywordscomputational intelligence; firefly algorithm; knight tour problem; optimization. I. INTRODUCTION

The knights tour problem (KTP) is a chess puzzle where the objective of the puzzle is to find the sequence of legal moves made by a knight in order for it visits every square of a chessboard exactly once. From the mathematical perspective, KTP is a combinatorial problem, which shared common features like other combinatorial problems such as Traveling Salesman Problem (TSP). Many researches related to KTP had been done. S.-S. Lin and C.-L. Wei proposed the divide-and-conquer

algorithm to solve KTP. The proposed approach is scalable for any sizes of chess board [1]. In the paper written by L. Paris, the author proposed three heuristic functions that aim to reduce the search time for the knight tour [2]. All of the three heuristic algorithms are based on greedy algorithm. In 2004, V. S. Gordon and T. J. Slocum study the effectiveness of genetic algorithm in solving KTP [3]. He compared the result obtained with the standard depth-first search with backtracking. N. J. Gotelli and G. L. Entsminger study the difference between using the swap-and-fill algorithm and randomly assigned moves in KTP [4]. Result obtained shows that there is not much difference between these two algorithms. The authors had suggested a need to study any proposed algorithm efficiency by examining the statistical properties of the null model of the proposed algorithm. Numerous literatures on the use of Ant Colony Optimization (ACO) in solving the knights tour problem have been conducted due to the nature of ACO that is suited for discrete problem. In year 2004, P. Hingston and G. Kendall proposed the use of ACO [5,6]. D. Jiang, S. Bai and W. Dong study the use of ACO for KTP with holes [7]. Other than the mentioned literatures above, there are also a large number of researches on KTP based on neural network (NN) algorithm. Y. Takefuji and K. C. Lee proposed NN computing to solve KTP in 1992 [8]. I. Paraberry studies the scalability of a NN for the KTP [9]. The author studies the effectiveness of proposed NN in [8] with standard algorithmic techniques. The experiment

3rd International Conference on Engineering and ICT (ICEI2012) Melaka, Malaysia 4 6 April 2012

indicates that NN is not suitable to be implemented on a normal computer. A more extensive study of the effectiveness of different variation of NN in solving KTP is done by R. G. Escalante and H. A. Malki [10]. The authors also emphasize the practical limitation of running the application due to extremely large neural network. This paper proposed a model based on Firefly Algorithm (FA). The main gist of this paper is modeling of KTP using FA. The remaining of the paper is organized as follows: Section II will describe the proposed model using FA and the explanation of fitness formulation of the model. Example will be provided along with the description for a better understanding of the formulation. The implementation and experimental result are described in details in Section III. Section IV concludes the paper. II. FIREFLY ALGORITHM IN KNIGHTS TOUR PROBLEM

! = [1 , 2 , , ] !

(1)

where is the total number of squares in the chess boards. In case of 8x8 chess board, = 8 8 = 64, so the firefly position as Eq. (2) ! = [1 , 2 , , 64 ] ! (2) The knight has 8 possible moves from a square it currently reside. These possible moves have the same pattern so in this paper they are referred as move patterns. An integer value of 0 to 7 can be assigned to these move patterns as illustrated in Fig 1.

X. S. Yang introduced FA in year 2007. This algorithm is fundamentally based on the mating behavior of fireflies [11,12]. During mating season, female firefly will be attracted to male firefly with higher intensity. To simplify this into optimization algorithm, the author assumed that the firefly is unisex. Thus, any firefly with lower intensity of flash light will be attracted to another firefly with greater intensity of flashlight. Similar to the other optimization algorithm, the author modeled the flashlight as the fitness of the algorithm and firefly position in the search space as the representation of the candidate solution. Algorithm below displays the adaptation of the original FA proposed by the original author [13]. Algorithm 1: FA for KTP
Set fitness function, (! ) according to (2) where ! = [!! , !! , . . , !" ]! Generate randomly initial population of firefly, ! where = 1,2, . . , Find firefly light intensity, ! at ! using (4) Define light absorption coefficient, while < for = 1 to for = 1 to if ! < ! Move firefly towards using (5) Evaluate new solution using (3), update ! using (6) and global best if necessary Perform correction if necessary end if end for end for end while Post process results and visualization

Figure1. Possible moves patterns for Knight.

In Cartesian coordinates, these 8 move pattern can be describes as Table 1. Note that bottom left of the chess board is assumed as (1,1) and top right of the chess board is (8,8).
Table 1.List of the Knights move patterns.

Move Pattern 0 1 2 3 4 5 6 7

Change in Coordinate X +1 +2 +2 +1 -1 -2 -2 -1

Change in Coordinate Y +2 +1 -1 -2 -2 -1 +1 +2

In modeling the tour made by a knight in KTP, the firefly position, ! in FA represents a candidate solution of the problem. Each dimension of the firefly position represents nth move made by the knight as Eq. (1)

For example, ! = [0,3,1, ]! means that the the first 3 moves of the knight will be from its initial position then follows pattern #0, #3 and #1. Assuming the initial position is at (2,4) This can be illustrated as Fig. 2. Another essential in modeling a problem using FA is to define the fitness function. This is better known as fitness formulation. For the proposed model, the fitness formulation can be calculated as Eq. (3) (! ) =
! ! !! !

where if ! = 0 then =

(3)

3rd International Conference on Engineering and ICT (ICEI2012) Melaka, Malaysia 4 6 April 2012

chess board, out of boundary condition can occur if one or more dimension of the firefly position is greater than 7 or less than 0. For this study, the remainder of mod 8 is taken as the new value, e.g. 9 become 1 and -1 become 1. This correction model assumes that the search space is a spherical space instead of the normal square space. The fitness of the new firefly position is calculated and the light intensity is updated. If the fitness obtained is smaller than the global best record, the new fitness will become the new global best and the firefly position is kept as the best solution found so far. From here on the algorithm will continue looping until stopping criteria is met. For this study, maximum iteration, is chosen as stopping criteria in which the algorithm will stop when the iteration, reached maximum iteration, . III.
Figure 2. Example of Knight movement for ! = [0,3,1, ]! .

IMPLEMENTATION AND EXPERIMENTAL RESULT

where ! is the variable that associates with legality of the move made by the knight for ith move. ! = 1means the move is valid for ith move, otherwise ! = 0. In layman term, the fitness value of a firefly is the total number of legal move made by the knight before it made an illegal move. By taking Fig. 2 as an example, ! = [0,3,1,1,1,1,3,4, ]! has a fitness value of 4 because the 5th move is illegal. While ! = [0,3,1,0,3,1,2,4, ]! has a fitness value of 5. KTP is a maximizing problem where the objective is to cover as many square as possible. The optimal fitness value should be total square on the chess board, for 8x8 chess board is 64. The algorithm starts by randomly positioned the fireflies in the search space. The positions of fireflies are evaluated using the fitness function in Eq. (3). Light intensity, ! is equates to be equal to the inverse value of the agents fitness function as shown in Eq. (4). ! =
! ! (! )

For the implementation, 16 locations have been chosen as the initial location of the knight to start its tour. This is shown in Fig. 3. Only these 16 locations are chosen because other locations are just mirror of these locations.

(4)

For every iteration, each firefly will move toward the firefly with a greater light intensity. The movement of this firefly is dictates by Eq. (5). ! = ! + !
! !! !!"

Figure 3. Initial positions of the Knight for experiment.

! ! + !

(5)

where is the Euclidean distance between two fireflies in search space. For firefly and firefly , the Euclidean distance is calculated using Eq. (6). !" = ! ! (6) ! is the firefly attractiveness at = 0, is absorption coefficient and is randomization parameter which in range [0,1]. ! is a vector random number taken from uniform distribution of [0,1]. Throughout the process of updating the position, the value obtained might be a floating value. For this study, the floating value is round up to its smaller integer using floor command, e.g., 2.542 to 2. Another problem that might occur is the out of boundary condition. For the 8x8

The computation is done using a laptop equipped with Intel Centrino Duo, 1.8GHz with 2GB RAM.. For the case study, FA parameters used are as listed in Table 2.
Table 2.Parameters of FA for TSP.

Parameters Number of agents, Number of iterations, Number of simulations Firefly attractiveness at = 0, ! Absorption coefficient, Randomization parameter,

Value 500 5000 20 1 1 [0,1]

The parameters chosen are not optimal. Number of simulations per location is 20 simulations. Average time

3rd International Conference on Engineering and ICT (ICEI2012) Melaka, Malaysia 4 6 April 2012

taken per computation is 2 hours. The result obtained from the simulations is listed in Table 3.
Table 3. Result obtained for KTP using FA.

[3]

[4]

Locat ion

Opti mal value of best foun d 64 64 64 64 64 64 63 63 64 64 64 63 64 64 63 63

Aver age fitnes s of best foun d 62.8 62.5 625 62.8 62.5 62.6 62.7 62.7 62.5 62.5 62.7 62.6 62.5 62.7 62.3 62.1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Stand ard deviat ion of fitnes s of best found 0.696 0.607 0.689 0.696 0.605 0.598 0.587 0.470 0.760 0.826 0.801 0.510 0.688 0.587 0.444 0.394

Least iteration before global converg ence 1997.6 1968.9 1752.8 1706.8 1474.8 1373.6 1391.9 2349.7 1708.4 1833.3 1916.6 1976.3 2074.9 1241.9 1709.7 2073.9

Average iteration before global converg ence 1429.09 1216.45 1585.42 1329.73 1149.23 1094.39 895.02 1510.80 1327.85 1493.29 1515.44 1228.30 1362.12 732.52 1581.78 1318.19

[5]

[6]

[7]

[8]

[9] [10]

[11]

[12]

[13]

S Gordon, T. J. Slocum, The knight's tour evolutionary vs. depth-first search, Proceedings of the 2004 Congress on Evolutionary Computation, pp. 60-64, 2004. N. J. Gotelli, G. L. Entsminger, (2001) Swap and fill algorithms in null model analysis: Rethinking the Knight's Tour, Oecologia, vol. 129, no. 2, 281-291, 2001. P. A. Hingston, G. Kendall, Ant colonies discover Knight's Tours, 17th Australian Joint Conference on Artificial Intelligence, pp. 60-64, 2004. P. A. Hingston, G. Kendall, Enumerating knight's tours using an ant colony algorithm, IEEE Congress on Evolutionary Computation, pp. 60-64, 2005. D. Jiang, S. Bai, W. Dong, An ant colony optimization algorithm for knight's tour problem on the chessboard with holes, 1st International Workshop on Education Technology and Computer Science, pp. 60-64, 2009. Y. Takefuji, K. C. Lee, Neural network computing for knight's tour problems, Neurocomputing, vol. 4, no. 5, pp. 249-254, 1992. Parberry, Scalability of a neural network for the knight's tour problem, Neurocomputing, vol. 12, no. 1, pp. 19-33, 1996. R. G. Escalante, H. A. Malki, Comparison of artificial neural network architectures and training algorithms for solving the knight's tours, International Joint Conference on Neural Network, pp. 60-64, 2006. S. X. Yang, Firefly Algorithm, Stochastic Test Functions and Design Optimisation, International Journal of Bio-Inspired Computation, vol. 2, no. 2, pp.7884, 2010. S. X. Yang, Firefly algorithms for multimodal optimization, Stochastic Algorithms: Foundations and Applications, Lecture Notes in Computer Sciences, vol. 5792, pp. 169-178, 2009. S. X. Yang, Firefly Algorithm, Engineering Optimization. Hoboken, New Jersey: Wiley, 2010, pp. 221-230.

From Table 3, the success rate of finding optimal solution for all 16 locations is 75%. This success rate is impressive as the parameters chosen are not optimal. Parameters tuning can be done using meta optimization technique. Further study on the sensitivity of parameters to KTP can be done to study. Small standard deviation in average fitness suggests that the proposed model does not too sensitive to number of computation done. Although maximum iteration is set to 5000, the algorithm on average required only 2000 iterations to find the best solution. IV. CONCLUSION

This paper presented the application of FA in solving KTP. Result obtained from the case study shows that the proposed approach has a potential solution for further improvement in solving KTP. This study can be extended by studying the sensitivity of FA parameters. Further study on KTP can also be done using other optimization algorithms such as Paddy Field Algorithm and Cuckoo Search. V.
[1]

REFERENCES

[2]

S.-S. Lin, C.-L. Wei, Optimal algorithms for constructing knight's tours on arbitrary nm chessboards, Journal of Discrete Applied Mathematics, vol. 146, no. 3, pp. pp. 219-232, 2005. L. Paris, Heuristic strategies for the knight tour problem, Proceedings of the International Conference on Artificial Intelligence, pp. 60-64, 2004.