optimizing with genetic algorithms by benjamin j. lynch

46
Optimizing with Genetic Algorithms by Benjamin J. Lynch

Upload: mohamed-hammock

Post on 14-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Optimizing with Genetic Algorithms by Benjamin J. Lynch

Optimizing with Genetic Algorithms

by

Benjamin J. Lynch

Page 2: Optimizing with Genetic Algorithms by Benjamin J. Lynch

2

Outline• What are genetic algorithms?

– Biological origins– Problems with Newton-type optimizers

• How do we apply genetic algorithms?– Options to include

• Encoding• Selection• Recombination• Mutation

– Strategies

• What programs can we use?– How do we parallelize?

• MPI, fork/wait

Page 3: Optimizing with Genetic Algorithms by Benjamin J. Lynch

3

What are genetic algorithms? (GAs)

• A class of stochastic search strategies modeled after evolutionary mechanisms

• A major difference between natural GAs and our GAs is that we do not need to follow the same laws observed in nature.

• Although modeled after natural processes, we can design our own encoding of information, our own mutations, and our own selection criteria.

Page 4: Optimizing with Genetic Algorithms by Benjamin J. Lynch

4

Why would we use genetic algorithms? Isn’t there a simple solution we learned in Calculus?

• Newton-Raphson and it’s many relatives and variants are based on the use of local information.

• The function value and the derivatives with respect to the parameters optimized are used to take a step in an appropriate direction towards a local maximum or minimum.

Page 5: Optimizing with Genetic Algorithms by Benjamin J. Lynch

5

Newton-Raphson

• Perfect for a parabola– H is the Hessian (2nd

derivative with respect to all parameters optimized)

– g is the gradient

– x is the step to minimize the gradient

gxH

x

Page 6: Optimizing with Genetic Algorithms by Benjamin J. Lynch

6

Where Newton-Raphson Fails

• A local method will only find local extrema

gxH

If we start our search here

We’ll end up here

Page 7: Optimizing with Genetic Algorithms by Benjamin J. Lynch

7

How do we use GAs to optimize the parameters we’re interested in?

• Choose parameters to optimize• Determine chromosomal representation of

parameters• Generate initial population of individuals

(chromosomes)• Evaluate fitness of each individual to

reproduce• Allow selection rules and random behavior

to select next population

Page 8: Optimizing with Genetic Algorithms by Benjamin J. Lynch

8

Definitions for this talk

• Gene – encoded form of a parameter being optimized

• Chromosome – the complete set of genes (parameters) which uniquely describe an individual

• Locus – the position of a piece of data within a chromosome

• Fitness – A value we are trying to maximize

Page 9: Optimizing with Genetic Algorithms by Benjamin J. Lynch

9

1.Initialization of population

2. Evaluation of fitness3. Selection4. Recombination5. Repeat 2-4

Steps in a GA

Page 10: Optimizing with Genetic Algorithms by Benjamin J. Lynch

10

Overview of a Genetic Algorithm

Create new population

Select the parentsbased on fitness

Evaluate the fitnessof each individual

Create Initial Population

Evaluation

Selection

Recombination

Page 11: Optimizing with Genetic Algorithms by Benjamin J. Lynch

11

Initialization

• Decide how to encode the parameters

• Create an initial population with randomized chromosomes

Page 12: Optimizing with Genetic Algorithms by Benjamin J. Lynch

12

Create Initial Population• Population size is chosen (1-10 individuals/parameter

optimized for most applications)• Parameters to be optimized are encoded.

– Binary, Base 10– Let’s say we have 2 parameters with initial values of 32 and

13. In binary and base 10 they would look like:

100000001101

3213

Chromosome of theindividual

Page 13: Optimizing with Genetic Algorithms by Benjamin J. Lynch

13

How binary genes translate into other general parameters

101011101001010111010000100110

698 349 38

