Content uploaded by Prachi Garg
Author content
All content in this area was uploaded by Prachi Garg on Nov 28, 2022
Content may be subject to copyright.
Hybrid Crossover Operator in Genetic Algorithm for solving N-Queens
Problem
Prachi Garg1 and Surjeet Singh Chauhan (Gonder)2
1,2Department of Mathematics, UIS, Chandigarh University
Email id: prachi.garg2019@gmail.com , surjeetschauhan@yahoo.com
Abstract
The N-Queens problem involves placing n numbers of queens in such a way that no two queens attack one other
in the same row, column, or diagonal location, also known as the NP-class problem. To put it in a simple way,
we can say that no two queens have the same row, column, or diagonal. In the past, a variety of strategies were
employed to find solutions to the N-Queens problem, each with its own set of complexity and drawbacks. In this
research, a new hybrid crossover operator is used for solving the N-Queens problem with sizes ranging from 4
to 12.
Keywords: N-Queens, Genetic algorithm, Hybrid crossover operator, Fitness function, Mutation.
1. Introduction
A non-deterministic algorithm produces a diverse result even using the same input. These problems are
classified as NP-class problems, and the N-Queens problem is one of them. [1, 2]. The N-Queens problem is an
N-Queens chess board problem in which n number of queens are arranged on the chess board so that no two
queens cross each other in row, column, or diagonally. Figure 1 depicts the different attack directions of a queen
in a given position. These directions are row wise, column wise and diagonal wise. If two queens are placed on
the same row or same column or same diagonal than it is considered as clash and other positions of the queens
are searched on N
×
N chess board. The main purpose of solving this problem is to arrange N-Queens in a
manner that the solution of the problem gives none of the queen share same row, column and diagonal with
other queens. It has non-trivial solutions. But for some particular values of N, solution does not exist for
example for N
¿4
the N-Queens problem does not have solution [3].
Fig1: Possible attack directions
2. Related work
Ivica M. et al compared the various heuristic algorithms like Genetic algorithm, simulated Annealing, Taboo
search used to solve N-Queens problem. These heuristics has been used to solve large dimensional N-Queens
problem and shows the efficiency of these algorithms for solving such complex problem [2]. Marko B. et al
applied genetic and parallel genetic algorithm to solve the N-Queens problem and compared the results. Parallel
genetic algorithm is used to reduce the computational speed of genetic algorithm and to improve the
performance of genetic algorithm [4]. Hitesh G. et al. [5] hybridized the genetic algorithm and the performance
of hybridized genetic algorithm has been compared with the traditional approaches Jagdish et al. [6] introduced
parallel genetic algorithm with two new approaches to solve N-Queens problem. Vikas T. et al. [7] compared
the performance of genetic algorithm and backtracking algorithm used for solving N-Queens problem on the
basis of their execution time. Lubna Z.B. et al designed genetic algorithm with a weight based objective
function to determine the fitness value of the solution of N-Queens problem [8]. Ahmed S.F. et al used genetic
algorithm to find out all 92 solutions of 8-Queens problem. Each chromosome or solution is represented by one
dimensional array with size equal to 8 with a fact to reduce the space search [9]. Harshada T. et al showed an
effective approach to solve N-Queens problem using genetic algorithm than the traditional backtracking method
[10].
3. Genetic Algorithm
Genetic algorithm is a population based stochastic search algorithm based on the concept of “Survival of the
fittest”. Genetic algorithm has its own terminology. It works with some initial guesses known as population. The
no. of initial guesses required to run algorithm is known as population size. It generally works with three
operators: selection, crossover and mutation. Each operator has its own significance and style of working. The
interesting thing about these operators is that it can be modified depending upon the problem solving. But main
purpose of applying them remains same. The Selection operators, selects the best population for the next
generation, crossover operator creates new population members by the mating of the parent member. Mutation
operator maintains the diversity in the search space by creating a new population member without mating. In
this paper, a hybridized genetic algorithm based on the new position based crossover has been designed to find
out the solutions of N-Queens problem.
4. Hybrid Genetic Algorithm
In
n × n
chessboard, placement of queens follow the pattern of permutation i.e., each queen must be settle on
different row and column. Suppose
mth
number of queen is settling on
mth
column and the value
qm
of the
mth
position gives the
(n−1)
row position. So the representation of the solutions of the N-Queens problem is of the
form:
(q1, q 2, …, q m, … ,q n)
where the value of the row position is counted from bottom to top. The
permutation representation of the problem eliminates the row and column clashes so only diagonal clashes
remain to eliminate. Figure 2 illustrate the 8-Queens problem.
Q
Q
Q
Q
Q
Q
Q
Q
( 5 3 6 0 2 4 1 7 )
Fig2: Representation of eight Queens
4.1 Evaluation of Fitness value:
Since, only diagonal clashes remain to eliminate so fitness function helps to eliminate the diagonal clashes
where the clashes use to count the number of clashes between each pair of queens. For counting the clashes
initialized the chessboard with the help of four co-ordinates
(
−1,1 ,−1,1
)
. These co-ordinates are divided to
form n number of rows and column to form a
n × n
chessboard. Then check the diagonal clashes of every row
position of each column with the help of these co-ordinates. With the help of fitness function, assign the fitness
value to evaluate the fitness of each solution to check how close it is to the optimal solution. A fitness function
scores each solution according to their clashes and arrange then by high error to low error. The worst score
would be the one with highest error and it calculated by the maximum clashes. The best score would be the one
with the lowest error and it is calculated by best score.
Best score=worst score−minimumclashes
then convert this into the weighted percentage using fitness value.
Fitness value=worst score−clashes of that solution
Best score
The working of selection, crossover and mutation operators used to hybridize genetic algorithm is as follows:
4.2 Selection operator:
Selection operator works with the mating probability which is used to check whether the solution is selected for
applying the position based crossover or not. Initially select a solution randomly from the population and if the
probability criteria satisfy then the solution selected as parent A and if not then again choose a solution and
repeat a process until the selection of parent A then go for the selection of second parent B. The process of
selection of parent B is same as the process of selection of parent A, but the parent B is different from the parent
A.
4.3 Position Based Crossover:
Position based crossover is held on the selected parents, it is a process of establish a new solution with the help
of the selected parents. A mating probability gives the freedom to every solution that the solution will go for the
PBC or not. PBC is taking place with a probability, ranging between 0 to 1. Typically, 0.7 probability is used in
order to crossover the solutions.
The steps of position based crossover are given in following list:
Step1: Randomly generate a new solution (offspring) of length n by using the numbers between 0 to n-1 without
repetition.
Step2: Check all the positions one by one, if the value of the position is odd then replace with 0 in offspring else
replace with 1 in the offspring on that position.
Step3: The position which is generated as 1 will be filled by parent A of same position.
Step4: The position which is generated as 0, will be filled by parent B one by one on comparing the value of
already filled position. i.e., for first position which is generated as 0, check first position of parent B if the value
of the position is not covered in the offspring then place that value on first position which is generated as 0 and
if it is already covered in the offspring then go to the second position of parent B and then again check the value
of that position. This process continues until the value for the first position will be obtained, which is generated
as 0.
Step5: Then go to the second position which is generated as 0 and find the value of that position by the same
process.
Continue this process until all positions are covered. For example:
Parent A:
Parent B:
Random offspring
Represent
even (1) & odd (0)
ParentA
ParentB
New Offspring
Fig 3: P.B.C. in H.G.A.
4.4 Mutation operator:
Mutation operator is used to interchange the value of two positions with the help of mutation probability in
the solution. Mutation is held when the mutation probability of two positions is in range then interchanges
the value of the positions, whose results gives the new solution. Mutation is taken place with probability
37416025
64307512
32017654
01100101
37416025
64307512
67431025
ranging between 0 and 1, typically it applies at 0.001 probability. Mutation is useful when the optimal
solution will obtain by interchanging the value of two positions. Generally, mutation take place after the
position based crossover. The mutation operator is given below:
37 416025
Become
37016425
Fig4: Mutation operator in HGA
5. Computational steps of HGA:
The flow chart representation of computational steps of HGA are as follows:
N0
Yes
Fig 5: Flow chart of HGA
The description of flow chart are as follows:
Step1: Randomly create a set of solutions (array list of population).
Step2: Evaluate the clashes of each solution in the array list of population.
Stop
Save changes in data
array
Selection of
Parents
Mutation within
Offspring if satisfies
Crossover b/w
parents to produce a
new offspring
Initialize the
Population
Satisfy
stop
criteria
Calculate fitness of
each solution
Start
Step3: If one or more than one solution satisfies the termination criteria then algorithm will stop. i.e., if the
appropriate solution(s) is/are acquire or the maximum number of generations has been completed.
Step4: Otherwise, Analysis the fitness value of each solution in the population with the help of worst and best
score.
Step5: Randomly select two solutions for treating them as parents.
Step6: Check the mating probability of the selected solutions. If the solution satisfies the mating probability,
then go to step 7 if not then go to step 5.
Step7: Change the solution using position based crossover and save the changes in the data array.
Step8: Check the mutation probability of the selected solutions. If satisfies then apply mutation to create a new
solution if not, then directly go to the step2.
Step9: Copy the new solution in the data array to form the next generation.
Repeat Step2 – Step9 until find the appropriate solutions(s) or some other stop criteria has been fulfilling.
6. Results
In this paper a hybridized genetic algorithm with position based crossover has been used to find the solution of
N-Queens problem. Position based operator is used to generate an offspring of length
n
with the help of number
between 0 to n-1, without repetition which the mutual involvement of two randomly selected parents. The
problem size has been varied from 4 to 12 and 10 trials of each has been made. Table 6 summarizes the number
of average generations taken by algorithm for solving the N-Queens problem with varying sizes. This paper is
an attempt to design a new crossover operator that can be used to solve this problem and results of table 6 shows
the success of this attempt.
Number of
Queens
Average generations
4 2
5 2
6 21
7 4
8 21
9 40
10 350
11 576
12 688
Table 1: Average generations for N-Queens.
Fig 6: graphical representation of Average generation of N-Queens
The graph shows that as the number of queens increases the average generation’s increases rapidly. Sometimes
hybrid genetic algorithm gives the solution of N-Queens problem in one or two generations and sometimes it
takes more than enough generations to provide the solution. The high amount of variation held between the
generations because it is a random process of initial solutions and also random in the process steps. As the
number of queens increases the average generation increase like an exponential time. It is difficult to find the
solution of large number of queens in realistic time frame. Some of the solutions of n number of queens are
given in the below table. In this table, the position represents the column and the value of
nth
position represents
the
(n+1)th
row position.
N Solutions
12 8 1 7 4 10 0 5 3 11 6 2 9
11 9 2 4 7 10 0 5 1 8 6 3
10 2 5 9 6 1 3 7 0 4 8
9 0 4 6 8 3 1 7 5 2
8 5 3 6 0 2 4 1 7
7 5 3 6 0 2 4 1
6 1 3 6 0 2 4
5 2 4 1 3 0
4 1 3 0 2
Table 2: Types of solution
7. Conclusion
The complexity of the N-Queens problem increases as the size of the chess board is increases. It comes to the
category of challenging problems and there is always a requirement of designing effective techniques that can
provide the solution of such problems. In this paper a position based crossover has been designed to generate the
various solutions of N-Queens problem and then with the help of selection and mutation operators a hybridized
genetic algorithm has been proposed to find the feasible solutions of this problem. Position based crossover is
used to reduce the function evaluation time and generations. Due to mutual involvement of parents, the chance
of repetition of any of parent is discarded. The experimental result shows that as the number of queens increases
then the average generation per solution increases in exponential time. Although hybrid genetic algorithm is
categorized as heuristic search algorithm, so it can provide an optimal solution of N-Queens problem almost
instantly. But it is a random process so it completely depends on the initial set of solutions.
References
[1] S. Khan, M. Bilal, M. Sharif, M. Sajid, and R. Baig, "Solution of n-queen problem using aco,"
in 2009 IEEE 13th International Multitopic Conference, 2009, pp. 1-5.
[2] I. Martinjak and M. Golub, "Comparison of heuristic algorithms for the n-queen problem," in
2007 29th International Conference on Information Technology Interfaces, 2007, pp. 759-
764.
[3] J. Bell and B. Stevens, "A survey of known results and research areas for n-queens," Discrete
mathematics, vol. 309, pp. 1-31, 2009.
[4] M. Bozikovic, M. Golub, and L. Budin, "Solving n-Queen problem using global parallel
genetic algorithm," in The IEEE Region 8 EUROCON 2003. Computer as a Tool., 2003, pp.
104-107.
[5] P. V. K. Hitesh G., "On Some Aspect Of Genetic Algorithm and N-Queen Problem,"
International Journal of Computer Information System, vol. Vol(2), p. 41, 2011.
[6] K. B. JAGDISH PRASAD, SURENDRA KUMAR YADAV, "Intelligent Heuristic Search
Algorithm for N Queens Problem of constraint satisfaction," International Journal of
Computer Science and Network (IJCSN), vol. Vol(1), 2012.
[7] V. Thada and S. Dhaka, "Performance analysis of N-Queen problem using backtracking and
genetic algorithm techniques," International Journal of Computer Applications, vol. 102,
2014.
[8] L. Z. Bashir and N. Mahdi, "Use Genetic Algorithm in Optimization Function For Solving
Queens Problem," World Scientific News, pp. 138-150, 2015.
[9] A. S. Farhan, W. Z. Tareq, and F. H. Awad, "Solving N queen problem using genetic
algorithm," International Journal of Computer Applications, vol. 122, 2015.
[10] H. Talnikar and S. Pansare, "An approach to solve n-Queens problem using Genetic
Algorithm."