SlideShare a Scribd company logo
1 of 64
Download to read offline
Genetic Algorithm (GA) Optimization โ€“
Step-by-Step Example with Python
Implementation
Ahmed Fawzy Gad
ahmed.fawzy@ci.menofia.edu.eg
MENOUFIA UNIVERSITY
FACULTY OF COMPUTERS AND INFORMATION
ARTIFICIAL INTELLIGENCE
ALL DEPARTMENTS
โ€ซุงู„ู…ู†ูˆููŠุฉโ€ฌ โ€ซุฌุงู…ุนุฉโ€ฌ
โ€ซุงู„ุญุงุณุจุงุชโ€ฌ โ€ซูƒู„ูŠุฉโ€ฌโ€ซูˆุงู„ู…ุนู„ูˆู…ุงุชโ€ฌ
โ€ซุงุฅู„ุตุทู†ุงุนูŠโ€ฌ โ€ซุงู„ุฐูƒุงุกโ€ฌ
โ€ซุงุฃู„ู‚ุณุงู…โ€ฌ โ€ซุฌู…ูŠุนโ€ฌโ€ซุงู„ู…ู†ูˆููŠุฉโ€ฌ โ€ซุฌุงู…ุนุฉโ€ฌ
Training a Machine Learning Model
โ€ข Goal is to find the set of parameters (๐’˜ ๐Ÿ:๐’˜ ๐Ÿ”) that maps the following
input to its output.
๐’š = ๐’˜ ๐Ÿ ๐’™ ๐Ÿ+๐’˜ ๐Ÿ ๐’™ ๐Ÿ+๐’˜ ๐Ÿ‘ ๐’™ ๐Ÿ‘+๐’˜ ๐Ÿ’ ๐’™ ๐Ÿ’+๐’˜ ๐Ÿ“ ๐’™ ๐Ÿ“+๐’˜ ๐Ÿ” ๐’™ ๐Ÿ”
๐’™ ๐Ÿ ๐’™ ๐Ÿ ๐’™ ๐Ÿ‘ ๐’™ ๐Ÿ’ ๐’™ ๐Ÿ“ ๐’™ ๐Ÿ” ๐’š
4 -2 7 5 11 1 44.1
๐’šโ€ฒ
= ๐Ÿ’๐’˜ ๐Ÿ โˆ’ ๐Ÿ๐’˜ ๐Ÿ + ๐Ÿ•๐’˜ ๐Ÿ‘ + ๐Ÿ“๐’˜ ๐Ÿ’ + ๐Ÿ๐Ÿ๐’˜ ๐Ÿ“ + ๐’˜ ๐Ÿ”
Data
๐’˜ ๐Ÿ ๐’˜ ๐Ÿ ๐’˜ ๐Ÿ‘ ๐’˜ ๐Ÿ’ ๐’˜ ๐Ÿ“ ๐’˜ ๐Ÿ”
2.4 0.7 8 -2 5 1.1
Solution 1
๐’šโ€ฒ
= ๐Ÿ’๐’˜ ๐Ÿ โˆ’ ๐Ÿ๐’˜ ๐Ÿ + ๐Ÿ•๐’˜ ๐Ÿ‘ + ๐Ÿ“๐’˜ ๐Ÿ’ + ๐Ÿ๐Ÿ๐’˜ ๐Ÿ“ + ๐’˜ ๐Ÿ”
๐’šโ€ฒ
= ๐Ÿ๐Ÿ๐ŸŽ. ๐Ÿ‘
Absolute Error ๐’†๐’“๐’“๐’๐’“ = |๐’š โˆ’ ๐’šโ€ฒ
|
๐’†๐’“๐’“๐’๐’“ = ๐Ÿ”๐Ÿ”. ๐Ÿ
๐’†๐’“๐’“๐’๐’“ = ๐Ÿ’๐Ÿ’. ๐Ÿ โˆ’ ๐Ÿ๐Ÿ๐ŸŽ. ๐Ÿ‘
๐’˜ ๐Ÿ ๐’˜ ๐Ÿ ๐’˜ ๐Ÿ‘ ๐’˜ ๐Ÿ’ ๐’˜ ๐Ÿ“ ๐’˜ ๐Ÿ”
-0.4 2.7 5 -1 7 0.1
Solution 2
๐’šโ€ฒ
= ๐Ÿ๐ŸŽ๐ŸŽ. ๐Ÿ
Absolute Error ๐’†๐’“๐’“๐’๐’“ = |๐’š โˆ’ ๐’šโ€ฒ
|
๐’†๐’“๐’“๐’๐’“ = ๐Ÿ“๐Ÿ”
๐’†๐’“๐’“๐’๐’“ = ๐Ÿ’๐Ÿ’. ๐Ÿ โˆ’ ๐Ÿ๐ŸŽ๐ŸŽ. ๐Ÿ
๐’˜ ๐Ÿ ๐’˜ ๐Ÿ ๐’˜ ๐Ÿ‘ ๐’˜ ๐Ÿ’ ๐’˜ ๐Ÿ“ ๐’˜ ๐Ÿ”
-1 2 2 -3 2 0.9
Solution 3
๐’šโ€ฒ
= ๐Ÿ๐Ÿ‘. ๐Ÿ—
Absolute Error ๐’†๐’“๐’“๐’๐’“ = |๐’š โˆ’ ๐’šโ€ฒ
|
๐’†๐’“๐’“๐’๐’“ = ๐Ÿ‘๐ŸŽ. ๐Ÿ
๐’†๐’“๐’“๐’๐’“ = ๐Ÿ’๐Ÿ’. ๐Ÿ โˆ’ ๐Ÿ๐Ÿ‘. ๐Ÿ—
๐’˜ ๐Ÿ ๐’˜ ๐Ÿ ๐’˜ ๐Ÿ‘ ๐’˜ ๐Ÿ’ ๐’˜ ๐Ÿ“ ๐’˜ ๐Ÿ”
-1 2 2 -3 2 0.9
Solution 3
๐’šโ€ฒ
= ๐Ÿ๐Ÿ‘. ๐Ÿ—
Absolute Error ๐’†๐’“๐’“๐’๐’“ = |๐’š โˆ’ ๐’šโ€ฒ
|
๐’†๐’“๐’“๐’๐’“ = ๐Ÿ‘๐ŸŽ. ๐Ÿ
๐’†๐’“๐’“๐’๐’“ = ๐Ÿ’๐Ÿ’. ๐Ÿ โˆ’ ๐Ÿ๐Ÿ‘. ๐Ÿ—
Difficult to find the best solution manually.
Use optimization technique such as Genetic Algorithm (GA).
โ€ข Genetic algorithm is based on natural evolution of organisms.
โ€ข A brief biological background will be helpful in understanding GA.
Genetic Algorithm (GA)
โ€ข Genetic algorithm is based on natural evolution of organisms.
โ€ข A brief biological background will be helpful in understanding GA.
Chromosomes
Genes
Eye
Color
Dimples Freckles
Organism
Cells
Genetic Algorithm (GA)
โ€ข Genetic algorithm is based on natural evolution of organisms.
โ€ข A brief biological background will be helpful in understanding GA.
Chromosomes
Genes
Eye
Color
Dimples Freckles
Organism
Cells
Genetic Algorithm (GA)
https://www.icr.org/article/myth-human-evolution
โ€ข Genetic algorithm is based on natural evolution of organisms.
โ€ข A brief biological background will be helpful in understanding GA.
Chromosome
Genes
GA
Individuals/Solutions
Genetic Algorithm (GA)
Chromosomes
Genes
Eye
Color
Dimples Freckles
Organism
Cells
What are the Genes?
โ€ข Gene is anything that is able to enhance the results when changed.
โ€ข By exploring the following model, the 6 weights are able to enhance
the results. Thus each weight will represent a gene in GA.
๐’š = ๐’˜ ๐Ÿ ๐’™ ๐Ÿ+๐’˜ ๐Ÿ ๐’™ ๐Ÿ+๐’˜ ๐Ÿ‘ ๐’™ ๐Ÿ‘+๐’˜ ๐Ÿ’ ๐’™ ๐Ÿ’+๐’˜ ๐Ÿ“ ๐’™ ๐Ÿ“+๐’˜ ๐Ÿ” ๐’™ ๐Ÿ”
Gene 0 Gene 1 Gene 2 Gene 3 Gene 4 Gene 5
๐’˜ ๐Ÿ ๐’˜ ๐Ÿ ๐’˜ ๐Ÿ‘ ๐’˜ ๐Ÿ’ ๐’˜ ๐Ÿ“ ๐’˜ ๐Ÿ”
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
Population Size = 6
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
Chromosome
Gene
Survival of the Fittest
Fitness Function
Fitness Value
The Higher the Value,
the Better the Solution
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Survival of the Fittest
Fitness Function
Fitness Value
The Higher the Value,
the Better the Solution
๐‘ญ ๐’„ =
๐Ÿ
๐’†๐’“๐’“๐’๐’“
=
๐Ÿ
|๐’š โˆ’ ๐’šโ€ฒ|
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
๐’šโ€ฒ ๐‘ญ(๐‘ช)
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
๐’šโ€ฒ ๐‘ญ(๐‘ช)
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
๐’šโ€ฒ ๐‘ญ(๐‘ช)
๐’šโ€ฒ
= ๐Ÿ’๐’˜ ๐Ÿ โˆ’ ๐Ÿ๐’˜ ๐Ÿ + ๐Ÿ•๐’˜ ๐Ÿ‘ + ๐Ÿ“๐’˜ ๐Ÿ’ + ๐Ÿ๐Ÿ๐’˜ ๐Ÿ“ + ๐’˜ ๐Ÿ”
๐’šโ€ฒ
= ๐Ÿ’ โˆ— ๐Ÿ. ๐Ÿ’ โˆ’2 โˆ— 0.7 + 7 โˆ— 8 + 5 โˆ— -2 + 11 โˆ— 5 + ๐Ÿ. ๐Ÿ
๐’šโ€ฒ
= ๐Ÿ๐Ÿ๐ŸŽ. ๐Ÿ‘
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
๐’šโ€ฒ ๐‘ญ(๐‘ช)
110.3
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
๐’šโ€ฒ ๐‘ญ(๐‘ช)
110.3
๐‘ญ ๐’„ =
๐Ÿ
๐’†๐’“๐’“๐’๐’“
=
๐Ÿ
|๐Ÿ’๐Ÿ’. ๐Ÿ โˆ’ ๐Ÿ๐Ÿ๐ŸŽ. ๐Ÿ‘|
=
๐Ÿ
๐Ÿ”๐Ÿ”. ๐Ÿ
= 0.015
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
๐’šโ€ฒ ๐‘ญ(๐‘ช)
110.3 0.015
๐‘ญ ๐’„ =
๐Ÿ
๐’†๐’“๐’“๐’๐’“
=
๐Ÿ
|๐Ÿ’๐Ÿ’. ๐Ÿ โˆ’ ๐Ÿ๐Ÿ๐ŸŽ. ๐Ÿ‘|
=
๐Ÿ
๐Ÿ”๐Ÿ”. ๐Ÿ
= 0.015
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Initial Population of Solutions (Generation 0)
2.4 0.7 8 -2 5 1.1
๐’šโ€ฒ ๐‘ญ(๐‘ช)
110.3 0.015
100.1 0.018
13.9 0.033
127.9 0.012
69.2 0.0398
3 0.024
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Mating Pool
Select best individuals as parents for mating to
generate new individuals.
๐’šโ€ฒ ๐‘ญ(๐‘ช)
110.3 0.015
100.1 0.018
13.9 0.033
127.9 0.012
69.2 0.0398
3 0.024
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Mating Pool
Add best 3 individuals to the mating pool for producing
the next generation of solutions.
๐’šโ€ฒ ๐‘ญ(๐‘ช)
110.3 0.015
100.1 0.018
13.9 0.033
127.9 0.012
69.2 0.04
3 0.024
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Mating Pool
2.4 0.7 8 -2 5 1.1
-0.4 2.7 5 -1 7 0.1
-1 2 2 -3 2 0.9
4 7 12 6.1 1.4 -4
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Add best 3 individuals to the mating pool for producing
the next generation of solutions.
๐’šโ€ฒ ๐‘ญ(๐‘ช)
110.3 0.015
100.1 0.018
13.9 0.033
127.9 0.012
69.2 0.04
3 0.024
Mating Pool
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
Mating Pool
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
Mating Pool Crossover
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
Mating Pool Crossover
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
Mating Pool Crossover
-1 2 2 2.4 4.8 0Offspring
Mating Pool Mutation
-1 2 2 2.4 4.8 0Offspring
Mating Pool Mutation
-1 2 2 2.4 4.8 0Offspring
Mating Pool Mutation
-1 2 2 2.4 4.8 0Offspring
= ๐Ÿ’. ๐Ÿ–/๐Ÿ
= ๐Ÿ. ๐Ÿ’
Mating Pool Mutation
-1 2 2 2.4 4.8 0Offspring
= ๐Ÿ’. ๐Ÿ–/๐Ÿ
= ๐Ÿ. ๐Ÿ’
-1 2 2 2.4 2.4 0Mutant
Mating Pool
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Crossover
3.1 4 0 6 3 3Offspring
Mating Pool
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
Crossover
3.1 4 0 6 3 3Offspring
Mutation
3.1 4 0 6 1.5 3Mutant
Mating Pool
-2 3 -7 6 3 3
Crossover
-2 3 -7 -3 2 0.9Offspring
-1 2 2 -3 2 0.9
Mating Pool
-2 3 -7 6 3 3
Crossover
-2 3 -7 -3 2 0.9Offspring
Mutation
-2 3 -7 -3 1 0.9Mutant
-1 2 2 -3 2 0.9
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
Old
Individuals
New
Individuals
New Population (Generation 1)
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
Old
Individuals
New
Individuals
New Population (Generation 1)
Why Reusing Old Individuals?
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
Old
Individuals
New
Individuals
New Population (Generation 1)
Why Reusing Old Individuals?
GA is a random-based optimization technique. There is no guarantee that the new individuals will be better
than the previous individuals. Keeping the old individuals at least saves the results from getting worse.
New Population (Generation 1)
Old
Individuals
New
Individuals
Population Mating Pool Crossover MutationFitness Value
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
New Population (Generation 1)
๐’šโ€ฒ ๐‘ญ(๐‘ช)
13.9 0.033
69.2 0.04
3 0.024
44.4 3.333
53.9 0.102
-66.1 0.009
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
New Population (Generation 1)
๐’šโ€ฒ ๐‘ญ(๐‘ช)
13.9 0.033
69.2 0.04
3 0.024
44.4 3.333
53.9 0.102
-66.1 0.009
Some new individuals make the results worse.
This is why it is preferred to keep the best old individuals.
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
New Population (Generation 1)
๐’šโ€ฒ ๐‘ญ(๐‘ช)
13.9 0.033
69.2 0.04
3 0.024
44.4 3.333
53.9 0.102
-66.1 0.009
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
New Population (Generation 1)
๐’šโ€ฒ ๐‘ญ(๐‘ช)
13.9 0.033
69.2 0.04
3 0.024
44.4 3.333
53.9 0.102
-66.1 0.009
-1 2 2 -3 2 0.9
3.1 4 0 2.4 4.8 0
-2 3 -7 6 3 3
-2 3 -7 -3 1 0.9
3.1 4 0 6 1.5 3
-1 2 2 2.4 2.4 0
Mating Pool
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
Mating Pool Crossover
3.1 4 0 2.4 2.4 0Offspring
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
Mating Pool Crossover
3.1 4 0 2.4 2.4 0Offspring
Mutation
3.1 4 0 2.4 1.2 0Mutant
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
Mating Pool
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
-1 2 2 6 1.5 3Offspring
Crossover
Mating Pool
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
-1 2 2 6 1.5 3Offspring
Mutation
-1 2 2 6 0.75 3Mutant
Crossover
Mating Pool
3.1 4 0 6 1.5 3
3.1 4 0 2.4 4.8 0Offspring
Crossover
3.1 4 0 2.4 4.8 0
Mating Pool
3.1 4 0 6 1.5 3
3.1 4 0 2.4 4.8 0Offspring
Mutation
3.1 4 0 2.4 2.4 0Mutant
Crossover
3.1 4 0 2.4 4.8 0
New Population (Generation 2)
Old
Individuals
New
Individuals
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
3.1 4 0 2.4 2.4 0
-1 2 2 6 0.75 3
3.1 4 0 2.4 1.2 0
New Population (Generation 2)
Old
Individuals
New
Individuals
Population Mating Pool Crossover MutationFitness Value
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
3.1 4 0 2.4 2.4 0
-1 2 2 6 0.75 3
3.1 4 0 2.4 1.2 0
New Population (Generation 2)
๐’šโ€ฒ ๐‘ญ(๐‘ช)
69.2 0.04
44.4 3.333
53.9 0.102
29.6 0.069
47.25 0.318
42.8 0.77
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
3.1 4 0 2.4 2.4 0
-1 2 2 6 0.75 3
3.1 4 0 2.4 1.2 0
New Population (Generation 2)
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
3.1 4 0 2.4 2.4 0
-1 2 2 6 0.75 3
3.1 4 0 2.4 1.2 0
๐’šโ€ฒ ๐‘ญ(๐‘ช)
69.2 0.04
44.4 3.333
53.9 0.102
29.6 0.069
47.25 0.318
42.8 0.77
New Population (Generation 2)
Continue.
3.1 4 0 2.4 4.8 0
-1 2 2 2.4 2.4 0
3.1 4 0 6 1.5 3
3.1 4 0 2.4 2.4 0
-1 2 2 6 0.75 3
3.1 4 0 2.4 1.2 0
๐’šโ€ฒ ๐‘ญ(๐‘ช)
69.2 0.04
44.4 3.333
53.9 0.102
29.6 0.069
47.25 0.318
42.8 0.77
import numpy
import GARI
# Population size
sol_per_pop = 6
# Mating pool size
num_parents_mating = 3
# Creating an initial population randomly.
new_population = numpy.zeros((6, 6))
new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1]
new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1]
new_population[2, :] = [-1, 2, 2, -3, 2, 0.9]
new_population[3, :] = [4, 7, 12, 6.1, 1.4, -4]
new_population[4, :] = [3.1, 4, 0, 2.4, 4.8, 0]
new_population[5, :] = [-2, 3, -7, 6, 3, 3]
for iteration in range(10):
# Measing the fitness of each chromosome in the population.
qualities = GARI.pop_fitness(new_population)
# Selecting the best parents in the population for mating.
parents = GARI.select_mating_pool(new_population, qualities,
num_parents_mating)
# Generating next generation using crossover.
new_population = GARI.crossover(parents,
n_individuals=sol_per_pop)
new_population = GARI.mutation(new_population)
in_sample = [4, -2, 7, 5, 11, 1]
for k in range(6):
print(numpy.sum(new_population[k, :] * in_sample))
Python
Implementation
import numpy
import GARI
# Population size
sol_per_pop = 6
# Mating pool size
num_parents_mating = 3
# Creating an initial population randomly.
new_population = numpy.zeros((6, 6))
new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1]
new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1]
new_population[2, :] = [-1, 2, 2, -3, 2, 0.9]
new_population[3, :] = [4, 7, 12, 6.1, 1.4, -4]
new_population[4, :] = [3.1, 4, 0, 2.4, 4.8, 0]
new_population[5, :] = [-2, 3, -7, 6, 3, 3]
for iteration in range(10):
# Measing the fitness of each chromosome in the population.
qualities = GARI.pop_fitness(new_population)
# Selecting the best parents in the population for mating.
parents = GARI.select_mating_pool(new_population, qualities,
num_parents_mating)
# Generating next generation using crossover.
new_population = GARI.crossover(parents,
n_individuals=sol_per_pop)
new_population = GARI.mutation(new_population)
in_sample = [4, -2, 7, 5, 11, 1]
for k in range(6):
print(numpy.sum(new_population[k, :] * in_sample))
Python
Implementation
import numpy
import GARI
# Population size
sol_per_pop = 6
# Mating pool size
num_parents_mating = 3
# Creating an initial population randomly.
new_population = numpy.zeros((6, 6))
new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1]
new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1]
new_population[2, :] = [-1, 2, 2, -3, 2, 0.9]
new_population[3, :] = [4, 7, 12, 6.1, 1.4, -4]
new_population[4, :] = [3.1, 4, 0, 2.4, 4.8, 0]
new_population[5, :] = [-2, 3, -7, 6, 3, 3]
for iteration in range(10):
# Measing the fitness of each chromosome in the population.
qualities = GARI.pop_fitness(new_population)
# Selecting the best parents in the population for mating.
parents = GARI.select_mating_pool(new_population, qualities,
num_parents_mating)
# Generating next generation using crossover.
new_population = GARI.crossover(parents,
n_individuals=sol_per_pop)
new_population = GARI.mutation(new_population)
in_sample = [4, -2, 7, 5, 11, 1]
for k in range(6):
print(numpy.sum(new_population[k, :] * in_sample))
Python
Implementation
import numpy
import GARI
# Population size
sol_per_pop = 6
# Mating pool size
num_parents_mating = 3
# Creating an initial population randomly.
new_population = numpy.zeros((6, 6))
new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1]
new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1]
new_population[2, :] = [-1, 2, 2, -3, 2, 0.9]
new_population[3, :] = [4, 7, 12, 6.1, 1.4, -4]
new_population[4, :] = [3.1, 4, 0, 2.4, 4.8, 0]
new_population[5, :] = [-2, 3, -7, 6, 3, 3]
for iteration in range(10):
# Measing the fitness of each chromosome in the population.
qualities = GARI.pop_fitness(new_population)
# Selecting the best parents in the population for mating.
parents = GARI.select_mating_pool(new_population, qualities,
num_parents_mating)
# Generating next generation using crossover.
new_population = GARI.crossover(parents,
n_individuals=sol_per_pop)
new_population = GARI.mutation(new_population)
in_sample = [4, -2, 7, 5, 11, 1]
for k in range(6):
print(numpy.sum(new_population[k, :] * in_sample))
Python
Implementation
22.def crossover(parents, n_individuals=6):
23. new_population = numpy.empty((6,6))
24.
25. #Previous parents (best elements).
26. new_population[0:parents.shape[0], :] = parents
27. new_population[3, 0:3] = parents[0, 0:3]
28. new_population[3, 3:] = parents[1, 3:]
29. new_population[4, 0:3] = parents[1, 0:3]
30. new_population[4, 3:] = parents[2, 3:]
31. new_population[5, 0:3] = parents[2, 0:3]
32. new_population[5, 3:] = parents[0, 3:]
33.
34. return new_population
35.
36.def mutation(population):
37. for idx in range(population.shape[0]):
38. population[idx, 4] = population[idx, 4]/3
39. return population
1.import numpy
2.
3.def fitness_fun(indiv_chrom):
4. in_sample = [4,-2,7,5,11,1]
5. quality = 1/numpy.abs(44.1-numpy.sum(indiv_chrom*in_sample))
6. return quality
7.
8.def pop_fitness(pop):
9. qualities = numpy.zeros(pop.shape[0])
10. for indv_num in range(pop.shape[0]):
11. qualities[indv_num] = fitness_fun(pop[indv_num, :])
12. return qualities
13.
14.def select_mating_pool(pop, qualities, num_parents):
15. parents = numpy.empty((num_parents, pop.shape[1]))
16. for parent_num in range(num_parents):
17. max_qual_idx = numpy.where(qualities == numpy.max(qualities))
18. max_qual_idx = max_qual_idx[0][0]
19. parents[parent_num, :] = pop[max_qual_idx, :]
20. qualities[max_qual_idx] = -1
21. return parents
Python Implementation
References
โ€ข Yu, Xinjie, and Mitsuo Gen. Introduction to
evolutionary algorithms. Springer Science &
Business Media, 2010.
โ€ข https://www.kdnuggets.com/2018/03/introduc
tion-optimization-with-genetic-algorithm.html
โ€ข https://www.linkedin.com/pulse/introduction-
optimization-genetic-algorithm-ahmed-gad

