SlideShare a Scribd company logo
1 of 64
Download to read offline
Genetic Algorithm (GA) Optimization –
Step-by-Step Example with Python
Implementation
Ahmed Fawzy Gad
ahmed.fawzy@ci.menofia.edu.eg
MENOUFIA UNIVERSITY
FACULTY OF COMPUTERS AND INFORMATION
ARTIFICIAL INTELLIGENCE
ALL DEPARTMENTS
‫المنوفية‬ ‫جامعة‬
‫الحاسبات‬ ‫كلية‬‫والمعلومات‬
‫اإلصطناعي‬ ‫الذكاء‬
‫األقسام‬ ‫جميع‬‫المنوفية‬ ‫جامعة‬
Training a Machine Learning Model
• Goal is to find the set of parameters (𝒘 𝟏:𝒘 𝟔) that maps the following
input to its output.
𝒚 = 𝒘 𝟏 𝒙 𝟏+𝒘 𝟐 𝒙 𝟐+𝒘 𝟑 𝒙 𝟑+𝒘 𝟒 𝒙 𝟒+𝒘 𝟓 𝒙 𝟓+𝒘 𝟔 𝒙 𝟔
𝒙 𝟏 𝒙 𝟐 𝒙 𝟑 𝒙 𝟒 𝒙 𝟓 𝒙 𝟔 𝒚
4 -2 7 5 11 1 44.1
𝒚′
= 𝟒𝒘 𝟏 − 𝟐𝒘 𝟐 + 𝟕𝒘 𝟑 + 𝟓𝒘 𝟒 + 𝟏𝟏𝒘 𝟓 + 𝒘 𝟔
Data
𝒘 𝟏 𝒘 𝟐 𝒘 𝟑 𝒘 𝟒 𝒘 𝟓 𝒘 𝟔
2.4 0.7 8 -2 5 1.1
Solution 1
𝒚′
= 𝟒𝒘 𝟏 − 𝟐𝒘 𝟐 + 𝟕𝒘 𝟑 + 𝟓𝒘 𝟒 + 𝟏𝟏𝒘 𝟓 + 𝒘 𝟔
𝒚′
= 𝟏𝟏𝟎. 𝟑
Absolute Error 𝒆𝒓𝒓𝒐𝒓 = |𝒚 − 𝒚′
|
𝒆𝒓𝒓𝒐𝒓 = 𝟔𝟔. 𝟐
𝒆𝒓𝒓𝒐𝒓 = 𝟒𝟒. 𝟏 − 𝟏𝟏𝟎. 𝟑
𝒘 𝟏 𝒘 𝟐 𝒘 𝟑 𝒘 𝟒 𝒘 𝟓 𝒘 𝟔
-0.4 2.7 5 -1 7 0.1
Solution 2
𝒚′
= 𝟏𝟎𝟎. 𝟏
Absolute Error 𝒆𝒓𝒓𝒐𝒓 = |𝒚 − 𝒚′
|
𝒆𝒓𝒓𝒐𝒓 = 𝟓𝟔
𝒆𝒓𝒓𝒐𝒓 = 𝟒𝟒. 𝟏 − 𝟏𝟎𝟎. 𝟏
𝒘 𝟏 𝒘 𝟐 𝒘 𝟑 𝒘 𝟒 𝒘 𝟓 𝒘 𝟔
-1 2 2 -3 2 0.9
Solution 3
𝒚′
= 𝟏𝟑. 𝟗
Absolute Error 𝒆𝒓𝒓𝒐𝒓 = |𝒚 − 𝒚′
|
𝒆𝒓𝒓𝒐𝒓 = 𝟑𝟎. 𝟐
𝒆𝒓𝒓𝒐𝒓 = 𝟒𝟒. 𝟏 − 𝟏𝟑. 𝟗
𝒘 𝟏 𝒘 𝟐 𝒘 𝟑 𝒘 𝟒 𝒘 𝟓 𝒘 𝟔
-1 2 2 -3 2 0.9
Solution 3
𝒚′
= 𝟏𝟑. 𝟗
Absolute Error 𝒆𝒓𝒓𝒐𝒓 = |𝒚 − 𝒚′
|
𝒆𝒓𝒓𝒐𝒓 = 𝟑𝟎. 𝟐
𝒆𝒓𝒓𝒐𝒓 = 𝟒𝟒. 𝟏 − 𝟏𝟑. 𝟗
Difficult to find the best solution manually.
Use optimization technique such as Genetic Algorithm (GA).
• Genetic algorithm is based on natural evolution of organisms.
• A brief biological background will be helpful in understanding GA.
Genetic Algorithm (GA)
• Genetic algorithm is based on natural evolution of organisms.
• A brief biological background will be helpful in understanding GA.
Chromosomes
Genes
Eye
Color
Dimples Freckles
Organism
Cells
Genetic Algorithm (GA)
• Genetic algorithm is based on natural evolution of organisms.
• A brief biological background will be helpful in understanding GA.
Chromosomes
Genes
Eye
Color
Dimples Freckles
Organism
Cells
Genetic Algorithm (GA)
https://www.icr.org/article/myth-human-evolution
• Genetic algorithm is based on natural evolution of organisms.
• A brief biological background will be helpful in understanding GA.
Chromosome
Genes
GA
Individuals/Solutions
Genetic Algorithm (GA)
Chromosomes
Genes
Eye
Color
Dimples Freckles
Organism
Cells
What are the Genes?
• Gene is anything that is able to enhance the results when changed.
• By exploring the following model, the 6 weights are able to enhance
the results. Thus each weight will represent a gene in GA.
𝒚 = 𝒘 𝟏 𝒙 𝟏+𝒘 𝟐 𝒙 𝟐+𝒘 𝟑 𝒙 𝟑+𝒘 𝟒 𝒙 𝟒+𝒘 𝟓 𝒙 𝟓+𝒘 𝟔 𝒙 𝟔
Gene 0 Gene 1 Gene 2 Gene 3 Gene 4 Gene 5
𝒘 𝟏 𝒘 𝟐 𝒘 𝟑 𝒘 𝟒 𝒘 𝟓 𝒘 𝟔
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
Population Size = 6
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
Chromosome
Gene
Survival of the Fittest
Fitness Function
Fitness Value
The Higher the Value,
the Better the Solution
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Survival of the Fittest
Fitness Function
Fitness Value
The Higher the Value,
the Better the Solution
𝑭 𝒄 =
𝟏
𝒆𝒓𝒓𝒐𝒓
=
𝟏
|𝒚 − 𝒚′|
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
𝒚′ 𝑭(𝑪)
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
𝒚′ 𝑭(𝑪)
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
𝒚′ 𝑭(𝑪)
𝒚′
= 𝟒𝒘 𝟏 − 𝟐𝒘 𝟐 + 𝟕𝒘 𝟑 + 𝟓𝒘 𝟒 + 𝟏𝟏𝒘 𝟓 + 𝒘 𝟔
𝒚′
= 𝟒 ∗ 𝟐. 𝟒 −2 ∗ 0.7 + 7 ∗ 8 + 5 ∗ -2 + 11 ∗ 5 + 𝟏. 𝟏
𝒚′
= 𝟏𝟏𝟎. 𝟑
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
𝒚′ 𝑭(𝑪)
110.3
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
𝒚′ 𝑭(𝑪)
110.3
𝑭 𝒄 =
𝟏
𝒆𝒓𝒓𝒐𝒓
=
𝟏
|𝟒𝟒. 𝟏 − 𝟏𝟏𝟎. 𝟑|
=
𝟏
𝟔𝟔. 𝟐
= 0.015
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
𝒚′ 𝑭(𝑪)
110.3 0.015
𝑭 𝒄 =
𝟏
𝒆𝒓𝒓𝒐𝒓
=
𝟏
|𝟒𝟒. 𝟏 − 𝟏𝟏𝟎. 𝟑|
=
𝟏
𝟔𝟔. 𝟐
= 0.015
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
𝒚′ 𝑭(𝑪)
110.3 0.015
100.1 0.018
13.9 0.033
127.9 0.012
69.2 0.0398
3 0.024
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Mating Pool
Select best individuals as parents for mating to
generate new individuals.
𝒚′ 𝑭(𝑪)
110.3 0.015
100.1 0.018
13.9 0.033
127.9 0.012
69.2 0.0398
3 0.024
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Mating Pool
Add best 3 individuals to the mating pool for producing
the next generation of solutions.
𝒚′ 𝑭(𝑪)
110.3 0.015
100.1 0.018
13.9 0.033
127.9 0.012
69.2 0.04
3 0.024
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Mating Pool
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Add best 3 individuals to the mating pool for producing
the next generation of solutions.
𝒚′ 𝑭(𝑪)
110.3 0.015
100.1 0.018
13.9 0.033
127.9 0.012
69.2 0.04
3 0.024
Mating Pool
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
Mating Pool
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
Mating Pool Crossover
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
Mating Pool Crossover
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
Mating Pool Crossover
-1 2 2 2.4 4.8 0Offspring
Mating Pool Mutation
-1 2 2 2.4 4.8 0Offspring
Mating Pool Mutation
-1 2 2 2.4 4.8 0Offspring
Mating Pool Mutation
-1 2 2 2.4 4.8 0Offspring
= 𝟒. 𝟖/𝟐
= 𝟐. 𝟒
Mating Pool Mutation
-1 2 2 2.4 4.8 0Offspring
= 𝟒. 𝟖/𝟐
= 𝟐. 𝟒
-1 2 2 2.4 2.4 0Mutant
Mating Pool
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Crossover
3.1 4 0 6 3 3Offspring
Mating Pool
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Crossover
3.1 4 0 6 3 3Offspring
Mutation
3.1 4 0 6 1.5 3Mutant
Mating Pool
-2 3 -7 6 3 3
Crossover
-2 3 -7 -3 2 0.9Offspring
-1 2 2 -3 2 0.9
Mating Pool
-2 3 -7 6 3 3
Crossover
-2 3 -7 -3 2 0.9Offspring
Mutation
-2 3 -7 -3 1 0.9Mutant
-1 2 2 -3 2 0.9
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
Old
Individuals
New
Individuals
New Population (Generation 1)
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
Old
Individuals
New
Individuals
New Population (Generation 1)
Why Reusing Old Individuals?
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
Old
Individuals
New
Individuals
New Population (Generation 1)
Why Reusing Old Individuals?
GA is a random-based optimization technique. There is no guarantee that the new individuals will be better
than the previous individuals. Keeping the old individuals at least saves the results from getting worse.
New Population (Generation 1)
Old
Individuals
New
Individuals
Population Mating Pool Crossover MutationFitness Value
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
New Population (Generation 1)
𝒚′ 𝑭(𝑪)
13.9 0.033
69.2 0.04
3 0.024
44.4 3.333
53.9 0.102
-66.1 0.009
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
New Population (Generation 1)
𝒚′ 𝑭(𝑪)
13.9 0.033
69.2 0.04
3 0.024
44.4 3.333
53.9 0.102
-66.1 0.009
Some new individuals make the results worse.
This is why it is preferred to keep the best old individuals.
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
New Population (Generation 1)
𝒚′ 𝑭(𝑪)
13.9 0.033
69.2 0.04
3 0.024
44.4 3.333
53.9 0.102
-66.1 0.009
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
New Population (Generation 1)
𝒚′ 𝑭(𝑪)
13.9 0.033
69.2 0.04
3 0.024
44.4 3.333
53.9 0.102
-66.1 0.009
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
Mating Pool
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
Mating Pool Crossover
3.1 4 0 2.4 2.4 0Offspring
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
Mating Pool Crossover
3.1 4 0 2.4 2.4 0Offspring
Mutation
3.1 4 0 2.4 1.2 0Mutant
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
Mating Pool
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
-1 2 2 6 1.5 3Offspring
Crossover
Mating Pool
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
-1 2 2 6 1.5 3Offspring
Mutation
-1 2 2 6 0.75 3Mutant
Crossover
Mating Pool
3.1 4 0 6 1.5 3
3.1 4 0 2.4 4.8 0Offspring
Crossover
3.1 4 0 2.4 4.8 0
Mating Pool
3.1 4 0 6 1.5 3
3.1 4 0 2.4 4.8 0Offspring
Mutation
3.1 4 0 2.4 2.4 0Mutant
Crossover
3.1 4 0 2.4 4.8 0
New Population (Generation 2)
Old
Individuals
New
Individuals
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
3.1 4 0 2.4 2.4 0
-1 2 2 6 0.75 3
3.1 4 0 2.4 1.2 0
New Population (Generation 2)
Old
Individuals
New
Individuals
Population Mating Pool Crossover MutationFitness Value
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
3.1 4 0 2.4 2.4 0
-1 2 2 6 0.75 3
3.1 4 0 2.4 1.2 0
New Population (Generation 2)
𝒚′ 𝑭(𝑪)
69.2 0.04
44.4 3.333
53.9 0.102
29.6 0.069
47.25 0.318
42.8 0.77
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
3.1 4 0 2.4 2.4 0
-1 2 2 6 0.75 3
3.1 4 0 2.4 1.2 0
New Population (Generation 2)
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
3.1 4 0 2.4 2.4 0
-1 2 2 6 0.75 3
3.1 4 0 2.4 1.2 0
𝒚′ 𝑭(𝑪)
69.2 0.04
44.4 3.333
53.9 0.102
29.6 0.069
47.25 0.318
42.8 0.77
New Population (Generation 2)
Continue.
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
3.1 4 0 2.4 2.4 0
-1 2 2 6 0.75 3
3.1 4 0 2.4 1.2 0
𝒚′ 𝑭(𝑪)
69.2 0.04
44.4 3.333
53.9 0.102
29.6 0.069
47.25 0.318
42.8 0.77
import numpy
import GARI
# Population size
sol_per_pop = 6
# Mating pool size
num_parents_mating = 3
# Creating an initial population randomly.
new_population = numpy.zeros((6, 6))
new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1]
new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1]
new_population[2, :] = [-1, 2, 2, -3, 2, 0.9]
new_population[3, :] = [4, 7, 12, 6.1, 1.4, -4]
new_population[4, :] = [3.1, 4, 0, 2.4, 4.8, 0]
new_population[5, :] = [-2, 3, -7, 6, 3, 3]
for iteration in range(10):
# Measing the fitness of each chromosome in the population.
qualities = GARI.pop_fitness(new_population)
# Selecting the best parents in the population for mating.
parents = GARI.select_mating_pool(new_population, qualities,
num_parents_mating)
# Generating next generation using crossover.
new_population = GARI.crossover(parents,
n_individuals=sol_per_pop)
new_population = GARI.mutation(new_population)
in_sample = [4, -2, 7, 5, 11, 1]
for k in range(6):
print(numpy.sum(new_population[k, :] * in_sample))
Python
Implementation
import numpy
import GARI
# Population size
sol_per_pop = 6
# Mating pool size
num_parents_mating = 3
# Creating an initial population randomly.
new_population = numpy.zeros((6, 6))
new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1]
new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1]
new_population[2, :] = [-1, 2, 2, -3, 2, 0.9]
new_population[3, :] = [4, 7, 12, 6.1, 1.4, -4]
new_population[4, :] = [3.1, 4, 0, 2.4, 4.8, 0]
new_population[5, :] = [-2, 3, -7, 6, 3, 3]
for iteration in range(10):
# Measing the fitness of each chromosome in the population.
qualities = GARI.pop_fitness(new_population)
# Selecting the best parents in the population for mating.
parents = GARI.select_mating_pool(new_population, qualities,
num_parents_mating)
# Generating next generation using crossover.
new_population = GARI.crossover(parents,
n_individuals=sol_per_pop)
new_population = GARI.mutation(new_population)
in_sample = [4, -2, 7, 5, 11, 1]
for k in range(6):
print(numpy.sum(new_population[k, :] * in_sample))
Python
Implementation
import numpy
import GARI
# Population size
sol_per_pop = 6
# Mating pool size
num_parents_mating = 3
# Creating an initial population randomly.
new_population = numpy.zeros((6, 6))
new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1]
new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1]
new_population[2, :] = [-1, 2, 2, -3, 2, 0.9]
new_population[3, :] = [4, 7, 12, 6.1, 1.4, -4]
new_population[4, :] = [3.1, 4, 0, 2.4, 4.8, 0]
new_population[5, :] = [-2, 3, -7, 6, 3, 3]
for iteration in range(10):
# Measing the fitness of each chromosome in the population.
qualities = GARI.pop_fitness(new_population)
# Selecting the best parents in the population for mating.
parents = GARI.select_mating_pool(new_population, qualities,
num_parents_mating)
# Generating next generation using crossover.
new_population = GARI.crossover(parents,
n_individuals=sol_per_pop)
new_population = GARI.mutation(new_population)
in_sample = [4, -2, 7, 5, 11, 1]
for k in range(6):
print(numpy.sum(new_population[k, :] * in_sample))
Python
Implementation
import numpy
import GARI
# Population size
sol_per_pop = 6
# Mating pool size
num_parents_mating = 3
# Creating an initial population randomly.
new_population = numpy.zeros((6, 6))
new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1]
new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1]
new_population[2, :] = [-1, 2, 2, -3, 2, 0.9]
new_population[3, :] = [4, 7, 12, 6.1, 1.4, -4]
new_population[4, :] = [3.1, 4, 0, 2.4, 4.8, 0]
new_population[5, :] = [-2, 3, -7, 6, 3, 3]
for iteration in range(10):
# Measing the fitness of each chromosome in the population.
qualities = GARI.pop_fitness(new_population)
# Selecting the best parents in the population for mating.
parents = GARI.select_mating_pool(new_population, qualities,
num_parents_mating)
# Generating next generation using crossover.
new_population = GARI.crossover(parents,
n_individuals=sol_per_pop)
new_population = GARI.mutation(new_population)
in_sample = [4, -2, 7, 5, 11, 1]
for k in range(6):
print(numpy.sum(new_population[k, :] * in_sample))
Python
Implementation
22.def crossover(parents, n_individuals=6):
23. new_population = numpy.empty((6,6))
24.
25. #Previous parents (best elements).
26. new_population[0:parents.shape[0], :] = parents
27. new_population[3, 0:3] = parents[0, 0:3]
28. new_population[3, 3:] = parents[1, 3:]
29. new_population[4, 0:3] = parents[1, 0:3]
30. new_population[4, 3:] = parents[2, 3:]
31. new_population[5, 0:3] = parents[2, 0:3]
32. new_population[5, 3:] = parents[0, 3:]
33.
34. return new_population
35.
36.def mutation(population):
37. for idx in range(population.shape[0]):
38. population[idx, 4] = population[idx, 4]/3
39. return population
1.import numpy
2.
3.def fitness_fun(indiv_chrom):
4. in_sample = [4,-2,7,5,11,1]
5. quality = 1/numpy.abs(44.1-numpy.sum(indiv_chrom*in_sample))
6. return quality
7.
8.def pop_fitness(pop):
9. qualities = numpy.zeros(pop.shape[0])
10. for indv_num in range(pop.shape[0]):
11. qualities[indv_num] = fitness_fun(pop[indv_num, :])
12. return qualities
13.
14.def select_mating_pool(pop, qualities, num_parents):
15. parents = numpy.empty((num_parents, pop.shape[1]))
16. for parent_num in range(num_parents):
17. max_qual_idx = numpy.where(qualities == numpy.max(qualities))
18. max_qual_idx = max_qual_idx[0][0]
19. parents[parent_num, :] = pop[max_qual_idx, :]
20. qualities[max_qual_idx] = -1
21. return parents
Python Implementation
References
• Yu, Xinjie, and Mitsuo Gen. Introduction to
evolutionary algorithms. Springer Science &
Business Media, 2010.
• https://www.kdnuggets.com/2018/03/introduc
tion-optimization-with-genetic-algorithm.html
• https://www.linkedin.com/pulse/introduction-
optimization-genetic-algorithm-ahmed-gad

