1. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt1
Basics
EVOLUTIONARY COMPUTING
GENETIC ALGORITHMS
Modern Methods of Information Technology
Guest lecture
Prof. Dr. Dirk Reichardt
February 2005
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt2
Basics
Overview
Introduction & Motivation
Application of GAs
What is this lecture all about? What‘s the story behind the
Mystic name „genetic algorithms“? How could they be used
and why are they better than „normal“ algorithms?
Explanation of the way genetic algorithms are used to solve
computer science problems.
Genetic Algorithms Motivation and short introduction to the basic computation
principles of genetic algorithms.
Practice / Implementation Using genetic algoritms in practical experiments.
Programming excercises in C.
Extension: advanced GA Advanced algorithm variations for modeling problems with
the genetic algorithm paradigm.
2. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt3
Basics
Genetic Algorithms – Theory of Evolution
Nature as role model
Bildquelle: www.aboutdarwin.com
Principle of natural selection
Every small change will be kept by nature if it is
beneficial for survival.
The ability of an organism to survive depends on
its ability to adapt to changes in its environment.
Idea: Take this principle to computer science
Charles Darwin
1809-1882
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt4
Basics
A2A3
Nature as role model - Generations of Algorithms
A2
A1
A1
A1
A1
A1
A2
A3A3
A3A3
Algorithm pool
„Today“
A3A1
A1
A1
A1
A3A3
A3A3
Algorithm pool
„Tomorrow“
Application
to Problem
X
Application
to Problem
X
??
A2
A3
A3
A3
Good
solution
Bad
solution
Genetic Algorithms – Theory of Evolution
3. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt5
Basics
Nature as role model - Generations of Algorithms
Observation: In the new generation there are no new
algorithms but just a new selection of algorithms
which already existed before !
How is this in nature?
Individuals show small changes in their appearance
and features as compared to their parent generation.
that means: features of the parents are mixed
and modified!
Genetic Algorithms – Theory of Evolution
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt6
Basics
Nature als role model - Biology
Image source: www.biologie.uni-hamburg.de
dominant-rezessive inheritance
Mendel‘s Rules
Figure:
If there are many features
we get a lot of combinations
for the child-generation
Genetic Algorithms – Theory of Evolution
4. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt7
Basics
Nature as role model - How do we get an algorithm from that?
Algorithm descriptions need to be made of components
which can be recombined.
Algorithm descriptions need to be made of components
which can be recombined.
We need a rating for the currently existing algorithm in
order to tell good ones from bad ones. The good ones
shall be selected and recombined to build the next
generation.
We need a rating for the currently existing algorithm in
order to tell good ones from bad ones. The good ones
shall be selected and recombined to build the next
generation.
Every successor generation
should now be better than its
parents!
Why would we write algorithms in that way?
The domain of application for genetic algorithms is
a set of complex problems for which there are no efficient
algorithmic solutions available.
Genetic Algorithms – Theory of Evolution
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt8
Basics
Genetic Algorithms – Theory of Evolution
Application of
genetic algorithms
Source:
Spiegel Online
www.spiegel.de
vom 22.6.2004
An example:
Formula 1 – race cars
5. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt9
Basics
The chromosome theory
Based upon a theory of Boveri und Sutton (early 20th Century)
Chromosome Carrier of the genetic information
Build the core of a cell of an organism
Diploid
Chromosomes occur as pairs (diploid)
Number and length are variing
Number of chromosomes
is characteristic for the
breed
Human : 46
Carp : 48
Potatoe: 104
Human : 46
Carp : 48
Potatoe: 104
Each chromosome consists of a number of genes
Genetic Algorithms – Theory of Evolution
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt10
Basics
Die chromosome theory
Inheritance
A B
1 2 1 2
split split
A1B1 A1B2 A2B1 A2B2
P-Generation
F1-Generation
Recombination
by
chromosomes
Recombination
by
chromosomes
Genetic Algorithms – Theory of Evolution
6. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt11
Basics
Die chromosome theory
Gene-Maps Genes are associated to a
Chromosome in a fixed manner
Gene A
Gene B
Gene C
Gene D
The coded information
is not changed by
inheritance …
... or is it?
CROSSOVER
splitting chromosomes
and recombining them
Genetic Algorithms – Theory of Evolution
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt12
Basics
Die chromosome theory
Mutations Random changes in genetic information codes
Gene mutation
Chromosome mutation
Genome mutation
Change rate of a gene in a generation
Is between 1:10.000 and 1:1.000.000.000
Structural changes in order etc.
Also the number of chromosomes can change
Genetic Algorithms – Theory of Evolution
7. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt13
Basics
Overview
Introduction & Motivation
Application of GAs
What is this lecture all about? What‘s the story behind the
Mystic name „genetic algorithms“? How could they be used
and why are they better than „normal“ algorithms?
Explanation of the way genetic algorithms are used to solve
computer science problems.
Genetic Algorithms Motivation and short introduction to the basic computation
principles of genetic algorithms.
Practice / Implementation Using genetic algoritms in practical experiments.
Programming excercises in C.
Extension: advanced GA Advanced algorithm variations for modeling problems with
the genetic algorithm paradigm.
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt14
Basics
The Basic Algorithm
Generate random populationGenerate random population
Rating of the individualsRating of the individuals
select best individuals
for a "mating-pool"
select best individuals
for a "mating-pool"
Recombination by CrossoverRecombination by Crossover
MutationsMutations
Population
Pi
Population
Pi
Good enough?Good enough?
Solution
found
A brief outline
Constraints
- Constant population size
- Stop if individuals are all identical
- Stop if solution quality is good
8. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt15
Basics
Representation of individuals
- the „genome" should represent a solution to a problem
- every individual is rated regarding a specific overall goal
Example: The function f(x) : 2552-(x - 10)2 for x ∈ [0,255]
shall be maximized
possible solutions x = 73 x = 128 x = 10
binary coded: 01001001 10000000 00001010
optimal solutionbad solutions
every digit is a geneevery digit is a gene
The Basic Algorithm
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt16
Basics
Fitness of a solution
- The solution (the genome / the individual) must be rated
- A simple way is to measure the distance to the optimum
Example:
possible solutions x = 73 x = 128 x = 10
binary coded: 01001001 10000000 00001010Let‘s take the function
Itself!
Fitness: 61056 51101 65025
The Basic Algorithm
The function f(x) : 2552-(x - 10)2 for x ∈ [0,255]
shall be maximized
9. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt17
Basics
Selection of the parent individuals
- the best of a generation should sprawn - „build pairs"
- The selection is made due to probabilities using the fitness function
The higher the fitness value the higher the probability to get in
The so called Mating-Pool.
PopulationPopulation
Mating-PoolMating-Pool
Good individuals may get there several times
Population and Mating-Pool
have the same size
The Basic Algorithm
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt18
Basics
copy the individuals
to the successor generation
copy the individuals
to the successor generation
Randomly determine a
Crossover-Site
and recombine the
individuals accordingly
Randomly determine a
Crossover-Site
and recombine the
individuals accordingly
Recombination - "Crossover"
- step by step two individuals are taken from the mating pool
- a random variable decides wheter to perform a crossover or not
CROSSOVER?CROSSOVER?
0 1 1
1 1 0
0 1 0
1 1 1
10 10
11 11
Crossover
Site
crossing
The Basic Algorithm
10. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt19
Basics
Mutation
- After some iterations the population may converge (all individuals are equal)
without arriving at an optimum solution!
- How do we get out of this? A solution is “mutation”
1 1 011 111
random selection
1 0 011 111
the probability for mutations
has to be set to a very low
value
The Basic Algorithm
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt20
Basics
An example
Definition of a starting population
No. Ai fi pi n·pi
1 101 5
2 001 1
3 010 2
4 110 6
fi = (simplified!) a binary coded number
0.357
0.071
0.143
0.429
1.429
0.286
0.571
1.714
total 14 1.000 4.000
avg 3.5 0.250 1.000
max 6 0.429 1.714
individuals are defined by 3 genes which
can thake either the value 1 or 0 (binary).
initial
population
rating of the solutins (individual)
by the fitness function
probability for the selection
depends on fitness value
fitness value of the
overall populationsource: [Muna 1998]
The Basic Algorithm
11. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt21
Basics
An example (cont’d)
Selection : Forming the mating pool
No. Ai fi pi n·pi
1 101 5
2 001 1
3 010 2
4 110 6
0.357
0.071
0.143
0.429
1.429
0.286
0.571
1.714
total 14 1.000 4.000
avg 3.5 0.250 1.000
max 6 0.429 1.714
randomize
000-356
357-427
428-570
571-999
select 4
individuals
1
0
1
2
mating pool
101
010
110
110
”throw the dice
four times"
the worst result
is omitted!
the best one
is included twice!
The Basic Algorithm
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt22
Basics
An example (cont’d)
Recombination : pairwise crossover
Mating pool:
No A(i) partner crossing site new population fitness
1 010 4 1 010 2
2 101 3 2 100 4
3 110 2 2 111 7
4 110 1 1 110 6
0 1 0
1 1 0
0 1 0
1 1 0
No 1 und 4
1 0 1
1 1 0
1 0 0
1 1 1
No 2 und 3
010
110
100
111
new population
The Basic Algorithm
12. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt23
Basics
An example (cont’d)
Comparison of populations
010
110
100
111
010
001
101
110
Population i Population i+1
2
1
5
6
2
6
4
7
14 19
The successor
generation is
better!
The successor
generation is
better!
The optimum
has already been
found!
The optimum
has already been
found!
The Basic Algorithm
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt24
Basics
Evolution over generations
Example A
250 individuals
100 generations
250 individuals
100 generations
The Basic Algorithm
13. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt25
Basics
Verlauf der Evolution
Beispiel
The number of
different individuals
decreases
The number of
different individuals
decreases
The Basic Algorithm
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt26
Basics
Genetic Algorithms – Advanced Techniques
Variations
Mating pool
selection
Population
Binary Integer Floating Point PermutationRepresentation
FPS Rankselektion SUS Tournament
Selection of
successor generation no survivor age selection fitness selektion
Generation model Steady State Modell
Successor population
Terminating the
algorithm All individuals are equal
(or "> x % equal")
Quality level is reached
14. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt27
Basics
Exercise
The genetic algorithm should operate with the following fitness function:
f(x) =
4 x
3(6-x)
2(x-6)
(12-x)
0
x ≤ 4
4 < x ≤ 6
6 < x ≤ 8
8 < x ≤ 12
otherwise
The population is coded by a 4 bit word
Start with the following population and determine the following two generations
0 1 1 0
1 1 0 1
1 1 0 0
0 1 1 1
population i population i+1 population i+2
use the presented scheme
of the basic algorithm without
mutation
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt28
Basics
Overview
Introduction & Motivation
Application of GAs
What is this lecture all about? What‘s the story behind the
Mystic name „genetic algorithms“? How could they be used
and why are they better than „normal“ algorithms?
Explanation of the way genetic algorithms are used to solve
computer science problems.
Genetic Algorithms Motivation and short introduction to the basic computation
principles of genetic algorithms.
Practice / Implementation Using genetic algoritms in practical experiments.
Programming excercises in C.
Extension: advanced GA Advanced algorithm variations for modeling problems with
the genetic algorithm paradigm.
15. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt29
Basics
Application Types
Application types of "Evolutionary Computing"
Optimizing
System identification
Simulation
Model and solution are known – optimal input is searched for.
Typical problem type: The Traveling Salesman Problem
Input and output of a system can be observed but we
cannot look inside
Socio-economic simulation
XXININ OUTOUT?
XXININ OUTOUT?
XXININ OUTOUT?
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt30
Basics
What is meant by "System identification"?
System identification
You can observe the reactions of a system to an input but
the goal is to determine the general principle of behavior -
The so called internal Model of the system.
apply
accelerator
pedal
acceleration
behavior
?
16. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt31
Basics
Problem schematic using a binary example
x1 x2 x3 y
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
0
1
1
0
0
0
1
0
Defining a function by a table:
w1x1 + w2x2 + w3x3 < 0
y =
0
1 otherwise
wi ∈ {-1,0,1}
A more compact way …:
Especially interesing when
using large tables!
Especially interesing when
using large tables!
System identification
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt32
Basics
Similar to „machine learning“ techniques
No. wi fi
1 1 0-1 2
2 0 0 1 2
3 0-1 0 1
4 -1 1 0 3
total 8
avg 2
max 3
Large Tables:
Testing input/output
pairs (by random)
Beispiel:
0 0 1 1
0 1 0 1
1 0 0 0
1 1 1 0
0 0 1 1 1 ok
0 1 0 1 0 no
1 0 0 0 1 no
1 1 1 0 1 no
fitness: 1
„Teacher"
System identification
17. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt33
Basics
Idea: stock market „forecast“
Observation Forecast
Genome = development of values
Fitness function = comparison to
real world
Witchcraft?Witchcraft?
System identification
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt34
Basics
Solving NP-complete problems
Traveling Salesman Problem (TSP)
Given a set of locations, ways and distances.
We are looking for a cost-optimal round trip.
Optimizing
22
77
66
55
11
44
33
99
88
254
115
210
95
104
45
23
232
92
45
133
187
19
27
304
129
49 153
18. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt35
Basics
Solving NP-complete problems
time table problem
Given a set of classes (courses) , lectures and lecturers.
We are looking for a time table with low gaps and good distribution of lectures.
Optimizing
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt36
Basics
The Job Shop Scheduling - Problem
M1M1 M2M2 M3M3
O1 O7 O3 O4
O4 O2 O5 O7
O3 O7 O4 O5
J1
J2
J3
1,2,3 4,5,6 7,8,9
A set of jobs J
A set of machines M
A set of operations O
capability: fMO : O → M (Maschine can perform operation)
sequence: PS : O x O (which operation bevor which other?)
duration : fd : M x O → R (Operation O lasts x minutes on machine M ...)
Simplification:
Operation can be done
by several machines-
Simplification:
Operation can be done
by several machines-
JSSP: all jobs must be carried out, all conditions / constraints must be fulfilled
… and all this must be done very fast !
Quelle: [ES 2003]
Optimizing
19. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt37
Basics
The Job Shop Scheduling - Problem
Problem encoding Permutation : every gene is an operation of a job
The permutation determines the order of processing the
operations. These are given to a „schedule builder“ which
assigns them to the machines.
1 / duration of completing all jobsFitness funktion
PMX or Index methodCrossover
Swap / Insert / Scramble / InversionMutation
The schedule builder guarantees the order constraints of
the jobs an determines the earliest starting point.
Given timing constraint is reached (or: equal solutions...)Termination
Optimizing
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt38
Basics
Overview
Introduction & Motivation
Application of GAs
What is this lecture all about? What‘s the story behind the
Mystic name „genetic algorithms“? How could they be used
and why are they better than „normal“ algorithms?
Explanation of the way genetic algorithms are used to solve
computer science problems.
Genetic Algorithms Motivation and short introduction to the basic computation
principles of genetic algorithms.
Practice / Implementation Using genetic algoritms in practical experiments.
Programming excercises in C.
Extension: advanced GA Advanced algorithm variations for modeling problems with
the genetic algorithm paradigm.
20. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt39
Basics
How does the main algorithm look like?
Implementing Genetic Algorithms
int main(void)
{
int generations = 0;
population pop;
population temp;
myrandomize(); // initializes random number generator
initPopulation(&pop,8,100,binary);
for(generations=1; generations < 200; generations++)
{
selectMatingPool(&pop,&temp);
recombine(&temp,&pop);
mutation(&pop,binary);
}
}
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt40
Basics
How do you represent individuals?
Implementing Genetic Algorithms
/* genome structure ------------------------------------------ */
/* a single individuum in the population is represented as one */
/* genome as defined in the following structure. */
typedef struct
{
int gene[MAX_NUMBER_OF_GENES];
int numberOfGenes; // actually used number of genes
int mutated; // 1: mutated in last cycle, 0: not mutated
int fitness; // current fitness of the genome
genetype type; // binary, digit, number
} genome;
21. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt41
Basics
How do you represent a population?
Implementing Genetic Algorithms
/* population ------------------------------------------------ */
/* a structure containing the set of solutions which represents */
/* the current population. */
typedef struct
{
genome solution[MAX_NUMBER_OF_SOLUTIONS];
int numberOfSolutions; // actual population size
} population;
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt42
Basics
The fitness function
Implementing Genetic Algorithms
/* Test Fitness Function ------------------------------------- */
int fitness(genome g)
{
int r;
/* simple 8-bit code to test a genetic algorithm principle */
r = g.gene[0]*8+g.gene[2]*-4+g.gene[1]*10+g.gene[3]*2+
g.gene[4]*-6+(g.gene[5]*2+g.gene[6]*5)+g.gene[7]*-5
+ 15;
return r;
}
22. Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt43
Basics
The crossover function
Implementing Genetic Algorithms
/* 1-point-crossover ---------------------------------------- */
void crossover (genome p1, genome p2, genome *np1, genome *np2, int site)
{
int i;
for (i=0;i<site;i++)
{
np1->gene[i] = p1.gene[i];
np2->gene[i] = p2.gene[i];
}
for (i=site;i<p1.numberOfGenes;i++)
{
np1->gene[i] = p2.gene[i];
np2->gene[i] = p1.gene[i];
}
}
Genetic Algorithms
BA Stuttgart – Guest Lecture @ VEDC Malang 2005 Prof. Dr. Dirk Reichardt44
Basics
Literature
[ES 2003]
A.E.Eiben, J.E.Smith
"Introduction to Evolutionary
Computing"
Springer, 2003
[TM 1998]
T.Munakata
"Fundamentals of the
New Artificial Intelligence"
Springer, 1998