Welcome to GeneticAlgos!
GeneticAlgos is a simple and powerful Python library for creating genetic algorithms to solve complex optimization problems. GeneticAlgos is built on NumPy and it is under active development.
Uses smart defaults for genetic algorithms parameters which are good enough for generic use cases or for testing.
A simple-to-use API to modify genetic algorithms parameters.
Lightweight and just one dependency - Numpy.
Excellent test coverage.
Tested on Python 3.7, 3.8, 3.9 and 3.10
Source code
Usage
geneticalgos is available on PYPI.
Install with pip:
$ pip install geneticalgos
Trivial example:
We want to find a set of X=(x1, x2, x3, x4) which maximizes sum(x1, x2, x3, x4),
when each element x is a float from interval (0, 10). Simple answer is: x1 = 10, x2 = 10, x3 = 10, x4 = 10.
First, we define our fitness function (sum) and then gene_intervals for each x.
All other parameters (population size, crossover method, mutation probability, …) are configured with default values. However, you can change and tweak them easily - Advanced usage.
import geneticalgos as ga
import numpy as np
def custom_fitness_function(chromosome):
return sum(chromosome)
gene_intervals = np.array([[0, 10]] * 4)
# Create genetic algorithms with default values for GA parameters
# and our fitness function and gene intervals
ga_model = ga.GeneticAlgo(
fitness_function=custom_fitness_function,
gene_intervals=gene_intervals,
)
# Start genetic algorithm simulation
ga_model.simulate()
# print best solution
print(ga_model.best_chromosome)
# print fitness value for best chromosome
print(ga_model.best_fitness)
When to use GeneticAlgos
The main goal of the GeneticAlgos is to be simple and powerful.
Simple, because it can be used with basic knowledge of python (data structures, functions, …).
Simple, because it can be used with basic knowledge of genetic algorithms (population, chromosome, fitness function, …).
Powerful, because we can tweak many genetic algorithms parameters very easily and create complex models with the minimum of configuration.
When not to use GeneticAlgos
Let’s be honest, genetic algorithms are very complex algorithms which have a lot of modifications from a standard scheme.
You should look somewhere else if you need:
Something other than binary or numerical encoding - like permutation, strings, …
Chromosome genes with different encoding within same chromosome - some genes are float numbers and some of them integers.
An end criterion that is different from a fixed number of generation cycles.
Issues
If you encounter any problems, please file an issue along with a detailed description. Thank you 😃
About GeneticAlgos
Created by Lukas Kozelnicky.
Distributed under the MIT license. See LICENSE.txt for more information.