Genetic Algorithm
Genetic Algorithm
Genetic Algorithms
Also known as evolutionary algorithms,
genetic algorithms demonstrate self
organization and adaptation similar to the
way that the fittest biological organism
survive and reproduce.
A genetic algorithm is an iterative procedure
that represents its candidate solutions as
strings of genes called chromosomes.
Genetic Algorithms
Simple GA
initialize population;
evaluate population;
while TerminationCriteriaNotSatisfied
{
select parents for reproduction;
perform crossover and mutation; Every loop called
repair();
generation
evaluate population;
}
Concepts
Stochastic operators
Simulated Evolution
We
Representation of an individual
Fitness Function
Reproduction Method
Selection Criteria
Representing an Individual
An
Binary Encoding
Permutation Encoding
Used in ordering problems
Every chromosome is a string of numbers,
which represents number is a sequence.
Chrom A: 1 5 3 2 6 4 7 9 8
Chrom B: 8 5 7 7 2 3 1 4 9
Another Example
To
Value Encoding
Crossover Example
Parent
A 011011
Parent B 101100
Mate the parents by splitting each
number as shown between the second
and third digits (position is randomly
selected)
01*1011
10*1100
Crossover Example
011100
101011
Mutation is an arbitrary
change in a situation. Sometimes it
is used to prevent the algorithm
from getting stuck. The procedure
changes a 1 to a 0 to a 1 instead of
duplicating them. This change
occurs with a very low probability
(say 1 in 1000)
Genetic Algorithm
Operators
Mutation and Crossover
Parent 1
1 0 1 0 1 1 1
Parent 2
1 1 0 0 0 1 1
Child 1
1 0 1 0 0 1 1
Child 2
1 1 0 0 1 1 0
Mutation
Crossover Operators
Singlepoint
point crossover:
Parent A: 1 0 0 1 0| 1 1 1 0 1
Parent B: 0 1 0 1 1 |1 0 1 1 0
Child AB: 1 0 0 1 0 1 0 1 1 0
Child BA: 0 1 0 1 1 1 1 1 0 1
Twopoint
point crossover:
Parent A: 1 0 0|1 0 1 1| 1 0 1
Parent B: 0 1 0* 1 1 1 0* 1 1 0
Child AB: 1 0 0 1 1 1 0 1 0 1
Child BA: 0 1 0 1 0 1 1 1 1 0
Examples
Mutation:
More examples:
Crossover
Recipe :
Parents 1-3-3 & 3-2-3. Crossover
point after the first digit. Generate
two offspring: 3-3-3 and 1-2-3.
Can have one or two point crossover.
Crossover Permutation
Encoding
Single point crossover - one crossover point is selected,
till this point the permutation is copied from the first
parent, then the second parent is scanned and if the
number is not yet in the offspring it is added
(1 2 3 4 5| 6 7 8 9) + (4 5 3 6 8| 9 7 2 1) = (1 2 3 4 5 6 8 9 7)
Mutation
Order changing - two numbers are selected and
exchanged
(1 2 3 4 5 6 8 9 7) => (1 8 3 4 5 6 2 9 7)
Crossover Value
Encoding
Crossover
All crossovers from binary encoding can
be used
Mutation
Adding a small number (for real value encoding)
- to selected values is added (or subtracted) a
small number
(1.29 5.68 2.86 4.11 5.55) => (1.29 5.68 2.73 4.22
5.55)
Selection Criteria
Fitness Function
5.
6.
Generate
Initial
Solutions
Step 1
Test: is initial
solution good enough?
No
Step 2
Step 3
Step 4
Step 5
Select parents
to reproduce
Yes
Stop
Components of a GA
A problem definition as input, and
Encoding principles
(gene,
chromosome)
Initialization procedure
(creation)
Selection of parents
(reproduction)
Genetic operators
(mutation,
recombination)
Evaluation function
Representation (encoding)
Possible individuals encoding
Bit strings
1100)
Real numbers
89.2)
Permutations of element
E15)
Lists of rules
R23)
Program elements
programming)
(0101 ...
(43.2 -33.1 ... 0.0
(E11 E3 E7 ... E1
(R1 R2 R3 ... R22
(genetic
Representation (cont)
When choosing an encoding method rely on the
following key ideas
Initialization
Start with a population of randomly
generated individuals, or use
- A previously saved population
- A set of solutions provided by
a human expert
- A set of solutions provided by
another heuristic algorithm
Selection
1
i 1
2
(
1
)
,1 2
n
n 1
Tournament Selection
Tournament Selection:
randomlyselecttwoindividualsandtheone
withthehighestrankgoesonandreproduces
caresonlyabouttheonewiththehigherrank,
notthespreadbetweenthetwofitnessscores
putsanupperandlowerboundonthechances
thatanyindividualtoreproduceforthenext
generationequalto:(2s 2r + 1) / s2
s isthesizeofthepopulation
r istherankofthe"winning"individual
canbegeneralizedtoselectbestofnindividuals
Recombination (Crossover)
* Enables the evolutionary process
to move toward promising
regions of the search space
* Matches good parents sub-solutions
to construct better offspring
Mutation
Purpose: to simulate the effect of errors
that happen with low probability during
duplication
Results:
- Movement in the search space
- Restoration of lost information to the
population
Termination condition
Examples:
Example:
the MAXONE problem
Suppose we want to maximize the number of
ones in a string of l binary digits
Is it a trivial problem?
It may seem so because we know the answer in
advance
However, we can think of it as maximizing the
number of correct answers, each encoded by 1,
to l yes/no difficult questions`
Example (cont)
Example (initialization)
We toss a fair coin 60 times and get the
following initial population:
s1=1111010101
f (s1)=7
s2=0111000101
f (s2)=5
s3=1110110101
f (s3)=7
s4=0100010011
f (s4)=4
s5=1110111101
f (s5)=8
s6=0100110000
f (s6)=3
Example (selection1)
Next we apply fitness proportionate selection with the
roulette wheel method: Individual i will have a f (fi()i)
i
probability to be chosen
2
3
Areais
Proportional
tofitness
value
Example (selection2)
Suppose that, after performing selection, we get
the following population:
s1`=1111010101
(s1)
s2`=1110110101
(s3)
s3`=1110111101
(s5)
s4`=0111000101
(s2)
s5`=0100010011
(s4)
s6`=1110111101
(s5)
Example (crossover1)
Next we mate strings for crossover. For each
couple we decide according to crossover
probability (for instance 0.6) whether to actually
perform crossover or not
Suppose that we decide to actually perform
crossover only for couples (s1`, s2`) and (s5`, s6`).
For each couple, we randomly extract a
crossover point, for instance 2 for the first and 5
for the second
Example (crossover2)
Before crossover:
s1`=1111010101
s2`=1110110101
s5`=0100010011
s6`=1110111101
After crossover:
s1``=1110110101
s2``=1111010101
s5``=0100011101
s6``=1110110011
Example (mutation1)
The final step is to apply random mutation: for each bit that
we are to copy to the new population we allow a small
probability of error (for instance 0.1)
Before applying mutation:
s1``=1110110101
s2``=1111010101
s3``=1110111101
s4``=0111000101
s5``=0100011101
s6``=1110110011
Example (mutation2)
After applying mutation:
s1```=1110100101
f(s1```)=6
s2```=1111110100
f(s2```)=7
s3```=1110101111
f(s3```)=8
s4```=0111000101
f(s4```)=5
s5```=0100011101
f(s5```)=5
s6```=1110110001
f(s6```)=6
Example (end)
In one generation, the total population fitness
changed from 34 to 37, thus improved by ~9%
Example :
Suppose a Genetic Algorithm uses chromosomes of the form
x=abcdefgh with a fixed length of eight genes . Each gene
can be any digit between 0 and 9 . Let the fitness of
individual x be calculated as :
f(x) =(a+b)-(c+d)+(e+f)- ( g+h)
And let the initial population consist of four individuals x1, ...
,x4 with the following chromosomes :
X1 = 6 5 4 1 3 5 3 2
F(x1) =(6+5)-(4+1)+(3+5)-(3+2) = 9
X2 = 8 7 1 2 6 6 0 1
F(x2) = (8+7)-(1+2)+(6+6)-(0+1) = 23
X3 = 2 3 9 2 1 2 8 5
F(x3) = (2+3)-(9+2)+(1+2)-(8+5) = -16
X4= 4 1 8 5 2 0 9 4
F(x4) = (4+1)-(8+5)+(2+0)-(9+4) = -19
The arrangement is ( assume maximization )
X2
x1
x3
x4
( the fittest individual )
( least fit individual )
Individual
Fitnes Arrangement Assume
Representatio
s
s
maximization
n
X1
X2
X3
X4
65413532
X2(fittest individual)
87126601
23
X1(second fittest
individual)
23921285
-16
X3 (third fittest
individual)
41852094
-19
X2 = 8
1
X1 = 6
2
Offspring 1 = 8
2
Offspring 2 = 6
1
7
5
4Middle1
crossover
Offspring 3 = 6
2
Offspring 4 = 2
crossover
X1 = 6
2
X3 = 2
5
crossover
X2 = 8
X3 = 2
7
3
Offspring 5 = 8
Offspring 6 = 2
1
9
2
2
6
1
6
2
0
8
1
5
3
7
1
1
2
2
6
6
6
2
8
8
1
1
Offspring 1 = 8
2
F (Offspring 1)
Offspring 2 = 6
1
F (Offspring 2)
Offspring 3 = 6
2
F (Offspring 3)
Offspring 4 = 2
5
F (Offspring 4)
Offspring 5 = 8
1
F (Offspring 5)
=(8+7)-(1+2)+(3+5)-(3+2) = 15
5
4
1
6
=(6+5)-(4+1)+(6+6)-(0+1) = 17
5
9
2
1
=(6+5)-(9+2)+(1+2)-(3+2) = -2
3
4
1
3
=(2+3)-(4+1)+(3+5)-(8+5) = -5
3
1
2
6
=(8+3)-(1+2)+(6+6)-(8+1) = 11
String Representation
Fitness
Offspring 1
87123532
15
Offspring 2
65416601
17
Offspring 3
65921232
-2
Offspring 4
23413585
-5
Offspring 5
87921201
11
Offspring 6
23926601
Best : 17
Item:
1 2 3 4 5 6 7
Benefit: 5 8 3 2 7 9 4
Weight: 7 8 4 10 4 6 4
Knapsack holds a maximum of 22 pounds
Fill it to get the maximum benefit
Solutions take the form of a string of 1s and 0s
Solutions: Also known as strings of genes called Chromosomes
1. 0101010
2. 1100100
3. 0100111
Knapsack Example
Typically,
Knapsack Example
Solution 1
Item
Solutio 0
n
Benefit 5
Weight
10
Benefit 8 + 2 + 9 = 19
Weight 8 + 10 + 6 = 24
Knapsack Example
Solution 2
Item
Solutio 1
n
Benefit 5
Weight
10
Benefit 5 + 8 + 7 = 20
Weight 7 + 8 + 4 = 19
Knapsack Example
Solution 3
Item
Solutio 0
n
Benefit 5
Weight
10
Benefit 8 + 7 + 9 + 4 = 28
Weight 8 + 4 + 6 + 4 = 22
Knapsack Example
Knapsack Problem
To understand GA must work with the following
problem:
(Knap Sack Problem)
Thief wants to steal gold store.
Thief has a bag(the bag can hold a specific
weight).
Every piece of gold has a specific weight and price.
Thief wants to steal gold with high price but the
weight must equal or less than the weight that bag
can hold it.
If we gave every gold piece a specific number
1,2,3,,n(suggest n=8 in this example).
weight
price
1
5
100
2
3
30
3
10
250
4
6
150
5
5
50
6
5
75
7
4
50
8
4
50
1-Encoding (representation)
(gene,chromosome)
Chromosome could be:
Bit strings (101101010100).
Real numbers (43.1,45.2,66.3,11.0).
Permutation of elements (E11 E3 E7 E1 E15).
Integer Numbers (11,12,54,98,625,1).
Any data structures.
In knap sack problem can represent any solution
as chromosome by using bit string of length 8.
Ex:- 1 1 0 1 0 0 0 1
87654321
1(gene):this piece taken, 0(gene):this piece
untaken.
2-Initialize population
1
2
3
4
5
6
"10100001"
"01110011"
"11110000"
"00110011"
"01010101"
"00001111"
3-Evaluation of population.
chromosome
Fitnessfunction
Fitnessvalue
Chromosome
1
2
3
4
5
6
"10100001"
"01110011"
"11110000"
"00110011"
"01010101"
"00001111"
fitness value
225
305
225
225
450
530
4-Selection of new
parents(reproduction)
Methods of selection
Oldpopulation
1
2
3
4
5
6
chromosome
fitnes value
"10100001"
"01110011"
"11110000"
"00110011"
"01010101"
"00001111"
225
chromosome
Searchbest3
chromosomes
Newpopulation
1
2
3
4
5
6
305
225
225
450
530
fitnes value
1 "01110011"
2 "01010101"
3 "00001111"
305
chromosome
fitnes value
"01010101"
"00001111"
"00001111"
"00001111"
"01110011"
"01010101"
450
450
530
530
530
530
305
450
5-Crossover
wewilldoitjust
forfirsttwo
parents.
Pcross=0.6
1
2
3
4
5
6
chromosome
fitnes value
"01010101"
"00001111"
"00001111"
"00001111"
"01110011"
"01010101"
450
530
530
530
305
450
Selecttwoparent(1,2)
Generaterandomnumberbetween0.0-1.0(0.3)
0.3<=0.6(yes)applycrossover
generaterandomnumberbetween1-8(3)
old 010101 0 1
000011 1 1
new 010101 1 1
000011 0 1 swap tails
Dothisforeachpairinpopulation.
6-Mutation
Applied to each child individually after crossover
.
It alters some of genes in chromosome with
small probability .
Must specify Pmut(mutation probability that is
relatively small) therefore a few number of
chromosomes will be mutated.
In our example:
suppose Pmut =0.2
generate number between 0-1 (0.01)
0.01<=0.2(yes) apply mutation.
Generate number between 1-8(6)
0 1 0 1 0 1 0 1 => 0 1 1 1 0 1 0 1
Do this for each chromosome in population.
Termination Criteria
There exist three termination
condition type:
1-Time:in seconds, in minutes and may
be in hours according to the problem
that you have it.
2-Number of generations: in hundreds,
in thousands may be in millions
according to the problem you have it.
3-convergence: when 95% of populations
have the same fitness value we can say
the convergence started to appear and
the user can stop its genetic program to
take the result.
The knapsack
problem,
though simple,
has many
important
applications
including
determining
what items to
take on a space
ship mission.
Genetic Algorithms
Genetic Algorithms are a type of machine
learning for representing and solving
complex problems.
They provide a set of efficient, domainindependent search heuristics for a broad
spectrum of applications.
A genetic algorithm interprets information
that enables it to reject inferior solutions
and accumulate good ones, and thus it
learns about its universe.
Genetic Algorithm
Application Areas
Business Applications