Csci2300 Ga Lecture

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 33

Genetic Algorithms

Evolutionary Computing
 Evolutionary computing produces
high-quality partial solutions
to problems through
natural selection and
survival of the fittest
– Compare to natural
biological systems that
adapt and learn over time
Genetic Algorithm Example

 Find the maximum value of function g raph


i ng.com
web
f(x) = –x2 + 15x : //www.
http

– Represent problem using chromosomes built from four


genes:Integer Binary code Integer Binary code Integer Binary code
1 0001 6 0110 11 1011
2 0010 7 0111 12 1100
3 0011 8 1000 13 1101
4 0100 9 1001 14 1110
5 0101 10 1010 15 1111
Chromosome Chromosome Decoded Chrom
label string integer fitn
X1 1100 12 36
X2 0100 4 44
Genetic Algorithm Example
X3 0001 1 14
X4 1110 14 14
X5 0111 7 56
 Initial random
X6 population
1 0of
0 1size N= 6: 9 54
60 60
f(x)
50 50

40 40

30 30

20 20

10 10

0 0
0 5 10 15 0
x
(a) Chromosome initial locations. (b) Chrom
Genetic Algorithm Example
fitness function here is
simply the original function
 Determine chromosome fitness for f(x) = –x2 + 15x

each chromosome:
Chromosome Chromosome Decoded Chromosome Fitness
label string integer fitness ratio, %
X1 1 1 00 12 36 16.5
X2 0 1 00 4 44 20.2
X3 0 0 01 1 14 6.4
X4 1 1 10 14 14 6.4
X5 0 1 11 7 56 25.7
X6 1 0 01 9 54 24.8
60 60
f(x)
50 218 100.0 50

40 40
Genetic Algorithm Example
 Use fitness ratios to determine which
chromosomes are selected for crossover
and mutation operations:
100 0
X1: 16.5%
X2: 20.2%
75.2 X3: 6.4%
X4: 6.4%
X5: 25.3%
36.7 X6: 24.8%
49.5 43.1
osome Decoded Chromosome Fitness
ng integer fitness ratio, %
00 12 36 16.5
00
01
Genetic Algorithm Example
4
1
44
14
20.2
6.4
10 14 14 6.4
11 7 56 25.7
 Converge on a near-optimal solution:
01 9 54 24.8
60

50

40

30

20

10

0
10 15 0 5 10 15
x
nitial locations. (b) Chromosome final locations.
Convergence Example
ax i mum
lm
globa

a x i mum
m
local
Genetic Algorithms – Step 1
 Represent the problem domain as
a chromosome of fixed length
– Use a fixed number of genes to represent a solution
– Use individual bits or characters for efficient
memory use and speed
1 0 1 1 0 1 0 0 0 0 0 1 0 1 0 1
– e.g. Traveling Salesman Problem (TSP)
http://www.lalena.com/AI/Tsp/
Genetic Algorithms – Step 2
 Define a fitness function f(x) to measure
the quality of individual chromosomes
 The fitness function determines
– which chromosomes carry over to the next generation
– which chromosomes are crossed over with one another
– which chromosomes are individually mutated
Genetic Algorithms – Step 3
 Establish our genetic algorithm parameters:
– Choose the size of the population, N
– Set the crossover probability, pc
– Set the mutation probability, pm

 Randomly generate an initial population


of chromosomes:1 0 1 1 10 10 10 101 00 011 010 100 010 100 000 110 0 1 0 1
0 0 1 0 1 0 1
– x1, x2, ..., xN 1 0 1 1 10 10 10 01 0 01 0 10 0 10 0 1 0 1 0 1
...
1 0 1 1 0 1 0 0 0 01 0 1 01 10 01 10 0 0 0 0 1
Genetic Algorithms – Step 4
 Calculate the fitness of each
individual chromosome using f(x):
– f(x1), f(x2), ..., f(xN)

 Order the population based on fitness values


Genetic Algorithms – Step 5
 Using pc, select pairs of chromosomes
for crossover
 Using pm, select chromosomes for mutation
 Chromosomes are selected 100 0
X1: 16.5%
based on their fitness X2: 20.2%
75.2 X3: 6.4%
values using a X4: 6.4%
roulette wheel approach: 36.7
X5: 25.3%
X6: 24.8%
49.5 43.1
Genetic Algorithms – Step 6
 Create a pair of offspring chromosomes by
applying a crossover operation:

X6i 1 0 00 1 0 1 00 00 X2i

X1i 0 11 00 00
1 0 11 11 11 X5i
Genetic Algorithms – Step 6
 Mutate an offspring chromosome by applying
a mutation operation:
X6'i 1 0 0 0

X2'i 0 1 0 1
0

