Grail (C#)  1.4.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
Grail.Evolution.EAOptimizer Class Reference

A class that serves as the main interface for evolutionary algorithms in Grail. More...

Public Member Functions

 EAOptimizer (List< Individual > initialPopulation, uint maxEpochCount, Crossover crossover=null, Mutation mutation=null, Selection selection=null)
 Constructor. The EA algorithm will maintain the size of the initial population after the selection phase. More...
 
List< IndividualGetPopulation ()
 
Returns
A list, sorted by fitness, that contains deep clones of the current population evolved by the algorithm.

 
Individual GetBestIndividual ()
 Returns the individual with the highest fitness value from the population. More...
 
void SerializeState (string filename)
 Serializes the inner state of the evolutionary algorithm, including the current population.
You can load the serialized state again by calling the DeserializeState(string) method. More...
 
bool DeserializeState (string filename)
 Loads a previously serialized state of the evolutionary algorithm.
You can, for instance, get the results or continue calculations. More...
 
float CalculatePopulationDiversity ()
 
Returns
The diversity ratio as a number [0,1]. The lower the value, the population is more homogeneous

 
int CalculatePopulationIdenticalFrontCount ()
 Finds the most frequent individuals (which are identical to each other) and returns the number of them. More...
 
float CalculatePopulationIdenticalFrontRatio ()
 
Returns
The number of the most frequent identical individuals divided by the population size.

 
void RunInteractive (System.Action< Individual > onEvaluateNeeded, System.Action onFinished=null, System.Action onNewEpoch=null)
 Advances the computations of the evolutionary algorithm (EA). This is the main method to run it. This method will call provided functions on specific events. More...
 

Public Attributes

int PopulationSize => corePopulation.Count
 Returns the number of individuals in the current population. More...
 

Properties

double ElitismRate [get, set]
 Percent of most fit individuals in a population that will be considered for the next population without any change. More...
 
ElitismType ElitismType = 0.2 [get, set]
 Determines if and how the elitism is enabled in the evolutionary algorithm. See ElitismType. More...
 
uint MaxEpochCount = ElitismType.NONE [get]
 The number of epochs (iterations) of EA, after which the algorithm will stop. More...
 
uint EpochNumber = 0 [get]
 The current epoch (iteration) number. More...
 

Detailed Description

A class that serves as the main interface for evolutionary algorithms in Grail.

Constructor & Destructor Documentation

◆ EAOptimizer()

Grail.Evolution.EAOptimizer.EAOptimizer ( List< Individual initialPopulation,
uint  maxEpochCount,
Crossover  crossover = null,
Mutation  mutation = null,
Selection  selection = null 
)
inline

Constructor. The EA algorithm will maintain the size of the initial population after the selection phase.

Parameters
initialPopulationThe initial population to the EA algorithm. For example, you can create one individual and call GetRandomRealizations() on it.
maxEpochCountThe maximum number of full iterations after which the algorithm will stop making any further optimizations.
crossoverThe Crossover instance that encapsulates the crossover (recombination) procedure of evolutionary algorithm.
mutationThe Mutation instance that encapsulates the mutation procedure of evolutionary algorithm.
selectionThe Selection instance that encapsulates the selection procedure of evolutionary algorithm.

Member Function Documentation

◆ CalculatePopulationIdenticalFrontCount()

int Grail.Evolution.EAOptimizer.CalculatePopulationIdenticalFrontCount ( )
inline

Finds the most frequent individuals (which are identical to each other) and returns the number of them.

Returns
The number of the most frequent identical individuals.

◆ DeserializeState()

bool Grail.Evolution.EAOptimizer.DeserializeState ( string  filename)
inline

Loads a previously serialized state of the evolutionary algorithm.
You can, for instance, get the results or continue calculations.

Parameters
filenameThe path where the state has been saved.
Returns
A boolean value that tells if the deserialization was successful.

◆ GetBestIndividual()

Individual Grail.Evolution.EAOptimizer.GetBestIndividual ( )
inline

Returns the individual with the highest fitness value from the population.

Returns
The individual with the highest fitness value from the population.

◆ RunInteractive()

void Grail.Evolution.EAOptimizer.RunInteractive ( System.Action< Individual onEvaluateNeeded,
System.Action  onFinished = null,
System.Action  onNewEpoch = null 
)
inline

Advances the computations of the evolutionary algorithm (EA). This is the main method to run it. This method will call provided functions on specific events.

Parameters
onEvaluateNeededWhen EA needs an individual to have it fitness evaluated, it will invoke this callback function with this individual as a parameter.
onFinishedThis callback will be invoked when the algorithm has reached its maximum number of iterations.
onNewEpochThis callback will be invoked when a new epoch (iteration) starts. An epoch is one round of evaluating a population and applying genetic operators on it.

◆ SerializeState()

void Grail.Evolution.EAOptimizer.SerializeState ( string  filename)
inline

Serializes the inner state of the evolutionary algorithm, including the current population.
You can load the serialized state again by calling the DeserializeState(string) method.

Parameters
filenameThe path where the state will be saved. If the file exists, it will be overridden.

Member Data Documentation

◆ PopulationSize

int Grail.Evolution.EAOptimizer.PopulationSize => corePopulation.Count

Returns the number of individuals in the current population.

Property Documentation

◆ ElitismRate

double Grail.Evolution.EAOptimizer.ElitismRate
getset

Percent of most fit individuals in a population that will be considered for the next population without any change.

◆ ElitismType

ElitismType Grail.Evolution.EAOptimizer.ElitismType = 0.2
getset

Determines if and how the elitism is enabled in the evolutionary algorithm. See ElitismType.

◆ EpochNumber

uint Grail.Evolution.EAOptimizer.EpochNumber = 0
get

The current epoch (iteration) number.

◆ MaxEpochCount

uint Grail.Evolution.EAOptimizer.MaxEpochCount = ElitismType.NONE
get

The number of epochs (iterations) of EA, after which the algorithm will stop.


The documentation for this class was generated from the following file: