Genetic algorithms Prof Kang Li Email: [email protected].

37
Genetic algorithms Prof Kang Li Email: [email protected]

Transcript of Genetic algorithms Prof Kang Li Email: [email protected].

Page 1: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Genetic algorithmsProf Kang Li

Email: [email protected]

Page 2: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Last lectureRBF

This LectureGenetic algorithmBasic GA operations

2 /39

Page 3: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

What is Genetic Algorithm? How to implement GA?

◦ Encoding◦ Selection◦ Crossover◦ Mutation

A example Matlab demo Differential evolution

Content

3 /39

Page 4: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Bio-Inspired artificial intelligence class of probabilistic optimization algorithms

Well-suited for nonlinear/hard problems with a large search space

Influenced by Darwin’s Origin of species

Developed by John Holland, Adaptation in Natural and Artificial Systems, U of Michigan Press, 1975

What is Genetic Algorithm?

4 /39

Page 5: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Natural Selection (Darwin’s theory)

Genetic contents -> survival capacity -> features Reproduction

◦ recombination (or crossover) first occurs. Genes from parents combine to form a whole new chromosome.

◦ Newly created offspring mutated, elements of DNA are changed. Diversity

Survival of the fittest◦ Gene of the fittest survive, gene of weaker die out◦ Elitism

Evolution - change of species’ feature to fit environment

5 /39

Page 6: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Natural Selection

6 /39

Page 7: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Produce a population of candidate solutions for a given problem

Use operators inspired by the mechanisms of natural genetic variation

Apply selective pressure toward certain properties Evolve to a more fit solution

Main idea

7 /39

Page 8: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Nature to Computer Mapping

Nature ComputerPopulationIndividualFitnessChromosomeGene

Set of solutionsSolution to a problemQuality of a solutionEncoding for a SolutionPart of the encoding of a solution

8 /39

Page 9: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

14 2

7 11

10 10

12 12

196

170

200

288

2 21 2 1 2 1 2( , ) x , [0,15] f x x x x x

9

