Skip to main content

Genetic Algorithms toolbox in Python3

Project description

PyGenAlgo: A simple and powerful toolkit for genetic algorithms.

Logo DOI

This repository implements a genetic algorithm toolbox 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 three new features were added. The "Neighborhood Selector" operator is used for problems with multi-modal objective functions. It allows the population to focus on multiple areas of the search space and detect more than one optimal values. This features works in both computational modes (StandardGA and IslandModelGA). Moreover, the Blend-α (BLX-α) crossover and Polynomial (PM-η) mutation operators were implemented with focus on real coded genomes.

The current implementation offers a variety of genetic operators including:

(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:

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

After the download of the code (or the git clone), one can use the following commands:

cd PyGeneticAlgorithms
pip install .

This will install the PyGenAlgo in the package management system.

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. 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.

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
Tanaka M (=2) 2 2 single
Zakharov M (=8) 1 no single
Osyczka 6 2 6 single
Shubert 2 1 no multiple
Gaussian Mixture 2 1 no multiple

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. For multimodal optimizations check examples Shubert and Gaussian Mixture.

References and Documentation

This work is described in:

You can find the latest documentation here.

Contact

For any questions/comments (regarding this code) please contact me at: vrettasm@gmail.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygenalgo-1.8.0.tar.gz (64.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pygenalgo-1.8.0-py3-none-any.whl (82.7 kB view details)

Uploaded Python 3

File details

Details for the file pygenalgo-1.8.0.tar.gz.

File metadata

  • Download URL: pygenalgo-1.8.0.tar.gz
  • Upload date:
  • Size: 64.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for pygenalgo-1.8.0.tar.gz
Algorithm Hash digest
SHA256 ee08ceccd8cefafe4a017b3e64c81779a7bc670fe7ffbf438ed97f19c35eeb48
MD5 9f1f5826e67830a8bc5c593e577acf3a
BLAKE2b-256 a25300f3f8e916c523ae81f7c2c45f8db9f2e590a004d00a21358215629a4c2a

See more details on using hashes here.

File details

Details for the file pygenalgo-1.8.0-py3-none-any.whl.

File metadata

  • Download URL: pygenalgo-1.8.0-py3-none-any.whl
  • Upload date:
  • Size: 82.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for pygenalgo-1.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f7441f24213b708df34ddaabb342552e6f43f6a13020728ace0c9650a5781ff
MD5 2ba6a58e4124d82ddef44bdb05d3b3e5
BLAKE2b-256 21715b67f85bed0d2de5e0a6e3373f797aee9929f57fc1bc0d7a65a38681c020

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page