X1'i 1
0 1 1 1 1 1 X1"i

X5'i 0 1 0
1 0
1

X2i 0 1 0 0 1 0 X2"i

X5 0 1 1 1
Genetic Algorithms – Steps 7 & 8
 Step 7:
– Place all generated offspring
chromosomes in a new population

 Step 8:
– Go back to Step 5 until the size of the new population
is equal to the size of the initial population, N
Genetic Algorithms – Steps 9 & 10
 Step 9: X1i
Generation i
1 1 0 0 f = 36 X6i
C rossover

1 0 00 1 0 1 00 00 X2i

– Replace the initial population with


X2i 0 1 0 0 f = 44
X3i 0 0 0 1 f = 14
X4i 1 1 1 0 f = 14 X1i 0 11 00 00
1 0 11 11 11 X5i

the new population


X5i 0 1 1 1 f = 56
X6i 1 0 0 1 f = 54
X2i 0 1 0 0 0 1 1 1 X5i
Generation (i + 1)
X1i+1 1 0 0 0 f = 56 Mutation
X2i+1 0 1 0 1 f = 50 X6'i 1 0 0 0
X3i+1 1 0 1 1 f = 44
X2'i 0 1 0 1
0
X4i+1 0 1 0 0 f = 44

 Step 10:
X1'i 1
0 1 1 1 1 1 X1"i
X5i+1 0 1 1 0 f = 54
X6i+1 0 1 1 1 f = 56 X5'i 0 1 0
1 0
1

X2i 0 1 0 0 1 0 X2"i

Go back to Step 4 and repeat the process


X5i 0 1 1 1


until termination criteria are satisfied
– Typically repeat this process for 50-5000+ generations
Iteration Generation i
C rossover

X1i 1 1 0 0 f = 36 X6i 1 0 00 1 0 1 00 00 X2i


X2i 0 1 0 0 f = 44
X3i 0 0 0 1 f = 14
X4i 1 1 1 0 f = 14 X1i 0 11 00 00
1 0 11 11 11 X5i
X5i 0 1 1 1 f = 56
X6i 1 0 0 1 f = 54
X2i 0 1 0 0 0 1 1 1 X5i
Generation (i + 1)
X1i+1 1 0 0 0 f = 56 Mutation
X2i+1 0 1 0 1 f = 50 X6'i 1 0 0 0
X3i+1 1 0 1 1 f = 44
X2'i 0 1 0 1
0
X4i+1 0 1 0 0 f = 44
X1'i 1
0 1 1 1 1 1 X1"i
X5i+1 0 1 1 0 f = 54
X6i+1 0 1 1 1 f = 56 X5'i 0 1 0
1 0
1

X2i 0 1 0 0 1 0 X2"i

X5i 0 1 1 1
Crossword Puzzle Construction

 Given:
– Dictionary of valid words
and phrases
– Empty crossword grid
 Problem:
– Fill the crossword grid such
that all words both across
and down are valid
 (assign clues later)
Crossword Puzzle Construction

 Genetic Algorithm (GA) X1i


Generation i
1 1 0 0 f = 36 X6i
C rossover

1 0 00 1 0 1 00 00 X2i

Evolve a solution by crossovers and


X2i 0 1 0 0 f = 44

– X3i
X4i
0 0 0 1
1 1 1 0
f = 14
f = 14 X1i 0 11 00 00
1 0 11 11 11 X5i
X5i 0 1 1 1 f = 56

mutations through many generations X6i 1 0 0 1 f = 54

Generation (i + 1)
X1i+1 1 0 0 0 f = 56
X2i 0 1 0 0 0 1 1 1 X5i

Mutation
X2i+1 0 1 0 1 f = 50 X6'i 1 0 0 0

– Initial population of crossword grids: X3i+1 1 0 1 1


X4i+1 0 1 0 0
X5i+1 0 1 1 0
f = 44
f = 44
f = 54
X2'i 0 1 0 1

X1'i 1
0
0

1 1 1 1 1 X1"i

X6i+1 0 1 1 1 f = 56 X5'i 0 1 0
1 0
1

 Random letters? X2i

X5i
0 1

0 1 1 1
0 0 1 0 X2"i

 Random letters based on Scrabble® frequencies?


 Random words from dictionary?
– Fitness of each grid is number of valid words
Termination Criteria
 When do we stop?
Pause a genetic algorithm after a

?

given number of generations, then
check the fittest chromosomes
 If the fittest chromosomes are fit
beyond a given threshold,
terminate the genetic algorithm

– Also consider stopping when the highest fitness value


does not change for a large number of generations
Computational Complexity
 How long does it take for an algorithm to
produce a solution?
– Depends on the size of the input and
the complexity of the algorithm

– The size of the input is n

