15

Click here to load reader

Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Embed Size (px)

Citation preview

Page 1: Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Planning and Search

Genetic algorithms

Genetic algorithms 1

Page 2: Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Outline

♦ Genetic algorithms

♦ Representing states (individuals, or chromosomes)

♦ Genetic operations (mutation, crossover)

♦ Example

Genetic algorithms 2

Page 3: Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Stochastic local beam search recap

Idea: keep k states instead of 1

choose k of all their successors, stochastically favouring better ones

Observe the close analogy to natural selection!

Genetic algorithms 3

Page 4: Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Genetic algorithms

= stochastic local beam search + generate successors from pairs of states

k states (individuals) - population

original population randomly generated

each individual represented as a string (chromosome)

each individual rated by an objective function (fitness function)

probability of being chosen for reproduction directly proportional to fitness

two parents produce offspring by crossover

then with some small probablity, mutation (bits of the string changed)

Genetic algorithms 4

Page 5: Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Example: 8 queens problem states

States: assume each queen has its own column, represent a state by listinga row where the queen is in each column (digits 1 to 8)

for example, the state below will be represented as 16257483

Genetic algorithms 5

Page 6: Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Example: 8 queens problem fitness

Fitness function: instead of−h as before, use the number of nonattacking

pairs of queens. There are 28 pairs of different queens, smaller columnfirst, all together, so solutions have fitness 28. (Basically, fitness function is28− h.)

for example, fitness of the state below is 27 (queens in columns 4 and 7attack each other)

Genetic algorithms 6

Page 7: Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Example: 8 queens problem crossover

choose pairs for reproduction (so that those with higher fitness are morelikely to be chosen, perhaps multiple times)

for each pair, choose a random crossover point between 1 and 8, say 3

produce offspring by taking substring 1-3 from the first parent and 4-8 fromthe second (and vice versa)

apply mutation (with small probablity) to the offspring

32252124

Selection Cross−Over Mutation

24748552

32752411

24415124

24

23

20

32543213 11

29%

31%

26%

14%

32752411

24748552

32752411

24415124

32748552

24752411

32752124

24415411

24752411

32748152

24415417

Fitness Pairs

Genetic algorithms 7

Page 8: Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Importance of representation

Parts we swap in crossover should result in a well-formed solution (and inaddition better be meaningful)

consider what would happen with binary representation (where position re-quires 3 digits)

also, chosen representation reduced search space considerably (compared torepresenting each square for example)

+ =

Genetic algorithms 8

Page 9: Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Genetic algorithm pseudocode

(this is one offspring per pair version, unlike in the example)

function Genetic-Algorithm(population,Fitness-Fn) returns an individ-

ual

inputs: population, a set of individuals

Fitness-Fn, a function that measures the fitness of an individual

repeat

new-population← empty set

for i=1 to Size(population) do

x←Random-Selection(population,Fitness-Fn)

y←Random-Selection(population,Fitness-Fn)

child←Reproduce(x, y)

if (small random probability) then child←Mutate(child)

add child to new-population

until some individual is fit enough or enough time has elapsed

return the best individual in population, according to Fitness-Fn

Genetic algorithms 9

Page 10: Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Reproduce pseudocode

function Reproduce( x, y) returns an individual

inputs: x, y, parent individuals

n←Length(x); c← random number from 1 to n

return Append(Substring(x, 1, c), Substring(y, c+1, n))

Genetic algorithms 10

Page 11: Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Schemata

Schema is a partial specification of a substring

for example, 246***** is a schema describing states where the first 3 queensare in rows 2, 4 and 6

states which match the schema are called instances of the schema

genetic algorithms work best where schemata correspond to meaningful com-ponents of a solution (and crossover combines substrings corresponding toschemata)

example: combine wheels from this car with the engine of that car (not onewheel from one and three wheels from another)

Genetic algorithms 11

Page 12: Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Genetic algorithms summary

GAs require states encoded as strings

Representation and choice of operations and other parameters (probabilities)matters a lot

Crossover is only useful if the swapped substrings a large chunks whichevolved for some purpose and are meaningful

Some precise charaterisations (of which parameters better for what kind ofproblems) but a lot of GA is still an art

Genetic algorithms 12

Page 13: Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Self-test question

Give the name of the algorithm which results from:

genetic algorithm with population size N=1.

Genetic algorithms 13

Page 14: Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Example

How would you solve the following problem using a genetic algorithm?

Evolve an arithmetical expression (using digits 0-9, +,-, * and /) whichevaluates to number 42. Use a binary encoding. What would be a suitablefitness function?

(Problem and solutions are by Mat Buckland http://www.ai-junkie.com)

Genetic algorithms 14

Page 15: Planning and Search - Nottinghampsznza/G52PAS/lecture4.pdf · Planning and Search Genetic algorithms Genetic algorithms 1. Outline ♦Genetic algorithms ... for each pair, choose

Next lecture

Search and SAT (chapter 7, mostly 7.4 and 7.6, in the 3rd edition.)

Genetic algorithms 15