0% found this document useful (0 votes)
11 views48 pages

SC - GA - 7th Sem

The document discusses Genetic Algorithms (GAs) as part of Evolutionary Computation, which is inspired by natural selection and aims to solve complex problems through optimization. It outlines the basic operations of GAs, including population initialization, selection, crossover, and mutation, and explains how these processes simulate natural evolution to improve solution quality iteratively. The lecture also highlights the advantages of using GAs, particularly in scenarios where traditional methods are insufficient or when dealing with large search spaces.

Uploaded by

roshangupta0904
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)
11 views48 pages

SC - GA - 7th Sem

The document discusses Genetic Algorithms (GAs) as part of Evolutionary Computation, which is inspired by natural selection and aims to solve complex problems through optimization. It outlines the basic operations of GAs, including population initialization, selection, crossover, and mutation, and explains how these processes simulate natural evolution to improve solution quality iteratively. The lecture also highlights the advantages of using GAs, particularly in scenarios where traditional methods are insufficient or when dealing with large search spaces.

Uploaded by

roshangupta0904
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/ 48

Lecture 27: Genetic Algorithm (EC1710)

Dr. Rashmi Sinha


Associate Professor,
Department of Electronics & Communication Engineering

National Institute of Technology, Jamshedpur,


Jharkhand , India

21-11-2023 1
Introduction to Evolutionary Computation
• Evolutionary Computation is the field of study
devoted to the design, development, and analysis
as problem solvers for solving complex problems
having multiple solutions based on natural
selection (simulated evolution).
• A nature inspired approach to optimization.
Process of getting the most out of something to get a better result. During this
process we are in search for best optimal solution. We are not finding a solution as
in other algorithms where input is given and output solution is obtained. Here, we
already have a set of solution but we have to get best solution out of it. During this
process, we are in search for best or optimal solution
• Abstraction of Real Biological Evolution.
• Evolution has proven to be a powerful search
process
Introduction to Evolutionary Computation
• Inspired by the Notion
• Natural Evolution: Survival of fittest from
Darwinian Evolution
• Mendel’s Modern genetics

IDEA: Adaptation is Intelligence


Can evolution be intelligent?
 Intelligence can be defined as the capability of a
system to adapt its behavior to ever-changing
environment.
 Evolutionary computation simulates evolution on a
computer.
 The result of such a simulation is a
series of optimization algorithms, usually based on
a simple set of rules.
 Optimization iteratively improves the quality of
solutions until an optimal, or at least feasible, solution
is found.

11/21/2023 4
 The behavior of an individual organism is an
inductive inference about some yet unknown
aspects of its environment.

 The evolutionary approach is based on


computational models of natural selection and
genetics.

 We call them evolutionary computation, an umbrella


term that combines genetic algorithms, evolution
strategies and genetic programming.

11/21/2023 5
How is a population with increasing
fitness generated?
 Let us consider a population of rabbits. Some
rabbits are faster than others, and we may say that
these rabbits possess superior fitness, because they
have a greater chance of avoiding foxes, surviving
and then breeding.

 If two parents have superior fitness, there is a good


chance that a combination of their genes will
produce an offspring with even higher fitness.
Over time the entire population of rabbits becomes
faster to meet their environmental challenges in the
face of foxes.
11/21/2023 6
Simulation of natural evolution

 All methods of evolutionary computation simulate


natural evolution by creating a population of
individuals, evaluating their fitness, generating a
new population through genetic operations, and
repeating this process a number of times.

 We will start with Genetic Algorithms (GAs) as


most of the other evolutionary algorithms can be
viewed as variations of genetic algorithms.

11/21/2023 7
Introduction to Evolutionary Computation
Basic operations

• Population initialization is done by randomly generating a


population of candidate solutions (CSs).
• Selection is the mechanism by which the “parents” are
chosen for producing offspring to be passed into next
generation. Selection tends to pick best population
elements as parents.
• Crossover takes parent-pairs from selection step and
creates offspring.
• Mutation makes “slight” random modifications to some or
all of the offspring in next generation
New Population !
Variations of Evolutionary Algorithms

 Genetic Algorithms (GA)


• Evolution Strategies (ES)
• Evolutionary Programming
(EP)
• Genetic Programming (GP)
• Etc.
Why and When should we use Genetic Algorithms?
When?
• When other simpler methods are not good enough.
• When we don’t have much knowledge about the problem
that we are trying to solve.
• … and when the search space is very large (making
complete enumeration unfeasible).

