AForge.NET Framework
2.2.5 version is available!

Genetic Algorithms Library

AForge.NET framework provides genetic algorithms library, which contains set of classes providing functionality allowing to solve many different problems with the help of evolutionary computations based on Genetic Algorithms (GA), Genetic Programming (GP) and Gene Expression Programming (GEP).

The library consists of 3 main sets of classes:

  • Collection of different chromosomes' classes implementing IChromosome interface suiting different common problems. These classes implement such operators like crossover and mutation, which are used for driving search through solutions' space by creating new chromosomes representing new solutions.
  • Collection of fitness functions' classes implementing IFitnessFunction interface, which are used to evaluate chromosomes by calculating their fitness values for different tasks;
  • Collection of selection algorithms' classes implementing ISelectionMethod interface, which are used to perform selection of population members (chromosomes).
The main class of the library is Population class, which organizes the work of genetic algorithm (GA/GP/GEP) creating initial population of random members, creating new members with the help of crossover and mutations operators, calculating fitness values of new members and performing selection of members to keep basing on members' usefulness (fitness). Creating population object it is required to specify which chromosomes, fitness function and selection algorithm to use. Once this is done, the population object is ready to be used for searching of problem's solution:

// create genetic population
Population population = new Population( 40,
       selectionAlgorithm );
while ( true )
    // run one epoch of the population
    population.RunEpoch( );
    // check current best fitness
    if ( population.FitnessMax > limit )
        // ...

As an example, below is small a sample code, which demonstrates usage of Population class for solving quite simple problem - searching function's maximum value.

// define optimization function
public class UserFunction : OptimizationFunction1D
    public UserFunction( ) :
       base( new DoubleRange( 0, 255 ) ) { }
    public override double OptimizationFunction( double x )
       return Math.Cos( x / 23 ) * Math.Sin( x / 50 ) + 2;
// create genetic population
Population population = new Population( 40,
       new BinaryChromosome( 32 ),
       new UserFunction( ),
       new EliteSelection( ) );
// run one epoch of the population
population.RunEpoch( );

The Population class also supports migration of members from one population to another, which allows to exchange by good solutions between populations. Also this feature allows to run several populations simultaneously in different threads (distributing computations on multiple cores/CPUs if they are available) and exchange by good solutions from time to time bringing "fresh blood" to populations.

For additional information check genetic algorithms' samples provided with AForge.NET framework.