You are on page 1of 9

UET New Campus

Lab Manual for Artificial Intelligence


Genetic Algorithm in Artificial Intelligence
Lab 10: Genetic Algorithm in Artificial Intelligence

Lab: Genetic Algorithm in Artificial Intelligence


1. Introduction
A genetic algorithm is a heuristic search method used in artificial intelligence and computing. It is used
for finding optimized solutions to search problems based on the theory of natural selection and
evolutionary biology. Genetic algorithms are excellent for searching through large and complex data sets.
They are considered capable of finding reasonable solutions to complex issues as they are highly capable
of solving unconstrained and constrained optimization issues.

Objective of the Experiment


After completing this lab, the student should be able to:
 Clearly understand genetic algorithm in artificial intelligence.

2. Overview
A Genetic Algorithm is one of many optimization algorithms. Its purpose is to guide a search process to
find a global optimal solution for a problem in a very large search space. It belongs to the class of
evolutionary algorithms and can solve very complex.

On some classes of problems a Genetic Algorithm can not guarantee to find a global optimal solution, but
it may approximate a sufficiently good solution in a relative short time.

One example is the automatic design of antennas for radio communication. There is an infinite amount of
possible shapes for antennas. An evolutionary algorithm can be used to find the (near-)optimal shape of
an antenna with a given requirement. In fact the NASA used Genetic Algorithms to evolve an antenna
design for its Space Technology 5 (ST5) mission which launched in March 2006.

When a problem can be represented as a combination (DNA) of multiple things (Genes) and a function
can be provided that calculates the quality of such combination (Fitness Function), a Genetic Algorithm
may hopefully find the best of all possible combinations - if given enough time! The algorithm may
automatically evolve better solutions from generation to generation with the help of Selection, Crossover
and Mutation.

2.1 Selection
Selection is the most important Phase of GA. All Population is evaluated on the basis of the fitness function.
New population is generated by probabilistic-ally selecting the most fit individual. Some selected individuals are
carried forward (selection).

2.1.1 Proportionate Selection


In Proportionate Selection, individuals are assigned a probability of being selected based on their fitness:
 pi = fi / fj
 Where pi is the probability that individual i will be selected,
 fi is the fitness of individual i,
 fj represents the sum of all the finesses of the individuals with the population.
This type of selection is similar to using a roulette wheel where the fitness of an individual is represented as
proportionate slice of wheel. The wheel is then spun and the slice underneath the wheel when it stops determine
which individual becomes a parent.

2.1.2 Tournament Selection

2
Lab 10: Genetic Algorithm in Artificial Intelligence

Tournament selection is best explained with a concrete example.


Suppose you want to pick 20 individuals from 100. Randomly choose (with uniform probability) a small
number of individuals (typically fewer than 10) from the 100 (with replacement). Keep the fittest one. Do
this again and again until you have got your 20.
Tournament selection, while slower and more complicated, can create more diverse populations.

2.2 Crossover

2.2.1 One Point Crossover


In this one-point crossover, a random crossover point is selected and the tails of its two parents are
swapped to get new off-springs.

2.2.2 Multi Point Crossover


Multi point crossover is a generalization of the one-point crossover wherein alternating segments are
swapped to get new off-springs.

2.2.3 Uniform Crossover

2.3 Mutation
2.3.1 Bit Flip Mutation
In this bit flip mutation, we select one or more random bits and flip them. This is used for binary encoded.

2.3.2 Swap Mutation

3
Lab 10: Genetic Algorithm in Artificial Intelligence

In swap mutation, we select two positions on the chromosome at random, and interchange the values.
This is common in permutation based encoding.

3. Concept Map
A search using domain-specific knowledge. Suppose that we have a way to estimate how close a state is
to the goal, with an evaluation function.

3.1 Genetic Algorithm


Pseudo-code:
 START
 — create the initial population
 Compute fitness
 REPEAT
 — Selection
 — Crossover
 — Mutation
 — Compute fitness
 UNTIL population has converged
 STOP
Lets understand this with MAX ONE example.
We toss a coin and we choose tail. In computer we can represent Head by 0 and Tail by 1.
Head = 0.

4
Lab 10: Genetic Algorithm in Artificial Intelligence

