• Tidak ada hasil yang ditemukan

Isomorphic Solutions of the N-queens Problem

N/A
N/A
Protected

Academic year: 2017

Membagikan "Isomorphic Solutions of the N-queens Problem"

Copied!
9
0
0

Teks penuh

(1)
(2)
(3)
(4)

Isomorphic Solutions of the N-queens Problem

Isomorphic Solutions of the

N

-queens Problem

Sawaluddin

1

, Opim Salim Sitompul

2

, Erna Budhiarti Nababan

3

1 University of Sumatera Utara, Dept. of Mathematics, Medan 20155, Indonesia

sawal@usu.ac.id

2

University of Sumatera Utara, Dept. of Mathematics, Medan 20155, Indonesia opim@usu.ac.id

3 University of Sumatera Utara, Dept. of Information Technology, Medan 20155, Indonesia

ernabrn@usu.ac.id

Abstract. The N-queens problem is a classic example in mathematics as well as computer science that receives many attentions from researches for nearly two centuries. Despite its usefulness in teaching computational intelligence algorithms, another interesting features of the N-queens problem is the notion of isomorphism and transformation groups. Once a solution of the N-queens problem is found, the isomorphic solutions can be transformed easily by performing two kinds of permutations, i.e. rotation and reflection. In this paper, we proposed a two-stage approach to find the isomorphic solutions to the N-queens problem. In the first stage, a genetic algorithm is used to find a solution of the N-queens problem. In the second stage, the solution is transformed into seven isomorphic solutions. Using this approach, a complete solution for

Keyword: object-oriented approach, genetic algorithm, N-queens problem, isomorphism.

1. Introduction

The N-queens problem is a classic example in mathematics as well as computer science that receives many attentions from many researches for nearly two centuries. For the last two decades and more recent years, the N-queens problem has been solved using some heuristic algorithms, especially genetic algorithm (see for examples [1-7]). Despite its usefulness in teaching computational intelligence algorithms, another interesting features of the

N-As an example, Fig. 1 illustrates a solution of an 8-queens problem. As can be seen from the figure, each queen is located in different column and no queens are located on the same rows or on the same diagonals.

Fig. 1. Example of solution for the 8-queens

One of the many interesting features of the N-queens problem is the notion of isomorphism of its solutions. Among many of the solutions, we can identify that several of them are isomorphic form of the others. The isomorphic solutions can be obtained from a current solution by means of permutation. There are seven different permutations that can be performed to obtain complete isomorphic solutions, three of them are clock-wise rotations of 90o, 180o,

and 270o. The other four permutations are reflections

of the horizontal middle axes, vertical middle axes, left diagonal axes, and right diagonal axes (see Fig. 2).

(5)

Isomorphic Solutions of the N-queens Problem

Fig. 2 Permutations of the original position

In this paper, we proposed a two-stage approach to find the isomorphic solutions of the N-queens problem. In the first stage, a genetic algorithm is used to find a solution and in the second stage the seven permutations are performed to the initial solution in order to get the isomorphic solutions.

The structure of this paper is organized as follows: in section 2 we briefly describe the characteristics of the group of permutation in which the N-queens problem is formulated. In section 3, we describe the class data structure used by the genetic algorithm to solve the problem. In section 4 we provides an overview of the genetic algorithm approach used in this paper and discuss the properties of the three main operations in the genetic algorithm, i.e. selection, reproduction, and mutation. Section 5 presents some results and discusses the output obtained for several cases of N, and finally in section 6 we will summarize the work and present the conclusion.

2. The N-queens problem of Group of

Permutations

In algebra, the solutions of the N-queens problem are one example of a group of permutations that can be represented as a group of symmetries of geometric figures (symmetries of the squares) [10]. The set of transformations for N-queens is a dihedral group Δ4, i.e. the group of symmetries of

the squares of 4 sides. Elements of the group can be obtained by the operations of rotation R through 360o/4, and reflection M about some side [9, 10].

As a group of permutation, we can find among the solutions of the problem that the permutation operations are closed, has an identity element, has an inverse, and associative. The closed properties can beshown from the fact that a permutation of a set C of the solutions of the N-queens problem is a

bijective function from a cartesian product of

C

=

C ×C

. Thus, for a solution C0 = {(3,1),

(6,2), (8,3), (1,4), (4,5), (7,6), (5,7), (2,8)} of the 8-queens, the permutation yields:

C1 = {(5,1),(1,2),(8,3),(4,4),(2,5),(7,6),(3,7),(6,8)}

(rotation 90o clockwise)

C2 = {(3,1),(6,2),(2,3),(5,4),(8,5),(1,6),(3,7),(6,8)}

(rotation 180o clockwise)

C3 = {(3,1),(6,2),(2,3),(7,4),(5,5),(1,6),(8,7),(4,8)}

(rotation 270o clockwise)

