Clone wiki

GAF / Fitness and Termination

Fitness and Termination

Fitness Function

Fitness refers to the assessment of a solution provided by the genetic algorithm. For example if the GAF is being used to optimise the shape of a fan blade, the genetic algorithm will constantly present a solution representing a collection of fan blade parameters to the fitness function for evaluation. If you are a developer using the GAF, the fitness function is where your effort should be focused as this is what controls the process. In the example above the developer is responsible for implementing a fitness function that can evaluate any given set of parameters representing a fan blade.

The GAF simply requires that you create a fitness function in the following form. The method name is unimportant, but the signature is. The contents of this function are determined by the developer. When the method is called by the GAF, the GAF will pass a populated Chromosome for evaluation.

private double CalculateFitness(Chromosome chromosome)
    //calculate fitness 

    return fitness;

This function is passed to the GAF as a delegate during initialisation and will be called by the GAF when the GAF needs to evaluate a solution.

var ga = new GeneticAlgorithm(population, CalculateFitness);

Solutions from the population are passed to this fitness function by the GAF. The value returned by the fitness function should be set to a real number between 0 and 1.0, the higher the value, the fitter the solution. Therefore, using the above example a solution representing a fan blade would have a fitness value somewhere between 0 and 1.0 and proportional to how good it is as a fan blade.

Terminate Function

Stopping the genetic algorithm, hopefully once it has a suitable solution to the problem, is carried out by the Terminate function. This function is provided by the developer and passed to the GAF as a delegate at which point it will be called periodically by the GAF as required. Returning 'true' from this function will terminate the running genetic algorithm. It should take the following form.

The signature of the TerminateFunction is shown below. The method name is unimportant, but the signature is.

private bool TerminateFunction(Population population, 
                                int currentGeneration, 
                                long currentEvaluation) 
    //determine criteria on which to terminate

    return result; 

The function is then passed to the GAF when it is executed.


When the function is called by the GAF, it will pass the current generation number and the number of evaluations that have occurred so far. The following example Terminate Function would terminate the algorithm after 400 generations.

private bool TerminateFunction(Population population, 
                                int currentGeneration, 
                                long currentEvaluation) 
    //return true if 400 generations have been reached.
    return currentGeneration >= 400; 

Using a Separate Assembly for Fitness

It may, on occasions be desirable to place the fitness function within its own, separate, assembly. This can be achieved by creating a class within a dll that implements the IFitness interface.

A complete example of how this can be achieved is given in the examples section, Using a Separate Assembly for Fitness.

See Also
Getting Started Population
Chromosomes and Genes Genetic Operators
Events Fitness and Termination
Examples The GAF Evaluation Server
Evaluating using Docker Swarm