Getting started
Python version support
GeneticAlgos should be used with latest version of Python, and it is tested on Python 3.7, 3.8 and 3.9.
Installation
geneticalgos is available on PYPI.
Install with pip:
$ pip install geneticalgos
Usage
Every problem we are trying to solve might be different and may require a different approach. When using GeneticAlgos, there are some mandatory and some optional steps for successful genetic algorithms application in practical scenarios.
The best way to demonstrate this is to show GeneticAlgos in use on a real example and illustrate all the steps required.
For example, we can define -(x^2) + y maximization function that takes input variables from
range (-10, 10) in float numbers. This function has optima at f(0, 9.999) = 9.999
Create custom Fitness function which returns the numerical value that represents the suitability of the given solution.
import geneticalgos as ga
import numpy as np
def custom_fitness_function(chromosome):
return -(chromosome[0] ** 2) + chromosome[1]
Warning
When fitness_function does not return anything (returns None python default) then the``FitnessFunctionReturnsNone`` exception is raised.
Gene_intervals - specify the length and encoding of the chromosome together with the value range for each gene.
gene_intervals = np.array([[-10, 10], [-10, 10]])
Create the genetic algorithms model.
ga_model = ga.GeneticAlgo(
fitness_function=custom_fitness_function,
gene_intervals=gene_intervals,
)
The good news is that GeneticAlgos has default values for almost all parameters, but when it is necessary, we can change them very quickly - Advanced usage.
Start genetic simulation.
ga_model.simulate()
Explore the results. We can display the best solution, fitness value. Results will vary because of the stochastic nature of the genetic algorithms.
print(ga_model.best_chromosome)
# array([2.99317567e-03, 9.99101672e+00])
print(ga_model.best_fitness)
# 9.99100