0% found this document useful (0 votes)
31 views24 pages

09 Ga

The document discusses genetic algorithms and their use in solving combinatorial problems like the travelling salesman problem. It provides an overview of genetic algorithms and their components including individuals, populations, fitness functions, encoding genes, and processes like selection, crossover and mutation. It also notes that genetic algorithms work to find optimal solutions through principles of natural selection.
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)
31 views24 pages

09 Ga

The document discusses genetic algorithms and their use in solving combinatorial problems like the travelling salesman problem. It provides an overview of genetic algorithms and their components including individuals, populations, fitness functions, encoding genes, and processes like selection, crossover and mutation. It also notes that genetic algorithms work to find optimal solutions through principles of natural selection.
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/ 24

Lecture 9:

Genetic Algorithms

Dr. Roman V Belavkin

BIS4435
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 1

OVERVIEW
1. Combinatorial Problems

2. Natural Selection

3. Individuals and Population

4. Fitness Functions

5. Encoding Genes of an Individual

6. Selection, Crossover and Mutation

7. GA Algorithm

8. Why GA work?

9. Problem Example
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 2

THE TRAVELLING SALESMAN PROBLEM


A salesman goes on a tour around sev- ..b
...
y....................................
London...................
.. ... ...............
..........Tokyo
*b
... ...........
.
...
....
.........
eral cities. Each city has to be visited ...
...
... ..........
.........
.....
. .
...
..
... .......
only once. What is the shortest tour? Ub ... ...............
.
............

Cairo
The problem becomes
b.... y .......................................
London.....................
London
bY
..........
... ...............
.........Tokyo ... ............ Tokyo
harder if we increase
*b
... ........... ... ............
... b .. ... ......... .........
... .............


... . ... ......... . .
...
. ......... ..
.... ......... ................ .....
ª
... ...
...
:
... ... ....... ..
the number of cities ... ......
..
... . .......... ..............
Ub
.
Ub ............... .... b
...
..
...
...
.....
. ... .............
.................... Hong-Kong ......... Hong-Kong
(n = 4, 5, . . .). Cairo Cairo

Mathematicians calculated that for n cities the


Moscow
9
..
London ................. ......
.
b number of possible tours is:
o
..
...
...
....
... ..
.
.... ...
b ....
... .... Tokyo
*b
... .... .
... ? ....
. ..
.................
.
... ..... ........ .. .
. .

ª
. .... ..
...
...
... ........
..
.... .
............... .....
..... (n − 1)!
Ub ... ................
b
........ Hong-Kong
Cairo
(e.g. for n = 10, (10 − 1)! = 9! = 362, 880)
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 3

THE TIMETABLING PROBLEM


Problem of balancing the resources (teachers, rooms) so that all
teaching is done and students can take sensible combinations of
courses. The input variables are teachers, rooms and time slots.
There are many more objectives:

• Teachers can only be at one place at one time


• Rooms can only be used by one teacher
• Each teacher can only teach certain subjects
• Subject clashes must be minimal
• Rooms must be big enough for all students
• All subjects must be taught to all registered students
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 4

“Neural networks are second best way to solve just about


anything... and genetic algorithms are the third”
• There are problems for which there are no clear methods
of finding an optimal (best) solution.
• Some problems seem to have only one way of finding a
solution: by random search. These are so–called
combinatorial problems.
• The difficulty is that many tasks have so many possible
combinations, that it is simply impossible to try all of them.
• Various search methods and heuristics have been
developed to tackle such problems. Genetic Algorithms
is one of such techniques that was inspired by the laws of
natural evolution.
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 5

NATURAL SELECTION

• Living organisms are fighting the forces of na-


ture to survive. Those who are the fittest (i.e.
strongest, fastest, biggest) are more likely to
survive. Charles Darwin

• Those who survive (the fittest) mate and reproduce (selection).


• Children are similar (inheritance), but not exactly like the parents
because of cross–fertilisation and mutation. Thus, children can
be either less or more fit than their parents.

• Children repeat the path of their parents. After several generations


the organisms become much fitter than at the beginning.
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 6

SOLUTIONS AS INDIVIDUALS IN POPULATION


• Suppose that there are many possible solutions for the problem:
x1 , x2 ,...,xn . The main idea of GA is to view each solution xi of
the problem as an individual living organism

• The number of all possible solutions for the problem can be


