Successfully reported this slideshow.
Upcoming SlideShare
×

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

Selection of the optimal parameters for machine learning tasks is challenging. Some results may be bad not because the data is noisy or the used learning algorithm is weak, but due to the bad selection of the parameters values. This presentation gives a brief introduction about evolutionary algorithms (EAs) and describes genetic algorithm (GA) which is one of the simplest random-based EAs. A step-by-step example is given in addition to its implementation in Python 3.5.
---------------------------------
Yu, Xinjie, and Mitsuo Gen.Β Introduction to evolutionary algorithms. Springer Science & Business Media, 2010.
https://www.kdnuggets.com/2018/03/introduction-optimization-with-genetic-algorithm.html

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

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

1. 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. 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. 3. π π π π π π π π π π π π 2.4 0.7 8 -2 5 1.1 Solution 1 πβ² = ππ π β ππ π + ππ π + ππ π + πππ π + π π πβ² = πππ. π Absolute Error πππππ = |π β πβ² | πππππ = ππ. π πππππ = ππ. π β πππ. π
4. 4. π π π π π π π π π π π π -0.4 2.7 5 -1 7 0.1 Solution 2 πβ² = πππ. π Absolute Error πππππ = |π β πβ² | πππππ = ππ πππππ = ππ. π β πππ. π
5. 5. π π π π π π π π π π π π -1 2 2 -3 2 0.9 Solution 3 πβ² = ππ. π Absolute Error πππππ = |π β πβ² | πππππ = ππ. π πππππ = ππ. π β ππ. π
6. 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. 7. β’ Genetic algorithm is based on natural evolution of organisms. β’ A brief biological background will be helpful in understanding GA. Genetic Algorithm (GA)
8. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 27. -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 Mating Pool
28. 28. -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 Mating Pool Crossover
29. 29. -1 2 2 -3 2 0.9 3.1 4 0 2.4 4.8 0 Mating Pool Crossover
30. 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. 31. Mating Pool Mutation -1 2 2 2.4 4.8 0Offspring
32. 32. Mating Pool Mutation -1 2 2 2.4 4.8 0Offspring
33. 33. Mating Pool Mutation -1 2 2 2.4 4.8 0Offspring = π. π/π = π. π
34. 34. Mating Pool Mutation -1 2 2 2.4 4.8 0Offspring = π. π/π = π. π -1 2 2 2.4 2.4 0Mutant
35. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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