More Related Content

What's hot

Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithmJari Abbas
 
Genetic algorithm ppt
Genetic algorithm pptGenetic algorithm ppt
Genetic algorithm pptMayank Jain
 
Optimization for Deep Learning
Optimization for Deep LearningOptimization for Deep Learning
Optimization for Deep LearningSebastian Ruder
 
Nature-Inspired Optimization Algorithms
Nature-Inspired Optimization Algorithms Nature-Inspired Optimization Algorithms
Nature-Inspired Optimization Algorithms Xin-She Yang
 
Optimization problems and algorithms
Optimization problems and  algorithmsOptimization problems and  algorithms
Optimization problems and algorithmsAboul Ella Hassanien
 
Recurrent Neural Network (RNN) | RNN LSTM Tutorial | Deep Learning Course | S...
Recurrent Neural Network (RNN) | RNN LSTM Tutorial | Deep Learning Course | S...Recurrent Neural Network (RNN) | RNN LSTM Tutorial | Deep Learning Course | S...
Recurrent Neural Network (RNN) | RNN LSTM Tutorial | Deep Learning Course | S...Simplilearn
 
MACHINE LEARNING - GENETIC ALGORITHM
MACHINE LEARNING - GENETIC ALGORITHMMACHINE LEARNING - GENETIC ALGORITHM
MACHINE LEARNING - GENETIC ALGORITHMPuneet Kulyana
 
