The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals at random from the current population to be parents and uses them to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution. You can apply the genetic algorithm to solve a variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear.
- Virtual Environment - This keeps your dependencies for each project separate and organized. Initialize and activate a virtual environment using:
python -m venv env
source env/bin/activateNote - In Windows, the env does not have a bin directory. Therefore, you'd use the analogous command shown below:
env/Scripts/activate- PIP Dependencies - Once the virtual environment is setup and running, install the required dependencies by running:
pip install -r requirements.txtAll required packages are included in the requirements file.
To change the parameters of the genetic algorithm, open the main.py file and edit the following lines of code:
# Genetic Algorithm Parameters
fitness_function # Objective function
boundary_range # The boundaries of each string
num_generations # Number of generations
population_size # Number of chromosomes in the population
strings # Number of strings in each chromosome
gene # Number of genes/bytes in each stringTo run the genetic algorithm, execute the following command in the terminal:
python main.pyAfter running the genetic algorithm, a plot image named best_fitness_plot.png will be created in the current working directory. This image illustrates the improvement of the best fitness over generations.

The best fitness and solution will also be printed to the terminal. An example is shown below:
Best Solution: [1 1 1 1 1 1 1 1 1 0 0 0 0 1 0]
Best Fitness: [86.41935484]
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
- Introduction to Genetic Algorithms
- Genetic Algorithm Implementation in Python
- Genetic Algorithm and its practicality in Machine Learning
Precious Michael