More Related Content

What's hot

Introduction to Genetic Algorithms
Introduction to Genetic AlgorithmsIntroduction to Genetic Algorithms
Introduction to Genetic AlgorithmsAhmed Othman
ย 
Genetic Algorithms - Artificial Intelligence
Genetic Algorithms - Artificial IntelligenceGenetic Algorithms - Artificial Intelligence
Genetic Algorithms - Artificial IntelligenceSahil Kumar
ย 
Metaheuristics
MetaheuristicsMetaheuristics
Metaheuristicsossein jain
ย 
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
ย 
Genetic algorithm ppt
Genetic algorithm pptGenetic algorithm ppt
Genetic algorithm pptMayank Jain
ย 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithmJari Abbas
ย 
Introduction to Genetic Algorithms
Introduction to Genetic AlgorithmsIntroduction to Genetic Algorithms
Introduction to Genetic AlgorithmsPremsankar Chakkingal
ย 
Simulated Annealing
Simulated AnnealingSimulated Annealing
Simulated AnnealingJoy Dutta
ย 
Genetic Algorithm
Genetic Algorithm Genetic Algorithm
Genetic Algorithm Bhushan Mohite
ย 
Loss functions (DLAI D4L2 2017 UPC Deep Learning for Artificial Intelligence)
Loss functions (DLAI D4L2 2017 UPC Deep Learning for Artificial Intelligence)Loss functions (DLAI D4L2 2017 UPC Deep Learning for Artificial Intelligence)
Loss functions (DLAI D4L2 2017 UPC Deep Learning for Artificial Intelligence)Universitat Politรจcnica de Catalunya
ย 
Genetic algorithm fitness function
Genetic algorithm fitness functionGenetic algorithm fitness function
Genetic algorithm fitness functionProf Ansari
ย 
Adversarial search with Game Playing
Adversarial search with Game PlayingAdversarial search with Game Playing
Adversarial search with Game PlayingAman Patel
ย 
Genetic algorithms
Genetic algorithmsGenetic algorithms
Genetic algorithmszamakhan
ย 
Ga
GaGa
Gavenki249
ย 
Genetic Algorithms
Genetic AlgorithmsGenetic Algorithms
Genetic AlgorithmsShruti Railkar
ย 
Genetic algorithms in Data Mining
Genetic algorithms in Data MiningGenetic algorithms in Data Mining
Genetic algorithms in Data MiningAtul Khanna
ย 
Nature-Inspired Metaheuristic Algorithms
Nature-Inspired Metaheuristic AlgorithmsNature-Inspired Metaheuristic Algorithms
Nature-Inspired Metaheuristic AlgorithmsXin-She Yang
ย 
Genetic Algorithm
Genetic AlgorithmGenetic Algorithm
Genetic AlgorithmFatemeh Karimi
ย 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithmgarima931
ย 

