GeneticAlgorithmPython
This project implements the genetic algorithm (GA) in Python mainly using NumPy.
The project has 2 main files which are:
-
ga.py
: Holds all necessary methods for implementing the GA. -
example.py
: Just gives an example of how to use the project by calling the methods in thega.py
file.
To test the project, you can simply run the example.py
file.
python example.py
How to Use the Project?
To use the project, here is the summary of the minimum required steps:
- Prepare the required parameters.
- Import the
ga.py
module. - Create an instance of the
GA
class. - Train the genetic algorithm.
Let's discuss how to do each of these steps.
Preparing the Parameters
Before running the GA, some parameters are required such as:
equation_inputs
: Inputs of the function to be optimized.equation_output
: Function output.sol_per_pop
: Number of solutions in the population.num_parents_mating
: Number of solutions to be selected as parents in the mating pool.num_generations
: Number of generations.mutation_percent_genes
: Percentage of genes to mutate.mutation_num_genes
: Number of genes to mutate. If only themutation_percent_genes
argument is specified, then the value ofmutation_num_genes
will be implicitly calculated.
Here is the code for preparing such parameters:
function_inputs = [4,-2,3.5,5,-11,-4.7]
function_output = 44
sol_per_pop = 8
num_parents_mating = 4
num_generations = 50
mutation_percent_genes=10
ga.py
Module
Import the The next step is to import the ga
module as follows:
import ga
This module has a class named GA
which holds the implementation of all methods for running the genetic algorithm.
GA
Class.
Create an Instance of the The GA
class is instantiated where the previously prepared parameters are fed to its constructor. The constructor is responsible for creating the initial population.
ga_instance = ga.GA(num_generations=num_generations,
sol_per_pop=sol_per_pop,
num_parents_mating=num_parents_mating,
function_inputs=function_inputs,
function_output=function_output,
mutation_percent_genes=10)
Train the Genetic Algorithm
After an instance of the GA
class is created, the next step is to call the train()
method as follows:
ga_instance.train()
Inside this method, the genetic algorithm evolves over a number of generations by doing the following tasks:
- Calculating the fitness values of the solutions within the current population.
- Select the best solutions as parents in the mating pool.
- Apply the crossover & mutation operation
- Repeat the process for the specified number of generations.
Plotting Results
There is a method named plot_result()
which creates 2 figures summarizing the results.
ga_instance.plot_result()
The first figure shows how the solutions' outputs change with the generations.
The second figure shows how the fitness values of the solutions change with the generations.
For More Information
To start with coding the genetic algorithm, you can check the tutorial titled Genetic Algorithm Implementation in Python available at these links:
-
https://www.linkedin.com/pulse/genetic-algorithm-implementation-python-ahmed-gad
-
https://towardsdatascience.com/genetic-algorithm-implementation-in-python-5ab67bb124a6
This tutorial is prepared based on a previous version of the project but it still a good resource to start with coding the genetic algorithm.
You can also check my book cited as Ahmed Fawzy Gad 'Practical Computer Vision Applications Using Deep Learning with CNNs'. Dec. 2018, Apress, 978-1-4842-4167-7.
Important Note
It is important to note that this project does not implement everything in GA and there are a wide number of variations to be applied. For example, this project uses decimal representation for the chromosome and the binary representations might be preferred for other problems.