Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

Solving the 5-Queens Problem Using Genetic Algorithm
Computer Science

Solving the 5-Queens Problem Using Genetic Algorithm

Last Updated on March 18, 2021 by Editorial Team

Author(s): Arman Assankhanov

Computer Science

What is the N-QueenΒ problem?

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 placing 5 chess queens on a 5Γ—5 chessboard so that no two queens attack each other. When we find all possible cases, it would look like the following:

Steps which we need toΒ do

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.

In order to solve the 5-Queen problem the following steps areΒ needed:

1) Chromosome design
2) Initialization
3) Fitness evaluation
4) Selection
5) Crossover
6) Mutation
7) Update generation
8) Go back toΒ 3)

Let’s briefly explain each step of solving the 5-Queens problem using a Genetic Algorithm.

1) Chromosome design

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) Initialization

In the initialization process, we need to arrange a random population of chromosomes (potential solutions) are created. Here is the initial population, I took 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) Fitness evaluation

First of all, the fitness function is pairs of non-attacking queens. So, higher scores are better is 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:

Fitness function = F1+F2+F3+F4+F5

where:

F1 = number of pairs of nonattacking queens with queenΒ Q1.

F2 = number of pairs of nonattacking queens with queenΒ Q2.

F3 = number of pairs of nonattacking queens with queenΒ Q3.

F4 = number of pairs of nonattacking queens with queenΒ Q4.

F5 = 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.

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%

4) Selection

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 Fmax which is the maximum number fitness value in the chromosome that satisfies the condition of the solution of the 5-Queen problem. Fmax is equal toΒ 10.

5) Crossover

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]

In other words, in order to create the first child from pair in crossover process, we took the parent #1 chromosome first part and parent #2 chromosome second part which makes the new individual which consistsΒ of

[(first part of parent #1 chromosome) [(second part of parent #2 chromosome)]

In order to create the second child from the same pair we took the parent #1 chromosome second part and parent #2 chromosome first part which makes the new individual which consistsΒ of

[(second part of parent #1 chromosome) [(first part of parent #2 chromosome)]

So in our case when we create the children of pair [4 3 5 1 4] and [5 2 4 3 5], for producing the first child, we took [(first part of parent #1 chromosome) [(second part of parent #2 chromosome)] = [ 4 3 4 3Β 5]

For producing second child, we took [(second part of parent #1 chromosome) [(first part of parent #2 chromosome)] = [5 2 5 1Β 4]

The same process we will do to the second pair ([4 3 5 1 4] and [2 1 3 2Β 4]).

6) Mutation

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 gens 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:

7) Update generation

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.

8) Go back to stepΒ 3

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

References:

[1]Solving N Queen Problem using Genetic Algorithm

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

Post also was published at:

Github repository

If you have questions, suggestions or a compliment, clap or hit the section belowπŸ˜πŸ‘‡


Solving the 5-Queens Problem Using Genetic Algorithm was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.

Published via Towards AI

Feedback ↓