0% found this document useful (0 votes)
13 views

AI Lab7

The document discusses genetic algorithms and provides examples of using genetic algorithms to solve two problems. It includes code and results for a genetic algorithm using binary encoding to maximize a fitness function of f(x)=x^2. It also provides an example of a genetic algorithm using a different encoding scheme to solve the 4 queens problem using non-attacking pairs as the fitness function.

Uploaded by

umerali.dev
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)
13 views

AI Lab7

The document discusses genetic algorithms and provides examples of using genetic algorithms to solve two problems. It includes code and results for a genetic algorithm using binary encoding to maximize a fitness function of f(x)=x^2. It also provides an example of a genetic algorithm using a different encoding scheme to solve the 4 queens problem using non-attacking pairs as the fitness function.

Uploaded by

umerali.dev
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/ 6

Genetic Algorithms

Question 1:
Write the genetic algorithm using binary encoding scheme with following fitness function 𝒇(𝒙)=𝒙𝟐.
You can use 6-bit random binary numbers for your initial population.
Answer:
Flow-Chart:

Code:
Results:
Discussion:
Genetic algorithms will generate four random values for x as given in table also it will find their
sum, average, maximum and actual count. Actual count will determine how each value will be
selected for further mutation and cross-over.
String No. Initial Population X Value Fitness Actual Count
f(x) = x2
1 010000 16 256 1 (0)
2 000011 3 9 0
3 110110 54 2916 2
4 100111 39 1521 1
Sum 4702
Average 75.5
Maximum 2916

Note: Actual count for first string is ‘0’ but sum of all actual counts is 3 therefore we increase
the count for first string to ‘1’. Our algorithm will make sure that the sum of actual counts is 4.
From above table count of third string is ‘2’ so it will be selected twice and count for second
string is ‘0’ so it will be discarded.
String No. Mating Crossover Offspring after X value Fitness
Pool Point crossover and f(x) = x2
mutation
1 010000 011110 30 900
2 110110 Randomly 010000 16 256
3 110110 selected 110110 54 2916
4 100111 101110 46 2116
Sum 6188
Average 1547
Maximum 2916

We will repeat this process until we get the highest fitness function. In the above example after 2
more iterations, we will get our goal. Children after crossover and mutation are [63, 42, 62, 42]
Question 2:
Write the genetic algorithm by changing the encoding scheme(digits) for 4-queen problem. You
can use non attacking pairs as your fitness function.
Answer:
Code:
Results:
After running our algorithm several times, we have two correct solutions.

Figure 2 First Solution Figure 1 Second Solution

Discussion:

First Solution Second Solution

Both outputs represents a valid solution because they satisfies the constraints of the N-Queens problem,
ensures that no two queens are in the same row, column, or diagonal, thus avoiding any conflicts.

You might also like