What's hot (20)

Introduction to Genetic Algorithms
Introduction to Genetic AlgorithmsIntroduction to Genetic Algorithms
Introduction to Genetic Algorithms
ย 
Genetic Algorithm
Genetic AlgorithmGenetic Algorithm
Genetic Algorithm
ย 
Genetic Algorithms - Artificial Intelligence
Genetic Algorithms - Artificial IntelligenceGenetic Algorithms - Artificial Intelligence
Genetic Algorithms - Artificial Intelligence
ย 
Metaheuristics
MetaheuristicsMetaheuristics
Metaheuristics
ย 
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...
ย 
Genetic algorithm ppt
Genetic algorithm pptGenetic algorithm ppt
Genetic algorithm ppt
ย 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
ย 
Introduction to Genetic Algorithms
Introduction to Genetic AlgorithmsIntroduction to Genetic Algorithms
Introduction to Genetic Algorithms
ย 
Simulated Annealing
Simulated AnnealingSimulated Annealing
Simulated Annealing
ย 
Genetic Algorithm
Genetic Algorithm Genetic Algorithm
Genetic Algorithm
ย 
Loss functions (DLAI D4L2 2017 UPC Deep Learning for Artificial Intelligence)
Loss functions (DLAI D4L2 2017 UPC Deep Learning for Artificial Intelligence)Loss functions (DLAI D4L2 2017 UPC Deep Learning for Artificial Intelligence)
Loss functions (DLAI D4L2 2017 UPC Deep Learning for Artificial Intelligence)
ย 
Genetic algorithm fitness function
Genetic algorithm fitness functionGenetic algorithm fitness function
Genetic algorithm fitness function
ย 
Adversarial search with Game Playing
Adversarial search with Game PlayingAdversarial search with Game Playing
Adversarial search with Game Playing
ย 
Genetic algorithms
Genetic algorithmsGenetic algorithms
Genetic algorithms
ย 
Ga
GaGa
Ga
ย 
Genetic Algorithms
Genetic AlgorithmsGenetic Algorithms
Genetic Algorithms
ย 
Genetic algorithms in Data Mining
Genetic algorithms in Data MiningGenetic algorithms in Data Mining
Genetic algorithms in Data Mining
ย 
Nature-Inspired Metaheuristic Algorithms
Nature-Inspired Metaheuristic AlgorithmsNature-Inspired Metaheuristic Algorithms
Nature-Inspired Metaheuristic Algorithms
ย 
Genetic Algorithm
Genetic AlgorithmGenetic Algorithm
Genetic Algorithm
ย 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
ย 

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

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

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

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