incredibly large (more than atoms in the universe). So, we
consider only a relatively small number m ¿ n of them. We shall
call a collection of m solutions at the current time moment a
population P (t) = {xt1 , . . . , xtm }.
• With time the individuals (like organisms) and the whole
population will be evolving. So, time t here simply describes the
generation of the population and its individuals.
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 7

FITNESS FUNCTION
Fitness function is simply another word for the utility function. It is
needed to compare different solutions (individuals) and choose the
better ones. As with utility, it returns a number such that if
f (x1 ) > f (x2 ), then x1 is preferable to x2 (x1 is ‘fitter’).
Fitness f (x)
6 Maximum fitness

x4
x1
a
a x6
x2
a
a x5
a
Population P (t) = {xt , . . . , xt }
1 6

x3

-
a
Individuals xi
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 8

EXAMPLE

We can choose the tours by comparing their lengths

b.... y .......................................
London..................... London
bY
..........
... ..........
... ............... ... ............ Tokyo
*
... .........Tokyo
........... ......... ..
.............
... b ...
. ... ...........
b ..
...
..


... .
. .
... ... ......... ... .
......... ................ .....
ª
... .. ...
.. ... ..
... .. ... ..
.
. .
:
... . .
.. ... ......... ...........
Ub
... . .... b
......... ..
Ub
... ......... .
... ............... ...........
............................. Hong-Kong Hong-Kong
Cairo Cairo

x1 x2
Thus, fitness is f (x) = −length(x) and

length(x1 ) < length(x2 )

f (x1 ) > f (x2 )

So, we choose x1
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 9

GA and THE CHOICE PROBLEM


• Problems, solved by GA (e.g. TSP, timetable, etc), are the choice
problems (Indeed, we have preference between different
solutions, there is a fitness function, which acts as a utility).

• The GA problems, however, usually have astronomically large


choice sets.

• It is not clear how to find the optimal solution other then by


searching through all possible combinations.

• GA is a very sophisticated parallel search algorithm that uses a


special representation of solutions (encoding), a mixture of
stochastic (random mutation) and deterministic (crosover and
inheritance) variations of solutions. Result: it saves time.
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 10

ENCODING GENES OF AN INDIVIDUAL


Before we can start GA we need to represent each individual by its
genes. This is probably the biggest challenge in GA.

• Identify the simplest elements (building blocks) of which a solution


may consist, and assign a letter (or a number) to each element.
We shall call each letter a gene.

• The number of these simplest elements (and hence the genes) is


finite and not very large. For example, in DNA there are only four
kinds of genes: A, G, T and C . We shall call the collection of all
these letters an alphabet of the algorithm.

• This way a solution xi is represented by a string of genes. We


shall call this string a chromosome of an individual.
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 11

EXAMPLE: TSP

For the Travelling Salesman Problem the genes may represent


different cities, and the chromosomes represent different routes:

L London b.... y .......................................


London.....................
London
bY
..........
... ...............
.........Tokyo ... ............ Tokyo
*b
... ........... ... ............
... b .. ......... ........
...
... ..............


C Cairo ...
...
.. ... .........
......... ..
...
. .
......... ................ .....
ª
... ...
...
:
... ... ...... .
... ......
..
... . .......... ..............
Ub
.
Ub
.
............... ...
...
.
...
...
...
...
. ... ..............
b
.................... Hong-Kong ......... Hong-Kong
H Hong–Kong Cairo Cairo

T Tokyo x1 = LCHT L x2 = LCT HL


Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 12

EXAMPLE: TIMETABLING
We can assign numbers to a teacher, to a time slot and to a room:

Number Teacher Time Room

1 Prof. Newell 9:00am Room B48

2 Dr. Ritter 11:00am Room C52

3 Prof. Wood 1:00pm


. . .
. . .
. . .

Each gene will be a sequence Teacher Time Room


Gene
of three numbers, such as 132 b "
b "
b "
means “Prof. Newell teaches b "
at 1:00pm in Room C52”. Chromosome
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 13

SELECTION
• After the fitness of the individuals of the whole population has
been calculated, we should pick some of them for reproduction.
This process is called selection.

• There are several strategies for selction. For example a roulette


wheel selection, when individuals are selected randomly with
probability proportional to their fitness.

• Ranked selction is when we select only the few fittest individuals.


• If the size of the population remains the same in all generations,
then not all individuals will be allowed to reproduce. This is
because usually the fitter reproduce more.
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 14

CROSSOVER

• The selcted parents are grouped in pairs in order to produce their


offspring (children).

• Crossover is an operation of replacing some genes in one parent