Page 10: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Simple_Genetic_Algorithm(){Initialize the Population;Calculate Fitness Function;

While(the number of generation > maximum number){

Selection;//Natural Selection, Survival Of Fittest

Crossover;//Reproduction, Propagate favorable characteristics

Mutation;//MutationCalculate Fitness Function;}

}

Simple Genetic Algorithm

10 /39

Page 11: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Structure of Genetic Algorithm

1 0 1 1 0

0 1 0 1 1

selection

1 0 0 1 1

0 1 1 1 0

1 0 0 1 1

0 1 1 1 0

crossover

1 0 1 1 0

0 1 0 1 1

1 1 0 1 0

0 1 0 0 0

population

A

B

C

D

Fitnessevaluation

Search space

reproduction

Substitution

mutation

11 /39

Page 12: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Genetic Algorithm Process

Initial population 5th generation 10th generation

12 /39

Page 13: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Applications of GA

Difficult Problems such as NP-class◦ Nonlinear dynamical systems - predicting, data analysis ◦ Designing neural networks, both architecture and weights ◦ Robot Trajectory Planning◦ Evolving LISP programs (genetic programming) ◦ Strategy planning ◦ Finding shape of protein molecules ◦ TSP and sequence scheduling ◦ Functions for creating images ◦ VLSI layout planning◦ …

13 /39

Page 14: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

GA Process

Crossover

Selection

Encoding

Mutation

14 /39

Page 15: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

The process of representing the solution in the form of a string that conveys the necessary information.

Just as in a chromosome, each gene controls a particular characteristics of the individual, and each bit in the string represents a characteristics of the solution.

Encoding

15 /39

Page 16: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Binary Encoding Most common method of encoding. Chromosomes are strings

of 1s and 0s and a gene of chromosomes represents the a particular characteristics of the problem.

Encoding Methods

11111110000000011111

Chromosome B

10110010110011100101

Chromosome A

16 /39

Page 17: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Permutation Encoding Useful in ordering problems such as the Traveling Salesman

Problem (TSP). Example. In TSP, every chromosome is a string of numbers, each of which represents a city to be visited.

Encoding Methods (contd.)

8  5  6  7  2  3  1  4  9Chromosome B

1  5  3  2  6  4  7  9  8Chromosome A

17 /39

Page 18: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Value Encoding Used in problems where complicated values, such as real

numbers, are used and where binary encoding would not suffice. Good for some problems, but often necessary to develop some specific crossover and mutation techniques for these chromosomes.

Encoding Methods (contd.)

(left), (back), (left), (right), (forward)Chromosome B

1.235  5.323  0.454  2.321  2.454Chromosome A

18 /39

Page 19: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Based on fitness function:◦ Determines how ‘good’ an individual is (fitness)◦ Better fitness, higher probability of survival

Selection of individuals for differential reproduction of offspring in next generation

Favors better solutions Decreases diversity in population

Selection

BB

A A C C

19 /39

Page 20: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Each solution gets a region on a roulette wheel according to its fitness

Spin wheel, select solution marked by roulette-wheel pointer

stochastic selection (better fitness = higher chance of reproduction)

Selection – Roulette Wheel

Individual i will have a

probability to be chosen

i

if

if

)(

)(

20 /39

Page 21: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

randomly select q individuals from current population

Winner: individual(s) with best fitness among these q individuals

Example:select the best two individuals as parents for recombination

Selection - Tournament

q=6 selection

21 /39

Page 22: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Crossover is a critical feature of genetic algorithms: Crossover process simulates the exchange of genetic material that occurs

during biological reproduction In this process pairs in the breeding population are mated randomly with a

crossover rate, Pc Typical crossover properties include that an offspring inherits the common

feature from the parents along with the ability of the offspring to inherit two completely different features

Popular crossover techniques: one point, two point and uniform crossover

Crossover

Chromosome A

Chromosome B

Chromosome A*

Chromosome B*

22 /39

Page 23: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Single point crossover

Two point Crossover

Example-Bit string Crossover

11001011 + 11011110 = 11011111+11001010

11001011+11011111 = 11001111+11011011

23 /39

Page 24: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Bit string Crossover

Uniform crossover : bits are randomly copied from the parents

11001011 + 11011101 = 11011111

if random-number > Crossover-rate then Crossover otherwise remain unaltered

Page 25: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Mutation consists of making small alterations to the values of one or more genes in a chromosome

Mutation randomly perturbs the population’s characteristics, and prevents evolutionary dead ends

Most mutations are damaging rather than beneficial and hence mutation rate must be low to avoid the destruction of species

It works by randomly selecting a bit with a certain mutation rate in the string and reversing its value

Mutation

42 58

Offspring 0010 1010

Mutated Offspring 0011 1010

25 /39

Page 26: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

A measure of how successful an individual is in the environment ◦ Problem dependent

Given chromosome, the fitness function returns a number◦ f : S R

Smooth and regular◦ Similar chromosomes produce close fitness values◦ Not have too many local extremes and isolated global extreme

Fitness Function

26 /39

Page 27: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Example( 1)

Finding the maximum of a function: ◦ f(x) = x²◦ Range [0, 31] Goal: find max (31² = 961)◦ Binary representation: string length 5 = 32 numbers (0-31)

= f(x)

27 /39

Page 28: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Typical parameter values◦ population size : 30 -100◦ crossover rate : 0.7 -1◦ mutation rate : 0.01 - 0.2

Start Population

100001String 5

4412110101String 4

1001001010String 3

9300011String 2

36600110String 1

fitnessvaluebinary

1

28 /39

Page 29: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Selection

100001String 5

4412110101String 4

1001001010String 3

9300011String 2

36600110String 1

fitnessvaluebinary

1

Worst one removed

6.13%

1.53%

17.04%75.13%0.17%

29 /39

Page 30: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Best individual: reproduces twice keep population size constant

Selection

100001String 5

4412110101String 4

1001001010String 3

9300011String 2

36600110String 1

fitnessvaluebinary

1

30 /39

Page 31: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Selection

44110101String 5

4412110101String 4

1001001010String 3

9300011String 2

36600110String 1

fitnessvaluebinary

21

All others are reproduced once

31 /39

Page 32: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Crossover

Parents and x-position randomly selected

0 0 1 1 0

0 0 0 1 1

0 0 1 1 1

0 0 0 1 0

String 1:

String 2:

2String 4String 3

4String 2String 1

x-positionpartner

0 1 0 1 0

1 0 1 0 1

0 1 1 0 1

1 0 0 1 0

String 3:

String 4:

6

3

10

21

7

23

21

17

32 /39

Page 33: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Mutation

bit-flip

◦ Offspring -String 1: 00111 (7) 10111 (23)

◦ Offspring -String 3: 10101 (21) 10001 (17)

33 /39

Page 34: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

• All individuals in the parent population are replaced by offspring in the new generation• (generations are discrete!)

• New population (Offspring):

New population

441String 52891710001String 44412110101String 35292310111String 249700111String 1

fitnessvaluebinary

2110101

34 /39

Page 35: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Iterate until termination condition reached, e.g.:◦ Number of generations◦ Best fitness◦ Process time◦ No improvements after a number of generations

Result after one generation:◦ Best individual: 10111 (23) – fitness 529

Best solution after 100 generation:◦ Best solution: 11111 (31) – fitness 961

End

35 /39

Page 36: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

When to Use a GA

Alternate solutions are too slow or overly complicated Need an exploratory tool to examine new approaches Problem is similar to one that has already been

successfully solved by using a GA Want to hybridize with an existing solution Benefits of the GA technology meet key problem

requirements

36 /39

Page 37: Genetic algorithms Prof Kang Li Email: K.Li@qub.ac.uk.

Summary Introduction to GA Basic GA operations

37 /39