Fitness Repository

FitnessRepository is a helper class available with Grail’s evolutionary algorithm.

It stores fitness values computed for Individuals having the same encodings and allows updating their values based on the average value obtained so far. This is useful in scenarios where:

  1. The fitness function is not deterministic, to increase confidence and reduce variance. Please take a look at Evolutionary Algorithm: Designing Fitness Function

  2. Subsequent fitness calculations are done under the same conditions so they can be compared to each other (e.g., the setup/environment does not change drastically).

The idea behind FitnessRepository is shown in the picture below:

The idea of FitnessRepository

Public Interface

Constructor

Takes a maxSamplesPerIndividual parameter, which limits the number of fitness scores stored per Individual. Whenever a new value would be added, when this limit has been reached, the oldest one is removed. This helps to keep the memory complexity low.

AddSampleWithFitnessUpdate

This method serves two purposes at once.

First, it adds a new sample to the repository. The current fitness value is read from the Individual that is passed as a parameter.

Then, it updates the current fitness value of this Individual to the average value obtained by this and equal individuals so far.

TryGetFitness

This method only retrieves the fitness value of the given Individual without updating it. If it does not exist, i.e., no AddSampleWithFitnessUpdate has been called for this Individual, then it returns FALSE. Otherwise, it returns TRUE and assigns the fitness score to the out parameter.

GetSampleCount

Returns the number of fitness values currently stored for a given individual.

Serialize

You can use this method to store the contents of the entire FitnessRepository to a file with the specified path.

Deserialize

You can use this method to load the contents of FitnessRepository from a file.