Why?
• Easy to code
• Gives many solutions (can
avoid local extrema)
• Can be parallelized)
Genetic Algorithms
 In the early 1970s, John Holland introduced the
concept of genetic algorithms.
 His aim was to make computers do what nature does.
Holland was concerned with algorithms that
manipulate strings of binary digits.

 Each artificial “chromosomes” consists of a number of


“genes”, and each gene is represented by 0 or 1:

1 0 1 1 0 1 0 0 0 0 0 1 0 1 0 1

11/21/2023 11
 The GA uses a measure of fitness of individual
chromosomes to carry out reproduction. As
reproduction takes place, the crossover operator
exchanges parts of two single chromosomes, and the
mutation operator changes the gene value in
some randomly chosen location of the chromosome.

11/21/2023 12
Basic genetic algorithms
Step 1: Represent the problem variable domain as
a chromosome of a fixed length, choose the size
of a chromosome population N, the crossover
probability pc and the mutation probability pm.

Step 2: Define a fitness function to measure the


performance, or fitness, of an individual
chromosome in the problem domain. The fitness
function establishes the basis for selecting
chromosomes that will be mated during
reproduction.
11/21/2023 13
Step 3: Randomly generate an initial population of
chromosomes of size N:
X1, X2 , . . . , XN.
Step 4: Calculate the fitness of each individual
chromosome:
f (X1), f (X2), . . . , f (XN)
Step 5: Select a pair of chromosomes for mating
from the current population. Parent
chromosomes are selected with a probability
related to their fitness.
Step 6: Create a pair of offspring chromosomes by
applying the genetic operators - crossover and
mutation.
11/21/2023 14
Step 7: Place the created offspring chromosomes
in the new population.
Step 8: Repeat Step 5 until the size of the new
chromosome population becomes equal to the
size of the initial population, N.
Step 9: Replace the initial (parent) chromosome
population with the new (offspring) population.
Step 10: Go to Step 4, and repeat the process until the
termination criterion is satisfied.

11/21/2023 15
Genetic algorithms
 GA represents an iterative process. Each iteration is
called a generation. A typical number of generations
for a simple GA can range from 50 to over 500. The
entire set of generations is called a run.
 Because GAs use a stochastic search method, the
fitness of a population may remain stable for a number
of generations before a superior chromosome appears.
 A common practice is to terminate a GA after a
specified number of generations and then examine the
best chromosomes in the population. If no satisfactory
solution is found, the GA is restarted.

11/21/2023 Intelligent Systems and Soft Computing 16


Genetic algorithms: case study
 A simple example will help us to understand how
a GA works. Let us find the maximum value of
the function (15x - x2) where parameter x varies
between 0 and 15. For simplicity, we may
assume that x takes only integer values. Thus,
chromosomes can be built with only four genes:

Integer Binary code Integer Binary code Integer Binary code


1 0001 6 0110 11 1011
2 0010 7 0111 12 1100
3 0011 8 1000 13 1101
4 0100 9 1001 14 1110
5 0101 10 1010 15 1111

11/21/2023 17
Suppose that the size of the chromosome population
N is 6, the crossover probability pc equals 0.7, and
the mutation probability pm equals 0.001. The
fitness function in our example is defined by

f(x) = 15 x – x2

11/21/2023 Intelligent Systems and Soft Computing 18


The fitness function and chromosome locations
Chromosome Chromosome Decoded Chromosome Fitness
label string integer fitness ratio, %
X1 1 1 0 0 12 36 16.5
X2 0 1 0 0 4 44 20.2
X3 0 0 0 1 1 14 6.4
X4 1 1 1 0 14 14 6.4
X5 0 1 1 1 7 56 25.7
X6 1 0 0 1 9 54 24.8
60 60
f(x)
50 50

40 40

30 30

20 20

10 10

0 0
0 5 10 15 0 5 10 15
x x
(a) Chromosome initial locations. (b) Chromosome final locations.

11/21/2023 19
 In natural selection, only the fittest species can
survive, breed, and thereby pass their genes on to
the next generation. GAs use a similar approach,
but unlike nature, the size of the chromosome
population remains unchanged from one
generation to the next.
 The last column in Table shows the ratio of the
individual chromosome’s fitness to the population’s
total fitness. This ratio determines the chromosome’s
chance of being selected for mating. The
chromosome’s average fitness improves from one
generation to the next.