C4 = {(2,1),(5,2),(7,3),(4,4),(1,5),(8,6),(6,7),(3,8)}

(reflection on vertical axis)

C5 = {(6,1),(3,2),(1,3),(8,4),(5,5),(2,6),(4,7),(7,8)}

(reflection on horizontal axis)

C6 = {(4,1),(8,2),(1,3),(5,4),(7,5),(2,6),(6,7),(3,8)}

(reflection on left diagonal)

C7 = {(6,1),(3,2),(7,3),(2,4),(4,5),(8,6),(1,7),(5,8)}

(reflection on right diagonal)

The identity element is a special transformation, namely a rotation of 0o

that doesn’t do anything. The inverse element of the solutions is an act to undo the transformation. In this case, the inverse can be formed by taking the inverses of the transformation in reverse order for any sequence of rotation and mirror images. The associativity of the transformation can be shown by composition operation, such that the following equation holds: (C1 ○ C2) ○ C3 = C1 ○ (C2 ○ C3) [11].

3. The N-queens Class Data Structure

The class structure for the N-queens problem is depicted in Fig. 3. The class consists of two members, i.e. the variable and function members.

(6)

Isomorphic Solutions of the N-queens Problem

Fig. 3. The N-queens class structure

The member variable maintain a private population list, which points to the head of an orthogonal list used to store the population and the individual data. The member function consists of the operations necessary for the genetic algorithm processing, namely creating an initial population, calculating non attacking queen, calculating fitness of each individual, and the three main operations of genetic algorithm: random selection, reproduction, and mutation.

The private variable member of the N-queens class points to a structure consisting of two lists of nodes representing a list of population which points to a list of individuals. The two lists form an orthogonal list as shown in Fig. 4, where the horizontallists are representing the individuals and the vertical lists are representing the population. Each node of the individual lists maintains the column and row position information of the queen, whereas each node of the population list maintains

the individual as well as the number of non attacking queens and their fitness.

Fig. 4. Orthogonal list of the N-queens data structure

Each queen is placed into a fixed column ranging from 1 to N. During the initial creation of the individuals, the row position for each individual node is generated randomly. Each column is initialized with a number between 1 and N, showing the row position of the queens. Fig. 5 shows an example of the 8-queens problem with an initial population of four individuals, where the rows are positions of each queen in each column position of 1 to 8.

Fig. 5. An instance of an initial population with four individuals

4. Overview of the Genetic Algorithm

Approach

The N-queens class has several public member functions for the main operations of the genetic algorithm. Function create initial population

performs the initialization of the population. After the initial population has been populated with individuals, then the non attacking queens are calculated using the following simple rule:

• Two queens are attacking each other

whenever:

(7)

Isomorphic Solutions of the N-queens Problem more queens located on the same row whenever Yi = Yi+1 =… = Yj, where Yi and Yj are row positions of different column, labeled from 1 to N.

Function calculate fitness is then used to calculate the fitness of each individual. The fitness function is determined in the following steps:

• Determine the maximum number of

non-attacking pairs of queens

max

(

A

´

Qi

)

=

N ×

(

N

1

)

2

where

A

´

Qi denotes the number

non-attacking queens and N is the number of queens.

• Calculate the number of attacking queens (

A

Q

i ), which located on the same rows and/or the same diagonals. The queens are attacking each other either directly or are for the main operation of the genetic algorithm, i.e. random selection, reproduction, and mutation. The random selection function chooses a pair of different individual from the population randomly. Each pair of individuals is then undergoes a reproduction process in which some genes from one individual are exchanged with genes from another individual. The crossover point is randomly chosen from the positions in the strings of genes [11]. By this means, two new individuals are reproduced with combination of genes inherited from the two parents. The reproduction step is illustrated in Fig. 7.

Fig. 7. Reproduction step with crossover point (c) randomly chosen

The last process of the genetic algorithm is mutation, where one or more genes from the new created individual are altered into different genes. The mutation point denoting which gene will undergo the alteration is chosen randomly from the string of genes of each new individual.

5. Result and Discussion

The genetic algorithm appoach is written using GNU C/C++ and implemented on Intel CPU T2130 @1.86GHz, 2.0 GHz of RAM with GNU C/C++ running on Windows XP. In the first stage, a set of data consisting of several chess board size are tested by executing the program ten times for each data set of certain chess board sizes. A complete results obtained from the test for several number of queens is shown in Table 1.

(8)

Isomorphic Solutions of the N-queens Problem

Table 1. Results of ten times execution for several number of queens

# of Queen s

Max Gen #Pop

Ave # of Gen

Ave Exec Time (sec)

Ave # of Exec

4 100 20 1.1 0 10.4

5 20 7.3 0.015625 1

