You are on page 1of 56

Subj: Soft Computing

Topic: Genetic Algorithm


Soumen Paul

Department of Computer
Science and Informatics
Haldia Institute of Technology

ICARE Complex, HIT Campus,


P.O – HIT,PIN 721657
Haldia, West Bengal
Soft Computing


Search and optimization


Exploration
Definition of Genetic
Algorithm

The Genetic algorithm (GA) is a
randomized search and
optimization technique guided by
the principle of natural genetic
systems.

Recently, there has been a great
deal of interest in GAs and their
application to various engineering
fields.
Application of GA
The GA is being applied to a wide range of
optimization and learning problems in many
domains. GA is being applied to a wide range
of optimization and learning problems in many
domains. GA also lend themselves well to
power system optimization problems, since
there known to exibit robustness, require no
auxulliary, and can offer significant advantages
in solution methodology and optimization
performance.
Genetic Algorithm………

The main advantage of the GA
formulation is that fairly accurate
results may be obtained using a
very simple algorithm.

The objective of fitness function is
a judge of GA’s attempts. GAs do
not know how to derive a
problem’s solution, but they do
know, from the objective function,
how close they are to a better soln.
GA versus Conventional
Optimization and search
Procedure

Sometimes near optimal solutions
that can be generated quickly,
using GAs, are more desirable
than optimal solutions which
require a large amount of time.


Using GAs, problems can be
modelled as optimization
problems.
GA versus Conventional
Optimization and search
Procedure……….

GAs work with a coding of the
parameter set, not the parameters
themselves. Therefore they can
easily handle integral or discrete
variables.


GAs can provide globally optimum
solutions.
GA versus Conventional
Optimization and search
Procedure……….

GA use only objective function
information, not derivatives or
other auxiliary knowledge.
Therefore, they can deal with the
non-smooth, non-continuous, and
non differentiable functions.

GA use probabilistic transition
rules, not deterministic rules.
GA Problem Formulation

States: A population of individuals, each
with an associated fitness value

Operators: Transform the population
into a new generation, using Darwinian
principle of reproduction and survival of
the fittest

Goal test: Criterion for terminating the
search
Formalization

Representation scheme

Fitness measures

Parameters for controlling the
algorithm
Genetic Representation
Scheme
Binary representation:
Binary representation consists of
binary coding, the strings are made
up of 1’ and 0’ (i.e
11110010101011). The length of
the string is determined by the
precision desired for the solution.
Binary coding
2 11 Remainder
2 5 1
2 2 1
2 1 0
Binary equivalent of 11 is 1011
Procedures of GA
Suppose we wish to maximize a function of k
variables, f(x1,x2,….xk): Rk->R. Suppose further
that each variable xi can take values from a
domain Di=[ai,bi] and f(x1,x2,….xk) > 0 for all xi in
Di. We wish to optimize the function f with
required precision say upto 6 decimal places for
the variables. Then Di must be cut into (bi-ai)*106
equal sized ranges.
Procedures of GA
Let mi be the smallest integer such that 2mi-
1<=(b -a )*106 <=2m . Then m be the size of
i i i i
the Chromosome.

The equivalent value for any mi bit string


can be obtained as
xi=xil+[(xiu-xil)/(2mi-1)]*(decoded value of
string)
Fitness Measures

Numerical evaluation

Fitness = f(character in S)

The character representation in S
is also referred as chromosomes.
Parameters for controlling
the algorithm

Population size (M)

Maximum number of generations
in a run (G)

Number of runs (N)

Selection & Replacement strategy

Selection function to identify the
result (goal)
Genetic search program

Create some initial population (M
many) randomly

Iterate (G times or less)
 Evaluate the fitness of each
individual
Genetic search program
(contd…)
 Evolve the population I.e.,
randomly select individuals and
randomly apply operators among:
 Reproduction
 Crossover
 Mutation

Select the fittest individual as a
solution
Run the GA many (N) times.
Stages of GA

A GA operates through a simple cycle of stages

i) Creation of a “population” of strings,

ii) Evaluation of each string,

iii) Selection of best strings and

iv) Genetic manipulation to create new
population of strings.
Cycle of Genetic Algorithm
Description of stages of GA

Each cycle in GA produces a new generation of
possible solutions for a given problem. In the
first phase, an initial population, describing
representatives of the potential solution, is
created to initiate the search process. The
elements of the population are encoded into bit-
strings, called
Description of stages of GA…….
The performance of the strings, often called
fitness, is then evaluated with the help of some
functions, representing the constraints of the
problem. Depending on the fitness of the
chromosomes, they are selected for a
subsequent genetic manipulation process. It
should be noted that the selection process is
mainly responsible for assuring survival of the
best-fit individuals.
1 Step of Genetic Algorithm
st


