You are on page 1of 32

General remarks and comparison with other method

Genetic algorithms step-by-step


Advanced topics
Summary

Genetic Algorithms
A step by step tutorial

Max Moorkamp

Dublin Institute for Advanced Studies

Barcelona, 29th November 2005

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step
Advanced topics
Summary

Outline

1 General remarks and comparison with other method

2 Genetic algorithms step-by-step


A hand-calculated example
Some experiments on simple problems

3 Advanced topics
Multi-objective optimization
A geophysical example

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step
Advanced topics
Summary

Comparison of different optimization methods

Method Func. Eval. Class of Problems


Analytical Gradient 1 One special problem
Numerical Gradient 10 − 102 differentiable, lim. no. of min.
Genetic Algorithm 103 − 104 limited parameter range
Monte Carlo 105 none
Grid Search 108 none

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step
Advanced topics
Summary

Advantages and Disadvantages of Genetic Algorithm

Advantages:
No derivatives needed
Easy to parallelize
Can escape local minima
Works on a wide range of problems
Disadvantages:
Need much more function evaluations than linearized methods
No guaranteed convergence even to local minimum
Have to discretize parameter space

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step
Advanced topics
Summary

General program flow of a genetic algorithm

Determine parameter encoding

Generate random population

The general structure is the same for all


Calculate probabilities
from Objective function
genetic algorithms
Individual steps can be done in a variety
Select models into new population
of ways
N Iterations

We will only discuss the most basic


Crossover
operators

Mutation

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

Outline

1 General remarks and comparison with other method

2 Genetic algorithms step-by-step


A hand-calculated example
Some experiments on simple problems

3 Advanced topics
Multi-objective optimization
A geophysical example

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

Encoding the parameters

Model parameters are encoded into a binary string

x = (1.2, 3.4, . . . , 100) ← (0101110011 . . . 100011)

For each paramter a minimum value, stepsize and encoding length


has to be specified. For example two parameters x1 , x2 :

x1 ∈ [−1 . . . 1] and x2 ∈ [0 . . . 3.1]

5-bit x1 x2 10-bit x1 x2
Basevalue -1 0 Basevalue -1 0
Stepsize 0.0625 0.1 Stepsize 0.0002 0.003

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

Generating a population

At the start a population of N random models is generated.

p1 = (0101110011) → (01011) = −1 + 26 ∗ 0.0625 = 0.625


(10011) = 0 + 25 ∗ 0.1 = 2.5
p2 = (1111010110) → (11110) = −1 + 15 ∗ 0.0625 = −0.0625
(10110) = 0 + 13 ∗ 0.1 = 1.3
p3 = (1001010001) → (10010) = −1 + 9 ∗ 0.0625 = −0.4375
(10001) = 0 + 17 ∗ 0.1 = 1.7
.. ..
. .
pN = (0110100001) → (01101) = −1 + 22 ∗ 0.0625 = 0.375
(00001) = 0 + 16 ∗ 0.1 = 1.6

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

Calculating the objective function values

For each member of the population calculate the value of the


objective function:
O(x1 , x2 ) = x12 + x22

p1 → O(0.625, 2.5) = 6.64


p2 → O(−0.0625, 1.3) = 1.69
p3 → O(−0.4375, 1.7) = 3.08
.. ..
. .
pN → O(0.375, 1.6) = 2.70

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

Assigning probabilities
Based on the objective function value, a probability is assigned to
make it into the next generation P(pj ) = f (O(xi )). In our example
1
P(pj ) = P (max {O(xi )} − O(xi ))
i max {O(xi )} − O(xi )
P
In our case: i max {O(xi )} − O(xi ) = 12.45 this gives:
6.64 − 6.64
P(p1 ) = = 0.
12.45
6.64 − 1.69
P(p2 ) = = 0.40
12.45
6.64 − 3.08
P(p3 ) = = 0.28
12.45
6.64 − 2.70
P(p4 ) = = 0.32
12.45
Now we can generate a new population of the same size.
Max Moorkamp Genetic Algorithms
General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

Selection

Each model’s chance to mate is determined by their probability.


The offsprings undergo two steps: Crossover and Mutation

Mating Cand. Old Pos. Prob.


m1 p2 0.4
m2 p3 0.28
m3 p4 0.32
m4 p2 0.4

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

Crossover

With probability Pc two members of the population exchange their


genes after a randomly chosen point.

(000111) (000001)
3
−→
(110001) (110111)

Typically 0.2 ≤ Pc ≤ 0.7.

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

Mutation

With a probability Pm one bit changes its value.

(000111) → (010111)

Typically Pm is chosen so that 80-90 percent of the population do


not mutate.

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