11/21/2023 20
Roulette wheel selection
The most commonly used chromosome selection
techniques is the roulette wheel selection.

100 0
X1: 16.5%
X2: 20.2%
75.2 X3: 6.4%
X4: 6.4%
X5: 25.3%
36.7 X6: 24.8%
49.5 43.1

11/21/2023 21
Crossover operator
 In our example, we have an initial population of 6
chromosomes. Thus, to establish the same
population in the next generation, the roulette
wheel would be spun six times.

 Once a pair of parent chromosomes is selected, the


crossover operator is applied.

11/21/2023 22
 First, the crossover operator randomly chooses a
crossover point where two parent chromosomes
“break”, and then exchanges the chromosome
parts after that point. As a result, two new
offspring are created.

 If a pair of chromosomes does not cross over, then the


chromosome cloning takes place, and the offspring are
created as exact copies of each parent.

11/21/2023 23
Crossover

X6i 1 0 00 1 0 1 00 00 X2i

X1i 10 11 00 00 0 11 11 11 X5i

X2i 0 1 0 0 0 1 1 1 X5i

11/21/2023 24
Mutation operator
 Mutation represents a change in the gene.

 Mutation is a background operator. Its role is to


provide a guarantee that the search algorithm is not
trapped on a local optimum.
 The mutation operator flips a randomly selected gene
in a chromosome.

 The mutation probability is quite small in nature, and is


kept low for GAs, typically in the range between 0.001
and 0.01.

11/21/2023 25
Mutation

X6'i 1 0 0 0

X2'i 0 1 0 1
0

X1'i 1
0 1 1 1 1 1 X1"i

X5'i 0 1 0
1 0
1

X2i 0 1 0 0 1 0 X2"i

X5i 0 1 1 1

11/21/2023 26
The genetic algorithm cycle

C rossover
Generation i
X1i 1 1 0 0 f = 36 X6i 1 0 00 1 0 1 00 00 X2i
X2i 0 1 0 0 f = 44
X3i 0 0 0 1 f = 14
X4i 1 1 1 0 f = 14 X1i 0 11 00 00
1 0 11 11 11 X5i
X5i 0 1 1 1 f = 56
X6i 1 0 0 1 f = 54
X2i 0 1 0 0 0 1 1 1 X5i
Generation (i + 1)
X1i+1 1 0 0 0 f = 56 Mutation
X2i+1 0 1 0 1 f = 50 X6'i 1 0 0 0
X3i+1 1 0 1 1 f = 44
X2'i 0 1 0 1
0
X4i+1 0 1 0 0 f = 44
X1'i 0
1 1 1 1 1 1 X1"i
X5i+1 0 1 1 0 f = 54
X6i+1 0 1 1 1 f = 56 X5'i 0 1 0
1 0
1

X2i 0 1 0 0 1 0 X2"i

X5i 0 1 1 1

11/21/2023 27
Genetic algorithms: case study
 Suppose it is desired to find the maximum of the
“peak” function of two variables:
2  x 2 ( y 1) 2  x2  y 2
f ( x, y)  (1  x) e  (x  x  y ) e
3 3

where parameters x and y vary between -3 and 3.


 The first step is to represent the problem variables
as a chromosome - parameters x and y as a
concatenated binary string:

1 0 0 0 1 0 1 0 0 0 1 1 1 0 1 1

x y
11/21/2023 28
 We also choose the size of the chromosome
population, for instance 6, and randomly generate
an initial population.
 The next step is to calculate the fitness of each
chromosome. This is done in two stages.
 First, a chromosome, that is a string of 16 bits, is
partitioned into two 8-bit strings:
1 0 0 0 1 0 1 0 and 0 0 1 1 1 0 1 1
 Then these strings are converted from binary
(base 2) to decimal (base 10):
(10001010) 2  1 27  0  26  0  2 5  0  2 4  1  23  0  2 2  1  21  0  2 0  (138)10
and
(00111011) 2  0  2 7  0  2 6  1  25  1  2 4  1  2 3  0  2 2  1 21  1  2 0  (59)10
11/21/2023 29
 Now the range of integers that can be handled by
8-bits, that is the range from 0 to (28 - 1), is
mapped to the actual range of parameters x and y,
that is the range from -3 to 3:

6
 0.0235294
256  1

 To obtain the actual values of x and y, we multiply