After selection of the population strings is over,
the genetic manipulation process consisting of
two steps is carried out. In the first step, the
crossover operation that recombines the bits
(genes) of each two selected strings
(chromosomes) is executed. Various types of
crossover operators are found in the literature.
The single point and two points crossover
operations are illustrated in fig. 15.2 and 15.3
respectively. The crossover points of any
twochromosomes are selected randomly.
2 step of Genetic Algorithm
nd


The second step in the genetic manipulation
process is termed mutation, where the bits at
one or more randomly selected positions of the
chromosomes are altered. The mutation
process helps to overcome trapping at local
maxima. The offsprings produced by the
genetic manipulation process are the next
population to be evaluated.
Crossover in Genetic Algorithm
Mutation in Genetic Algorithm
Crossover Rate
In GA literature, the term cross over
rate is usually denoted as PC, the
probability of crossover. The
probability varies from 0 to 1. This is
calculated in GA by finding out the
ratio of the number of pairs to be
crossed to some fixed population.
Typically for a population size 30 to
200, cross over rates are ranged from
0.5 to 1.
Mutation Rate Pm
Mutation rate is the probability of
mutation which is used to calculate
number of bits to be muted. The
mutation operator preserves the
diversity among the population which
is also very important for the search.
Typically, the simple genetic
algorithm uses the population size of
30 to 200 with the mutation rates
varying from 0.001 to 0.5.
Sample Problem I

Example 15.1: The GA cycle is illustrated in this
example for maximizing a function f(x) = x2 in the
interval 0 ≤ x ≤ 31. In this example the fitness
function is f (x) itself. The larger is the functional
value, the better is the fitness of the string. In this
example, we start with 4 initial strings. The fitness
value of the strings and the percentage fitness of
the total are estimated in the 1st table
Solution by GA
Since fitness of the second string is large, we
select 2 copies of the second string and one
each for the first and fourth string in the mating
pool.The selection of the partners in the mating
pool is also done randomly.
Here in 2nd table , we selected partner of string
1 to be the 2-nd string and partner of 4-th string
to be the 2nd string.
Solution by GA……..
The crossover points for the first-second
and second-fourth strings have been
selected after o-th and 2-nd bit positions
respectively in 2nd table. The second
generation of the population without
mutation in the first generation is presented
in 3rd table
Initial population and their fitness
values

Table 1: Initial population and their fitness values
Mating pool strings and crossover
Table 2: Mating pool strings and crossover
Fitness value in second generation

Table 3: Fitness value in second generation


Sample problem

The function is defined as

f(X) = xsin(10Πx)+1.0

X=[-1,2]
Solution by GA

Representation: In this case a
binary vector (chromosome) has
been considered to represent the
real values of the variable x.

Let six places after the decimal
point is our required precision. The
domain of the variable x has a
length equal to three; the precision
requirement implies the [-1,2]
Solution by GA...

It should be divided into at least 3,000,000
equal-sized intervals. This means that 22
bits are required to represent one binary
vector (chromosome):
2,097,152=221<3000000<=222 =4,194,304
The mapping
Solution
The mapping from a binary string to a real
number x is performed in two steps.
1. Convert the binary string from base to base10
and set it equal to x1, and
2. Find a corresponding real number x=-1+[3/(2 22-
1)]* x1, where -1.0 be left boundary and 3 is the
length of the domain.
Solution …….
Of course the Chromosomes
(0000000000000000000000) and
(1111111111111111111111) represent
boundaries of domain i.e -1 and 2 respectively.
For example three chromosomes
V1=(1000101110110101000111)
V2=(0000001110000000010000)
V3=(1110000000111111000101)
Solution…….
correspond to the values x1=0.637197,
x2=-0.958973,x3=1.627888.
consequently the evaluation function as follows
eval(v1)=f(x1)=1.586345
eval(v2)=f(x2)=0.078878
eval(v3)=f(x3)=2.250650
Solution …….
Genetic operators: (mutation)
v31=(110000001111111000101).
Correspondingly, x31=1.630818 and
f(x31)=2.343555, an improvement over original
value of f(x3)=2.250650.
Crossover operator:
v2=(00000|01110000000010000)
v3=(11100|00000111111000101)
Solution …….
The offspring evaluate to
f(v21)=f(-0.998113)=0.940865
f(v31)=f(1.666028)=2.459245
For this particular problem, pop_size=50, Pc
=0.25, Pm=0.01
The best chromosome after 200 generations was
Vmax=(1111001101000100000101) which
corresponds to value xmax=1.850773
Traveling Salesman Problem