– The complexity of the algorithm is classified


based on its expected run time
Computational Complexity
 Big-O notation measures the expected run time
of an algorithm (i.e. its computational complexity)
– Constant time: O(1)
– Logarithmic time: O(log n)
– Linear time: O(n) P
– Linearithmic time: O(n log n)
– Quadratic time: O(n2)
– Exponential time: O(c n)
– Factorial time: O(n!)N
P
Genetic Algorithms
 Genetic algorithms are often well-suited to
producing reasonable solutions to intractable
problems
– Intractable problems are problems with
excessive computational complexity
 i.e. in the Nondeterministic Polynomial (NP) class of
problems

– A reasonable solution is a partial or inexact solution


that adequately solves the problem in polynomial time
Genetic Algorithms Example
 Consider the Traveling Salesman Problem (TSP)
in which a salesman aims to visit n cities exactly
once covering the least distance
http://mathworld.wolfram.com/TravelingSalesmanProblem.html

http://www.tsp.gatech.edu/games/index.html
yike
s!
– Starting at any given node, choose from n–1 remaining
nodes, then choose from n–2 remaining nodes, etc.
– Testing every possible route takes (n–1)! steps
see http://bio.math.berkeley.edu/classes/195/2000/lec14/index.html
Genetic Algorithms Example
 Use a genetic algorithm to evolve a near-optimal
solution to the TSP
– Label cities A, B, C, D, E, F, etc.
– Example circuits: ABCDEF, BDAFCE, FBECAD
– BDADEF, ACBFCE
– How do we perform crossover operations?
 Basic crossovers might result in invalid members
of the population
 e.g. combining ABCDEF and BDAFCE may result in
ABCFCE

ABCDEF 123456 ACDEF ADEF AEF EF
BCD AEF 222111  551111 ABCDEF ABCDF ABCD
EFABCD
BCDDCB
EFAAEF
222321 ABCDEF ACDEF ADEF AEF AE
BCDFEA
ABCDEF 123456 ABCDF ABCD BCD BC
EFADCB 551321
Nhận xét: Vị trí đầu tiên <= 6, vị trí thứ 2 giá trị <=5, vị trí thứ 3 giá trị <=4, vị trí thứ 4 giá trị <= 3, vị trí thứ 5 giá trị <=2, vị trí thứ 6 giá trị bằng 1

Đột biến: chọn vị trí 1  thay bằng một giá trị khác <= 6
chọn vị trí 3  thay bằng một giá trị khác <= 4
552321  EFBDCA

BCDDCB 222321  BCDFEA


ACDEF ADEF AEF AE
EFAAEF 551111  EFABCD
ABCDF ABCD BCD CD
222551  BCD
111321 ABCFED
Genetic Algorithms Example
 Key challenge of developing a genetic algorithm
is often the representation of the problem
– For TSP, consider a standard ordering ABCDEF,
assigning the code 123456
– All other sequences encoded
based on the removal of letters

– Basic crossover works...


 ABCDEF 123456 > ACDEF 12345 >ADEF 1234
 DEF 123 >DF 12
Cần mã hóa: BCAEDF 221211
– Example circuits: ABCDEF: 111111 và BCAEDF: 221211
– 221111: BCADEF
– 111211: ABCEDF

Chú ý: Vị trí 1: giá trị không vượt quá 6, Vị trí 2: giá trị không vượt quá 5,
…, Vị trí 6: bằng 1
- Đột biến: Chọn cá thể để đột biến: chọn ra một (hoặc nhiều) vị trí, ví dụ
vị trí được chọn là vị trí 4 (giá trị không vượt quá 3)  Thay giá trị tạo
vị trí đó bằng giá trị khác không vượt quá 3 Một phương án chấp nhận
được
Genetic Algorithms Example
 All other sequences encoded based on
the removal of letters from standard ordering
– Sequence BDAFCE has code 231311
B is 2 in ABCDEF
D is 3 in ACDEF
A is 1 in ACEF
F is 3 in CEF
C is 1 in CE
E is 1 in E
Genetic Algorithms Example
 Crossing ACEDB with ABCED...

Crossover Operation
another approach:
http://www.dna-evolutions.com/dnaappletsample.html

Genetic Algorithms Example


 Combining ACEDB with ABCED...
...yields ACBED

from A.K. Dewdney’s The (New) Turing


Omnibus, Computer Science Press, New York, 1993
Genetic Algorithms
 Advantages of genetic algorithms:
– Often outperform “brute force” approaches by
randomly jumping around the search space
– Ideal for problem domains in which near-optimal
(as opposed to exact) solutions are adequate

 Disadvantages of genetic algorithms:


– Might not find any satisfactory partial solutions
– Tuning can be a challenge

You might also like