SlideShare una empresa de Scribd logo
1 de 64
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Genetic algorithm ppt
Genetic algorithm pptGenetic algorithm ppt
Genetic algorithm pptMayank Jain
 
Genetic algorithm raktim
Genetic algorithm raktimGenetic algorithm raktim
Genetic algorithm raktimRaktim Halder
 
Genetic Algorithms
Genetic AlgorithmsGenetic Algorithms
Genetic Algorithmsanas_elf
 
Genetic Algorithm
Genetic AlgorithmGenetic Algorithm
Genetic AlgorithmSHIMI S L
 
Genetic Algorithms Made Easy
Genetic Algorithms Made EasyGenetic Algorithms Made Easy
Genetic Algorithms Made EasyPrakash Pimpale
 
Particle swarm optimization
Particle swarm optimizationParticle swarm optimization
Particle swarm optimizationanurag singh
 
Particle Swarm optimization
Particle Swarm optimizationParticle Swarm optimization
Particle Swarm optimizationmidhulavijayan
 
Genetic Algorithm in Artificial Intelligence
Genetic Algorithm in Artificial IntelligenceGenetic Algorithm in Artificial Intelligence
Genetic Algorithm in Artificial IntelligenceSinbad Konick
 
Particle Swarm Optimization
Particle Swarm OptimizationParticle Swarm Optimization
Particle Swarm OptimizationStelios Petrakis
 
Nature-Inspired Optimization Algorithms
Nature-Inspired Optimization Algorithms Nature-Inspired Optimization Algorithms
Nature-Inspired Optimization Algorithms Xin-She Yang
 
Particle swarm optimization
Particle swarm optimization Particle swarm optimization
Particle swarm optimization Ahmed Fouad Ali
 

La actualidad más candente (20)

Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 
Genetic Algorithm
Genetic AlgorithmGenetic Algorithm
Genetic Algorithm
 
Genetic Algorithms
Genetic AlgorithmsGenetic Algorithms
Genetic Algorithms
 
Genetic algorithm ppt
Genetic algorithm pptGenetic algorithm ppt
Genetic algorithm ppt
 
Genetic algorithm raktim
Genetic algorithm raktimGenetic algorithm raktim
Genetic algorithm raktim
 
Genetic Algorithms
Genetic AlgorithmsGenetic Algorithms
Genetic Algorithms
 
Genetic Algorithms
Genetic AlgorithmsGenetic Algorithms
Genetic Algorithms
 
RM 701 Genetic Algorithm and Fuzzy Logic lecture
RM 701 Genetic Algorithm and Fuzzy Logic lectureRM 701 Genetic Algorithm and Fuzzy Logic lecture
RM 701 Genetic Algorithm and Fuzzy Logic lecture
 
Genetic Algorithm
Genetic AlgorithmGenetic Algorithm
Genetic Algorithm
 
Genetic Algorithms Made Easy
Genetic Algorithms Made EasyGenetic Algorithms Made Easy
Genetic Algorithms Made Easy
 
Particle swarm optimization
Particle swarm optimizationParticle swarm optimization
Particle swarm optimization
 
Genetic Algorithm
Genetic AlgorithmGenetic Algorithm
Genetic Algorithm
 
Particle Swarm optimization
Particle Swarm optimizationParticle Swarm optimization
Particle Swarm optimization
 
Genetic Algorithm in Artificial Intelligence
Genetic Algorithm in Artificial IntelligenceGenetic Algorithm in Artificial Intelligence
Genetic Algorithm in Artificial Intelligence
 
Particle Swarm Optimization
Particle Swarm OptimizationParticle Swarm Optimization
Particle Swarm Optimization
 
Evolutionary Computing
Evolutionary ComputingEvolutionary Computing
Evolutionary Computing
 
Nature-Inspired Optimization Algorithms
Nature-Inspired Optimization Algorithms Nature-Inspired Optimization Algorithms
Nature-Inspired Optimization Algorithms
 
Ga
GaGa
Ga
 
Cuckoo search algorithm
Cuckoo search algorithmCuckoo search algorithm
Cuckoo search algorithm
 
Particle swarm optimization
Particle swarm optimization Particle swarm optimization
Particle swarm optimization
 

Similar a 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 a 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
 

Más de 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
 

Más de 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
 

Último

April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramMoniSankarHazra
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Researchmichael115558
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz1
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...amitlee9823
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% SecurePooja Nehwal
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxolyaivanovalion
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxolyaivanovalion
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...amitlee9823
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...amitlee9823
 

Último (20)

April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics Program
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
Ravak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptxRavak dropshipping via API with DroFx.pptx
Ravak dropshipping via API with DroFx.pptx
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
 

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