More from Ahmed Gad

ICEIT'20 Cython for Speeding-up Genetic Algorithm
ICEIT'20 Cython for Speeding-up Genetic AlgorithmICEIT'20 Cython for Speeding-up Genetic Algorithm
ICEIT'20 Cython for Speeding-up Genetic AlgorithmAhmed Gad
ย 
NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...
NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...
NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...Ahmed Gad
ย 
Python for Computer Vision - Revision 2nd Edition
Python for Computer Vision - Revision 2nd EditionPython for Computer Vision - Revision 2nd Edition
Python for Computer Vision - Revision 2nd EditionAhmed Gad
ย 
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
ย 
AI Heuristic Search - Beam Search - Simulated Annealing
AI Heuristic Search - Beam Search - Simulated AnnealingAI Heuristic Search - Beam Search - Simulated Annealing
AI Heuristic Search - Beam Search - Simulated AnnealingAhmed Gad
ย 

More from Ahmed Gad (20)

ICEIT'20 Cython for Speeding-up Genetic Algorithm
ICEIT'20 Cython for Speeding-up Genetic AlgorithmICEIT'20 Cython for Speeding-up Genetic Algorithm
ICEIT'20 Cython for Speeding-up Genetic Algorithm
ย 
NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...
NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...
NumPyCNNAndroid: A Library for Straightforward Implementation of Convolutiona...
ย 
Python for Computer Vision - Revision 2nd Edition
Python for Computer Vision - Revision 2nd EditionPython for Computer Vision - Revision 2nd Edition
Python for Computer Vision - Revision 2nd Edition
ย 
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
ย 
AI Heuristic Search - Beam Search - Simulated Annealing
AI Heuristic Search - Beam Search - Simulated AnnealingAI Heuristic Search - Beam Search - Simulated Annealing
AI Heuristic Search - Beam Search - Simulated Annealing
ย 