Combined effect on the reproduction operators


Result of selection, crossover and mutation:

New Father Mother Cross. Mut. Result O(x)


p1 m1 m2 (1111010110) 1.69
p2 m1 m2 (1001010001) 2.7
p3 m3 m4 4 (0110|100001) → (0110010110) 2.08
p4 m3 m4 4 3 (1111|010110) → (1101100001) 3.03

The resulting genetic strings make up the new population.

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

Outline

1 General remarks and comparison with other method

2 Genetic algorithms step-by-step


A hand-calculated example
Some experiments on simple problems

3 Advanced topics
Multi-objective optimization
A geophysical example

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

A slightly more difficult problem

O(x) = (x − 6)(x − 2)(x + 4)(x + 6)


has two minima O(−5.11) = −78.03 and O(4.41) = −335.475.
We search x ∈ [−10 . . . 20] with 15-bit and a stepsize of 0.001.
N = 20 and we calculate 6 generations i.e 120 forward calculations.
The objective function ↔ probability scaling is

1 1000 g < 3
P(pi ) = P exp(−O(x)/T ) where T =
100 g ≥ 3

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

Statistics of GA performance

Fitness vs. Generation

4.5
400

Best member
Mean performance

4.4
200 25799.3 starting value
Function Value

4.3
0

GAResult

4.2
-200

4.1
-400

0 1 2 3 4
Generation 0 500 1000 1500 2000

Index

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

Performance of the GA
Generation 0 Generation 1 Generation 2
150000

6000

6000
+
100000

4000

4000
y

y
+ +
+
50000

2000

2000
+ +
+ + +
++ + + ++++ 0
++ + ++ +

0
0

−10 −5 0 5 10 15 20 −10 −5 0 5 10 −10 −5 0 5 10

x x x

Generation 3 Generation 4 Generation 5


6000

6000

6000
4000

4000

4000
y

y
2000

2000

2000
+
++ + ++
++ + ++ ++++ +
0

0
−10 −5 0 5 10 −10 −5 0 5 10 −10 −5 0 5 10

x x x

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

Looking at some statistics

Histogram of x

700
2000 runs, N=20, g=6

600
4

500
2

400
Frequency
0

300
-2

200
100
-4

-6
0

0 500 1000 1500 2000


−6 −4 −2 0 2 4 6

Min. 1st Qu. Median Mean 3rd Qu. Max.


-5.671 4.255 4.383 4.215 4.465 6.389

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

A hard problem

O(x) = |floor (x)| + 0.5 (1. − (|x| − |floor (x)|))


Histogram of result of 2000 runs
Objective function
N = 20 6 generations T = 1000
Histogram of ga
20

150
15

100
y

10

Frequency
5

50
0

−10 −5 0 5 10 15 20
0

x
−2 0 2 4

ga

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step A hand-calculated example
Advanced topics Some experiments on simple problems
Summary

Statistics for the hard problem


Statistical results for different GA-parameters

Parms Min. 1st Qu. Median Mean 3rd Qu. Max.


20/6/1000 -2.930 0.221 0.580 0.636 0.920 4.405
30/10/1000 -0.9550 0.2467 0.5070 0.5340 0.7930 2.9590
20/6/10 -3.4410 0.2357 0.5550 0.6158 0.8350 4.0990
30/10/10 -1.0000 0.2350 0.4990 0.5103 0.7552 2.9590

The algorithm fails to find the exact minimum, but identifies the
general region
More iterations do not improve the mean/median performance, but
decrease diversity in the population
In this case the parameter T has no big influence on the result

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step Multi-objective optimization
Advanced topics A geophysical example
Summary

Outline

1 General remarks and comparison with other method

2 Genetic algorithms step-by-step


A hand-calculated example
Some experiments on simple problems

3 Advanced topics
Multi-objective optimization
A geophysical example

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step Multi-objective optimization
Advanced topics A geophysical example
Summary

Multi-objective optimization
Multi-objective optimization means that we try to minimize several
functions simultaneously.
 
O1 (xi )
 O2 (xi ) 
O(xi ) = 
 
.. 
 . 
OM (xi )

Usually the weighted norm of this vector is then minimized e.g


M
X
kW · O(xi )k2 = (Wj Oj (xi ))2
j=1

Genetic algorithms can also directly work on the vector without


weighting.
Max Moorkamp Genetic Algorithms
General remarks and comparison with other method
Genetic algorithms step-by-step Multi-objective optimization
Advanced topics A geophysical example
Summary

Pareto optimality and domination


Domination:
(x < p y ) ⇔ (∀ i : xi ≤ yi ∧ ∃ i : xi < yi )
If a member of the population is not dominated by any other it is
called nondominated or pareto-optimal.
Population fitness
Generation 1