6 20 42.8 0.078125 35.8 7 20 48.5 0.140625 7.9 8 20 48.7 0.125 92.8 9 20 60.5 0.203125 151.1 10 200 20 103.9 0.421875 92.2 11 300 20 155.6 0.640625 142.5 12 20 180.7 0.734375 311.7 13 400 20 230.1 1.078125 413.4

Table 1 shows some parameters that are used in the program, namely the number of queens (# of queens), maximum number of generations (Max Gen), the number of population (#Pop), the average number of generations (Ave # of Gen), the average execution time (Ave Exec Time) in seconds, and the average number of execution in order to obtain the first solution (Ave # of Exec).

From the experiment performed, it was found that the complexity of the program is increasing as

the number of the queens is increased. This fact can be illustrated by the number of executions performed in order to obtain the first solution, since the solution is not always obtained in every execution (see Fig. 8). The number of executions are increasing exponentially as the number of queens increases.

Fig. 8. Complexity of the problem as the number of queens increases

As for the second stage, the program is executed in order to obtain the isomorphis solutions for each number of queens setup in the first stage. Results of the execution for the number of Queens of 4, 5, 6, 7, and 8 can be seen in Fig. 9.

Fig. 9. Isomorphic solutions for several N-queens

(9)

Isomorphic Solutions of the N-queens Problem

6. Conclusion

Even though the N-queens problem is often considered as a toy problem, the solution to this problem is in fact a difficult one. The N-queens problem is an instance of the contraint satisfaction problem (CSP) that can be setup for classroom demonstration purposes as an NP-complete problem. The nature of isomorphic solutions of the problem can also be used as a tool to teach student about the concepts of isomorphism, transformation groups, as well as equivalence classes.

References

[1] Erbas, C., Sarkhesik, S., Tanik, M. M.: Different perspectives of the N-queens problem. CSC ’92: Proc. of the 1992 ACM Annual Conference on Communications, 99-108 (1992)

[2] Crawford, K.D.: Solving the N-queens problem using genetic algorithm, SAC ’92: Proc. Of the 1992 ACM/SIGAPP Symposium on Applied Computing, 1039-1047 (1992)

[3] Homaifar, A., Turner, J., Ali, S.: The N-queens problem and genetic algorithm, Proc. IEEE Southeastcon ’92, 262-267 (1992)

[4] Bozikovic, A., Golub, M., Bodin, L.: Solving N-queens problem using global parallel genetic algorithm, The IEEE Reg. 8 EUROCON 2003, 104-107 (2003)

[5] Bozinovski, A., Bozinovski, S.: N-queens pattern generation: an insight into space complexity of a backtracking algorithm, Proc. of the 2004 International Symposium on Information and Communication Technologies (ISICT ’04), 281-286 (2004)

[6] Martinjak, I.: Comparison of heuristic algorithms for the N-Queen problem. Proc. Of the ITI 2007 29th

Int. Conf. on Information Technology Interfaces, Cavtat, Croatia, 759-764 (2007).

[7] Turkey, A.M., Ahmad, M.S.: Using genetic algorithm for solving N-queens problem. Proc. of the 2010 International Symposium in Information Technology (ITSim), Kuala Lumpur, 745-747 (2010)

[8] Eastridge, R., Schmidt, C.: Solving N-queens with a genetic algorithm and its usefulness in a computational intelligence course. J. of Computing Sciences in Colleges 23, 223-230 (2008)

[9] Cull, P., Pandey, R.: Isomorphism and the N-queens problem. SIGCSE Bulletin 26, 29-44 (1994)

[10] Pinter, C.C.: A Book of Abstract Algebra. McGraw-Hill: New York (1982)

[11] Russel, S., Norvig, P.: Artificial Intelligence A Modern Approach. 2nd Edition, Prentice

Hall: New Jersey (2003)

Gambar

Fig. 1. Example of solution for the 8-queens
Fig. 3. The N-queens class structure
Fig. 7. Reproduction step with crossover point (c)randomly chosen
Table 1 shows some parameters that are used in

Referensi

Dokumen terkait

[r]

[r]

[r]

teknologi informasi dengan tata kelola perusahaan dapat dilihat pada gambar 2.3.. dibawah

• Diperlukan peningkatan bobot pemeringkatan kegiatan kemahasiswaan dari yang berlaku saat ini dengan mempertimbangkan prestasi, partisipasi, level kegiatan, penyelenggara dan

X untuk proses perubahan data pribadi ketika dikelola oleh DPLK B per periode adalah sebesar Rp 4.400.000 yang terdiri dari upah C&B Specialist value added time selama 4

Dari hasil rapat yang dimaksud di atas maka berdasarkan erpres No 54 Tahun 2010 dan. perubahannya beserta aturan turunannya, pasal 83 ayat (3) point e maka PA

Akan tetapi, baik juga Anda perhatikan, sebagai penulis artikel kolom media bahwa prinsip yang disampaikan di atas tidak serta-merta sama dengan menganggap