Giter VIP home page Giter VIP logo

pygeneticalgorithms's Introduction

PyGenAlgo: A simple 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, the new "IslandModelGA" class was added that offers a new genetic operator (MigrationOperator), that allows for periodic migration of the best individuals, among the 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: Three new genetic operators have been added (OrderCrossover (OX1), InverseMutator and FlipMutator). These operators were added to address combinatorial problems where the genome can become invalid by the application of the other standard operators.

The current implementation offers a variety of genetic operators including:

Note that 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

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.

from pygenalgo.genome.chromosome import Chromosome

# Fitness function <template>.
def fitness_func(individual: Chromosome, f_min: 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: Boolean flag indicating whether we are dealing
    with a minimization or maximization problem.
    """
    
    # CODE TO IMPLEMENT.
    
    # Assign the estimated value.
    f_val = ...
    
    # If we want minimization we return the negative.
    return -f_val if f_min else f_val
# _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 this algorithm:

  1. StandardGA
    1. Minimization
    2. Maximization
    3. Minimization with constraints
    4. Multi-objective with constraints
  2. IslandModelGA
    1. Sphere Function
    2. Easom Function
  3. Fun Puzzles
    1. Traveling Salesman Problem
    2. N-Queens puzzle
    3. OneMax

Constraint optimization problems can be easily addressed using the Penalty Method as shown in the example (1.III) above. Moreover, multi-objective optimizations (with or without constraints) can also be solved as shown in the example (1.IV), using the weighted sum method.

Contact

For any questions/comments (regarding this code) please contact me at: [email protected]

pygeneticalgorithms's People

Contributors

vrettasm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.