Tail = 1.
As we have chosen tail so our desired output is tail. Lets toss the coin ten times and we got result something like
this:
0110011010
in the above example we got Tail(1) five times. But what we want is to maximize the possibility of number of tails
(1) in sequence.
Lets apply genetic algorithm so we can increase the possibility of 1’s.
Lets toss the coin 60 times and make sets(chromosome/DNA) of 10 tosses(gene) each. Let ( l ) is the length of each
set and n is the total time toss.
i.e.
n = 60 & l = 10.
S1 = 1 1 1 1 0 1 0 1 0 1
S2 = 0 1 1 1 0 0 0 1 0 1
S3 = 1 1 1 0 1 1 0 1 0 1
S4 = 0 1 0 0 0 1 0 0 1 1
S5 = 1 1 1 0 1 1 1 1 0 1
S6 = 0 1 0 0 1 1 0 0 0 0
now Step 1 is complete (create the initial population). Now we want to maximize number of one’s.
Now lets calculate fitness(number of one’s in each set).
S1 = 1 1 1 1 0 1 0 1 0 1 f(S1) = 7
S2 = 0 1 1 1 0 0 0 1 0 1 f(S2) = 5
S3 = 1 1 1 0 1 1 0 1 0 1 f(S3) = 7
S4 = 0 1 0 0 0 1 0 0 1 1 f(S4) = 4
S5 = 1 1 1 0 1 1 1 1 0 1 f(S5) = 8
S6 = 0 1 0 0 1 1 0 0 0 0 f(S6) = 3
total fitness = 7+5+7+4+8+3 = 34.

5
Lab 10: Genetic Algorithm in Artificial Intelligence

Roulette Wheel

f(S1)
f(S2)
f(S3)
f(S4)
f(S5)
f(S6)

Fitness of Sets Number of Ones


f(S1) 7
f(S2) 5
f(S3) 7
f(S4) 4
f(S5) 8
f(S6) 3
Now in the above roulette wheel we can say that there is more possibility of S5, S3 and S1 as compared to others
because they cover majority of the wheel.
Now the next step is selection. Apply any selection technique, let’s say Proportionate selection.
Now select pairs for Crossover.
S1 = 1 1 1 1 0 1 0 1 0 1
S5 = 1 1 1 0 1 1 1 1 0 1
S2 = 0 1 1 1 0 0 0 1 0 1
S3 = 1 1 1 0 1 1 0 1 0 1
S5 = 1 1 1 0 1 1 1 1 0 1
S3 = 1 1 1 0 1 1 0 1 0 1
There are different types of crossover as we studies earlier. I am going to do with single point (randomly choose any
point).
S1(parent) = 1 1 1 1 0 1 0 1 0 1 ==> C1(child) = 1 1 1 1 0 1 0 1 0 1
S5(parent) = 1 1 1 0 1 1 1 1 0 1 ==> C2(child) = 1 1 1 0 1 1 1 1 0 1
Similarly we will do crossover with other pairs too. Lets say single point crossover of S2 with S3(randomly choose).
S2(parent) = 0 1 1 1 0 0 0 1 0 1 ==> C3(child) = 0 1 1 0 1 1 0 1 0 1

6
Lab 10: Genetic Algorithm in Artificial Intelligence

S3(parent) = 1 1 1 0 1 1 0 1 0 1 ==> C4(child) = 1 1 1 1 0 0 0 1 0 1


Now lets do two point crossover with the last pair for better understanding.
S5(parent) = 1 1 1 0 1 1 1 1 0 1 ==> C5(child) = 1 1 1 0 1 1 0 1 0 1
S3(parent) = 1 1 1 0 1 1 0 1 0 1 ==> C6(child) = 1 1 1 0 1 1 1 1 0 1
Now new chromosome/population is:
C1 = 1 1 1 1 0 1 0 1 0 1
C2 = 1 1 1 0 1 1 1 1 0 1
C3 = 0 1 1 0 1 1 0 1 0 1
C4 = 1 1 1 1 0 0 0 1 0 1
C5 = 1 1 1 0 1 1 0 1 0 1
C6 = 1 1 1 0 1 1 1 1 0 1
Now we have done crossover, the next step is mutation. Change any random bits/gene from the above population.
i.e. 1=> 0 and 0=>1
C1 = 1 1 1 1 0 1 0 1 0 1 ==> C1 = 1 1 1 1 1 1 0 1 0 1
C2 = 1 1 1 0 1 1 1 1 0 1 ==> C2 = 1 1 1 0 1 1 1 0 0 1
C3 = 0 1 1 0 1 1 0 1 0 1 ==> C3 = 0 1 1 1 1 1 0 1 0 1
C4 = 1 1 1 1 0 0 0 1 0 1 ==> C4 = 1 1 1 0 1 0 0 1 0 1
C5 = 1 1 1 0 1 1 0 1 0 1 ==> C5 = 1 1 1 0 1 1 1 1 0 1
C6 = 1 1 1 0 1 1 1 1 0 1 ==> C6 = 1 1 1 0 1 1 1 1 0 0
Now calculate the fitness again:
C1 = 1 1 1 1 1 1 0 1 0 1 f(C1) = 8
C2 = 1 1 1 0 1 1 1 0 0 1 f(C2) = 7
C3 = 0 1 1 1 1 1 0 1 0 1 f(C3) = 7
C4 = 1 1 1 0 1 0 0 1 0 1 f(C4) = 6
C5 = 1 1 1 0 1 1 1 1 0 1 f(C5) = 8
C6 = 1 1 1 0 1 1 1 1 0 0 f(C6) = 7
Total Fitness now = 8+7+7+6+8+7 = 43
Repeat the process until desired output is reached.