by the corresponding genes of the other. Thus, each child
becomes an offsring of two parents.

• There are many ways to do the crossover, such as the one–point


crossover. For example, one–point crossover at point 3:

P1 = 101 0010 O1 = 1 0 1 1 0 0 1

P2 = 011 1001 O2 = 011 0010
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 15

MUTATION
A randomly chosen gene (or several genes) is changed to some other
gene (mutate). For example, mutation of genes 3 and 5 in the
offspring O1 will give:

O1 = 1 0 1 1 0 0 1 ⇒ O1 = 1 0 0 1 1 0 1

• Mutation helps to add diversity to the population. It works as a


random experimentation.

• Mutation can help the population to avoid local maximum.


• The process of mutation may occure at variable rate.
• Some problems cannot be solved at all without mutation.
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 16

SUMMARY OF GENETIC ALGORITHM


After the crossover and mutation operations the new generation may
have individuals which are even fitter than their parents (i.e. better
solutions of the problem). The process is repeated for several
generations untill a good enough solutions is found.
1. Define fitness function;

2. Encode solutions;

3. Choose initial (random) population;

4. Repeat

• Evaluate fitnesses of all the individuals in the population;


• Select the best individuals for reproduction;
• Create new generation by mutation and crossover;
5. Until the optimal or a sufficiently good solution is found.
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 17

WHY GA WORK?

• In each generation we check several solutions at once. Thus GA


is a kind of a parallel search.

• Fitness and selection filter out bad solutions from good ones.
• Offspring inherit properties of mostly good solutions.
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 18

VARIATIONS IN GA

• Different selection strategies: roulette wheel, ranked,


tournament selection.

• Different forms of the crossover operator: two–point, uniform.


• Static or variable population size
• Variable mutation rates
• Some “good” parts of the chromosomes may be kept intact. They
are usually called schemas.

• Some very good individuals may survive for several generations


(elitism).
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 19

APPLICATION OF GA

• Transport scheduling
• Timetabling
• Circuit design
• Route optimisation (TSP)
For some classes of problems there are other specific methods that
can perform better than GA on these problems. However, when no
special method is known for your problem, then try GA and see if it
can give you a good result. GA are easily programmable and can be
applied to a wide range of problems.
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 20

PROBLEM EXAMPLE
Consider a GA with chromosomes consisting of six genes
xi = abcdef , and each gene is a number between 0 and 9.
Suppose we have the following population of four chromosomes:

x1 = 4 3 5 2 1 6 x2 = 1 7 3 9 6 5
x3 = 2 4 8 0 1 2 x4 = 9 0 8 1 2 3
and let the fitness function be f (x) = (a + c + e) − (b + d + f ).
1. Sort the chromosomes by their fitness

2. Do one–point crossover in the middle between the 1st and 2nd


fittest, and two–points crossover (points 2, 4) for the 2nd and 3rd.

3. Calculate the fitness of all the offspring


Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 21

SOLUTION 1

Apply the fitness function f (x) = (a + c + e) − (b + d + f ):

f (x1 ) = (4 + 5 + 1) − (3 + 2 + 6) = −1
f (x2 ) = (1 + 3 + 6) − (7 + 9 + 5) = −11
f (x3 ) = (2 + 8 + 1) − (4 + 0 + 2) = 5
f (x4 ) = (9 + 8 + 2) − (0 + 1 + 3) = 15

So, the order is x4 , x3 , x1 and x2 .


Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 22

SOLUTION 2

One–point crossover in the middle between x4 and x3 :

x4 = 908 123 O1 = 9 0 8 0 1 2

x3 = 248 012 O2 = 2 4 8 1 2 3

Two–point crossover at points 2 and 4 between x3 and x1 :

x3 = 24 80 12 O3 = 2 4 5 2 1 2

x1 = 43 52 16 O4 = 4 3 8 0 1 6
Middlesex University, Dr. Roman V Belavkin, BIS4435, Lecture 9 23

SOLUTION 3

Again, apply the fitness function f (x) = (a + c + e) − (b + d + f ):

f (O1 ) = (9 + 8 + 1) − (0 + 0 + 2) = 16
f (O2 ) = (2 + 8 + 2) − (4 + 1 + 3) = 4
f (O3 ) = (2 + 5 + 1) − (4 + 2 + 2) = 0
f (O4 ) = (4 + 8 + 1) − (3 + 0 + 6) = 4

Has the fitness of the new population improved?

You might also like