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

Genetic Algorithms

Uploaded by

221020
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Genetic Algorithms

Uploaded by

221020
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Genetic Algorithms

Genetic Algorithms
• A Genetic Algorithm is a population based heuristic technique
• Inspired from the biological reproduction system and used to solve
search and optimization problems

• There are other similar algorithms inspired from the real world, e.g:
• Ant colony optimization
• Particle Swarm optimization
• Bee algorithm
• …etc

Machine Learning
Genetic Algorithms
• Developed by John Holland over the course of the 1960s and 1970s to
simulate Darwinian evolution

• Finally popularized in 1989 by David Goldberg who was able to solve a


very difficult optimization problem

Machine Learning
Evolutionary Algorithms
Chromosome (string)

gene
Population
individual

Fitness based
Selection

Crossover
Mutation

Genetic Operators

Generation i Generation i+1

Machine Learning
Genetic Algorithms
Genetic Algorithm Components:
• Population of individuals
• Fitness Function
• Genetic Operators

Machine Learning
Flowchart

Machine Learning
Genetic Algorithms
Individuals:

gene

0 1 0 0 01 1 1 0 0 1 1 0 1 0 1 1 1 1 0 0 0 1 1

• Each individual represent a candidate solution


• String of ‘1’s and ‘0’ (binary representation)
• Could take any other form (tree, integers, etc)
• Needs to be decoded to have meaning: Genotype to
Phenotype

Machine Learning
Genetic Algorithms
Can be a binary representation

 Example: encoding 4 parameters


 Param1 value = 1000 = 8
 Param2 value = 1011 = 11
 Etc.,

Machine Learning
Genetic Algorithms
GAs search from a population of chromosomes

i Chromosome Xi
1 1000
2 0101
3 1001
4 0111
Two issues:
Population size
Initializing the population

Machine Learning
Genetic Algorithms
Fitness Function
• It is analogous to the environment an animal lives in
• Gives a numerical description of how fit the solution encoded in a
particular chromosome is.
• Penalty Functions
• Approximate Function evaluation
• Fitness function is analogous to heuristic function.

Machine Learning
Genetic Algorithms
Every generation, a fitness function plays the role of the environment,
rating chromosomes in terms of their ability to solve the problem.

i chromosome Xi f(Xi)=[decode(Xi)]2
1 1000 64
2 0101 25
3 1001 81
4 0111 49
Total = 219
Average = 54.75

Machine Learning
Genetic Algorithms :Selection
Selection decides which individuals go into the mating pool

Promotes the “better” solutions to the next generation,


while “poorer” solutions die off

“better” and “poorer” solutions are determined by the


fitness function

Can spin roulette wheels, hold tournaments, make ranked


lists … but the main idea is to give more chances to better
individuals

Machine Learning
Genetic Algorithms
i chromosome Xi f(Xi)=(Xi)2
1 1000 64
2 0101 25
3 1001 81
4 0111 49
Total = 219

The sum of the fitness values in generation 0 is 219


The best individual in generation 0 (1001) has a fitness of 81 which is
almost one third of the population (81/219)
Therefore, individual 1001 is given a probability of (81/219) of being
selected for each of the four positions in the mating pool

Machine Learning
Genetic operations
Novel solutions are produced using the genetic operators
crossover and mutation.

Crossover
Explores the search space

Mutation
Recaptures “lost” genes due to crossover

Machine Learning
Crossover
Mimics sexual reproduction
Two individuals (parents) are selected randomly from the
mating pool
Cross site selected at random
Offspring are created by swapping genes to right of cross site
Parent 1 Child 1

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

Crossover
Parent 2 Site (5) Child 2

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

Machine Learning
Mutation
Mutation occurs for a given percentage of the children
Mutation can flip, swap or alter the value of a gene

selected gene for mutation mutated gene (flipped)


Child 1 Child 1
0 0 1 0 1 0 0 0
0 0 1 0 0 0 0 0

Before mutation After mutation

Child 2 Child 2
No mutation
0 1 1 0 1 0 1 1 0 1 1 0 1 0 1 1

Machine Learning
Termination
 The GA continues to produce new
generations of individuals until some
termination criterion is satisfied.

 Possible termination criteria:


◼ Maximum number of generations
◼ Optimal solution is reached
◼ Convergence

Machine Learning
Example 1

Consider the problem of maximizing the function f(x) = x^2, where x is


an integer and can vary between 0 and 15.

Machine Learning
representation

X Binary Encoding

0 0000

1 0001

2 0010

. .

. .

13 1101

14 1110

15 1111

Machine Learning
Search Space Initial Population

0001 0000
0100
1101 1000 0101 1000 f(x)=64
1111
0011 1001 0101 f(x)=25
1110
1011 1001 f(x)=81
0111 0010
1010 0110 0111 f(x)=49
1100
Avg  54

1011
1101

1011 1011 1001 1001