In the Traveling Salesman Problem (TSP)
location of all the cities is given and the
salesman’s task is to find the shortest route
connecting them all, with each city visited only
once and returns to city of origin. Since it is
NP complete problem, the method of finding
an optimal solution must search in a solution
space that expands exponentially with the
number of cities.
Steps of Genetic Algorithm

Step 1: Generate random population of n
Chromosome.

Step 2: Evaluate the fitness f(x) of each
Chromosome x in the population.

Step 3: Create a new population by repeating
the following.
Steps until the new population by repeating the
following.
Steps of Genetic Algorithm……
a): [Selection] select two parent
Chromosomes from a population according to
their fitness (the better fitness, the bigger
chance to be selected.)
b): [crossover] with a crossover probability
Crossover the parents to form new
offspring (children). If no Crossover was
performed, offspring is the exact copy of
parents.
Steps of Genetic Algorithm…..
c): [Mutation] with a mutation probability
mutate new offspring at each locus (position
in Chromosome).
d) [Accepting] place new offspring in the new
population.
Step 4: Use new generated population for a
further run of the algorithm.
Step 5: If the end condition is satisfied, then
stop and return the best. Solution in the
current population.
Go to step 2.
Fitness Measure
Fitness function fit(tk) (k=1,2,………pop_size) is
defined as
fit(fx) = 1/[(∑ci,ci+1)+ d(cn,c1)]
where d(ci,ci+1): travelling distance from city ci to
ci+1. If city ci is represented by pixel at point
P(x,y) and city ci+1 is represented by pixel at
point q(s,t) and d(ci,ci+1) is Euclidean distance
between two points P(x,y) and q(s,t) which is
defined as
d(ci,ci+1)=|(x-s)|+ |(y-t)| = √(x-s)2+(y-t)2
Greedy Crossover
Operation
Algortthm:
Step 1: Select randomlytwo parent
tours P1 and P2 to create first
offspring
Step 2: Start with a random city C.
Step 3: If the edge <C,Cright> or
<C,Cleft> is used in both P1 and
P2 then choose common edge.
Greedy Crossover
Operation….)
Step 4: Otherwise compare C’s right
side edge in each of P1 and P2.
Step 5: Choose shorter one if it will
not introduce a cycle.
Step 7: If longer one created
introduces a cycle, then extend the
tour by a carefully selected edge.
To generate second offspring, left
edges are compared instead of
right edges.
Sample Problem of TSP

1 2

6 3

5 4
Distance Table
1 0
2 10 0
3 17 10 0
4 20 17 10 0
5 17 20 17 10 0
6 10 17 20 17 10 0
1 2 3 4 5 6
Solution of TSP by GA
(Parent 1)P1: 1 3 4 2 5 6
(Parent 2)P2: 1 2 3 6 5 4

Offspring1: (if randomly starting from city 6)


6 5 4 2 3 1 -> 1 3 2 4 5 6
Offspring2: (if randomly starting from city 3)
3 2 1 6 5 4 -> 1 6 5 4 3 2
Application of Greedy Crossover
operation through GA
Assume that we have two parents : P1 and P2 .
To generate the first offspring, we randomly start
from citry 6. The two right sides are <6,1> in P1
and <6,5> in P2. The distance table shows that
<6,1> and <6,5> have exactly the same length.
In this situation , the edge in P1 would normally
be selected. However, in our example <6,5> is a
common edge between the two parents, so
<6,5> is choosen.
Application of Greedy Crossover
operation through GA…….
Next, from city 5, we have two right side edges
<5,6> and <5,4>. This time, we choose <5,4>
because the common edge <5,6> would create a
cycle.
From city 4, the edges are <4,2> and <4,1>: the
shorter one <4,2> is choosen. Then from city 2 ,
there are <2,5> and <2,3> : the shorter one <2,3>
is chosen,
Application of Greedy Crossover
operation through GA…….
Finally, we have only city 1 left, which has to be
the last one in the tour.
The second offspring is generated starting from
a different random city 3. This time, the left
edges are compared. Similarly, all of the best
edges in P1 and P2 have been inherited by
offspring 2.
Thank you!

You might also like