Kalman filter for Beginners
Kalman filter for BeginnersKalman filter for Beginners
Kalman filter for Beginnerswinfred lu
 
fuzzy fuzzification and defuzzification
fuzzy fuzzification and defuzzificationfuzzy fuzzification and defuzzification
fuzzy fuzzification and defuzzificationNourhan Selem Salm
 
Stochastic Gradient Decent (SGD).pptx
Stochastic Gradient Decent (SGD).pptxStochastic Gradient Decent (SGD).pptx
Stochastic Gradient Decent (SGD).pptxShubham Jaybhaye
 
Seq2Seq (encoder decoder) model
Seq2Seq (encoder decoder) modelSeq2Seq (encoder decoder) model
Seq2Seq (encoder decoder) model佳蓉 倪
 

What's hot (20)

Genetic Algorithms
Genetic AlgorithmsGenetic Algorithms
Genetic Algorithms
 
Practical Swarm Optimization (PSO)
Practical Swarm Optimization (PSO)Practical Swarm Optimization (PSO)
Practical Swarm Optimization (PSO)
 
Genetic Algorithms
Genetic AlgorithmsGenetic Algorithms
Genetic Algorithms
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 
Genetic algorithm ppt
Genetic algorithm pptGenetic algorithm ppt
Genetic algorithm ppt
 
Fuzzy expert system
Fuzzy expert systemFuzzy expert system
Fuzzy expert system
 
Ga
GaGa
Ga
 
Optimization for Deep Learning
Optimization for Deep LearningOptimization for Deep Learning
Optimization for Deep Learning
 
Nature-Inspired Optimization Algorithms
Nature-Inspired Optimization Algorithms Nature-Inspired Optimization Algorithms
Nature-Inspired Optimization Algorithms
 
Optimization problems and algorithms
Optimization problems and  algorithmsOptimization problems and  algorithms
Optimization problems and algorithms
 
Grey wolf optimizer
Grey wolf optimizerGrey wolf optimizer
Grey wolf optimizer
 
Recurrent Neural Network (RNN) | RNN LSTM Tutorial | Deep Learning Course | S...
Recurrent Neural Network (RNN) | RNN LSTM Tutorial | Deep Learning Course | S...Recurrent Neural Network (RNN) | RNN LSTM Tutorial | Deep Learning Course | S...
Recurrent Neural Network (RNN) | RNN LSTM Tutorial | Deep Learning Course | S...
 
MACHINE LEARNING - GENETIC ALGORITHM
MACHINE LEARNING - GENETIC ALGORITHMMACHINE LEARNING - GENETIC ALGORITHM
MACHINE LEARNING - GENETIC ALGORITHM
 
Genetic Algorithm
Genetic AlgorithmGenetic Algorithm
Genetic Algorithm
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 
Kalman filter for Beginners
Kalman filter for BeginnersKalman filter for Beginners
Kalman filter for Beginners
 
fuzzy fuzzification and defuzzification
fuzzy fuzzification and defuzzificationfuzzy fuzzification and defuzzification
fuzzy fuzzification and defuzzification
 
Stochastic Gradient Decent (SGD).pptx
Stochastic Gradient Decent (SGD).pptxStochastic Gradient Decent (SGD).pptx
Stochastic Gradient Decent (SGD).pptx
 
Seq2Seq (encoder decoder) model
Seq2Seq (encoder decoder) modelSeq2Seq (encoder decoder) model
Seq2Seq (encoder decoder) model
 

Similar to Genetic Algorithm (GA) Optimization - Step-by-Step Example

Ansi+b+16 9+tee-reducciones
Ansi+b+16 9+tee-reduccionesAnsi+b+16 9+tee-reducciones
Ansi+b+16 9+tee-reduccionesarleto
 
