Grail (C++)  1.4.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
grail::evolution::EAOptimizer Class Referencefinal

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

#include <EAOptimizer.hh>

Public Member Functions

 EAOptimizer (std::vector< std::unique_ptr< Individual >> &initialPopulation, size_t maxEpochCount, std::unique_ptr< Crossover > crossover=std::make_unique< Crossover >(), std::unique_ptr< Mutation > mutation=std::make_unique< Mutation >(), std::unique_ptr< Selection > selection=std::make_unique< Selection >(), std::mt19937_64::result_type seed=std::random_device{}())
 The EA algorithm will maintain the size of the initial population after the selection phase. More...
 
void RunInteractive (std::function< void(Individual &individualToEvaluate)> onEvaluateNeeded, std::function< void(void)> onFinished=nullptr, std::function< void(void)> onNewEpoch=nullptr)
 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...
 
size_t GetPopulationSize () const
 PopulationSize - returns the number of individuals in the current population. More...
 
std::vector< std::unique_ptr< Individual > > GetPopulation () const
 
void SetElitism (double rate, ElitismType type)
 SetElitism - sets how many individuals advance to the next generation in an unmodified form and how they are chosen. These are called elite individuals. More...
 
double GetElitismRate () const
 
ElitismType GetElitismType () const
 
float CalculatePopulationDiversity () const
 Returns the diversity ratio as a number [0,1]. The lower the value, the population is more homogeneous. More...
 
size_t CalculatePopulationIdenticalFrontCount () const
 Finds the most frequent individuals (which are identical to each other) and returns the number of them. More...
 
float CalculatePopulationIdenticalFrontRatio () const
 Returns the number of the most frequent identical individuals divided by the population size. More...
 
const IndividualGetBestIndividual () const
 GetBestIndividual - gets the individual with the highest fitness value from the population. More...
 
size_t GetEpochNumber () const
 
size_t GetMaxEpochCount () const
 
CrossoverGetCrossover () const
 GetCrossover - gets the crossover instance that is used by this evolutionary algorithm. More...
 
MutationGetMutation () const
 GetMutation - gets the mutation instance that is used by this evolutionary algorithm. More...
 
SelectionGetSelection () const
 GetSelection - gets the selection instance that is used by this evolutionary algorithm. More...
 
void SerializeState (const std::string &filename) const
 Serializes the inner state of the evolutionary algorithm, including the current population.
You can load the serialized state again by calling the 'DeserializeState' method. More...
 
bool DeserializeState (const std::string &filename)
 Loads a previously serialized state of the evolutionary algorithm.
You can, for instance, get the results or continue calculations. More...
 

Friends

std::ostream & operator<< (std::ostream &out, const EAOptimizer &eaOptimizer)
 Appends to stream the serialized state of the evolutionary algorithm including the whole population and basic statistics about the population. More...
 

Detailed Description

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

Constructor & Destructor Documentation

◆ EAOptimizer()

grail::evolution::EAOptimizer::EAOptimizer ( std::vector< std::unique_ptr< Individual >> &  initialPopulation,
size_t  maxEpochCount,
std::unique_ptr< Crossover crossover = std::make_unique<Crossover>(),
std::unique_ptr< Mutation mutation = std::make_unique<Mutation>(),
std::unique_ptr< Selection selection = std::make_unique<Selection>(),
std::mt19937_64::result_type  seed = std::random_device{}() 
)

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

Parameters
initialPopulation- the initial population to the EA algorithm. For example, you can create one individual and call GetRandomRealizations() on it.
maxEpochCount- the maximum number of full iterations after which the algorithm will stop making any further optimizations.
crossover- a crossover struct instance that encapsulates the crossover (recombination) procedure of evolutionary algorithm.
mutation- a mutation struct instance that encapsulates the mutation procedure of evolutionary algorithm.
selectiona selection class instance that encapsulates the selection procedure of evolutionary algorithm.
seed- the seed to the random numbers generator.

