You are on page 1of 25

Artificial Intelligence

CS-451

Instructor : Dr. Syed Musharaf Ali


What is Genetic Algorithm
1. Firstly, we defined our initial population as our countrymen.
2. We defined a function to classify whether is a person is good or bad.
3. Then we selected good people for mating to produce their off-
springs.
4. And finally, these off-springs replace the bad people from the
population and this process repeats.

This is how genetic algorithm actually works, which basically tries to


mimic the human evolution to some extent.

Genetic algorithm, is a search based optimization technique, which


tries to find out the values of input so that we get the best output
values or results.
What is Genetic Algorithm
Simple Genetic Algorithm

{
initialize population;
evaluate population;
while TerminationCriteriaNotSatisfied
{
select parents for reproduction;
perform crossover and mutation;
evaluate population;
}
}
What is Genetic Algorithm
The working of a genetic algorithm is also derived from biology, which is
as shown
Genetic Algorithm
Population − It is a subset of all the possible (encoded) solutions to the given
problem.

Chromosomes − A chromosome is one such solution to the given problem.

Gene − A gene is one element position of a chromosome.

Fitness Function − A fitness function takes the solution as input and produces the
suitability of the solution as the output.

Genetic Operators − These alter the genetic composition of the offspring. These
include crossover, mutation etc.
Example : Knapsack Problem
you are going to spend a month in the wilderness. Only thing you are
carrying is the backpack which can hold a maximum weight of 30 kg.
Now you have different survival items, each having its own “Survival
Points” (which are given for each item in the table). So, your objective is
maximise the survival points.
Example : Knapsack Problem
Population: Population is composed of individuals and individuals have
chromosomes
In GA, chromosomes are binary strings, where for this problem 1 means
that the following item is taken and 0 means that it is dropped.

The set of chromosomes considered as our initial population.


Example : Knapsack Problem
Fitness Function
Let us calculate fitness points for our first two chromosomes.
For A1 chromosome [100110],

Similarly for A2 chromosome [001110],

So, for this problem, our chromosome will be considered as more fit when it contains
more survival points. Therefore chromosome 1 is more fit than chromosome 2.
Genetic Algorithm

population

Chromosomes could be:


– Bit strings (0101 ... 1100)
– Real numbers (43.2 -33.1 ... 0.0 89.2)
– Lists of rules (R1 R2 R3 ... R22 R23)
– ... any data structure ...

A typical binary string chromosome may look like this:

10010101110101001010011101101110111111101
Genetic Algorithm
What's the Crossover Rate?

This is simply the chance that two chromosomes will swap their bits. Crossover is
performed by selecting a random gene along the length of the chromosomes and
swapping all the genes after that point.
Genetic Algorithm
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.

Multi Point Crossover


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

What's the Mutation?

This is the chance that a bit within a chromosome will be flipped (0


becomes 1, 1 becomes 0).
Genetic Algorithm
Mutation Operators
Mutation may be defined as a small random tweak in the
chromosome, to get a new solution.

Bit Flip Mutation


In this bit flip mutation, we select one or more random bits and flip
them. This is used for binary encoded GAs.

Swap Mutation
In swap mutation, we select two positions on the chromosome at
random, and interchange the values.
Genetic Algorithm
Mutation Operators

Mutation may be defined as a small random tweak in the


chromosome, to get a new solution.
Scramble Mutation
A subset of genes is chosen and their values are scrambled or shuffled
randomly.

Inversion Mutation
In inversion mutation, we select a subset of genes like in scramble
mutation, but instead of shuffling the subset, we merely invert the
entire string in the subset.
Genetic Algorithm
Example:

Given the digits 0 through 9 and the operators +, -, * and /,


find a sequence that will represent a given target number. The
operators will be applied sequentially from left to right as you read.

So, given the target number 17, the sequence 6+5*4/2+1 would be
one possible solution.
Genetic Algorithm
Stage 1: Encoding

First we need to encode a possible solution as a string of bits… a chromosome.


Represent all the different characters available to the solution... that is 0 through 9 and +, -, *
and /.

This will represent a gene. Each chromosome will be made up of several genes.

Four bits are required to represent the range of characters used as gene:

0: 0000
1: 0001
2: 0010
3: 0011
4: 0100
5: 0101
6: 0110
7: 0111
8: 1000
9: 1001
+: 1010
-: 1011
*: 1100
/: 1101
Genetic Algorithm
The solution mentioned above for 17, ' 6+5*4/2+1' would be
represented by nine genes like so:

0110 1010 0101 1100 0100 1101 0010 1010 0001

6 + 5 * 4 / 2 + 1

These genes are all strung together to form the chromosome:

011010100101110001001101001010100001
Genetic Algorithm
Stage 2: Deciding on a Fitness Function

Give a higher fitness score the closer a chromosome comes to solving the problem.
A fitness score can be assigned that's inversely proportional to the difference
between the solution and the value a decoded chromosome represents.

If we assume the target number is 42, the chromosome mentioned above

011010100101110001001101001010100001 has

Fitness score of 1/(42-17) or 1/25 or 0.04.

As it stands, if a solution is found, a divide by zero error would occur as the fitness
would be 1/(42-42).

This is not a problem however as we have found what we were looking for... a
solution. Therefore a test can be made for this occurrence and the algorithm halted
accordingly.
Example: Optimization of a Mathematic Function
Genetic algorithm can be used to find a maximum value of a
mathematic function.

We will attempt to maximize the following function:

f(x) = sin(x)

over the range of x from 1 to 15.

Each chromosome represents a possible value of x using four bits.


Example: Optimization of a Mathematic Function
We will use a population size of four chromosomes.

The first step is to generate a random population, which is our


first generation:

c1 = (9) = 1001
c2 = (3) = 0011
c3 = (10) =1010
c4 = (5) =0101

Next, we find the fitness of each chromosome


Example: Optimization of a Mathematic Function

To calculate the fitness of a chromosome, we need to first convert it to a


decimal integer and then calculate f(x) for this integer.

We will assign fitness score from 0 to 100, where 0 is the least fit and
100 is the most fit.

f(x) generates real numbers between -1 and 1.

We will assign a fitness as

If f(X) = 1 , fitness = 100


If f(X) = -1 , fitness = 0
If f(X) = 0 , fitness = 50
Example: Optimization of a Mathematic Function
Fitness Function
Hence, fitness of x, f’(x) is defined as follows:
f’(x) = 50(f(x) + 1) => 50(sin(x) + 1)

Chromosomes C1,C2 and C3 are most likely to produce next generation


Example: Optimization of a Mathematic Function

We now need to combine c1 and c2 to produce two new offspring.

First, we need to randomly select a crossover point. We will choose the


point between the second and third bits (genes):

C1 = 10 | 01
C2 = 00 | 11

Crossover is now applied to produce two offspring, c5 and c6:

C5 = 1011
C6 = 0001
Example: Optimization of a Mathematic Function

In a similar way, c1 and c3 are chosen to produce offspring c7 and c8,


using a crossover point between the third and fourth bits:

C1 = 100 | 1
C3 = 101 | 0

Crossover is now applied to produce two offspring, c7 and c8:

C7 = 1000
C8 = 1011
Example: Optimization of a Mathematic Function
This generation has produced two extremely fit chromosomes and two very unfit
chromosomes.

In fact, one of the chromosomes, c7, is the optimal solution.

At this point, the termination criteria would probably determine that the run could
stop. Otherwise, the algorithm will continue to run

You might also like