flip xover
1101 0101 0111 0111
1000
New Population
Machine Learning Mating Pool
Search Space Current Population

0001 0000
0100
1101 1000 0101 1000 f(x)=64
1111
0011 1001 0101 f(x)=25
1110
1011 1001 f(x)=81
0111 0010
1010 0110 0111 f(x)=49
1100
Avg  54

1011
1101

1010 1010 1000 1001 1001


flip xover
1001 1001 1001 1000 0111
1000
New Population
Mating Pool
Machine Learning
Search Space Current Population

0001 0000
0100
1101 1000 0101 1 1 1 1 f(x)=225
1111
0011 1001 1 1 1 1 f(x)=225
1011 1110
1 1 0 1 f(x)=169
0101 0010
1010 1110 1 1 1 0 f(x)=196
1100
Avg  203

The GA continues to produce new


generations of individuals until some
termination criterion is satisfied

Machine Learning
A Real Example: TSP

The Traveling Salesman Problem:

Find a tour of a given set of cities so that


• each city is visited only once
• the total distance traveled is minimized

Machine Learning
Representation

Representation is an ordered list of city


numbers known as an order-based GA.

1) London 3) Dunedin 5) Beijing 7) Tokyo


2) Venice 4) Singapore 6) Phoenix 8) Victoria

CityList1 (3 5 7 2 1 6 4 8)
CityList2 (2 5 7 6 8 1 3 4)

Machine Learning
Crossover

Crossover combines inversion and


recombination:

Parent1 (3 5 7 2 1 6 4 8)
Parent2 (2 5 7 6 8 1 3 4)

Child (5 8 7 2 1 6 3 4)

This operator is called the Order1 crossover.

Machine Learning
Mutation
Mutation involves reordering of the list:

Before: (5 8 7 2 1 6 3 4)

After: (5 8 6 2 1 7 3 4)

Machine Learning
TSP Example: 30 Cities

120

100

80

y 60

40

20

0
0 10 20 30 40 50 60 70 80 90 100
x

Machine Learning
Solution i (Distance = 941)

TSP30 (Performance = 941)

120

100

80

y 60

40

20

0
0 10 20 30 40 50 60 70 80 90 100
x

Machine Learning
Solution j(Distance = 800)
44
62 TSP30 (Performance = 800)
69
67 120
78
64 100
62
54
80
42
50
40 y 60
40
38 40
21
35
67 20

60
60 0
40 0 10 20 30 40 50 60 70 80 90 100
42 x
50
99

Machine Learning
Solution k(Distance = 652)
TSP30 (Performance = 652)

120

100

80

y 60

40

20

0
0 10 20 30 40 50 60 70 80 90 100
x

Machine Learning
Best Solution (Distance = 420)
42
38 TSP30 Solution (Performance = 420)
35
26 120
21
35 100
32
7
80
38
46
44 y 60
58
60 40
69
76
20
78
71
69 0
67 0 10 20 30 40 50 60 70 80 90 100

62 x
84
94

Machine Learning
Genetic Algorithms
0/1 Knapsack problem
Genetic algorithms

• GA questions
• What is the chromosome// vector of city ids in TSP
• Cross-over// no redundancy
• Mutation// swap
• Fitness function // find the distance between each two cities and return the
total distance
• Termination condition// number iterations. convergence
Examples of problems that can be solved
using GA

• TSP (done)
• 8 queen problem
• 0/1 knapsack
• Graph colouring problem
• Find a minima in a function (optimization)
• Sorting an array
0/1 knapsack:

• There is a bag with capacity C


• There are a set of N items each item has a volume and value$
• We need to put M items in the bag such that:
• M<=N
• The total value of the M items is maximum
• The total volume of the M items <= C
• What is the chromosome (what is a solution)

Set of items id of length N eg=6

[I1, I2,I3,I6]
[I3,I5,I7]

Vector of bits of length N


[0,1,1,0,1,1]

• Fitness function: distance between each two cities in the chromosome


• Termination condition: convergence. iterations
• Crossover
P1=[0,1,1,0,1,1]
P2=[1,0,1,0,0,1]

C1=[0,1,1,0,0,1]
C2=[1,0,1,0,1,1]
• Mutation
C1=[0,1,1,0,0,1] ➔ C1’[0,1,1,1,0,1] randomly flip one bit
finess

• Higher fitness values are better

• [0,1,1,0,1,1] ➔ fitness function → eg 17


0/1 knapsack
Float fitness(int chromosome [N])
{
Float totalValue=0; totalVolume=0
For (i=0;i<N;i++)
{ totalValue=totalValue+ (chromosome[i] * values[i])
totalVolume=totalVolume+ (chromosome[i] * volume[i])
} return totalValue/(totalVolme+1)
}
• Termination?

• Convergence
8-Queen problem
representation

• [1, 6, 2, 5, 7, 4, 8, 3].
Crossover and mutation

You might also like