4. Homework before Lab


Attempt the following questions. You are required to bring this task with you and submit to the lab
instructor.

4.1 Problem description


Implement local searches in Python.

5. Procedure & Tools

7
Lab 10: Genetic Algorithm in Artificial Intelligence

5.1 Walk-through Task

5.1.1 Single Polynomial Solution


Suppose you have a large number of data points (x, y), e.g., (1, 4), (3,9), (5, 8), ...
You would like to fit a polynomial (of up to degree 1) through these data points.
 That is, you want a formula y = mx + c that gives you a reasonably good fit to
the actual data
 Here's the usual way to compute goodness of fit of the polynomial on the data points
 Compute the sum of (actual y -predicted y)2 for all the data points.
 The lowest sum represents the best fit.
Use Genetic Algorithm to Find Solution.

By a pretty good solution we simply mean that you can get reasonably good
polynomial that best fits the given data.

Your formula is y = mx + c
Your unknowns are m and c; where m and c are integers
Your representation is the array [m, c]
Your evaluation function for one array is:
For every actual data point (x, y)
Compute ý = mx + c
Find the sum of (y - ý)2 over all x
The sum is your measure of "badness" (larger numbers are worse)
Example: For [5, 7] and the data points (1, 10) and (2, 13):
ý = 5x + 7 = 12 when x is 1
ý = 5x + 7 = 17 when x is 2
(10 - 12)^2 + (13 -17)^2 = 2^2 + 4^2 = 20
If these are the only two data points, the "badness" of [5,7] is 20.

Algorithm:
Your algorithm might be as follows:

 Create two-element arrays of random numbers


 Repeat 50 times (or any other number):
 For each of the arrays, compute its badness (using all data points)
 Keep the best arrays (with low badness)
 From the arrays you keep, generate new arrays as follows:
 Convert the numbers in the array to binary, toggle one of the bits at random
 Quit if the badness of any of the solution is zero
 After all 50 trials, pick the best array as your final answer

Example
Let us solve this problem in detail. Consider that the given points are as follows. (x, y) : {(1,5) (3, 9)}.

We start will the following initial population which are the arrays representing the solutions (m and c).
[2 7][1 3]

Compute badness for [2 7]

8
Lab 10: Genetic Algorithm in Artificial Intelligence

ý = 2x + 7 = 9 when x is 1
ý = 2x + 7 = 13 when x is 3
(5 -9)2 + (9 -13)2 = 42 + 42 = 32
ý = 1x + 3 = 4 when x is 1
ý = 1x + 3 = 6 when x is 3

(5 - 4)2 + (9 - 6)2 = 12 + 32 = 10

Lets keep the one with low "badness" [1 3]


Representation [001 011]
Apply mutation to generate new arrays [011 011]
Now we have [1 3] [3 3] as the new population considering that we keep the two best individuals.

In the examples so far, each "Individual" (or "solution") had only one parent. The only way to introduce
variation was through mutation (random changes). In Crossover, each "Individual" (or "solution") has two
parents. Assuming that each organism has just one chromosome, new offspring are produced by forming
a new chromosome from parts of the chromosomes of each parent.

6. Tasks
6.1 Task 1
Implement the Walkthrough Task in Python.

6.2 Links
https://www.tutorialspoint.com/prolog_in_artificial_intelligence/index.asp
http://lpn.swi-prolog.org/lpnpage.php?pageid=online
https://medium.com/analytics-vidhya/understanding-genetic-algorithms-in-the-artificial-intelligence-spectrum-
7021b7cc25e7
https://www.codemiles.com/c-examples/genetic-algorithm-example-t7548.html

You might also like