4.4 M. Ángeles Rol
4.4 M. Ángeles Rol4.4 M. Ángeles Rol
4.4 M. Ángeles Rolbrnmomentum
 
Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...
Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...
Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...inventionjournals
 
Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...
Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...
Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...inventionjournals
 
bioinfo_6th_20070720
bioinfo_6th_20070720bioinfo_6th_20070720
bioinfo_6th_20070720sesejun
 
Thesis-presentation: Tuenti Engineering
Thesis-presentation: Tuenti EngineeringThesis-presentation: Tuenti Engineering
Thesis-presentation: Tuenti EngineeringMarcus Ljungblad
 
2º tablas-multiplicar-mini
2º tablas-multiplicar-mini2º tablas-multiplicar-mini
2º tablas-multiplicar-minicarolian4
 
Missing Data in Research on Child Maltreatment
Missing Data in Research on Child MaltreatmentMissing Data in Research on Child Maltreatment
Missing Data in Research on Child MaltreatmentBASPCAN
 
Christian Hennig- Assessing the quality of a clustering
Christian Hennig- Assessing the quality of a clusteringChristian Hennig- Assessing the quality of a clustering
Christian Hennig- Assessing the quality of a clusteringPyData
 
Final Presentation
Final PresentationFinal Presentation
Final PresentationCody Deitz
 
A Presentation on "Human Action Recognition Based on a Sequential Deep Learni...
A Presentation on "Human Action Recognition Based on a Sequential Deep Learni...A Presentation on "Human Action Recognition Based on a Sequential Deep Learni...
A Presentation on "Human Action Recognition Based on a Sequential Deep Learni...Niloy Sikder
 
Map_Sheet Example_140514_FIT
Map_Sheet Example_140514_FITMap_Sheet Example_140514_FIT
Map_Sheet Example_140514_FITshakeel ahmad
 
Mechanical Set Packers
Mechanical Set PackersMechanical Set Packers
Mechanical Set Packersdicoiltools
 
Matriz ensamblada
Matriz ensambladaMatriz ensamblada
Matriz ensambladairanchoque1
 
Professional Center Jerome Ave Parking Layout New
Professional Center Jerome Ave Parking Layout NewProfessional Center Jerome Ave Parking Layout New
Professional Center Jerome Ave Parking Layout NewMark Testa
 

Similar to Genetic Algorithm (GA) Optimization - Step-by-Step Example (20)

Ansi+b+16 9+tee-reducciones
Ansi+b+16 9+tee-reduccionesAnsi+b+16 9+tee-reducciones
Ansi+b+16 9+tee-reducciones
 
Sequence Analysis.ppt
Sequence Analysis.pptSequence Analysis.ppt
Sequence Analysis.ppt
 
Record form wais r
Record form wais rRecord form wais r
Record form wais r
 
4.4 M. Ángeles Rol
4.4 M. Ángeles Rol4.4 M. Ángeles Rol
4.4 M. Ángeles Rol
 
Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...
Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...
Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...
 
Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...
Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...
Solving Poisson’s Equation Using Preconditioned Nine-Point Group SOR Iterativ...
 
bioinfo_6th_20070720
bioinfo_6th_20070720bioinfo_6th_20070720
bioinfo_6th_20070720
 
Thesis-presentation: Tuenti Engineering
Thesis-presentation: Tuenti EngineeringThesis-presentation: Tuenti Engineering
Thesis-presentation: Tuenti Engineering
 
2º tablas-multiplicar-mini
2º tablas-multiplicar-mini2º tablas-multiplicar-mini
2º tablas-multiplicar-mini
 
Missing Data in Research on Child Maltreatment
Missing Data in Research on Child MaltreatmentMissing Data in Research on Child Maltreatment
Missing Data in Research on Child Maltreatment
 
Christian Hennig- Assessing the quality of a clustering
Christian Hennig- Assessing the quality of a clusteringChristian Hennig- Assessing the quality of a clustering
Christian Hennig- Assessing the quality of a clustering
 
Dmxchart
DmxchartDmxchart
Dmxchart
 
Accelerate performance
Accelerate performanceAccelerate performance
Accelerate performance
 
Final Presentation
Final PresentationFinal Presentation
Final Presentation
 
A Presentation on "Human Action Recognition Based on a Sequential Deep Learni...
A Presentation on "Human Action Recognition Based on a Sequential Deep Learni...A Presentation on "Human Action Recognition Based on a Sequential Deep Learni...
A Presentation on "Human Action Recognition Based on a Sequential Deep Learni...
 
Map_Sheet Example_140514_FIT
Map_Sheet Example_140514_FITMap_Sheet Example_140514_FIT
Map_Sheet Example_140514_FIT
 
Mechanical Set Packers
Mechanical Set PackersMechanical Set Packers
Mechanical Set Packers
 
E1 f4 bộ binh
E1 f4 bộ binhE1 f4 bộ binh
E1 f4 bộ binh
 
Matriz ensamblada
Matriz ensambladaMatriz ensamblada
Matriz ensamblada
 
Professional Center Jerome Ave Parking Layout New
Professional Center Jerome Ave Parking Layout NewProfessional Center Jerome Ave Parking Layout New
Professional Center Jerome Ave Parking Layout New
 

More from Ahmed Gad

ICEIT'20 Cython for Speeding-up Genetic Algorithm
ICEIT'20 Cython for Speeding-up Genetic AlgorithmICEIT'20 Cython for Speeding-up Genetic Algorithm
ICEIT'20 Cython for Speeding-up Genetic AlgorithmAhmed Gad
 
NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...
NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...
NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...Ahmed Gad
 
Python for Computer Vision - Revision 2nd Edition
Python for Computer Vision - Revision 2nd EditionPython for Computer Vision - Revision 2nd Edition
Python for Computer Vision - Revision 2nd EditionAhmed Gad
 
Multi-Objective Optimization using Non-Dominated Sorting Genetic Algorithm wi...
Multi-Objective Optimization using Non-Dominated Sorting Genetic Algorithm wi...Multi-Objective Optimization using Non-Dominated Sorting Genetic Algorithm wi...
Multi-Objective Optimization using Non-Dominated Sorting Genetic Algorithm wi...Ahmed Gad
 
M.Sc. Thesis - Automatic People Counting in Crowded Scenes
M.Sc. Thesis - Automatic People Counting in Crowded ScenesM.Sc. Thesis - Automatic People Counting in Crowded Scenes
M.Sc. Thesis - Automatic People Counting in Crowded ScenesAhmed Gad
 
Derivation of Convolutional Neural Network from Fully Connected Network Step-...
Derivation of Convolutional Neural Network from Fully Connected Network Step-...Derivation of Convolutional Neural Network from Fully Connected Network Step-...
Derivation of Convolutional Neural Network from Fully Connected Network Step-...Ahmed Gad
 
Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...
Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...
Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...Ahmed Gad
 
Avoid Overfitting with Regularization
Avoid Overfitting with RegularizationAvoid Overfitting with Regularization
Avoid Overfitting with RegularizationAhmed Gad
 
ICCES 2017 - Crowd Density Estimation Method using Regression Analysis
ICCES 2017 - Crowd Density Estimation Method using Regression AnalysisICCES 2017 - Crowd Density Estimation Method using Regression Analysis
ICCES 2017 - Crowd Density Estimation Method using Regression AnalysisAhmed Gad
 
Backpropagation: Understanding How to Update ANNs Weights Step-by-Step
Backpropagation: Understanding How to Update ANNs Weights Step-by-StepBackpropagation: Understanding How to Update ANNs Weights Step-by-Step
Backpropagation: Understanding How to Update ANNs Weights Step-by-StepAhmed Gad
 
Computer Vision: Correlation, Convolution, and Gradient
Computer Vision: Correlation, Convolution, and GradientComputer Vision: Correlation, Convolution, and Gradient
Computer Vision: Correlation, Convolution, and GradientAhmed Gad
 
Python for Computer Vision - Revision
Python for Computer Vision - RevisionPython for Computer Vision - Revision
Python for Computer Vision - RevisionAhmed Gad
 
Anime Studio Pro 10 Tutorial as Part of Multimedia Course
Anime Studio Pro 10 Tutorial as Part of Multimedia CourseAnime Studio Pro 10 Tutorial as Part of Multimedia Course
Anime Studio Pro 10 Tutorial as Part of Multimedia CourseAhmed Gad
 