Recently uploaded

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
ย 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptDr. Soumendra Kumar Patra
ย 
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
ย 
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
ย 
Call Girls Begur Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service Bangalore
Call Girls Begur Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service BangaloreCall Girls Begur Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service Bangalore
Call Girls Begur Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service Bangaloreamitlee9823
ย 
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
ย 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
ย 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...SUHANI PANDEY
ย 
โžฅ๐Ÿ” 7737669865 ๐Ÿ”โ–ป malwa Call-girls in Women Seeking Men ๐Ÿ”malwa๐Ÿ” Escorts Ser...
โžฅ๐Ÿ” 7737669865 ๐Ÿ”โ–ป malwa Call-girls in Women Seeking Men  ๐Ÿ”malwa๐Ÿ”   Escorts Ser...โžฅ๐Ÿ” 7737669865 ๐Ÿ”โ–ป malwa Call-girls in Women Seeking Men  ๐Ÿ”malwa๐Ÿ”   Escorts Ser...
โžฅ๐Ÿ” 7737669865 ๐Ÿ”โ–ป malwa Call-girls in Women Seeking Men ๐Ÿ”malwa๐Ÿ” Escorts Ser...amitlee9823
ย 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightDelhi Call girls
ย 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Pooja Nehwal
ย 
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
ย 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
ย 
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
ย 
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
ย 
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
ย 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusTimothy Spann
ย 
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
ย 
Chintamani Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore ...Chintamani Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore ...amitlee9823
ย 
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
ย 

