0% found this document useful (0 votes)
406 views10 pages

Solving The N-Queens Problem Using Genetic Algorithm:: Chromosome Design

The document describes how to solve the n-queens problem using a genetic algorithm. It specifically discusses solving the 5-queens problem. It explains the steps as: 1) representing chromosomes as arrays, 2) initializing a random population, 3) calculating fitness scores, 4) selecting chromosomes for reproduction based on fitness, 5) performing crossover and mutation to generate new chromosomes, and 6) repeating the process until a solution is found where no queens can attack each other. The goal is to find a chromosome with a fitness score of 10, indicating the maximum number of non-attacking queen pairs.

Uploaded by

Vishvesh Muthu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
406 views10 pages

Solving The N-Queens Problem Using Genetic Algorithm:: Chromosome Design

The document describes how to solve the n-queens problem using a genetic algorithm. It specifically discusses solving the 5-queens problem. It explains the steps as: 1) representing chromosomes as arrays, 2) initializing a random population, 3) calculating fitness scores, 4) selecting chromosomes for reproduction based on fitness, 5) performing crossover and mutation to generate new chromosomes, and 6) repeating the process until a solution is found where no queens can attack each other. The goal is to find a chromosome with a fitness score of 10, indicating the maximum number of non-attacking queen pairs.

Uploaded by

Vishvesh Muthu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Solving the n-queens problem using Genetic Algorithm:

So specifically for this example we have 5 queens. You need to solve


it for 8 queens in the assignment with the given fitness function in the
assignment itself.
Brief Introduction:
First of all N-Queen problem is the problem where we need to find an
arrangement of N queens on the chessboard, such that no queen can
attack any other queens on the board. So in our 5-Queens problem we
need to place 5 queens on a 5×5 chessboard so that no two queens attack
each other. When we find all possible cases, it would somewhat look
like the following:

The steps to be followed for solving the problem are as follows:


In our task, we need to solve the 5-Queen problem using a Genetic
Algorithm. We need to use the principle of evolution to find a solution
to a problem.
 Chromosome design
 Initialization
 Fitness evaluation
 Selection
 Crossover
 Mutation
 Update generation
 Go back to 3)
Let’s briefly explain each step of solving the 5-Queens problem
using a Genetic Algorithm.
1) Firstly, we need to create a chromosome representation. For
showing a chromosome, the best way is to represent it as a list of
length N where in our case N=5. The value of each index shows the
row of the queen in a column. The value of each index is from 1 to 5.

2) In the initialization process, we need to arrange a random


population of chromosomes (potential solutions) are created. Here is
the initial population, wherein we take 4 chromosomes, each of which
has a length 5. They are:
 [5 2 4 3 5]
 [4 3 5 1 4]
 [2 1 3 2 4]
 [5 2 3 4 1]
In particular, these chromosomes can be shown as the following on
the chessboard:

3) First of all, the fitness function is pairs of non-attacking queens.


So, higher scores are better for us. In order to solve the fitness
function for the chromosome [5 2 4 3 5], I assigned each queen
uniquely as Q1, Q2, Q3, Q4 and Q5. And to find the fitness function
value I made the following equation:

𝐹𝑖𝑡𝑛𝑒𝑠𝑠 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 = 𝐹1 + 𝐹2 + 𝐹3 + 𝐹4 + 𝐹5

where:

𝐹1 = number of pairs of nonattacking queens with queen Q1.


𝐹2 = number of pairs of nonattacking queens with queen Q2.
𝐹3 = number of pairs of nonattacking queens with queen Q3.
𝐹4 = number of pairs of nonattacking queens with queen Q4.
𝐹5 = number of pairs of nonattacking queens with queen Q5.

Here for example if we already counted pair Q1 and Q2 to F1, we


should not count the same pair Q2 and Q1 to F2.
So we found that for chromosome [5 2 4 3 5] the fitness function will
be equal to 7. It is shown in the figure below:

We should evaluate all of our population individuals (chromosomes)


using the fitness function. So fitness functions will be the following:

[5 2 4 3 5] fitness function = 7