Brief Introduction to Deep Learning + Solving XOR using ANNs
Brief Introduction to Deep Learning + Solving XOR using ANNsBrief Introduction to Deep Learning + Solving XOR using ANNs
Brief Introduction to Deep Learning + Solving XOR using ANNsAhmed Gad
 
Operations in Digital Image Processing + Convolution by Example
Operations in Digital Image Processing + Convolution by ExampleOperations in Digital Image Processing + Convolution by Example
Operations in Digital Image Processing + Convolution by ExampleAhmed Gad
 
MATLAB Code + Description : Real-Time Object Motion Detection and Tracking
MATLAB Code + Description : Real-Time Object Motion Detection and TrackingMATLAB Code + Description : Real-Time Object Motion Detection and Tracking
MATLAB Code + Description : Real-Time Object Motion Detection and TrackingAhmed Gad
 
MATLAB Code + Description : Very Simple Automatic English Optical Character R...
MATLAB Code + Description : Very Simple Automatic English Optical Character R...MATLAB Code + Description : Very Simple Automatic English Optical Character R...
MATLAB Code + Description : Very Simple Automatic English Optical Character R...Ahmed Gad
 
Graduation Project - Face Login : A Robust Face Identification System for Sec...
Graduation Project - Face Login : A Robust Face Identification System for Sec...Graduation Project - Face Login : A Robust Face Identification System for Sec...
Graduation Project - Face Login : A Robust Face Identification System for Sec...Ahmed Gad
 
Introduction to MATrices LABoratory (MATLAB) as Part of Digital Signal Proces...
Introduction to MATrices LABoratory (MATLAB) as Part of Digital Signal Proces...Introduction to MATrices LABoratory (MATLAB) as Part of Digital Signal Proces...
Introduction to MATrices LABoratory (MATLAB) as Part of Digital Signal Proces...Ahmed Gad
 
Introduction to Digital Signal Processing (DSP) - Course Notes
Introduction to Digital Signal Processing (DSP) - Course NotesIntroduction to Digital Signal Processing (DSP) - Course Notes
Introduction to Digital Signal Processing (DSP) - Course NotesAhmed Gad
 

More from Ahmed Gad (20)

ICEIT'20 Cython for Speeding-up Genetic Algorithm
ICEIT'20 Cython for Speeding-up Genetic AlgorithmICEIT'20 Cython for Speeding-up Genetic Algorithm
ICEIT'20 Cython for Speeding-up Genetic Algorithm
 
NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...
NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...
NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...
 
Python for Computer Vision - Revision 2nd Edition
Python for Computer Vision - Revision 2nd EditionPython for Computer Vision - Revision 2nd Edition
Python for Computer Vision - Revision 2nd Edition
 
Multi-Objective Optimization using Non-Dominated Sorting Genetic Algorithm wi...
Multi-Objective Optimization using Non-Dominated Sorting Genetic Algorithm wi...Multi-Objective Optimization using Non-Dominated Sorting Genetic Algorithm wi...
Multi-Objective Optimization using Non-Dominated Sorting Genetic Algorithm wi...
 
M.Sc. Thesis - Automatic People Counting in Crowded Scenes
M.Sc. Thesis - Automatic People Counting in Crowded ScenesM.Sc. Thesis - Automatic People Counting in Crowded Scenes
M.Sc. Thesis - Automatic People Counting in Crowded Scenes
 
Derivation of Convolutional Neural Network from Fully Connected Network Step-...
Derivation of Convolutional Neural Network from Fully Connected Network Step-...Derivation of Convolutional Neural Network from Fully Connected Network Step-...
Derivation of Convolutional Neural Network from Fully Connected Network Step-...
 
Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...
Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...
Derivation of Convolutional Neural Network (ConvNet) from Fully Connected Net...
 
Avoid Overfitting with Regularization
Avoid Overfitting with RegularizationAvoid Overfitting with Regularization
Avoid Overfitting with Regularization
 
ICCES 2017 - Crowd Density Estimation Method using Regression Analysis
ICCES 2017 - Crowd Density Estimation Method using Regression AnalysisICCES 2017 - Crowd Density Estimation Method using Regression Analysis
ICCES 2017 - Crowd Density Estimation Method using Regression Analysis
 
Backpropagation: Understanding How to Update ANNs Weights Step-by-Step
Backpropagation: Understanding How to Update ANNs Weights Step-by-StepBackpropagation: Understanding How to Update ANNs Weights Step-by-Step
Backpropagation: Understanding How to Update ANNs Weights Step-by-Step
 
Computer Vision: Correlation, Convolution, and Gradient
Computer Vision: Correlation, Convolution, and GradientComputer Vision: Correlation, Convolution, and Gradient
Computer Vision: Correlation, Convolution, and Gradient
 
Python for Computer Vision - Revision
Python for Computer Vision - RevisionPython for Computer Vision - Revision
Python for Computer Vision - Revision
 
Anime Studio Pro 10 Tutorial as Part of Multimedia Course
Anime Studio Pro 10 Tutorial as Part of Multimedia CourseAnime Studio Pro 10 Tutorial as Part of Multimedia Course
Anime Studio Pro 10 Tutorial as Part of Multimedia Course
 
Brief Introduction to Deep Learning + Solving XOR using ANNs
Brief Introduction to Deep Learning + Solving XOR using ANNsBrief Introduction to Deep Learning + Solving XOR using ANNs
Brief Introduction to Deep Learning + Solving XOR using ANNs
 
Operations in Digital Image Processing + Convolution by Example
Operations in Digital Image Processing + Convolution by ExampleOperations in Digital Image Processing + Convolution by Example
Operations in Digital Image Processing + Convolution by Example
 
MATLAB Code + Description : Real-Time Object Motion Detection and Tracking
MATLAB Code + Description : Real-Time Object Motion Detection and TrackingMATLAB Code + Description : Real-Time Object Motion Detection and Tracking
MATLAB Code + Description : Real-Time Object Motion Detection and Tracking
 
MATLAB Code + Description : Very Simple Automatic English Optical Character R...
MATLAB Code + Description : Very Simple Automatic English Optical Character R...MATLAB Code + Description : Very Simple Automatic English Optical Character R...
MATLAB Code + Description : Very Simple Automatic English Optical Character R...
 
Graduation Project - Face Login : A Robust Face Identification System for Sec...
Graduation Project - Face Login : A Robust Face Identification System for Sec...Graduation Project - Face Login : A Robust Face Identification System for Sec...
Graduation Project - Face Login : A Robust Face Identification System for Sec...
 
Introduction to MATrices LABoratory (MATLAB) as Part of Digital Signal Proces...
Introduction to MATrices LABoratory (MATLAB) as Part of Digital Signal Proces...Introduction to MATrices LABoratory (MATLAB) as Part of Digital Signal Proces...
Introduction to MATrices LABoratory (MATLAB) as Part of Digital Signal Proces...
 
Introduction to Digital Signal Processing (DSP) - Course Notes
Introduction to Digital Signal Processing (DSP) - Course NotesIntroduction to Digital Signal Processing (DSP) - Course Notes
Introduction to Digital Signal Processing (DSP) - Course Notes
 

Recently uploaded

Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfBoston Institute of Analytics
 
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...ssuserf63bd7
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...limedy534
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in collegessuser7a7cd61
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
Identifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanIdentifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanMYRABACSAFRA2
 
Semantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxSemantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxMike Bennett
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.natarajan8993
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Cantervoginip
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
Heart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis ProjectHeart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis ProjectBoston Institute of Analytics
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhijennyeacort
 
Defining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryDefining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryJeremy Anderson
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 

Recently uploaded (20)

Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdfPredicting Salary Using Data Science: A Comprehensive Analysis.pdf
Predicting Salary Using Data Science: A Comprehensive Analysis.pdf
 
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
Statistics, Data Analysis, and Decision Modeling, 5th edition by James R. Eva...
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in college
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
Identifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanIdentifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population Mean
 
Semantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxSemantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptx
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Canter
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
Heart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis ProjectHeart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis Project
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
 
Defining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryDefining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data Story
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
 