Rank 1
Rank 2
Rank 3
0,06 Rank 4
Model Roughness

0,04

0,02

0
10 20 30 40
MT RMS

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step Multi-objective optimization
Advanced topics A geophysical example
Summary

Transforming rank into probability


(  α
r
1− rmax r < rmax
Pi =
0 r ≥ rmax
Fitness as a function of Rank
r max = 40
1

α=1
α=2
0,8 α=3
α=4
α = 0.5

0,6

0,4

0,2

0
0 10 20 30 40
Rank

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step Multi-objective optimization
Advanced topics A geophysical example
Summary

Designing an objective function


In linearized methods the objective function has to be scalar and is
a linear combination of the individual objective functions
Olin (di , si ) = WMT OMT (di , si ) + WRec ORec (di , si ) + WReg OReg (xj )
WMT , WRec and Wreg are usually constant, but could be variable
during inversion.
In contrast genetic algorithms can handle vector valued objective
functions without a need to weight the individual objective
functions.
 
OMT (di , f (xj ))
Ogen (di , xj ) =  ORec (di , f (xj )) 
OReg (xj )
n
1 X di − si 2
 
OMT (di , f (xj )) = with si ∈ [ρa , φ, Z ]
n ∆di
i=1
Max Moorkamp Genetic Algorithms
General remarks and comparison with other method
Genetic algorithms step-by-step Multi-objective optimization
Advanced topics A geophysical example
Summary

Outline

1 General remarks and comparison with other method

2 Genetic algorithms step-by-step


A hand-calculated example
Some experiments on simple problems

3 Advanced topics
Multi-objective optimization
A geophysical example

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step Multi-objective optimization
Advanced topics A geophysical example
Summary

Inversion of synthetic data


A simple two-layer model is inverted to test the inversion.
Input model Inversion result
8
8
7
7 S-Vel. S-Vel.
Density 6 Density
6 P-Vel. P-Vel.
5
5
4 4

3 3

2 2
0 10 20 30 40 50 60 70 0 20 40 60

140 140
120 120

Resistivity
100
Resistivity

100
80 80
60
60
40
40
20
20
0
0 10 20 30 40 50 60 70 0 20 40 60
Depth [km] Depth [km]
Data fit
Synthetic Input model
1000
ρa [Ωm]

100

10
0,01 1 100 10000
90
75
60
Phase

45
30
Input Data
15
Inversion
0
0,01 1 100 10000
Period [s]

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step Multi-objective optimization
Advanced topics A geophysical example
Summary

Evolution of misfit and roughness

Best models
No Regularization
1000
Iteration 0
Iteration 20
Max Misfit
Iteration 40 100 Min Misfit
Iteration 60 Median Misfit
Iteration 80 Min Roughness
Iteration 100 10 Median Roughness
Rec. Func. Misfit

0,1

0,01

0,0001 0,001

0,01 0,1 1 10 100 0,0001


MT Misfit 0 50 100 150 200

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step Multi-objective optimization
Advanced topics A geophysical example
Summary

Inversion of a more realistic model


Inversion fit
MT Data MT Models
1000

Original Model
1000 Inversion Result
ρa [Ωm]

100

ρa [Ωm]
10
0 1 100 10000
90
100
75
60
Phase

45
30
15
Original data
0 Inversion result 10
0,01 1 100 10000 1 10 100
Period [s] Depth [km]

Inversion Fit
Receiver function data Receiver function model comparison
5

Inversion result
4,5 Original model
0,04

3,5
Amplitude

Vs [km/s]

0,02
3

2,5

0 2

1,5
0 100 200 300 400 500 1 10 100
Time [s] Depth [km]

Max Moorkamp Genetic Algorithms


General remarks and comparison with other method
Genetic algorithms step-by-step
Advanced topics
Summary

Summary

Genetic algorithms can be applied to a wide range of problems


Their flexibility comes at the cost of more function evaluations
There are many variants of genetic algorithms available, each
with its own strengths and weaknesses

Max Moorkamp Genetic Algorithms


Appendix For Further Reading

For Further Reading

D. E. Goldberg
Genetic algorithms in search, optimization & machine learning
Addison Wesley, 1990.
K. Deb
A Fast and Elitist Multiobjective Genetic Algorithm: NSGA-II
IEEE Transactions on Evolutionary Computation, Vol. 6, No.
2, April 2002
Kanpur Genetic Algorithm Laboratory
NSGA-II and general information
http://www.iitk.ac.in/kangal/index.shtml
M. Wall
GALib programming library in C++
http://lancet.mit.edu/ga
Max Moorkamp Genetic Algorithms

You might also like