You are on page 1of 15

INTRODUCTION TO

ARTIFICIAL INTELLIGENCE
FOR IT & NON-IT PROFESSIONALS
8-QUEENS PROBLEM

.Consider a famous problem which is frequently discussed in GA.


Called the 8-Queens Problem.
The objective is to place 8-queens on a chess board so that none
of them can attack the other
8-Queens chess board
Eight Queens Problem

White boxes
show attack
range of a Q

queen
Eight Queens are shown
•We define a scheme to
denote the board’s
position at any given time
•For every Queen Q
Q
•Each No. below Q Q
represents the row No. Q
And position of the Q
number is the column Q

26834531 Q
8-Queens Problem
Fitness Function:
Q1 can attack NONE Q8

Q2 can attack NONE Q1

Q4 Q7
Q3 can attack Q6 Q5

Q4 can attack Q5 Q6

Q5 can attack Q4 Q2

Q6 can attack Q5 Q3

Q7 can attack Q4
Q8 can attack Q5
Fitness = No of. Queens that can attack none
Fitness = 2
8-Queens Problem

• So once representation and fitness function is decided,


the solution to the problem is simple.
8-Queens Problem

• Choose initial population


• Evaluate the fitness of each individual
• Choose best individuals from the population for crossover
• .Lets quickly go through an example to solve this problem using
GA
• Suppose individuals (board positions) chosen for crossover are:
Suppose the following board
positions (individuals) r chosen
for crossover

Q Q
Q Q
Q
2 Q
3
Q Q Q Q
Q
Q Q Q
Q Q

85727135 45827165
Eight Queens Problem
Using Crossover
Parents Children

85727135 85727165

45827165 45827135
Eight Queens Problem
Q Q
Q Q
Q
2 2
Q Q Q Q
Q
Q Q Q Q
Q Q
85727135 85727165

Q Q
Q 1 Q
Q
Q 3 Q 4
Q Q Q Q
Q
Q Q
Q Q
45827165 45827135
Eight Queens Problem
• Mutation, flip bits at random
45827165

0100 0101 1000 0010 0111 0001 0110 0101

0100 0101 1000 0010 0111 0001 0011 0101

45827135 Q
Q
Q
Q 4
Q Q

Q
Q

45827135
Eight Queens Problem
• Solution!
Q

Q 8
Q

46827135
The following flow chart summarizes the Genetic
Algorithm.

You might also like