Genetic Algorithm
Imroatul Hudati, S.T.,M.T.
Introduction Genetic Algorithm
• Darwinian selection:
“From a group of individuals the best will survive”
• By implementing this Darwinian selection to the problem only the best
solutions will remain, thus narrowing the search space.
• GA is an algorithm which makes it easy to search a large search space
Biological Background
• All living organism consists of cells
• Each cells has a set of chromosomes
• Chromosomes are strings of DNA and consists of genes
• Genes are blocks of DNA. Each gene encodes a trait
• Reproduction
• Organism produce an offspring (children) similar to themselves but can have
variations (changes in traits) due to:
1. Mutations (random changes)
2. Crossover (offspring have combinations of features inherited from each
parent)
Biological Background
Genetic Algorithm
GA Terminologies
Population: set of the chromosomes (solutions) available to test
Chromosome: a string of genes that represents a solution
Gene: one component of the solution pattern
Fitness value: Each individual is characterized by a fitness value.
Higher fitness is better solution
GA Terminologies
How GA Works
• Initialize population (initialization)
• Apply the fitness function to calculate the fitness value for each individuals (fitness
calculations)
• Select individuals for mating (selection)
• Mate individuals and produce an offspring (children) (crossover)
• Mutate children (mutation)
• Insert children into population (insertion)
• Test stopping criteria
GA Cycle
Evolution and Selection
• Evaluation : Method for measuring the quality of any chromosome (solution), using some
fitness function
• Selection:
• How to select the best chromosomes from the population to be parents to crossover and
produce offspring
• Parents are selected according to their fitness
• There are many methods to select the best chromosomes
1. Roulette wheel selection
2. Rank selection
3. Tournament selection
4. Steady state selection
Reproduction
• The process of selecting parents from the population to produce new offspring is
called reproduction
• Two genetic operators are used for reproduction:
1. Crossover
2. Mutation
• The new population is further evaluated and tested for termination
• If the termination criteria are not met, the population is iteratively operated
• One cycle of operations and the subsequent evaluation: Generation in GA
Crossover
Combine bits and pieces of good parents
Speculate on new, possibly better children
By itself, a random shuffle
Given two chromosomes
Choose a random bit along the length, say at position 9, and
swap all the bits after that point
so the above become:
Crossover
Mutation Operator
• Mutation is random alteration of a string
• Change a gene, small movement in the neighbourhood
• By itself, a random walk
Example 1
• There is an equation
Find the number of and . Use GA to solve this problem.
• Steps:
1. Create chromosome
2. Initialisation
3. Evaluate chromosome
4. Chromosome selection
5. Crossover
6. Mutation
Example 1
1. Create chromosomes
• Because what is sought after is the value of and thus will became a genes
to create chromosomes.
• The limit of the variable value of is integer from 0 to 20. Whereas the
limit of variables are integer number between 0 and 10.
Example 1
2. Initialization
• The initialization process is done by giving initial random number to the genes
accordance with predetermined limits. For instance, determined chromosome is 6, thus:
o Chromosome[1] = [a;b;c;d] = [08;05;03;07]
o Chromosome[2] = [a;b;c;d] = [02;01;08;03]
o Chromosome[3] = [a;b;c;d] = [10;02;03;04]
o Chromosome[4] = [a;b;c;d] = [06;01;10;03]
o Chromosome[5] = [a;b;c;d] = [01;04;03;07]
o Chromosome[6] = [a;b;c;d] = [09;05;07;01]
Example 1
3. Chromosome evaluation
• To evaluate chromosome uses objective function that can be used to get solutions
• Calculate the objective function from chromosomes of the generated
chromosome
The average of the objective function is:
Example 1
4. Chromosome selection
• Selection process is done by choosing chromosomes with small objective
function (Have a high likelihood of being selected or have a high
probability)
• So therefore, it can be used fitness function
• 1 is used to avoid error programming that is caused by 0 divided.
Example 1
4. Chromosome selection
Example 1
4. Chromosome
selection
Probability
equation:
Example 1
4. Chromosome selection
• For the selection process we use roulette wheel method, for that we
should compute the cumulative probability values and their sum
should be equal to 1 if their sum is not equal to 1 then there is some
error in above computations:
Example 1
4. Chromosome selection
• This process is used to generate random number R in the range 0-1 as
follows.
• If random number then select Chromosome[2] as a chromosome in the
new population for next generation
Example 1
4. Chromosome selection
• For instance, first we generate a random number R as the number of
population.
R[1] = 0,201
R[2] = 0,284
R[3] = 0,009
R[4] = 0,622
R[5] = 0,390
R[6] = 0,501
Example 1
K Bilangan pc Jika R<pc[k], pilih C[1]
acak (R) Jika pc[k-1]<R<pc[k], pilih C[k]
1 R[1] = 0,201 0,118 C2
2 R[2] = 0,284 0,317 C2
4. Chromosome selection
3 R[3] = 0,009 0,53 C1
4 R[4] = 0,799 0,667 C5
5 R[5] = 0,390 0,785 C2
6 R[6] = 0,501 1 C3
Example 1
4. Chromosome selection
From the generated random number above can be New chromosome resulting from the selection
resulted selection proces process
Chromosome[1] = chromosome[2] Chromosome[1] = [02;01;08;03]
Chromosome[2] = chromosome[2] Chromosome[2] = [02;01;08;03]
Chromosome[3] = chromosome[1] Chromosome[3] = [08;05;03;07]
Chromosome[4] = chromosome[5] Chromosome[4] = [01;04;03;07]
Chromosome[5] = chromosome[2] Chromosome[5] = [02;01;08;03]
Chromosome[6] = chromosome[3] Chromosome[6] = [09;05;07;01]
Example 1
5. Crossover
• In this example, we use one-cut point, i.e. we randomly choose a position in the parent chromosome and then
exchange sub-chromosome till we reach the one cut point.
• Parent chromosome which will mate is randomly choose and the number of mate Chromosomes is controlled
using crossover rate (ρc) parameters. Pseudo-code for the crossover process is as follows:
Begin
k← 0;
while(k<population)do
R[k] = random(0-1);
if(R[k]< ρc) then
select Chromosome[k] as parent;
end;
k = k + 1;
end;
end;
Example 1
5. Crossover
• For example, we set that crossover rate is 25% then chromosome number k
be selected for crossover if random generated value for Chromosome k
below 0.25. The process is as follows: First we generate a random number
R as the number of population.
Example 1
5. Crossover
a. Generating random number R for the population
R[1] = 0,191
R[2] = 0,259
R[3] = 0,760
R[4] = 0,006
R[5] = 0,159
R[6] = 0,340
For example
If R[k]<pc, parents are Chromosone[1], chromosome[4],
chromosome[5]
Example 1
5. Crossover
b. Determining the crossover position
This is accomplished by generating random numbers between 1 to length of Chromosome–
1. Position of crossover point = length of chromosome-1(4-1) = 3.
In this case, generated random numbers should be between 1 and 3.
C[1] = 2
C[2] = 1
C[3] = 3
Example 1
5. Crossover
Chromosome[1] >< chromosome[4]
Chromosome[4] >< chromosome[5]
Chromosome[5] >< chromosome[1]
Offspring[1] = chromosome[1] >< chromosome[4]
= [02;01;08;03] >< [01;04;03;07]
= [02;01;03;07]
Offspring[4] = chromosome[4] >< chromosome[5]
= [01;04;03;07] >< [02;01;08;03]
= [01;01;08;03]
Example 1
5. Crossover
Offspring[5] = chromosome[5] >< chromosome[1]
= [02;01;08;03] >< [02;01;08;03]
= [02;01;08;03]
Example 1
5. Crossover
Thus, the population of chromosomes after undergoing the crossover process
becomes:
Chromosome[1] = [02;01;03;07]
Chromosome[2] = [02;01;08;03]
Chromosome[3] = [08;05;03;07]
Chromosome[4] = [01;01;08;03]
Chromosome[5] = [02;01;08;03]
Chromosome[6] = [08;05;08;03]
Example 1
6. Mutasi
• The number of chromosomes that have mutations in a population is
mutation_rate
• Mutation process is done by replacing the generation at random position
with a new value.
• first we have to calculate the total length of generation in the population.
The total length of the generation is
Total_gen = number_of_generation_in_Chromosome * number of
population
= 4*6 =24
Example 1
6. Mutasi
• Mutation process is done by generating a random integer between 1 and
total gen (1 to 24).
• If the generated random number is smaller than the mutation rate(ρm)
variable then marked the position of gen in chromosomes.
• Suppose we define ρm 10%, it is expected that 10% (0.1) of total gen in
the population that will be mutated
number of mutations = 0,1*24=2,4=2
Example 1
6. Mutasi
• Suppose generation of random number yield 10 and 16 then the
chromosome which have mutation are Chromosome number 3 gen number
2 , and chromosome number 4 gen number 4.
• The value of mutated gets at mutation point is replaced by random number
between 0-20.
Example 1
6. Mutasi
Suppose generated random number are 2 , 0 and 5 then Chromosome
composition after mutation are:
Chromosome[1] = [02;01;03;07]
Chromosome[2] = [02;01;08;03]
Chromosome[3] = [08;02;03;07]
Chromosome[4] = [01;01;04;03]
Chromosome[5] = [02;01;08;03]
Chromosome[6] = [08;05;08;03]
Example 1
• Now, we can evaluate the objective function after one generation:
The average of object function