Genetic Algorithm
Genetic Algorithm
Genetic Algorithm
Credits:
Richard Frankel
Stanford University
And
Anas S. To’meh
1
Outline
• Motivations/applicable situations
• What are genetic algorithms?
• Example
• Pros and cons
2
Motivation
• Searching some search spaces with traditional
search methods would be intractable. This is
often true when states/candidate solutions
have a large number of successors.
– Example: Designing the surface of an aircraft.
4
Applicable situations
• Genetic algorithms work best when the
“fitness landscape” is continuous (in some
dimensions). This is also true of standard
search, e.g. A*.
– Intuitively, this just means that we can find a
heuristic that gives a rough idea of how close a
candidate is to being a solution.
6
Basic algorithm
• Create an initial population, either random or
“blank”.
• While the best candidate so far is not a
solution:
– Create new population using successor functions.
– Evaluate the fitness of each candidate in the
population.
• Return the best candidate found.
7
Simple example – alternating string
• Let’s try to evolve a length 4 alternating string
• Initial population: C1=1000, C2=0011
• We roll the dice and end up creating C1’ =
cross (C1, C2) = 1011 and C2’ = cross (C1, C1) =
1000.
• We mutate C1’ and the fourth bit flips, giving
1010. We mutate C2’ and get 1001.
• We run our solution test on each. If C1’ is a
solution, so we return it and are done.
8
Basic components
• Candidate representation
– Important to choose this well. More work here means
less work on the successor functions.
• Successor function(s)
– Mutation, crossover
• Fitness function
• Solution test
• Some parameters
– Population size
– Generation limit
9
Candidate representation
• We want to encode candidates in a way that
makes mutation and crossover easy.
• The typical candidate representation is a
binary string. This string can be thought of as
the genetic code of a candidate – thus the
term “genetic algorithm”!
– Other representations are possible, but they make
crossover and mutation harder.
10
Candidate representation example
• Let’s say we want to represent a rule for classifying
bikes as mountain bikes or hybrid, based on these
attributes*:
– Make (Bridgestone, Cannondale, Nishiki, or Gary Fisher)
– Tire type (knobby, treads)
– Handlebar type (straight, curved)
– Water bottle holder (Boolean)
• We can encode a rule as a binary string, where each bit
represents whether a value is accepted.
Make Tires Handlebars Water bottle
B C N G K T S C Y N
B C N G K T S C Y N
12
Successor functions
• Mutation – Given a candidate, return a slightly
different candidate.
• Crossover – Given two candidates, produce
one that has elements of each.
• We don’t always generate a successor for each
candidate. Rather, we generate a successor
population based on the candidates in the
current population, weighted by fitness.
13
Successor functions
• If your candidate representation is just a
binary string, then these are easy:
– Mutate(c): Copy c as c’. For each bit b in c’, flip b
with probability p. Return c’.
– Cross (c1, c2): Create a candidate c such that c[i] =
c1[i] if i % 2 = 0, c[i] = c2[i] otherwise. Return c.
• Alternatively, any other scheme such that c gets roughly
equal information from c1 and c2.
14
Fitness function
• The fitness function is analogous to a heuristic
that estimates how close a candidate is to
being a solution.
16
New population generation
• How do we come up with a new population?
– Given a population P, generate P’ by performing
crossover |P| times, each time selecting
candidates with probability proportional to their
fitness.
– Get P’’ by mutating each candidate in P’.
– Return P’’.
17
Basic algorithm (recap)
• Create an initial population, either random or
“blank”.
• While the best candidate so far is not a
solution:
– Create new population using successor functions.
– Evaluate the fitness of each candidate in the
population.
• Return the best candidate found.
18
Knapsack Problem
Problem Description:
• Item: 1 2 3 4 5 6 7
• Benefit: 5 8 3 2 7 9 4
• Weight: 7 8 4 10 4 6 4
• Knapsack holds a maximum of 22 pounds
• Fill it to get the maximum benefit
Genetic Algorithm
Outline of the Basic Genetic Algoritm
1. [Start]
✔ Encoding: represent the individual.
✔ Generate random population of n chromosomes
(suitable solutions for the problem).
2. [Fitness] Evaluate the fitness of each chromosome.
3. [New population] repeating following steps until the
new population is complete.
4. [Selection] Select the best two parents.
5. [Crossover] cross over the parents to form a new
offspring (children).
Genetic Algorithm
Outline of the Basic Genetic Algoritm Cont.
6. [Mutation] With a mutation probability.
7. [Accepting] Place new offspring in a new
population.
8. [Replace] Use new generated population
for a further run of algorithm.
9. [Test] If the end condition is satisfied,
then stop.
10. [Loop] Go to step 2 .
Basic Steps
Start
Parent 1 1 1 0 0 1 0 0
Parent 2 0 1 0 0 0 1 1
Child 1 1 1 0 0 0 1 1
Child 2 0 1 0 0 1 0 1
0 Mutation
Basic Steps Cont.
Accepting, Replacing & Testing
28