Member Function Documentation

◆ CalculatePopulationDiversity()

float grail::evolution::EAOptimizer::CalculatePopulationDiversity ( ) const

Returns the diversity ratio as a number [0,1]. The lower the value, the population is more homogeneous.

◆ CalculatePopulationIdenticalFrontCount()

size_t grail::evolution::EAOptimizer::CalculatePopulationIdenticalFrontCount ( ) const

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

◆ CalculatePopulationIdenticalFrontRatio()

float grail::evolution::EAOptimizer::CalculatePopulationIdenticalFrontRatio ( ) const

Returns the number of the most frequent identical individuals divided by the population size.

◆ DeserializeState()

bool grail::evolution::EAOptimizer::DeserializeState ( const std::string &  filename)

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

Parameters
filename- the path where the state has been saved.
Returns
- a boolean value that tells if the deserialization was successful.

◆ GetBestIndividual()

const Individual & grail::evolution::EAOptimizer::GetBestIndividual ( ) const

GetBestIndividual - gets the individual with the highest fitness value from the population.

◆ GetCrossover()

Crossover & grail::evolution::EAOptimizer::GetCrossover ( ) const

GetCrossover - gets the crossover instance that is used by this evolutionary algorithm.

◆ GetElitismRate()

double grail::evolution::EAOptimizer::GetElitismRate ( ) const
Returns
the ElitismRate - the portion of elite individuals to the whole population.

◆ GetElitismType()

ElitismType grail::evolution::EAOptimizer::GetElitismType ( ) const
Returns
the ElitismType that determines how elite individuals are chosen. See the description of ElitismType.

◆ GetEpochNumber()

size_t grail::evolution::EAOptimizer::GetEpochNumber ( ) const
Returns
the current epoch (iteration) number.

◆ GetMaxEpochCount()

size_t grail::evolution::EAOptimizer::GetMaxEpochCount ( ) const
Returns
the number of epochs (iterations) of EA, after which the algorithm will stop.

◆ GetMutation()

Mutation & grail::evolution::EAOptimizer::GetMutation ( ) const

GetMutation - gets the mutation instance that is used by this evolutionary algorithm.

◆ GetPopulation()

std::vector< std::unique_ptr< Individual > > grail::evolution::EAOptimizer::GetPopulation ( ) const
Returns
A vector, sorted by fitness, that contains deep clones of the current population evolved by the algorithm.

◆ GetPopulationSize()

size_t grail::evolution::EAOptimizer::GetPopulationSize ( ) const

PopulationSize - returns the number of individuals in the current population.

Returns
the size of the current population.

◆ GetSelection()

Selection & grail::evolution::EAOptimizer::GetSelection ( ) const

GetSelection - gets the selection instance that is used by this evolutionary algorithm.

◆ RunInteractive()

void grail::evolution::EAOptimizer::RunInteractive ( std::function< void(Individual &individualToEvaluate)>  onEvaluateNeeded,
std::function< void(void)>  onFinished = nullptr,
std::function< void(void)>  onNewEpoch = nullptr 
)

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
onEvaluateNeeded- when EA needs an individual to have it fitness evaluated, it will invoke this callback function with this individual as a parameter.
onFinished- this callback will be invoked when the algorithm has reached its maximum number of iterations.
onNewEpoch- this 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 ( const std::string &  filename) const

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

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

◆ SetElitism()

void grail::evolution::EAOptimizer::SetElitism ( double  rate,
ElitismType  type 
)

SetElitism - sets how many individuals advance to the next generation in an unmodified form and how they are chosen. These are called elite individuals.

Parameters
rate- the portion of elite individuals to the whole population.
type- determines how elite individuals are chosen. See the description of ElitismType.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  out,
const EAOptimizer eaOptimizer 
)
friend

Appends to stream the serialized state of the evolutionary algorithm including the whole population and basic statistics about the population.


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