Recently uploaded (20)

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
ย 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
ย 
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
ย 
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
ย 
Call Girls Begur Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service Bangalore
Call Girls Begur Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service BangaloreCall Girls Begur Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service Bangalore
Call Girls Begur Just Call ๐Ÿ‘— 7737669865 ๐Ÿ‘— Top Class Call Girl Service Bangalore
ย 
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
ย 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
ย 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
ย 
โžฅ๐Ÿ” 7737669865 ๐Ÿ”โ–ป malwa Call-girls in Women Seeking Men ๐Ÿ”malwa๐Ÿ” Escorts Ser...
โžฅ๐Ÿ” 7737669865 ๐Ÿ”โ–ป malwa Call-girls in Women Seeking Men  ๐Ÿ”malwa๐Ÿ”   Escorts Ser...โžฅ๐Ÿ” 7737669865 ๐Ÿ”โ–ป malwa Call-girls in Women Seeking Men  ๐Ÿ”malwa๐Ÿ”   Escorts Ser...
โžฅ๐Ÿ” 7737669865 ๐Ÿ”โ–ป malwa Call-girls in Women Seeking Men ๐Ÿ”malwa๐Ÿ” Escorts Ser...
ย 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
ย 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
ย 
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
ย 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
ย 
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...
ย 
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...
ย 
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
ย 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
ย 
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
ย 
Chintamani Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore ...Chintamani Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: ๐Ÿ“ 7737669865 ๐Ÿ“ High Profile Model Escorts | Bangalore ...
ย 
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...
ย 

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