GeneticAlgoithm
Implementation of the genetic algorithm
Genetic Algorithm

Introduction:

This project is a first of my new one-day-build mini-projects serie. It is my take on implementing a simple version of the Genetic Algorithm using C++, with a demo use case consisting of fitting a gaussian distribution to a data histogram.


General Code Description:

For a description of the code and compiling/running instructions, see Genetic Algorithm README.


Algorithms Description:

The genetic algorithm consist of finding the best set of paramters for a given model using the following concepts (shamelessly stolen from the work of our mother nature over the last several billions of years):

The genetic algorithm repeats the selection, cross-over and mutations steps until the best fitted individual reach a certain fitness threshold.


Implementation details:

The demo case:

The demo case generates pseudo-random data following a gaussian distribution and uses the genetic algorithm to guess the parameters of the gaussian from the data. The results are then compared with a likelihood fitting method as implemented in the ROOT library.

To make this implementation reusable in future projects, abstract interfaces are used for the different elements: generic features are implemented in the interface base classes, while features that are specific to the demo case are implemented in derived classes.

The Model:

The model is what we are trying to optimize. In general, it is described by a set of properties that can have different values.

The figure of merit:

Sometimes it's called a fitness function: I prefer the term figure of merit over fitness function, because the latter suggest a function that increases with goodness (i.e. the higher the better) while there is no reason for this to always be the case. In fact, in our demo case example, a better \(\chi^2\) is a smaller one.

The population:

The population defines a generation of models and controlls the way models are selected, crossed-over and mutated to build the next generation.

Fig.1: Probability for parents to give offspring as function of their rank within the population.
Test Parent Selection.

The algorithm:

The GeneticAlgorithm class wraps everything above and controls the flow of the algorithm using the defined interfaces. It performs the following steps:


Results:

An example output of the demo looks like this:

Algorithm Configuration:
==> nmc = 10000
==> acceptThreshold = 0.85
==> mutateRate = 0.01
==> mutateSize = 0.1
==> maxGenerations = 10000
==> populationSize = 500
Input parameters:
==> Constant : 0.173453 - range: [0.001, 1]
==> Mean : 1.5 - range: [-10, 10]
==> Sigma : 2.3 - range: [0.001, 10]
Done after 27 generations.
==> Best score is: 0.830388
After GA fit:
==> Constant : 0.173211
==> Mean : 1.50147
==> Sigma : 2.30937
After Likelihood fit:
==> Constant : 0.173272
==> Mean : 1.50329
==> Sigma : 2.28956
Fig.1: Animation.
Fig.1: Animation showing the convergence of the genetic algorithm.