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

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

  • 1.
    Genetic Algorithm (GA)Optimization – Step-by-Step Example with Python Implementation Ahmed Fawzy Gad [email protected] MENOUFIA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATION ARTIFICIAL INTELLIGENCE ALL DEPARTMENTS ‫المنوفية‬ ‫جامعة‬ ‫الحاسبات‬ ‫كلية‬‫والمعلومات‬ ‫اإلصطناعي‬ ‫الذكاء‬ ‫األقسام‬ ‫جميع‬‫المنوفية‬ ‫جامعة‬
  • 2.
    Training a MachineLearning 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 algorithmis based on natural evolution of organisms. • A brief biological background will be helpful in understanding GA. Genetic Algorithm (GA)
  • 8.
    • Genetic algorithmis 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 algorithmis 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 algorithmis 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 theGenes? • 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 theFittest 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 theFittest 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 ofSolutions (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 ofSolutions (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 ofSolutions (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 ofSolutions (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 ofSolutions (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 ofSolutions (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 ofSolutions (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 bestindividuals 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 best3 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.78 -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 22 -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 -12 2 2.4 4.8 0Offspring
  • 32.
    Mating Pool Mutation -12 2 2.4 4.8 0Offspring
  • 33.
    Mating Pool Mutation -12 2 2.4 4.8 0Offspring = 𝟒. 𝟖/𝟐 = 𝟐. 𝟒
  • 34.
    Mating Pool Mutation -12 2 2.4 4.8 0Offspring = 𝟒. 𝟖/𝟐 = 𝟐. 𝟒 -1 2 2 2.4 2.4 0Mutant
  • 35.
    Mating Pool 3.1 40 2.4 4.8 0 -2 3 -7 6 3 3 Crossover 3.1 4 0 6 3 3Offspring
  • 36.
    Mating Pool 3.1 40 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 (Generation1) 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 (Generation1) 𝒚′ 𝑭(𝑪) 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 (Generation1) 𝒚′ 𝑭(𝑪) 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 (Generation1) 𝒚′ 𝑭(𝑪) 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 (Generation1) 𝒚′ 𝑭(𝑪) 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 40 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.14 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.14 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 22 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 22 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 40 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 40 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 (Generation2) 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 (Generation2) 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 (Generation2) 𝒚′ 𝑭(𝑪) 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 (Generation2) 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 (Generation2) 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