[4 3 5 1 4] fitness function = 6

[2 1 3 2 4] fitness function = 6

[5 2 3 4 1] fitness function = 5
Then we need to compute the probability of being chosen from the
fitness function. This will be needed for the next selection step. First,
we need to add all fitness functions which will be equal as the
following:

7 + 6 + 6 + 5 = 24

Then we need to compute the probability of being chosen from the


fitness function. We need to divide the fitness function by the sum of
the fitness function and multiply it to 100%.

[5 2 4 3 5] probability of being chosen = 7/24 ∗ 100% = 29%


[4 3 5 1 4] probability of being chosen = 6/24 * 100% = 25%
[2 1 3 2 4] probability of being chosen = 6/24 * 100% = 25%
[5 2 3 4 1] probability of being chosen = 5/24 * 100% = 21%

In the next step, we randomly choose the two pairs to reproduce based
on probabilities which we counted on the previous step. In other words,
a certain number of chromosomes will survive into the next generator
using a selection operator. Here selected chromosomes to act as parents
that are combined using crossover operator to make children. In
addition to this, we pick a crossover point per pair.
Here we took randomly following chromosomes based on their
probabilities:

[4 3 5 1 4]

[5 2 4 3 5]

[4 3 5 1 4]

[2 1 3 2 4]
We can notice that we did not take the chromosome [5 2 3 4 1] because
its probability of being chosen is the least among chromosomes.

For the first pair

[4 3 5 1 4]

[5 2 4 3 5]

The crossover point will be picked after two genes.

In the case of the second pair

[4 3 5 1 4]

[2 1 3 2 4]

The crossover point will be picked after three genes.

Here we go to the next step because our fitness value is not equal to
𝐹𝑚𝑎𝑥 which is the maximum number fitness value in the chromosome
that satisfies the condition of the solution of the 5-Queen problem.
𝐹𝑚𝑎𝑥 is equal to 10.
In the crossover, selected chromosomes act as parents that are
combined using crossover operator to make children. In other words, it
combines the genetic information of two parents to generate new
offspring.

Here we can see that children generated from the first pair ([4 3 5 1 4]
and [5 2 4 3 5]) are the following:

[4 3 4 3 5]

[5 2 5 1 4]

From the second pair ([4 3 5 1 4] and [2 1 3 2 4]) the children are the
following:

[4 3 5 2 4]

[2 1 3 1 4]
The next step is mutation. In the mutation process, we alter one or more
gene values in chromosomes which we found after crossover. So it
randomly changes a few genes and the mutation probability is low. So
in our example, our mutation will look like the following:

[4 3 4 3 5] → [4 3 1 3 5]

[5 2 5 1 4] → [5 2 3 1 4]

[4 3 5 2 4] → [4 3 5 2 4]

[2 1 3 1 4] → [2 1 3 5 4]

where we can notice that the third gene in the chromosome [4 3 4 3 5]


changed from 4 to 1.

Also the third gene in the chromosome [5 2 5 1 4] changed from 5 to


3. In addition to this, the fourth gene in the chromosome [2 1 3 1 4]
changed from 1 to 5.

So until this, the genetic algorithm to solve the 5-Queen algorithm will
look like the following:
In the next step, we need to update the generation. New chromosomes
will update the population but the population number will not change.
So the chromosomes

[4 3 1 3 5]

[5 2 3 1 4]

[4 3 5 2 4]

[2 1 3 5 4]

Will be our new population.

So on the next step, we need to come back to step 3 (fitness evaluation)


to find the fitness function of our updated population.

Steps 3–7 are repeated until chromosome (solution) will satisfy the
following:

Fitness value == Fmax


Where Fmax is equal to 10

For assignment’s output involving 8 queens using GA print the


following:

 Final state

 And the corresponding score which equals none other than Fmax.

 Define the mutation rate of changing to a different number


and print it.

 Also print the generation (or iteration) at which the


solution was found.

References:

[1] Solving N Queen Problem using Genetic Algorithm.


[2] Video explanation of solving 5 Queen Problem using Genetic
Algorithm.

*******************************************************

You might also like