Genetic Algorithms Genetic Algorithms
An An algorithmalgorithm is a set of instructions that is is a set of instructions that is repeated to solve a problem.
repeated to solve a problem.
AA ii ll i hi h ll f llll f ll
A A genetic algorithmgenetic algorithm conceptually follows steps conceptually follows steps inspired by the biological processes of
inspired by the biological processes of evolution.
evolution.
evolution.
evolution.
Genetic Algorithms follow the idea of Genetic Algorithms follow the idea of SURVIVAL OF THE FITTEST
SURVIVAL OF THE FITTEST-- Better and Better and better solutions evolve from previous
better solutions evolve from previous
generations until a near optimal solution is generations until a near optimal solution is obtained
obtained obtained.
obtained.
Genetic Algorithms Genetic Algorithms
Also known as Also known as evolutionary algorithmsevolutionary algorithms, , genetic algorithms demonstrate self
genetic algorithms demonstrate self
organization and adaptation similar to the organization and adaptation similar to the organization and adaptation similar to the organization and adaptation similar to the way that the fittest biological organism
way that the fittest biological organism survive and reproduce.
survive and reproduce.
A genetic algorithm is an iterative procedureA genetic algorithm is an iterative procedure
A genetic algorithm is an iterative procedure A genetic algorithm is an iterative procedure that represents its candidate solutions as
that represents its candidate solutions as strings of genes called chromosomes.
strings of genes called chromosomes.
Generally applied to spaces which are too large
too large
Genetic Algorithms Genetic Algorithms
Genetic Algorithms are often used to improve Genetic Algorithms are often used to improve the performance of other AI methods such as the performance of other AI methods such as expert systems or neural networks.
expert systems or neural networks.
The method learns by producing offspring The method learns by producing offspring
h b d b d b
h b d b d b
that are better and better as measured by a that are better and better as measured by a fitness function, which is a measure of the fitness function, which is a measure of the objective to be obtained (maximum or
objective to be obtained (maximum or objective to be obtained (maximum or objective to be obtained (maximum or minimum).
minimum).
Simple GA Simple GA
{
initialize population;
evaluate population;
evaluate population;
while TerminationCriteriaNotSatisfied {
select parents for reproduction;
perform crossover and mutation;
repair();
Every loop called generation
evaluate population;
} } }
Concepts Concepts pp
l i f d d l h
Population:set of individuals each representing a possible solution to a given problem.
Gene:a solution to problem represented as a set
Gene:a solution to problem represented as a set of parameters, these parameters known as
genes.
genes.
Chromosome:genes joined together to form a string of values called chromosome.g
Fitness score(value):every chromosome has fitness score can be inferred from the
chromosome itself by using fitness function.
Stochastic operators Stochastic operators
SelectionSelection replicates the most successful replicates the most successful solutions found in a population at a rate solutions found in a population at a rate
Recombination
proportional to their relative quality proportional to their relative quality
RecombinationRecombination (Crossover)(Crossover) decomposes decomposes
di i l i d h d l i
di i l i d h d l i
two distinct solutions and then randomly mixes two distinct solutions and then randomly mixes their parts to form novel solutions
their parts to form novel solutions M t ti
M t ti dd ll t bt b did tdid t
MutationMutation randomly perturbs a candidate randomly perturbs a candidate solution
solution
Simulated Evolution Simulated Evolution
We need the following We need the following
Representation of an individualRepresentation of an individualRepresentation of an individualRepresentation of an individual
Fitness FunctionFitness Function
Reproduction MethodReproduction Method
Reproduction MethodReproduction Method
Selection CriteriaSelection Criteria
Representing an Individual Representing an Individual
An individual is data structure An individual is data structure
representing the “genetic structure” of representing the “genetic structure” of p p g g g g a possible solution.
a possible solution.
Genetic structure consists of an Genetic structure consists of an
Genetic structure consists of an Genetic structure consists of an alphabet (usually
alphabet (usually 00,,11))
Binary Encoding Binary Encoding
Most Common Most Common –– string of bits, 0 or 1.string of bits, 0 or 1.
Chrom
Chrom: A = 1 0 11 0 0 1 0 1 1: A = 1 0 11 0 0 1 0 1 1 Chrom
Chrom: B: B 1 1 1 1 1 1 0 0 0 01 1 1 1 1 1 0 0 0 0 Chrom
Chrom: B = 1 1 1 1 1 1 0 0 0 0: B = 1 1 1 1 1 1 0 0 0 0
Gives you many possibilitiesGives you many possibilities
Example Problem: Knapsack problemExample Problem: Knapsack problempp pp pp
The problem: there are things with given value and The problem: there are things with given value and size. The knapsack has given capacity. Select things size. The knapsack has given capacity. Select things to maximize the values
to maximize the values to maximize the values.
to maximize the values.
Encoding: Each bit says, if the corresponding thing is Encoding: Each bit says, if the corresponding thing is in the knapsack
in the knapsack
Permutation Encoding Permutation Encoding
Used in “ordering problems”Used in “ordering problems”
Every chromosome is a string of numbers, Every chromosome is a string of numbers,
hi h b i
hi h b i
which represents number is a sequence.
which represents number is a sequence.
Chrom A:
Chrom A: 1 5 3 2 6 4 7 9 81 5 3 2 6 4 7 9 8 Chrom B:
Chrom B: 8 5 7 7 2 3 1 4 98 5 7 7 2 3 1 4 9 Chrom B:
Chrom B: 8 5 7 7 2 3 1 4 98 5 7 7 2 3 1 4 9
Example: Travelling salesman problemExample: Travelling salesman problem
The problem: cities that must be visited.The problem: cities that must be visited.
The problem: cities that must be visited.The problem: cities that must be visited.
Encoding says order of cities in which Encoding says order of cities in which salesman willl visit.
salesman willl visit.
Another Example Another Example
To find optimal quantity of three major To find optimal quantity of three major ingredients (sugar, milk, sesame oil)
ingredients (sugar, milk, sesame oil) g g ( g , ( g , , , ) ) denoting ounces.
denoting ounces.
Use an alphabet ofUse an alphabet of 11--99 denoting ounces..Use an alphabet of Use an alphabet of 11 9 9 denoting ounces..denoting ounces..denoting ounces..
Solutions might be Solutions might be 11--11--11, , 22--11--44, , 33--33--11..
Value Encoding Value Encoding
Used for complicated values (real numbers) Used for complicated values (real numbers) and when binary coding would be difficult and when binary coding would be difficult
E h h i i f l
E h h i i f l
Each chromosome is a string of some values.Each chromosome is a string of some values.
Chrom A: 1.2323 5.3243 0.4556 Chrom A: 1.2323 5.3243 0.4556 Chrom B: abcdjeifjdhdierjfd
Chrom B: abcdjeifjdhdierjfd Chrom B: abcdjeifjdhdierjfd Chrom B: abcdjeifjdhdierjfd
Chrom C: (back), (back), (right), (forward), (left) Chrom C: (back), (back), (right), (forward), (left)
Example: Finding weights for neural nets.
Example: Finding weights for neural nets.pp gg gg The problem: find weights for network
The problem: find weights for network
Encoding: Real values that represent weights Encoding: Real values that represent weightsgg pp gg
How offspring are How offspring are produced
produced -- Reproduction Reproduction
Reproduction Reproduction -- Through Through reproduction reproduction, , genetic algorithms produce new
genetic algorithms produce new
g g p
g g p
generations of improved solutions by generations of improved solutions by selecting parents with higher fitness selecting parents with higher fitness g p g p g g ratings or by giving such parents a ratings or by giving such parents a
greater probability of being contributors greater probability of being contributors
g p y g
g p y g
and by using random selection
and by using random selection
How offspring are How offspring are produced
produced
(Recombination)(Recombination) CrossoverCrossover-- Many genetic Many genetic algorithms use strings of binary symbols for algorithms use strings of binary symbols for chromosomes, as in our Knapsack example, chromosomes, as in our Knapsack example, to represent solutions.
to represent solutions. CrossoverCrossover means means
choosing a random position in the string (say choosing a random position in the string (say choosing a random position in the string (say, choosing a random position in the string (say, after 2 digits) and exchanging the segments after 2 digits) and exchanging the segments either to the right or to the left of this point either to the right or to the left of this point either to the right or to the left of this point either to the right or to the left of this point with another string partitioned similarly to with another string partitioned similarly to produce two new off spring.
produce two new off spring.
C E l
C E l
Crossover Example Crossover Example
Parent A 011011 Parent A 011011
Parent B 101100 Parent B 101100
Parent B 101100 Parent B 101100
“Mate the parents by splitting each number “Mate the parents by splitting each number
h b t th d d thi d
h b t th d d thi d
as shown between the second and third as shown between the second and third digits (position is randomly selected)
digits (position is randomly selected)
01*1011 01*1011 10*1100 10*1100
Crossover Example Crossover Example
N bi th fi t di it f A ith th l t di it N bi th fi t di it f A ith th l t di it
Now combine the first digits of A with the last digits Now combine the first digits of A with the last digits of B, and the first digits of B with the last digits of A of B, and the first digits of B with the last digits of A
This gives you two new offspringThis gives you two new offspring
This gives you two new offspringThis gives you two new offspring
011100011100
101011101011
If these new solutions, or offspring, are better If these new solutions, or offspring, are better
solutions than the parent solutions, the system will solutions than the parent solutions, the system will keep these as more optimal solutions and they will keep these as more optimal solutions and they will keep these as more optimal solutions and they will keep these as more optimal solutions and they will become parents. This is repeated until some
become parents. This is repeated until some
condition (for example number of populations or condition (for example number of populations or (( pp p pp p improvement of the best solution) is satisfied.
improvement of the best solution) is satisfied.
How offspring are How offspring are produced
produced
Mutation Mutation -- Mutation Mutation is an arbitrary is an arbitrary change in a situation. Sometimes it is change in a situation. Sometimes it is gg used to prevent the algorithm from used to prevent the algorithm from
getting stuck. The procedure changes a getting stuck. The procedure changes a
g g p g
g g p g
1 to a 0 to a 1 instead of duplicating 1 to a 0 to a 1 instead of duplicating them. This change occurs with a very them. This change occurs with a very g g y y low probability (say 1 in 1000)
low probability (say 1 in 1000)
Genetic Algorithm Operators Genetic Algorithm Operators
Mutation and Crossover Mutation and Crossover
1 0 1 0 1 1 1 1 1 0 0 0 1 1 Parent 1
1 1 0 0 0 1 1 Parent 2
1 0 1 0 0 1 1 Child 1
1 1 0 0 1 1 0
Child 2 Mutation
C O
C O
Crossover Operators Crossover Operators
SingleSingle--point crossover: gg pppoint crossover:
Parent A: Parent A: 1 0 0 1 01 0 0 1 0| | 1 1 1 0 1 1 1 1 0 1
Parent B: Parent B: 0 1 0 1 1 0 1 0 1 1 ||1 0 1 1 0 1 0 1 1 0
Child AB: Child AB: 1 0 0 1 01 0 0 1 0 1 0 1 1 0 1 0 1 1 0
Child BA: Child BA: 0 1 0 1 1 0 1 0 1 1 1 1 1 0 11 1 1 0 1
TwoTwo--point crossover: point crossover:
Parent A: Parent A: 1 0 01 0 0||1 0 1 11 0 1 1| | 1 0 11 0 1
Parent B:Parent B: 0 1 00 1 0** 1 1 1 01 1 1 0** 1 1 01 1 0
Parent B: Parent B: 0 1 00 1 0 1 1 1 01 1 1 0 1 1 0 1 1 0
Child AB: Child AB: 1 0 0 11 0 0 1 1 1 0 1 1 0 1 0 11 0 1
Child BA:Child BA: 0 1 0 10 1 0 1 0 1 10 1 1 1 1 01 1 0
Child BA: Child BA: 0 1 0 1 0 1 0 1 0 1 10 1 1 1 1 0 1 1 0
Uniform Crossover and Uniform Crossover and
A random mask is generated A random mask is generated -- A random mask is generatedA random mask is generated
-- The mask determines which bits are copied from one The mask determines which bits are copied from one parent and which from the other parent
parent and which from the other parent
p p
p p
-- Bit density in mask determines how much material is Bit density in mask determines how much material is taken from the other parent.
taken from the other parent.
Examples Examples
Mutation: Mutation:
The recipe example:
The recipe example:
The recipe example:
The recipe example:
11--22--3 may be changed to 13 may be changed to 1--33--3 or 33 or 3--22--3, 3, giving two new offspring. How often? How giving two new offspring. How often? How
g g p g
g g p g
many digits change? How big?
many digits change? How big?
(parameters to adjust) (parameters to adjust)
More examples:
More examples:
Crossover Crossover Recipe : Recipe : Recipe : Recipe : Parents 1
Parents 1--33--3 & 3 3 & 3--22--3. Crossover point 3. Crossover point after the first digit Generate two
after the first digit Generate two after the first digit. Generate two after the first digit. Generate two offspring: 3
offspring: 3--33--3 and 1 3 and 1--22--3. 3.
C h t i t
C h t i t
Can have one or two point crossover.
Can have one or two point crossover.
Selection Criteria Selection Criteria
Fitness proportionate selection, rank selection Fitness proportionate selection, rank selection methods.
methods.
Fitness proportionate Fitness proportionate –– each individual, each individual, I,I, has the has the probability fitness(I)/sum_over_all_individual_j probability fitness(I)/sum_over_all_individual_j Fitness(j
Fitness(j) where) where Fitness(I)Fitness(I) is the fitness functionis the fitness function Fitness(j
Fitness(j), where ), where Fitness(I)Fitness(I) is the fitness function is the fitness function value for individual
value for individual I.I.
Rank selection Rank selection –– sorts individual by fitness and the sorts individual by fitness and the probability that an individual will be selected is
probability that an individual will be selected is proportional to its rank in this sorted list.
proportional to its rank in this sorted list.
Fitness Function Fitness Function
Represents a rank of the “representation”Represents a rank of the “representation”
It is usually a real number.It is usually a real number.
The function usually has a value between 0 The function usually has a value between 0 and 1 and is monotonically increasing.
and 1 and is monotonically increasing.
O h bj ti j d t ( 1
O h bj ti j d t ( 1 55
One can have a subjective judgment (e.g. 1One can have a subjective judgment (e.g. 1--5 5 for recipe 2
for recipe 2--11--4.)4.)
Similarly the length of the route in theSimilarly the length of the route in the
Similarly the length of the route in the Similarly the length of the route in the traveling salesperson problem is a good traveling salesperson problem is a good
measure, because the shorter the route, the measure, because the shorter the route, the
b h l
b h l
better the solution.
better the solution.
Outline of the Basic Genetic Algorithm
[Start] Generate random population of n chromosomes (suitable solutions for the problem)
for the problem)
[Fitness] Evaluate the fitness f(x) of each chromosome x in the population each chromosome x in the population
[New population] Create a new population by repeating following
p p y p g g
steps until the new population is
complete
O tli f th B i
O tli f th B i G ti Al Al ith ith Outline of the Basic
Outline of the Basic Genetic Algorithm Algorithm
4. [Selection] Select two parent chromosomes
from a population according to their fitness (the
b fi h bi h b l d)
better fitness, the bigger chance to be selected) The idea is to choose the better parents.
5 [Crossover] With a crossover probability cross
5. [Crossover] With a crossover probability cross over the parents to form a new offspring
(children). If no crossover was performed, offspring is an exact copy of parents.
6. [Mutation] With a mutation probability mutate new offspring at each locus (position in
new offspring at each locus (position in chromosome).
O tli f th B i G ti Al ith
Outline of the Basic Genetic Algorithm
7. [Accepting] Place new offspring in a new population
8. [Replace] Use new generated population for a further run of algorithm
[T t] If th d diti i ti fi d
9. [Test] If the end condition is satisfied, stop, and return the best solution in current population
current population
10. [Loop] Go to step 2
Flow Diagram of the Genetic Algorithm Process
Algorithm Process
Describe Problem Generate Initial Solutions
Test: is initial
solution good enough? Stop
Step 1 Yes
solution good enough?
Select parents No Select parents to reproduce
Apply crossover process Step 2
Step 3 Apply crossover process
and create a set of offspring Apply random mutation Step 3
Step 4 Step 5
Components of a GA Co po e ts o a G
A problem definition as input, and
E di i i l ( h )
Encoding principles (gene, chromosome)
Initialization procedure (creation)
S l i f ( d i )
Selection of parents (reproduction)
Genetic operators (mutation, recombination)
Evaluation function (environment)
Termination condition
Termination condition
Examples:
A pre determined number of generations or time
A pre-determined number of generations or time has elapsed
A satisfactory solution has been achieved
A satisfactory solution has been achieved
No improvement in solution quality has taken
place for a pre-determined number of generations place for a pre determined number of generations
Example : Example :
Suppose a Genetic Algorithm uses chromosomes of the form x=abcdefgh with a fixed length of eight genes .
Each gene can be any digit between 0 and 9 . Let the fitness of individual x be calculated as :
calculated as :
f(x) =(a+b)-(c+d)+(e+f)- ( g+h)
And let the initial population consist of four individuals x1, ... ,x4 with the following chromosomes :
X1 = 6 5 4 1 3 5 3 2 F(x1) =(6+5)-(4+1)+(3+5)-(3+2) = 9 X2 = 8 7 1 2 6 6 0 1 F(x2) = (8+7)-(1+2)+(6+6)-(0+1) = 23 X3 = 2 3 9 2 1 2 8 5 F(x3) = (2+3)-(9+2)+(1+2)-(8+5) = -16( ) ( ) ( ) ( ) ( ) X4= 4 1 8 5 2 0 9 4 F(x4) = (4+1)-(8+5)+(2+0)-(9+4) = -19 The arrangement is ( assume maximization )
X2 x1 x3 x4
( the fittest individual ) ( least fit individual ) ( the fittest individual ) ( least fit individual )
Th l l ti i t bl f i li it The calculations in table for simplicity
Individuals String
Representation Fitness Arrangement Assume maximization
X1 65413532 9 X2(fittest individual)
X1 65413532 9 X2(fittest individual)
X2 87126601 23 X1(second fittest individual) X3 23921285 -16 X3 (third fittest individual)
41852094 19 X4 (least fit individual) X4 41852094 -19 X4 (least fit individual)
So Average fitness : 0 75 Best : 23 Worst : 19 So Average fitness :-0.75 Best : 23 Worst : -19 Average fitness = ( 9+23+ -16 + -19)/ 4 =-0.75
X2 = 8 7 1 2 6 6 0 1
X1 = 6 5 4 1 3 5 3 2
Offspring 1 = 8 7 1 2 3 5 3 2
Middle crossover
Offspring 1 = 8 7 1 2 3 5 3 2
Offspring 2 = 6 5 4 1 6 6 0 1
X1 = 6 5 4 1 3 5 3 2
X3 = 2 3 9 2 1 2 8 5
Offspring 3 = 6 5 9 2 1 2 3 2
crossover crossover
Offspring 3 = 6 5 9 2 1 2 3 2
Offspring 4 = 2 3 4 1 3 5 8 5
X2 = 8 7 1 2 6 6 0 1
X3 = 2 3 9 2 1 2 8 5
Off i 5 8 3 1 2 6 6 8 1
Offspring 5 = 8 3 1 2 6 6 8 1
Offspring 6 = 2 7 1 2 6 2 8 1
Offspring 1 = 8 7 1 2 3 5 3 2 F (Offspring 1) =(8+7)-(1+2)+(3+5)-(3+2) = 15
Offspring 2 = 6 5 4 1 6 6 0 1
F (Offspring 2) =(6+5)-(4+1)+(6+6)-(0+1) = 17
Offspring 3 = 6 5 9 2 1 2 3 2
F (Offspring 3) =(6+5)-(9+2)+(1+2)-(3+2) = -2
Offspring 4 = 2 3 4 1 3 5 8 5
F (Offspring 4) =(2+3)-(4+1)+(3+5)-(8+5) = -5
Offspring 5 = 8p g 3 1 2 6 6 8 1
F (Offspring 5) =(8+3)-(1+2)+(6+6)-(8+1) = 11
Offspring 6 = 2 7 1 2 6 2 8 1
F (Offspring 6) =(2+7)-(1+2)+(6+2)-(8+1) = 5( p g ) ( ) ( ) ( ) ( )
I di id l
I di id l St iSt i RR t tit ti FitFit
Put the calculation in table for simplicity
Individuals
Individuals String RepresentationString Representation FitnessFitness Offspring 1
Offspring 1 8712353287123532 1515
Offspring
Offspring 22 6541660165416601 1717
Offspring
Offspring 33 6592123265921232 --22
Offspring 4
Offspring 4 2341358523413585 --55
Offspring 5
Offspring 5 8792120187921201 1111
Offspring 5
Offspring 5 8792120187921201 1111
Offspring 6
Offspring 6 2392660123926601 55
A fit 6 833 B t 17 W t 5
Average fitness : 6.833 Best : 17 Worst : -5
So that , the overall fitness is improved , since the average is better and worst is improved
is better and worst is improved .
Average fitness = (15+17+ -5 + -2 + 11+5 )/ 6 = 6.8333
Example: MAXONE problem Example: MAXONE problem
Suppose we want to maximize the number of ones in a string of l binary digits
Example (cont)
An individual is encoded (naturally) as a string of l binary digits
The fitness f of a candidate solution to the
MAXONE problem is the number of ones in its
i d
genetic code
We start with a population of n random
t i S th t l 10 d 6
strings. Suppose that l = 10 and n = 6
Example (initialization) Example (initialization) Example (initialization) Example (initialization)
W t f i i 6 ti d t th f ll i
We toss a fair coin 6 times and get the following initial population:
s1 = 1111010101 f (s1) = 7 s2 = 0111000101 f (s2) = 5 s3 = 1110110101 f (s3) = 7 s4 = 0100010011 f (s4) = 4 s5 = 1110111101 f (s5) = 8 s6 = 0100110000 f (s6) = 3 s6 0100110000 f (s6) 3
Example (selection1) Example (selection1) Example (selection1) Example (selection1)
Ne t e appl fitness proportionate selection ith the Next we apply fitness proportionate selection with the roulette wheel method: Individual i will have a ∑
i
i f
i f
) ( ) (
1 2 Area is
probability to be chosen
We repeat the extraction
1 2 n
3
Proportional to fitness value
as many times as the
number of individuals we
d t h th 3
4
need to have the same parent population size (6 in our case)
(6 in our case)
Example (selection2) Example (selection2) Example (selection2) Example (selection2)
Suppose that, after performing selection, we get Suppose that, after performing selection, we get the following population:
s1` = 1111010101 (s1) s1 1111010101 (s1) s2` = 1110110101 (s3)
` 1110111101 ( ) s3` = 1110111101 (s5) s4` = 0111000101 (s2) s5` = 0100010011 (s4) s6` = 1110111101 (s5)
E ample (c osso e 1) E ample (c osso e 1) Example (crossover1) Example (crossover1)
Next we mate strings for crossover. For each couple we decide according to crossover
probability (for instance 0 6) whether to actually probability (for instance 0.6) whether to actually perform crossover or not
Suppose that we decide to actually perform crossover only for couples (s1`, s2`) and (s5`, s6`).
For each couple we randomly extract a For each couple, we randomly extract a
crossover point, for instance 2 for the first and 5 for the second
for the second
Example (crossover2) Example (crossover2)
B f
s1` = 1111010101 s ` = 1110110101
s5` = 0100010011 s ` = 1110111101
Before crossover:
s2 1110110101 s6 1110111101
After crossover:
s1`` = 1110110101 s2`` = 1111010101
s5`` = 0100011101 s6`` = 1110110011
Example (mutation Example (mutation11))
Th fi l t i t l d t ti f h bit th t t
The final step is to apply random mutation: for each bit that we are to copy to the new population we allow a small probability of error (for instance 0.1)
Before applying mutation:
s1`` = 1110110101 s2`` = 1111010101 s3`` = 1110111101 s3 1110111101 s4`` = 0111000101
`` 0100011101 s5 = 0100011101 s6`` = 1110110011
Example (mutation2) Example (mutation2) Example (mutation2) Example (mutation2)
After applying mutation:
s1``` = 1110100101 f (s1``` ) = 6 s2``` = 1111110100 f (s2``` ) = 7 s3``` = 1110101111 f (s3``` ) = 8 s3 1110101111 f (s3 ) 8 s4``` = 0111000101 f (s4``` ) = 5
``` 0100011101 f ( ``` ) 5 s5 = 0100011101 f (s5 ) = 5 s6``` = 1110110001 f (s6``` ) = 6
Example (end) Example (end)
In one generation, the total population fitness changed from 34 to 37, thus improved by ~9%
changed from 34 to 37, thus improved by 9%
At this point, we go through the same process all over again, until a stopping criterion is met
Example: The Knapsack Problem
You are going on an overnight hike and have a number of items that you could take along.
Each item has a weight (in pounds) and a Each item has a weight (in pounds) and a benefit or value to you on the hike(for
measurements sake let’s say, in US dollars), y, ), and you can take one of each item at most.
There is a capacity limit on the weight you can carry (constraint) This problem only can carry (constraint). This problem only
illustrates one constraint, but in reality there could be many constraints including volume, time, etc.
The Knapsack Problem The Knapsack Problem
Item: 1 2 3 4 5 6 7
Benefit: 5 8 3 2 7 9 4
Weight: 7 8 4 10 4 6 4
Weight: 7 8 4 10 4 6 4
Knapsack holds a maximum of 22 pounds
Fill it to get the maximum benefit
Solutions take the form of a string of 1’s and 0’s
Solutions: Also known as strings of genes called Chromosomes
Chromosomes
Th K k P bl
The Knapsack Problem
We represent a solution as a string of seven 1s and 0s and the fitness function as the total benefit which is the sum of the gene values benefit, which is the sum of the gene values in a string solution times their representative benefit coefficient.
The method generates a set of random
solutions (initial parents), uses total benefit
th fit f ti d l t th t
as the fitness function and selects the parents randomly to create generations of offspring by crossover and mutation.
by crossover and mutation.
Knapsack Example
Typically, a string of 1s and 0s can represent a solution.
Possible solutions generated by the
system using Reproduction, Crossover , or Mutations
1. 0101010
2. 1100100
3. 0100111
Knapsack Example Solution 1
Item 1 2 3 4 5 6 7
Solution 0 1 0 1 0 1 0
Benefit 5 8 3 2 7 9 4
Weight 7 8 4 10 4 6 4
Benefit 8 + 2 + 9 = 19
Weight 7 8 4 10 4 6 4
Weight 8 + 10 + 6 = 24
Knapsack Example Knapsack Example Solution 2
Solution 2
Item 1 2 3 4 5 6 7
Solution 1 1 0 0 1 0 0
Solution 1 1 0 0 1 0 0
Benefit 5 8 3 2 7 9 4
Weight 7 8 4 10 4 6 4
Benefit 5 + 8 + 7 = 20
Weight 7 + 8 + 4 = 19g
Knapsack Example Knapsack Example Solution 3
Solution 3
Item 1 2 3 4 5 6 7
S l ti 0 1 0 0 1 1 1
Solution 0 1 0 0 1 1 1
Benefit 5 8 3 2 7 9 4
Weight 7 8 4 10 4 6 4
Benefit 8 + 7 + 9 + 4 = 28
Weight 8 + 4 + 6 + 4 = 22
Knapsack Example
Solution 3 is clearly the best solution and has met our conditions, therefore, item number 2, 5, 6, and 7 will be taken on the hiking trip.
We will be able to get the most benefit out of these items while still having weight equal to these items while still having weight equal to 22 pounds.
This is a simple example illustrating a genetic
This is a simple example illustrating a genetic algorithm approach.
K k P bl
Knapsack Problem
To understand GA must work with the following To understand GA must work with the following
problem:
(Knapsack Problem)
Thief wants to steal gold store
Thief wants to steal gold store.
Thief has a bag(the bag can hold a specific weight).
Every piece of gold has a specific weight and price.
Thief wants to steal gold with high price but the weight must equal or less than the weight that bag can hold it.
If ld i ifi b
If we gave every gold piece a specific number 1,2,3,…,n(suggest n=8 in this example).
1 2 3 4 5 6 7 8
weight 5 3 10 6 5 5 4 4
price 100 30 250 150 50 75 50 50
Encoding ( representation )(gene chromosome) Encoding ( representation )(gene,chromosome)
Ch ld b
Chromosome could be:
Bit strings (101101010100).
Real numbers (43.1,45.2,66.3,11.0).Real numbers (43.1,45.2,66.3,11.0).
Permutation of elements (E11 E3 E7 … E1 E15).
Integer Numbers (11,12,54,98,625,1).
Any data structures.
In knapsack problem can represent any solution as chromosome by using bit string of length 8.y g g g
Ex:- 1 1 0 1 0 0 0 1
1(gene):this piece taken, 0(gene):this piece untaken.
2 Initialize population 2-Initialize population
Implementers specify population size .
To initialize population create chromosomes randomly and store them in list of length the population size
and store them in list of length the population size.
In our problem lets take population size 6 chromosomes.
We can initialize population a following:
1 "10100001"
2 "01110011"
2 01110011
3 "11110000"
4 "00110011"
5 "01010101"
5 "01010101"
6 "00001111"
I l ill k fit f ti th Fitness Function
In our example we will make fitness function as the sum of price of all gold pieces.
-To complete our example must apply fitness function p p pp y on all chromosomes.
1 "10100001" 225 2 "01110011" 305
2 01110011 305
3 "11110000" 225 4 "00110011" 225 5 "01010101" 450
5 01010101 450
6 "00001111" 530
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
weight 5 3 10 6 5 5 4 4
price 50 50 75 50 150 250 30 100
4-Selection of new parents(p (reproductionp ))
Individuals are selected from population randomly or by using any selection method to improve the
population itself.
Good individuals will probably be selected several times in a generation ,poor ones may not be at all.
In our example we will use Truncation selection with parameter 3.
search best 3 chromosomes and then select 6 chromosomes randomly from these three
chromosomes and store them as new population to be used in the next step.
chromosome fitnes value
1 "10100001" 225
1 10100001 225
2 "01110011" 305 3 "11110000" 225 4 "00110011" 225
Old population
5 "01010101" 450 6 "00001111" 530
chromosome fitnes value chromosome fitnes value
1 "01110011" 305 2 "01010101" 450 3 "00001111" 530
Search best 3 chromosomes
chromosome fitnes value
1 "01010101" 450 2 "00001111" 530 3 "00001111" 530 4 "00001111" 530
New population
5 "01110011" 305 6 "01010101" 450
5-Crossover
chromosome fitnes value
5 Crossover
1 "01010101" 450 2 "00001111" 530 3 "00001111" 530 4 "00001111" 530
we will do it just for first two parents.
Pcross=0.6
4 "00001111" 530 5 "01110011" 305 6 "01010101" 450
S l (1 2)
Select two parent(1,2)
Generate random number between 0.0-1.0(0.3) 0.3<=0.6(yes) apply crossover
generate random number between 1 8(3) generate random number between 1-8(3) old 0 1 0 1 0 1 0 1 0 0 0 0 1 1 1 1
new 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 1 swap tails Do this for each pair in population.
Do this for each pair in population.
6 Mutation
Applied to each child individually after crossover
6-Mutation
Applied to each child individually after crossover .
It alters some of genes in chromosome with small probability .
Must specify Pmut(mutation probability that is relatively
Must specify Pmut(mutation probability that is relatively small) therefore a few number of chromosomes will be mutated.
In our example:
In our example:
suppose Pmut =0.2
generate number between 0-1 (0.01) 0.01<=0.2(yes) apply mutation.
Generate number between 1-8(6)
0 1 0 1 0 1 0 1 => 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 > 0 1 1 1 0 1 0 1
Do this for each chromosome in population.
Termination Criteria Termination Criteria Termination Criteria Termination Criteria
There exist three termination condition type:
type:
1-Time:in seconds, in minutes and may be in hours according to the problem that you
hours according to the problem that you have it.
2-Number of generations: in hundreds, in thousands may be in millions according to the problem you have it.
3 convergence: when 95% of populations 3-convergence: when 95% of populations
have the same fitness value we can say the convergence started to appear and the user g pp can stop its genetic program to take the
result.
The Knapsack Problem The Knapsack Problem
The knapsack The knapsack
problem, though problem, though simple has
simple has simple, has simple, has
many important many important applications
applications pppp including
including
determining determining what items to what items to what items to what items to take on a space take on a space ship mission.
ship mission.
Genetic Algorithms Genetic Algorithms
Genetic Algorithms are a type of machine Genetic Algorithms are a type of machine
learning for representing and solving complex learning for representing and solving complex problems
problems problems.
problems.
They provide a set of efficient, domainThey provide a set of efficient, domain-- independent search heuristics for a broad independent search heuristics for a broad independent search heuristics for a broad independent search heuristics for a broad spectrum of applications.
spectrum of applications.
A genetic algorithm interprets information A genetic algorithm interprets information
that enables it to reject inferior solutions and that enables it to reject inferior solutions and accumulate good ones, and thus it learns
accumulate good ones, and thus it learns about its universe
about its universe about its universe.
about its universe.
Genetic Algorithm Application Genetic Algorithm Application g g pp pp Areas
Areas
Dynamic process controlDynamic process control
Induction of rule optimizationInduction of rule optimization
Discovering new connectivity topologiesDiscovering new connectivity topologiesgg yy pp gg
Simulating biological models of behavior and evolutionSimulating biological models of behavior and evolution
Complex design of engineering structuresComplex design of engineering structures
Pattern recognitionPattern recognitionPattern recognitionPattern recognition
SchedulingScheduling
TransportationTransportation
Layout and circuit designLayout and circuit design
Layout and circuit designLayout and circuit design
TelecommunicationTelecommunication
GraphGraph--based problemsbased problems
Business Applications Business Applications
Schedule Assembly lines at Volvo Truck NorthSchedule Assembly lines at Volvo Truck North
Schedule Assembly lines at Volvo Truck North Schedule Assembly lines at Volvo Truck North America
America
Channel 4 Television (England) to schedule Channel 4 Television (England) to schedule (( gg )) commercials
commercials
Driver scheduling in a public transportation systemDriver scheduling in a public transportation system
Jobshop scheduling Jobshop scheduling
Assignment of destinations to sourcesAssignment of destinations to sources
Trading stocksTrading stocks
Trading stocks Trading stocks
Productivity in whiskyProductivity in whisky--making is increased making is increased
Often genetic algorithm hybrids with other AI methodsOften genetic algorithm hybrids with other AI methods
Often genetic algorithm hybrids with other AI methodsOften genetic algorithm hybrids with other AI methods