Pylint score: 9.47 / 10
This repository implements a genetic algorithm toolbox in Python3 programming language, using only Numpy and Joblib as additional libraries. The toolbox offers the following implementations (as engines):
- A StandardGA class, where the whole population of chromosomes is replaced by a new one at the end of each iteration (or epoch).
- An IslandModelGA class offers a new genetic operator (MigrationOperator), which allows for periodic migration of the best individuals among the (co-evolving) different island populations. The island populations are coevolving in parallel using separate CPUs.
- A brand new MultiObjectiveGA class is added that allows the user to solve more complex multiobjective optimization problems. The major difference in the new class is that the fitness function is expected to return a tuple with all the objective function values, e.g. (fx1, fx2, ..., fxn) rather a single function value fx. Note, that if the problems has additional constraints to satisfy, as is usually the case, they should be summed in one 'penalty' variable and included in the tuple before any other objective value i.e. (sum_penalty, fx1, fx2, ..., fxn). This way, when the chromosomes are sorted those that minimize all constraints (sum_penalty == 0) will be placed higher in the rank.
For computationally expensive fitness functions the StandardGA and MultiObjectiveGA classes provide the option of parallel evaluation (of the individual chromosomes), by setting in the method run(..., parallel=True). However, for fast fitness functions this will actually cause the algorithm to execute slower (due to the time required to open and close the parallel pool). So the default setting here is "parallel=False". Regarding the IslandModelGA this is running in parallel mode by definition.
NEWS: In this new release two additional selection operators have been implemented (i.e. ParetoFrontSelector and ParetoTournamentSelector) that are used exclusively with the 'MultiObjectiveGA' and select the new parents using pareto-front selection techniques. Note that both of these classes provide a base for the development of possible new selection methodologies for multi-objective problems. Examples that use the new techniques have also been added to demonstrate their use.
The current implementation provides (out of the box) a variety of genetic operators, including:
-
Selection operators:
-
Crossover operators:
-
Mutation operators:
-
Migration operators
-
Meta operators
NOTE(1): Meta operators call randomly other compatible operators (selection/crossover/mutation/migration) from a predefined set, with equal probability.
NOTE(2): Crossover operators marked by '*' support variable chromosome lengths (VLC). By definition all mutation operators support VCL too, because they operate on a single chromosome at a time.
Incorporating additional genetic operators is easily facilitated by inheriting from the base classes:
and implementing the basic interface as described therein. In the examples that follow I show how one can use this code to run a GA for optimization problems (maximization/minimization) with and without constraints. The project is ongoing so new things might come along the way.
There are two options to install the software.
The easiest way is to download it from PyPI. Simply run the following command on a terminal:
pip install pygenalgo
Alternatively one can clone directly the latest version using git as follows:
git clone https://github.com/vrettasm/PyGeneticAlgorithms.git
After the download of the code (or the git clone), one can use the following commands:
cd PyGeneticAlgorithms
pip install .
This will install the latest PyGenAlgo version in the package management system.
The recommended version is Python 3.10 (and above). To simplify the required packages just use:
pip install -r requirements.txt
The most important thing the user has to do is to define the fitness function. A template for single objective function is provided here in addition to the examples below. The cost_function decorator is used to indicate whether the function will be maximized (default), or minimized. The second output parameter ("solution_found") is optional; only in the cases where we can evaluate if a termination condition is satisfied.
from pygenalgo.genome.chromosome import Chromosome
from pygenalgo.utils.utilities import cost_function
# Fitness function <template>.
@cost_function(minimize=True)
def fitness_func(individual: Chromosome):
"""
This is how a fitness function should look like. The whole
evaluation should be implemented (or wrapped around) this
function.
:param individual: Individual chromosome to be evaluated.
:return: the function value evaluated at the individual.
"""
# Extract gene values from the chromosome.
x = individual.values()
# ... CODE TO IMPLEMENT ...
# Compute the function value.
f_value = ...
# Condition for termination.
# We set it to True / False.
solution_found = ...
# Return the solution.
return f_value, solution_found
# _end_def_Once the fitness function is defined correctly the next steps are straightforward as described in the examples.
Some optimization examples on how to use these algorithms:
| Problem | Variables | Objectives | Constraints | Optima |
|---|---|---|---|---|
| Sphere | M (=5) | 1 | no | single |
| Rastrigin | M (=5) | 1 | no | single |
| Rosenbrock | M (=2) | 1 | 1 | single |
| Binh & Korn | M (=2) | 2 | 2 | single |
| Sphere (parallel) | M (=10) | 1 | no | single |
| Easom (parallel) | M (=2) | 1 | no | single |
| Traveling Salesman | M (=10) | 1 | yes | single |
| N-Queens | M (=8) | 1 | yes | single |
| OneMax | M (=50) | 1 | no | single |
| Zakharov | M (=8) | 1 | no | single |
| Shubert | 2 | 1 | no | multiple |
| Gaussian Mixture | 2 | 1 | no | multiple |
| Multi-Depot VRP | M | 1 | yes | multiple |
| MOO: Binh & Korn | M (=2) | 2 | 2 | single |
| MOO: Tanaka | M (=2) | 2 | 2 | single |
| MOO: Osyczka & Kundu | 6 | 2 | 6 | single |
Constraint optimization problems can be easily addressed using the Penalty Method.
This work is described in:
- Michail D. Vrettas and Stefano Silvestri (2025) "PyGenAlgo: a simple and powerful toolkit for genetic algorithms". SoftwareX, vol. 30. DOI: 10.1016/j.softx.2025.102127.
You can find the latest documentation here.
For any questions/comments (regarding this code) please contact me at: vrettasm@gmail.com