minminmax10)(

12

38PPPP

You need to understand the system you areoptimizing inorder to determinethe proper parameter range

Page 14: Optimizing with Genetic Algorithms by Benjamin J. Lynch

14

Create Initial Population

• After we choose a population size and encoding method, we must choose a maximum range for each parameter. Ranges for parameters should be determined based on what would be physically reasonable (if you’re interested in solving a physical problem).

• The initial population can be generated by randomizing the genes for each chromosome of the initial population

Page 15: Optimizing with Genetic Algorithms by Benjamin J. Lynch

15

Evaluation

• Assign a fitness value to each individual based on the parameters derived from its chromosome

Page 16: Optimizing with Genetic Algorithms by Benjamin J. Lynch

16

Evaluate the Fitness

• Next we must evaluate the fitness. The fitness function is somehow based on the genes of the individual and should reflect how good a given set of parameters is. – Ability to swim faster than other robotic fish– Ability of a density functional to better predict

chemical phenomena– Power output of a chemical laser

• A larger fitness value will give the individual a higher probability of being the parent of one or more children.

Page 17: Optimizing with Genetic Algorithms by Benjamin J. Lynch

17

Evaluate the Fitness

• Evaluation of the fitness is the computationally-intensive portion of a GA optimization

• Each chromosome holds the information that uniquely describes an individual.

• Each chromosome/(parameters set)/individual can be evaluated separate from the other individuals.– GA optimizations are typically described as

embarrassingly parallelizable

• The evaluation of the chromosomes reduces down to a fitness value for each individual which will be used in the next step

Page 18: Optimizing with Genetic Algorithms by Benjamin J. Lynch

18

Selection of the Parents

• The parents must be selected based on their fitness• The individuals with a higher fitness must have a

higher probability of having offspring• There are several methods for selection

Page 19: Optimizing with Genetic Algorithms by Benjamin J. Lynch

19

Roulette Wheel Selection

• Probability of parenthood is proportional to fitness.

• The wheel until two parents are selected.

• The two parents create one offspring.

• The process is repeated to create a new population for the next generation.

Fit(#1)

Fit(#2)

Fit(#3)

Fit(#4)

Fit(#5)

Page 20: Optimizing with Genetic Algorithms by Benjamin J. Lynch

20

Roulette Wheel Selection

• This form of selection has problems if the fitness changes by orders of magnitude.

• If two individuals have a much higher fitness, they could be the parents for every child in the next generation.

Fit(#1)

Fit(#2)

Fit(#3)

Fit(#4)

Fit(#5)

Page 21: Optimizing with Genetic Algorithms by Benjamin J. Lynch

21

Another Reason Not to Use the Roulette Wheel

• If the fitness value is very close, the parents will be chosen with equal probability, and the function will cease to optimize.

• Roulette selection is very sensitive to the problem being solved and generally requires modifications to work at all.

Fit(#1)

Fit(#2)

Fit(#3)

Fit(#4)

Fit(#5)

Page 22: Optimizing with Genetic Algorithms by Benjamin J. Lynch

22

Rank Selection

• All individuals in the population are ranked according to fitness

• Each individual is assigned a weight inversely proportional to the rank (or other similar scheme).

rank

1

7.11

rank

Fit(#1)

Fit(#2)

Fit(#3)

Fit(#4)

Fit(#5)

Fit(#1)

Fit(#2)

Fit(#3)

Fit(#4)

Fit(#5)

Page 23: Optimizing with Genetic Algorithms by Benjamin J. Lynch

23

Tournament Selection

• Tournament Selection – 4 individuals are randomly

selected from the population. Two are eliminated and two become the parents of a child in the next generation

A

B

C

D

A

DFitness(D) > Fitness(C)

Fitness(A) > Fitness(B)

A simple and veryrobust method for choosing parents of the next generation

Page 24: Optimizing with Genetic Algorithms by Benjamin J. Lynch

24

Tournament Selection

• Selection of parents continues until a new population is completed.

• Individuals might be the parent to several children, or no children. A

B

C

D

A

DFitness(D) > Fitness(C)Fitness(A) > Fitness(B)

Page 25: Optimizing with Genetic Algorithms by Benjamin J. Lynch

25

Tournament and Rank Selection

• Tournament selection is very similar to rank selection (when we assign a weight = 1/rank).

• In the limit of a large populations, both have similar statistics

16

9

16

1

16

6

Both parents wereabove the median

One parent wasabove the median

Neither parent wasabove the median

Fraction of children

Page 26: Optimizing with Genetic Algorithms by Benjamin J. Lynch

26

Recombination

• Using the chromosomes of the parents, we create the chromosome of the child

Page 27: Optimizing with Genetic Algorithms by Benjamin J. Lynch

27

Recombination with crossover points• We can choose a

number of crossover points

ABCDEFGH

abcdefgh

ABCDefgh

Param. 1(eyes)

Param. 2(nose)

momdadchild

In this case, the parameters remained intact, and thechild inherited the same eyesas mom and the same nose as dad.

Page 28: Optimizing with Genetic Algorithms by Benjamin J. Lynch

28

Recombination with crossover points• If a crossover

point occurs within a parameter:

ABCDEFGH

abcdefgh

ABCDEFgh

Param. 1(eyes)

Param. 2(nose)

momdadchild

In this case the child will have a new nose that is not the same as mom’s or dad’s.

Page 29: Optimizing with Genetic Algorithms by Benjamin J. Lynch

29

Recombination with crossover points• This is where our

representation of parameters becomes important.

11110000

10101111

11110011

Param. 1

Param. 2

momdadchild

15

00

10

15

03

15

Not possible if we used base 10encoding

Page 30: Optimizing with Genetic Algorithms by Benjamin J. Lynch

30

Recombination – Uniform Crossover

• Uniform crossover– No limit to crossover points

ABCDEFGH

abcdefgh

aBcDefgH

Allows more variation in offspringand decreases need for randommutations

Page 31: Optimizing with Genetic Algorithms by Benjamin J. Lynch

31

Random mutations• Mutations can be

applied after recombination

ABCDEFGH

abcdefgh

ABCDefgh

Param. 1

Param. 2

momdadchild

A random mutation has been applied to the child

ABCDefgh

Page 32: Optimizing with Genetic Algorithms by Benjamin J. Lynch

32

Random mutations• Creep mutations are a special type

of random mutation.• Creep mutations cause a

parameter to change by a small amount, rather than randomizing any one element.

11100000

11111000

11101000

Param. 1

Param. 2

11011000

11111000

OR

Possible creepmutations for param. 1

Page 33: Optimizing with Genetic Algorithms by Benjamin J. Lynch

33

Random mutations• The frequency of mutations depends greatly on

the other GA options chosen.

ABCDEFGH

abcdefgh

ABCDefgh

Param. 1

Param. 2

ABCDefgH

Page 34: Optimizing with Genetic Algorithms by Benjamin J. Lynch

34

Other Operators for Recombination

• Other rearrangements of information are possible

• Swap locus

04285903

24085903

• Swap entire genes

04285903

59030428

Page 35: Optimizing with Genetic Algorithms by Benjamin J. Lynch

35

Elitism

• Elitism refers to the safeguarding of the chromosome of the most fit individual in a given generation.

• If elitism is used, only N-1 individuals are produced by recombining the information from parents. The last individual is a copy of the most fit individual from the previous generation.

• This ensures that the best chromosome is never lost in the optimization process due to random events.

Page 36: Optimizing with Genetic Algorithms by Benjamin J. Lynch

36

More GA Options• Separate “islands” with populations that interact

infrequently.• Use “male” & “female” populations• “Alpha male” selection• Use 3 parents for each offspring• Use 3 sexes• Recessive genes• …. and many more. Most of which are only useful

for very specific types of fitness functions.

Page 37: Optimizing with Genetic Algorithms by Benjamin J. Lynch

37

Micro-GA• Small population size of 1 individual per

parameter optimized.

• No random mutations.

• When the genetic variance is below a certain threshold (~5%), the most fit individual goes on, while the chromosomes of all other individuals are randomized

• Cycle this process

Page 38: Optimizing with Genetic Algorithms by Benjamin J. Lynch

38

Overview of a micro-GA

Is the variance in genes< than 5%

Randomize the chromosomesfor all but the most fit individual

Recombine parents tocreate new population

Select the parentsbased on fitness

Evaluate the fitnessof each individual

Create initial population

No

Yes

Page 39: Optimizing with Genetic Algorithms by Benjamin J. Lynch

39

How do you know if it’s converged?

• You don’t

• Even GAs are not a “black-box” optimizer for any function

• You can gain confidence by running several optimizations with different starting parameters, different algorithm options, and different parameter ranges.

Page 40: Optimizing with Genetic Algorithms by Benjamin J. Lynch

40

Some Programs available

• David Carrol’s GA Driver– http://cuaerospace.com/carroll/ga.html

• GAUL– http://gaul.sourceforge.net/

• GALOPPS (already parallelized)– http://garage.cps.msu.edu/software/galopps/index.html

• Simple Generalized GA– http://www.skamphausen.de/software/AI/ga.html

Page 41: Optimizing with Genetic Algorithms by Benjamin J. Lynch

41

How might one parallelize a GA?• The GA calculations are minimal. An

optimization might require 1000 generations, and each generation is dominated by the cost to evaluate the fitness– If your fitness is related to an engineering design, this

might run a simulation for each individual.– If your fitness is 1/(rms error) for a predictive quantum

chemistry tool, the fitness evaluation might require lengthy determinations of wavefunctions.

• Standard serial GA programs can handle the GA routines with ~1 ms of cpu time, while your fitness routine can parallelize the fitness evaluations.

Page 42: Optimizing with Genetic Algorithms by Benjamin J. Lynch

42

Parallel GAs

• MPI is always a good choice if you’re already familiar the language. This option would also enable GA algorithms with “islands” on heterogeneous clusters.

• Fork/wait would be very easy– Can be done in several languages

Page 43: Optimizing with Genetic Algorithms by Benjamin J. Lynch

43

Perl fork/wait#!/usr/bin/perluse Parallel::ForkManager;$max_process=4;$pm = new Parallel::ForkManager($max_process);@all_chromosomes=('1101101010001011110', '1100101010010101101', '1101101110010001110', '1100111010010101100', '1110101010010101100');# enter main loop, no more than $max_process# children will run at a timeforeach $chromosome (@all_chromosomes) { my $pid = $pm->start and next;# prepare an appropriate input file with # this set of parameters &writeinput($chromosome); # run the program to evaluate the fitness &evalfitness($chromosome); $pm->finish;}

# make sure that all the processes are done $pm->wait_all_children;

#parse output and return to our GA&parse_output;

Page 44: Optimizing with Genetic Algorithms by Benjamin J. Lynch

44Create new population

Select the parentsbased on fitness

Evaluate the fitnessof each individual

Create Initial Population

Evaluation

Selection

Recombination

Review Elitism is a very commonoption

Evaluation of the fitness isthe time-consuming portion

Page 45: Optimizing with Genetic Algorithms by Benjamin J. Lynch

45

Overview of a micro-GA

Is the variance in genes< than 5%

Randomize the chromosomesfor all but the most fit individual

Recombine parents tocreate new population

Select the parentsbased on fitness

Evaluate the fitnessof each individual

Create initial population

No

Yes

Page 46: Optimizing with Genetic Algorithms by Benjamin J. Lynch

46

The end