Genetic Algorithm (GA) Optimization - Step-by-Step Example

  • 1. Genetic Algorithm (GA) Optimization – Step-by-Step Example with Python Implementation Ahmed Fawzy Gad ahmed.fawzy@ci.menofia.edu.eg MENOUFIA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATION ARTIFICIAL INTELLIGENCE ALL DEPARTMENTS ‫المنوفية‬ ‫جامعة‬ ‫الحاسبات‬ ‫كلية‬‫والمعلومات‬ ‫اإلصطناعي‬ ‫الذكاء‬ ‫األقسام‬ ‫جميع‬‫المنوفية‬ ‫جامعة‬
  • 2. Training a Machine Learning Model • Goal is to find the set of parameters (𝒘 𝟏:𝒘 𝟔) that maps the following input to its output. 𝒚 = 𝒘 𝟏 𝒙 𝟏+𝒘 𝟐 𝒙 𝟐+𝒘 𝟑 𝒙 𝟑+𝒘 𝟒 𝒙 𝟒+𝒘 𝟓 𝒙 𝟓+𝒘 𝟔 𝒙 𝟔 𝒙 𝟏 𝒙 𝟐 𝒙 𝟑 𝒙 𝟒 𝒙 𝟓 𝒙 𝟔 𝒚 4 -2 7 5 11 1 44.1 𝒚′ = 𝟒𝒘 𝟏 − 𝟐𝒘 𝟐 + 𝟕𝒘 𝟑 + 𝟓𝒘 𝟒 + 𝟏𝟏𝒘 𝟓 + 𝒘 𝟔 Data
  • 3. 𝒘 𝟏 𝒘 𝟐 𝒘 𝟑 𝒘 𝟒 𝒘 𝟓 𝒘 𝟔 2.4 0.7 8 -2 5 1.1 Solution 1 𝒚′ = 𝟒𝒘 𝟏 − 𝟐𝒘 𝟐 + 𝟕𝒘 𝟑 + 𝟓𝒘 𝟒 + 𝟏𝟏𝒘 𝟓 + 𝒘 𝟔 𝒚′ = 𝟏𝟏𝟎. 𝟑 Absolute Error 𝒆𝒓𝒓𝒐𝒓 = |𝒚 − 𝒚′ | 𝒆𝒓𝒓𝒐𝒓 = 𝟔𝟔. 𝟐 𝒆𝒓𝒓𝒐𝒓 = 𝟒𝟒. 𝟏 − 𝟏𝟏𝟎. 𝟑
  • 4. 𝒘 𝟏 𝒘 𝟐 𝒘 𝟑 𝒘 𝟒 𝒘 𝟓 𝒘 𝟔 -0.4 2.7 5 -1 7 0.1 Solution 2 𝒚′ = 𝟏𝟎𝟎. 𝟏 Absolute Error 𝒆𝒓𝒓𝒐𝒓 = |𝒚 − 𝒚′ | 𝒆𝒓𝒓𝒐𝒓 = 𝟓𝟔 𝒆𝒓𝒓𝒐𝒓 = 𝟒𝟒. 𝟏 − 𝟏𝟎𝟎. 𝟏
  • 5. 𝒘 𝟏 𝒘 𝟐 𝒘 𝟑 𝒘 𝟒 𝒘 𝟓 𝒘 𝟔 -1 2 2 -3 2 0.9 Solution 3 𝒚′ = 𝟏𝟑. 𝟗 Absolute Error 𝒆𝒓𝒓𝒐𝒓 = |𝒚 − 𝒚′ | 𝒆𝒓𝒓𝒐𝒓 = 𝟑𝟎. 𝟐 𝒆𝒓𝒓𝒐𝒓 = 𝟒𝟒. 𝟏 − 𝟏𝟑. 𝟗
  • 6. 𝒘 𝟏 𝒘 𝟐 𝒘 𝟑 𝒘 𝟒 𝒘 𝟓 𝒘 𝟔 -1 2 2 -3 2 0.9 Solution 3 𝒚′ = 𝟏𝟑. 𝟗 Absolute Error 𝒆𝒓𝒓𝒐𝒓 = |𝒚 − 𝒚′ | 𝒆𝒓𝒓𝒐𝒓 = 𝟑𝟎. 𝟐 𝒆𝒓𝒓𝒐𝒓 = 𝟒𝟒. 𝟏 − 𝟏𝟑. 𝟗 Difficult to find the best solution manually. Use optimization technique such as Genetic Algorithm (GA).
  • 7. • Genetic algorithm is based on natural evolution of organisms. • A brief biological background will be helpful in understanding GA. Genetic Algorithm (GA)
  • 8. • Genetic algorithm is based on natural evolution of organisms. • A brief biological background will be helpful in understanding GA. Chromosomes Genes Eye Color Dimples Freckles Organism Cells Genetic Algorithm (GA)
  • 9. • Genetic algorithm is based on natural evolution of organisms. • A brief biological background will be helpful in understanding GA. Chromosomes Genes Eye Color Dimples Freckles Organism Cells Genetic Algorithm (GA) https://www.icr.org/article/myth-human-evolution
  • 10. • Genetic algorithm is based on natural evolution of organisms. • A brief biological background will be helpful in understanding GA. Chromosome Genes GA Individuals/Solutions Genetic Algorithm (GA) Chromosomes Genes Eye Color Dimples Freckles Organism Cells
  • 11. What are the Genes? • Gene is anything that is able to enhance the results when changed. • By exploring the following model, the 6 weights are able to enhance the results. Thus each weight will represent a gene in GA. 𝒚 = 𝒘 𝟏 𝒙 𝟏+𝒘 𝟐 𝒙 𝟐+𝒘 𝟑 𝒙 𝟑+𝒘 𝟒 𝒙 𝟒+𝒘 𝟓 𝒙 𝟓+𝒘 𝟔 𝒙 𝟔 Gene 0 Gene 1 Gene 2 Gene 3 Gene 4 Gene 5 𝒘 𝟏 𝒘 𝟐 𝒘 𝟑 𝒘 𝟒 𝒘 𝟓 𝒘 𝟔
  • 12. 2.4 0.7 8 -2 5 1.1 -0.4 2.7 5 -1 7 0.1 -1 2 2 -3 2 0.9 4 7 12 6.1 1.4 -4 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3 Initial Population of Solutions (Generation 0) Population Size = 6
  • 13. 2.4 0.7 8 -2 5 1.1 -0.4 2.7 5 -1 7 0.1 -1 2 2 -3 2 0.9 4 7 12 6.1 1.4 -4 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3 Initial Population of Solutions (Generation 0) Chromosome Gene
  • 14. Survival of the Fittest Fitness Function Fitness Value The Higher the Value, the Better the Solution Initial Population of Solutions (Generation 0) 2.4 0.7 8 -2 5 1.1 -0.4 2.7 5 -1 7 0.1 -1 2 2 -3 2 0.9 4 7 12 6.1 1.4 -4 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3
  • 15. Survival of the Fittest Fitness Function Fitness Value The Higher the Value, the Better the Solution 𝑭 𝒄 = 𝟏 𝒆𝒓𝒓𝒐𝒓 = 𝟏 |𝒚 − 𝒚′| Initial Population of Solutions (Generation 0) 2.4 0.7 8 -2 5 1.1 -0.4 2.7 5 -1 7 0.1 -1 2 2 -3 2 0.9 4 7 12 6.1 1.4 -4 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3
  • 16. Initial Population of Solutions (Generation 0) 𝒚′ 𝑭(𝑪) 2.4 0.7 8 -2 5 1.1 -0.4 2.7 5 -1 7 0.1 -1 2 2 -3 2 0.9 4 7 12 6.1 1.4 -4 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3
  • 17. Initial Population of Solutions (Generation 0) 2.4 0.7 8 -2 5 1.1 𝒚′ 𝑭(𝑪) -0.4 2.7 5 -1 7 0.1 -1 2 2 -3 2 0.9 4 7 12 6.1 1.4 -4 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3
  • 18. Initial Population of Solutions (Generation 0) 2.4 0.7 8 -2 5 1.1 𝒚′ 𝑭(𝑪) 𝒚′ = 𝟒𝒘 𝟏 − 𝟐𝒘 𝟐 + 𝟕𝒘 𝟑 + 𝟓𝒘 𝟒 + 𝟏𝟏𝒘 𝟓 + 𝒘 𝟔 𝒚′ = 𝟒 ∗ 𝟐. 𝟒 −2 ∗ 0.7 + 7 ∗ 8 + 5 ∗ -2 + 11 ∗ 5 + 𝟏. 𝟏 𝒚′ = 𝟏𝟏𝟎. 𝟑 -0.4 2.7 5 -1 7 0.1 -1 2 2 -3 2 0.9 4 7 12 6.1 1.4 -4 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3
  • 19. Initial Population of Solutions (Generation 0) 2.4 0.7 8 -2 5 1.1 𝒚′ 𝑭(𝑪) 110.3 -0.4 2.7 5 -1 7 0.1 -1 2 2 -3 2 0.9 4 7 12 6.1 1.4 -4 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3
  • 20. Initial Population of Solutions (Generation 0) 2.4 0.7 8 -2 5 1.1 𝒚′ 𝑭(𝑪) 110.3 𝑭 𝒄 = 𝟏 𝒆𝒓𝒓𝒐𝒓 = 𝟏 |𝟒𝟒. 𝟏 − 𝟏𝟏𝟎. 𝟑| = 𝟏 𝟔𝟔. 𝟐 = 0.015 -0.4 2.7 5 -1 7 0.1 -1 2 2 -3 2 0.9 4 7 12 6.1 1.4 -4 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3
  • 21. Initial Population of Solutions (Generation 0) 2.4 0.7 8 -2 5 1.1 𝒚′ 𝑭(𝑪) 110.3 0.015 𝑭 𝒄 = 𝟏 𝒆𝒓𝒓𝒐𝒓 = 𝟏 |𝟒𝟒. 𝟏 − 𝟏𝟏𝟎. 𝟑| = 𝟏 𝟔𝟔. 𝟐 = 0.015 -0.4 2.7 5 -1 7 0.1 -1 2 2 -3 2 0.9 4 7 12 6.1 1.4 -4 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3
  • 22. Initial Population of Solutions (Generation 0) 2.4 0.7 8 -2 5 1.1 𝒚′ 𝑭(𝑪) 110.3 0.015 100.1 0.018 13.9 0.033 127.9 0.012 69.2 0.0398 3 0.024 -0.4 2.7 5 -1 7 0.1 -1 2 2 -3 2 0.9 4 7 12 6.1 1.4 -4 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3
  • 23. Mating Pool Select best individuals as parents for mating to generate new individuals. 𝒚′ 𝑭(𝑪) 110.3 0.015 100.1 0.018 13.9 0.033 127.9 0.012 69.2 0.0398 3 0.024 2.4 0.7 8 -2 5 1.1 -0.4 2.7 5 -1 7 0.1 -1 2 2 -3 2 0.9 4 7 12 6.1 1.4 -4 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3
  • 24. Mating Pool Add best 3 individuals to the mating pool for producing the next generation of solutions. 𝒚′ 𝑭(𝑪) 110.3 0.015 100.1 0.018 13.9 0.033 127.9 0.012 69.2 0.04 3 0.024 2.4 0.7 8 -2 5 1.1 -0.4 2.7 5 -1 7 0.1 -1 2 2 -3 2 0.9 4 7 12 6.1 1.4 -4 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3
  • 25. Mating Pool 2.4 0.7 8 -2 5 1.1 -0.4 2.7 5 -1 7 0.1 -1 2 2 -3 2 0.9 4 7 12 6.1 1.4 -4 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3 Add best 3 individuals to the mating pool for producing the next generation of solutions. 𝒚′ 𝑭(𝑪) 110.3 0.015 100.1 0.018 13.9 0.033 127.9 0.012 69.2 0.04 3 0.024
  • 26. Mating Pool -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3
  • 27. -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 Mating Pool
  • 28. -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 Mating Pool Crossover
  • 29. -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 Mating Pool Crossover
  • 30. -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 Mating Pool Crossover -1 2 2 2.4 4.8 0Offspring
  • 31. Mating Pool Mutation -1 2 2 2.4 4.8 0Offspring
  • 32. Mating Pool Mutation -1 2 2 2.4 4.8 0Offspring
  • 33. Mating Pool Mutation -1 2 2 2.4 4.8 0Offspring = 𝟒. 𝟖/𝟐 = 𝟐. 𝟒
  • 34. Mating Pool Mutation -1 2 2 2.4 4.8 0Offspring = 𝟒. 𝟖/𝟐 = 𝟐. 𝟒 -1 2 2 2.4 2.4 0Mutant
  • 35. Mating Pool 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3 Crossover 3.1 4 0 6 3 3Offspring
  • 36. Mating Pool 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3 Crossover 3.1 4 0 6 3 3Offspring Mutation 3.1 4 0 6 1.5 3Mutant
  • 37. Mating Pool -2 3 -7 6 3 3 Crossover -2 3 -7 -3 2 0.9Offspring -1 2 2 -3 2 0.9
  • 38. Mating Pool -2 3 -7 6 3 3 Crossover -2 3 -7 -3 2 0.9Offspring Mutation -2 3 -7 -3 1 0.9Mutant -1 2 2 -3 2 0.9
  • 39. -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3 -2 3 -7 -3 1 0.9 3.1 4 0 6 1.5 3 -1 2 2 2.4 2.4 0 Old Individuals New Individuals New Population (Generation 1)
  • 40. -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3 -2 3 -7 -3 1 0.9 3.1 4 0 6 1.5 3 -1 2 2 2.4 2.4 0 Old Individuals New Individuals New Population (Generation 1) Why Reusing Old Individuals?
  • 41. -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3 -2 3 -7 -3 1 0.9 3.1 4 0 6 1.5 3 -1 2 2 2.4 2.4 0 Old Individuals New Individuals New Population (Generation 1) Why Reusing Old Individuals? GA is a random-based optimization technique. There is no guarantee that the new individuals will be better than the previous individuals. Keeping the old individuals at least saves the results from getting worse.
  • 42. New Population (Generation 1) Old Individuals New Individuals Population Mating Pool Crossover MutationFitness Value -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3 -2 3 -7 -3 1 0.9 3.1 4 0 6 1.5 3 -1 2 2 2.4 2.4 0
  • 43. New Population (Generation 1) 𝒚′ 𝑭(𝑪) 13.9 0.033 69.2 0.04 3 0.024 44.4 3.333 53.9 0.102 -66.1 0.009 -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3 -2 3 -7 -3 1 0.9 3.1 4 0 6 1.5 3 -1 2 2 2.4 2.4 0
  • 44. New Population (Generation 1) 𝒚′ 𝑭(𝑪) 13.9 0.033 69.2 0.04 3 0.024 44.4 3.333 53.9 0.102 -66.1 0.009 Some new individuals make the results worse. This is why it is preferred to keep the best old individuals. -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3 -2 3 -7 -3 1 0.9 3.1 4 0 6 1.5 3 -1 2 2 2.4 2.4 0
  • 45. New Population (Generation 1) 𝒚′ 𝑭(𝑪) 13.9 0.033 69.2 0.04 3 0.024 44.4 3.333 53.9 0.102 -66.1 0.009 -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3 -2 3 -7 -3 1 0.9 3.1 4 0 6 1.5 3 -1 2 2 2.4 2.4 0
  • 46. New Population (Generation 1) 𝒚′ 𝑭(𝑪) 13.9 0.033 69.2 0.04 3 0.024 44.4 3.333 53.9 0.102 -66.1 0.009 -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 -2 3 -7 6 3 3 -2 3 -7 -3 1 0.9 3.1 4 0 6 1.5 3 -1 2 2 2.4 2.4 0
  • 47. Mating Pool 3.1 4 0 2.4 4.8 0 -1 2 2 2.4 2.4 0 3.1 4 0 6 1.5 3
  • 48. Mating Pool Crossover 3.1 4 0 2.4 2.4 0Offspring 3.1 4 0 2.4 4.8 0 -1 2 2 2.4 2.4 0
  • 49. Mating Pool Crossover 3.1 4 0 2.4 2.4 0Offspring Mutation 3.1 4 0 2.4 1.2 0Mutant 3.1 4 0 2.4 4.8 0 -1 2 2 2.4 2.4 0
  • 50. Mating Pool -1 2 2 2.4 2.4 0 3.1 4 0 6 1.5 3 -1 2 2 6 1.5 3Offspring Crossover
  • 51. Mating Pool -1 2 2 2.4 2.4 0 3.1 4 0 6 1.5 3 -1 2 2 6 1.5 3Offspring Mutation -1 2 2 6 0.75 3Mutant Crossover
  • 52. Mating Pool 3.1 4 0 6 1.5 3 3.1 4 0 2.4 4.8 0Offspring Crossover 3.1 4 0 2.4 4.8 0
  • 53. Mating Pool 3.1 4 0 6 1.5 3 3.1 4 0 2.4 4.8 0Offspring Mutation 3.1 4 0 2.4 2.4 0Mutant Crossover 3.1 4 0 2.4 4.8 0
  • 54. New Population (Generation 2) Old Individuals New Individuals 3.1 4 0 2.4 4.8 0 -1 2 2 2.4 2.4 0 3.1 4 0 6 1.5 3 3.1 4 0 2.4 2.4 0 -1 2 2 6 0.75 3 3.1 4 0 2.4 1.2 0
  • 55. New Population (Generation 2) Old Individuals New Individuals Population Mating Pool Crossover MutationFitness Value 3.1 4 0 2.4 4.8 0 -1 2 2 2.4 2.4 0 3.1 4 0 6 1.5 3 3.1 4 0 2.4 2.4 0 -1 2 2 6 0.75 3 3.1 4 0 2.4 1.2 0
  • 56. New Population (Generation 2) 𝒚′ 𝑭(𝑪) 69.2 0.04 44.4 3.333 53.9 0.102 29.6 0.069 47.25 0.318 42.8 0.77 3.1 4 0 2.4 4.8 0 -1 2 2 2.4 2.4 0 3.1 4 0 6 1.5 3 3.1 4 0 2.4 2.4 0 -1 2 2 6 0.75 3 3.1 4 0 2.4 1.2 0
  • 57. New Population (Generation 2) 3.1 4 0 2.4 4.8 0 -1 2 2 2.4 2.4 0 3.1 4 0 6 1.5 3 3.1 4 0 2.4 2.4 0 -1 2 2 6 0.75 3 3.1 4 0 2.4 1.2 0 𝒚′ 𝑭(𝑪) 69.2 0.04 44.4 3.333 53.9 0.102 29.6 0.069 47.25 0.318 42.8 0.77
  • 58. New Population (Generation 2) Continue. 3.1 4 0 2.4 4.8 0 -1 2 2 2.4 2.4 0 3.1 4 0 6 1.5 3 3.1 4 0 2.4 2.4 0 -1 2 2 6 0.75 3 3.1 4 0 2.4 1.2 0 𝒚′ 𝑭(𝑪) 69.2 0.04 44.4 3.333 53.9 0.102 29.6 0.069 47.25 0.318 42.8 0.77
  • 59. import numpy import GARI # Population size sol_per_pop = 6 # Mating pool size num_parents_mating = 3 # Creating an initial population randomly. new_population = numpy.zeros((6, 6)) new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1] new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1] new_population[2, :] = [-1, 2, 2, -3, 2, 0.9] new_population[3, :] = [4, 7, 12, 6.1, 1.4, -4] new_population[4, :] = [3.1, 4, 0, 2.4, 4.8, 0] new_population[5, :] = [-2, 3, -7, 6, 3, 3] for iteration in range(10): # Measing the fitness of each chromosome in the population. qualities = GARI.pop_fitness(new_population) # Selecting the best parents in the population for mating. parents = GARI.select_mating_pool(new_population, qualities, num_parents_mating) # Generating next generation using crossover. new_population = GARI.crossover(parents, n_individuals=sol_per_pop) new_population = GARI.mutation(new_population) in_sample = [4, -2, 7, 5, 11, 1] for k in range(6): print(numpy.sum(new_population[k, :] * in_sample)) Python Implementation
  • 60. import numpy import GARI # Population size sol_per_pop = 6 # Mating pool size num_parents_mating = 3 # Creating an initial population randomly. new_population = numpy.zeros((6, 6)) new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1] new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1] new_population[2, :] = [-1, 2, 2, -3, 2, 0.9] new_population[3, :] = [4, 7, 12, 6.1, 1.4, -4] new_population[4, :] = [3.1, 4, 0, 2.4, 4.8, 0] new_population[5, :] = [-2, 3, -7, 6, 3, 3] for iteration in range(10): # Measing the fitness of each chromosome in the population. qualities = GARI.pop_fitness(new_population) # Selecting the best parents in the population for mating. parents = GARI.select_mating_pool(new_population, qualities, num_parents_mating) # Generating next generation using crossover. new_population = GARI.crossover(parents, n_individuals=sol_per_pop) new_population = GARI.mutation(new_population) in_sample = [4, -2, 7, 5, 11, 1] for k in range(6): print(numpy.sum(new_population[k, :] * in_sample)) Python Implementation
  • 61. import numpy import GARI # Population size sol_per_pop = 6 # Mating pool size num_parents_mating = 3 # Creating an initial population randomly. new_population = numpy.zeros((6, 6)) new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1] new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1] new_population[2, :] = [-1, 2, 2, -3, 2, 0.9] new_population[3, :] = [4, 7, 12, 6.1, 1.4, -4] new_population[4, :] = [3.1, 4, 0, 2.4, 4.8, 0] new_population[5, :] = [-2, 3, -7, 6, 3, 3] for iteration in range(10): # Measing the fitness of each chromosome in the population. qualities = GARI.pop_fitness(new_population) # Selecting the best parents in the population for mating. parents = GARI.select_mating_pool(new_population, qualities, num_parents_mating) # Generating next generation using crossover. new_population = GARI.crossover(parents, n_individuals=sol_per_pop) new_population = GARI.mutation(new_population) in_sample = [4, -2, 7, 5, 11, 1] for k in range(6): print(numpy.sum(new_population[k, :] * in_sample)) Python Implementation
  • 62. import numpy import GARI # Population size sol_per_pop = 6 # Mating pool size num_parents_mating = 3 # Creating an initial population randomly. new_population = numpy.zeros((6, 6)) new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1] new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1] new_population[2, :] = [-1, 2, 2, -3, 2, 0.9] new_population[3, :] = [4, 7, 12, 6.1, 1.4, -4] new_population[4, :] = [3.1, 4, 0, 2.4, 4.8, 0] new_population[5, :] = [-2, 3, -7, 6, 3, 3] for iteration in range(10): # Measing the fitness of each chromosome in the population. qualities = GARI.pop_fitness(new_population) # Selecting the best parents in the population for mating. parents = GARI.select_mating_pool(new_population, qualities, num_parents_mating) # Generating next generation using crossover. new_population = GARI.crossover(parents, n_individuals=sol_per_pop) new_population = GARI.mutation(new_population) in_sample = [4, -2, 7, 5, 11, 1] for k in range(6): print(numpy.sum(new_population[k, :] * in_sample)) Python Implementation
  • 63. 22.def crossover(parents, n_individuals=6): 23. new_population = numpy.empty((6,6)) 24. 25. #Previous parents (best elements). 26. new_population[0:parents.shape[0], :] = parents 27. new_population[3, 0:3] = parents[0, 0:3] 28. new_population[3, 3:] = parents[1, 3:] 29. new_population[4, 0:3] = parents[1, 0:3] 30. new_population[4, 3:] = parents[2, 3:] 31. new_population[5, 0:3] = parents[2, 0:3] 32. new_population[5, 3:] = parents[0, 3:] 33. 34. return new_population 35. 36.def mutation(population): 37. for idx in range(population.shape[0]): 38. population[idx, 4] = population[idx, 4]/3 39. return population 1.import numpy 2. 3.def fitness_fun(indiv_chrom): 4. in_sample = [4,-2,7,5,11,1] 5. quality = 1/numpy.abs(44.1-numpy.sum(indiv_chrom*in_sample)) 6. return quality 7. 8.def pop_fitness(pop): 9. qualities = numpy.zeros(pop.shape[0]) 10. for indv_num in range(pop.shape[0]): 11. qualities[indv_num] = fitness_fun(pop[indv_num, :]) 12. return qualities 13. 14.def select_mating_pool(pop, qualities, num_parents): 15. parents = numpy.empty((num_parents, pop.shape[1])) 16. for parent_num in range(num_parents): 17. max_qual_idx = numpy.where(qualities == numpy.max(qualities)) 18. max_qual_idx = max_qual_idx[0][0] 19. parents[parent_num, :] = pop[max_qual_idx, :] 20. qualities[max_qual_idx] = -1 21. return parents Python Implementation
  • 64. References • Yu, Xinjie, and Mitsuo Gen. Introduction to evolutionary algorithms. Springer Science & Business Media, 2010. • https://www.kdnuggets.com/2018/03/introduc tion-optimization-with-genetic-algorithm.html • https://www.linkedin.com/pulse/introduction- optimization-genetic-algorithm-ahmed-gad