pygeneticalgorithms
This repository implements a genetic algorithm (GA) in Python3 programming language, using only Numpy and Joblib as additional libraries. It provides a basic StandardGA model as well as a more advanced IslandModelGA that evolves in parallel several different populations..
Science Score: 67.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
✓DOI references
Found 1 DOI reference(s) in README -
✓Academic publication links
Links to: sciencedirect.com -
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (12.9%) to scientific vocabulary
Keywords
Repository
This repository implements a genetic algorithm (GA) in Python3 programming language, using only Numpy and Joblib as additional libraries. It provides a basic StandardGA model as well as a more advanced IslandModelGA that evolves in parallel several different populations..
Basic Info
Statistics
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
- Releases: 9
Topics
Metadata Files
README.md
PyGenAlgo: A simple and powerful toolkit for genetic algorithms.

"Genetic Algorithms (GA), are meta heuristic algorithms inspired by the process of natural selection and belong to a larger class of evolutionary algorithms (EA)."
-- (From Wikipedia, the free encyclopedia)
This repository implements a genetic algorithm (GA) in Python3 programming language, using only Numpy and Joblib as additional libraries. The basic approach offers a "StandardGA" class, where the whole population of chromosomes is replaced by a new one at the end of each iteration (or epoch). More recently, a new computational model was added named "IslandModelGA" class that offers a new genetic operator (MigrationOperator), that allows for periodic migration of the best individuals, among the (co-evolving) different island populations.
NOTE: For computationally expensive fitness functions the StandardGA class provides 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: Recently a new feature was added "adapt_probs: (bool)". This option if enabled, will allow the crossover and mutation probabilities to adapt according to the convergence of the population to a single solution. This uses the average Hamming distance to set a threshold value and either increase or decrease the genetic probabilities by a pre-defined amount.
The current implementation offers a variety of genetic operators including:
Selection operators:
Crossover operators:
Mutation operators:
Migration operators
Meta operators
(NOTE: Meta operators call randomly the other operators (crossover/mutation/migration) from a predefined set, with equal probability.)
Incorporating additional genetic operators is easily facilitated by inheriting from the base classes: - SelectionOperator - CrossoverOperator - MutationOperator - MigrationOperator
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.
Installation
There are two options to install the software.
The easiest way is to visit the GitHub web-page of the project and simply download the source code in zip format. This option does not require a prior installation of git on the computer.
Alternatively one can clone the project directly using git as follows:
git clone https://github.com/vrettasm/PyGeneticAlgorithms.git
Required packages
The recommended version is Python 3.10 (and above). To simplify the required packages just use:
pip install -r requirements.txt
Fitness function
The most important thing the user has to do is to define the "fitness function". A template is provided here, in addition to the examples below.
```python from pygenalgo.genome.chromosome import Chromosome
Fitness function .
def fitnessfunc(individual: Chromosome, fmin: bool = False): """ 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.
:param f_min: Bool flag indicating whether we are dealing
with a minimization or maximization problem.
"""
# CODE TO IMPLEMENT.
# Condition for termination.
# We set it to True / False.
solution_found = ...
# Compute the function value.
f_val = ...
# Assign the fitness value (check for minimization).
fit_value = -f_val if f_min else f_val
# Return the solution tuple.
return fit_value, solution_found
enddef_
``` Once the fitness function is defined correctly the next steps are straightforward as described in the examples.
Examples
Some optimization examples on how to use these algorithms:
| Problem | Variables | Objectives | Constraints | Mode | |:-----------------------------------------------------------|:-------------:|:--------------:|:---------------:|:--------:| | Sphere | M (=5) | 1 | no | serial | | Rastrigin | M (=5) | 1 | no | serial | | Rosenbrock | M (=2) | 1 | 1 | serial | | Binh & Korn | M (=2) | 2 | 2 | serial | | Sphere | M (=10) | 1 | no | parallel | | Easom | M (=2) | 1 | no | parallel | | Traveling Salesman Problem | M (=10) | 1 | yes | serial | | N-Queens puzzle | M (=8) | 1 | yes | parallel | | OneMax | M (=50) | 1 | no | serial | | Tanaka | M (=2) | 2 | 2 | serial | | Zakharov | M (=8) | 1 | no | serial | | Osyczka | 6 | 2 | 6 | parallel |
Constraint optimization problems can be easily addressed using the Penalty Method. Moreover, multi-objective optimizations (with or without constraints) can also be solved, using the weighted sum method, as shown in the examples above.
References and Documentation
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.
Contact
For any questions/comments (regarding this code) please contact me at: vrettasm@gmail.com
Owner
- Name: Michalis Vrettas, PhD
- Login: vrettasm
- Kind: user
- Location: Napoli, Italy
- Website: https://vrettasm.weebly.com
- Repositories: 10
- Profile: https://github.com/vrettasm
Computational scientist with a background in Bayesian analysis and machine learning.
Citation (CITATION.cff)
# This CITATION.cff file was generated with cffinit.
# Visit https://bit.ly/cffinit to generate yours today!
cff-version: 1.2.0
title: PyGenAlgo
message: >-
If you use this software, please cite it using the
metadata from this file.
type: software
authors:
- given-names: Michail
family-names: Vrettas
email: vrettasm@gmail.com
orcid: 'https://orcid.org/0000-0002-5456-3226'
affiliation: ICAR-CNR
repository-code: 'https://github.com/vrettasm/PyGeneticAlgorithms'
abstract: >-
'PyGenAlgo' is a research toolkit for genetic algorithms
(GA). Using a fully object-oriented paradigm it provides a
set of classes that can be used to solve optimization
problems (constrained and unconstrained). Its clear and
simple design, makes it useful for someone that just want
to use GAs as a black-box. At the same time, its
object-oriented structure allows for easy additions of
numerous new genetic operators that some researcher would
like to incorporate.
keywords:
- python3
- genetic algorithms
- island model
- optimization problems
license: GPL-3.0
GitHub Events
Total
- Release event: 8
- Watch event: 2
- Delete event: 5
- Push event: 196
- Fork event: 1
- Create event: 10
Last Year
- Release event: 8
- Watch event: 2
- Delete event: 5
- Push event: 196
- Fork event: 1
- Create event: 10