their decimal values by 0.0235294 and subtract 3
from the results:
x  (138)10  0.0235294 3  0.2470588
and
y  (59)10  0.0235294  3  1.6117647
11/21/2023 30
 Using decoded values of x and y as inputs in the
mathematical function, the GA calculates the
fitness of each chromosome.

 To find the maximum of the “peak” function, we will


use crossover with the probability equal to 0.7 and
mutation with the probability equal to 0.001. As we
mentioned earlier, a common practice in GAs is to
specify the number of generations. Suppose the desired
number of generations is 100. That is, the GA will
create 100 generations of 6
chromosomes before stopping.

11/21/2023 31
Chromosome locations on the surface of the
“peak” function: initial population

11/21/2023 32
Chromosome locations on the surface of the
“peak” function: first generation

11/21/2023 33
Chromosome locations on the surface of the
“peak” function: local maximum

11/21/2023 34
Chromosome locations on the surface of the
“peak” function: global maximum

11/21/2023 35
Performance graphs for 100 generations of 6
chromosomes: local maximum
pc = 0.7, pm = 0.001
0. 7

0. 6

0. 5

0. 4
Fitness

0. 3

0. 2

0. 1

Best
0 Average

-0.1
0 10 20 30 40 50 60 70 80 90 100
Generations
11/21/2023 36
Performance graphs for 100 generations of 6
chromosomes: global maximum
pc = 0.7, pm = 0.01
1.8

1.6

1.4

1.2
Fitness

1.0

0.8

0.6

Best
0.4 Average

0.2
0 10 20 30 40 50 60 70 80 90 100
Generations
11/21/2023 37
Performance graphs for 20 generations of
60 chromosomes
pc = 0.7, pm = 0.001
1.8

1.6

1.4

1. 2
Fitness

1.0

0.8

0.6

Best
0.4 Average

0.2
0 2 46 8 10 12 14 16 18 20
Generations
11/21/2023 38
String x Initial pop. Actual
No. Value (randomly generated) Count
from
Roulette
Wheel
1 13 01101 168 0.16 0.64 1

2 24 11000 576 0.54 2.17 2

3 8 01000 64 0.06 0.24 0

4 16 10000 256 0.24 0.96 1


Mating Pool Mate Cross-Over New x Value
after (randoml Site Pop.
Reproduction y (randomly
selected) selected)

01101 2 4 01100 12 144


11000 1 4 11001 25 625
11000 4 2 11000 24 576
10000 3 2 10000 16 256

Mutation done on 3rd


string as it has least
selection probability.
Steps in the GA development

1. Specify the problem, define constraints and optimum


criteria;
2. Represent the problem domain as a chromosome;
3. Define a fitness function to evaluate the chromosome
performance;
4. Construct the genetic operators;
5. Run the GA and tune its parameters.

11/21/2023 42
Applications Of GA
• Optimization − Genetic Algorithms are most commonly used in optimization
problems wherein we have to maximize or minimize a given objective function
value under a given set of constraints.
• Neural Networks − GAs are also used to train neural networks, particularly
recurrent neural networks.
• Image Processing − GAs are used for various digital image processing (DIP) tasks
as well like dense pixel matching.
• Vehicle routing problems − With multiple soft time windows, multiple depots and
a heterogeneous fleet.
• Machine Learning − as already discussed, genetics based machine learning
(GBML) is a niche area in machine learning.
• DNA Analysis − GAs have been used to determine the structure of DNA using
spectrometric data about the sample.
• Multimodal Optimization − GAs are obviously very good approaches for
multimodal optimization in which we have to find multiple optimum solutions.
• Travelling salesman problem and its applications − GAs have been used to solve
the TSP, which is a well-known combinatorial problem using novel crossover and
packing strategies.
11/21/2023
Exercise (Knapsack Problem)
There are 4 items, each item is associated with some weight (W) and value at
item (v)
ITEM WEIGHT (Kg) VALUE ($)
A 5 12
B 3 5
C 7 10
D 2 7

There is a knapsack (k) with limited capacity that can hold atmost 12Kg.
Which item should be kept in the Knapsack so as it will maximize Knapsack
value without breaking the Knapsack. Using stepwise algorithm of GA, solve
the problem. Obtain the new population (solution) after single mutation
operation.
(Knapsack Problem)
(Knapsack Problem)
Any
Questions
?????
Dr. Rashmi Sinha, NIT Jamshedpur

48

You might also like