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

Represents an individual for evolutionary algorithms (EA).
It stores the encoding consisting of optimizable parameters (represented by 'BaseEvoParam' and specialized 'EvoParam<Type>'.
The members (fields or properties) should be registered for optimization using 'SetMemberParameterOptimizable' methods in your implementation of the constructor.
In the optimization process, the EA will require assigning the fitness evaluation to the individual. Calculate it in your game environment based on the current values of Individual's parameters. More...

#include <Individual.hh>

Public Member Functions

virtual std::unique_ptr< IndividualInstantiate () const =0
 Create your final optimizable object that is based on this Individual. In EA such object is often referred to as the phenotype. You do not need to clone the values of EvoParams, because they will be synchronized automatically.
 
void SetFitness (double fitness)
 SetFitness - sets the fitness value (evaluation of how good the particular Individual object is). More...
 
double GetFitness () const
 GetFitness - gets the fitness value (evaluation of how good the particular Individual object is). More...
 
std::unique_ptr< IndividualClone () const
 Performs a deep copy of the individual. More...
 
std::vector< std::unique_ptr< Individual > > CloneMany (int cloneCount) const
 Performs a given number of copies of the individual. More...
 
void Randomize (std::mt19937_64 &randomGenerator)
 Changes the values of each parameter in the encoding of the individual to random available ones. More...
 
long GetPossibleRealizationsCount () const
 
std::vector< std::unique_ptr< Individual > > GetRandomRealizations (std::mt19937_64 &randomGenerator, std::size_t count) const
 Returns "count" number of copies of the individuals with randomized parameter values. More...
 
std::size_t EvoParamsCount () const
 EvoParamsCount - returns the number of parameters set as optimizable for evolutionary algorithms. More...
 
float CalculateSimilarity (const Individual &other) const
 CalculateSimilarity - this function calculates the similarity between two Individuals in the form of a number of corresponding EvoParam pairs that share the same value divided by the total number of such pairs. Thus, the value is normalized to the [0,1] interval. More...
 
virtual std::string ToString () const
 ToString - returns the string representation of the Individual object.
 
const BaseEvoParamGetEvoParamAt (std::size_t index) const
 GetEvoParamAt - returns the i-th EvoParam of the collection of parameters stored in this Individual. More...
 
std::size_t GetHashCode () const
 Custom hash implementation.
 
bool Equals (const Individual &other) const
 Tests whether two individuals have the same structure (even if they point to different objects in memory).
 

Protected Member Functions

void SetMemberParameterOptimizable (BaseEvoParam &parameter)
 Call this method in a constructor and pass a reference to a field stored in this object.
Marks a certain stored parameter as optimizable allowing it to be manipulated by the evolutionary algorithm (EA).
This operation essentially binds your class member with the object used by EA. During optimization, EA might change its value.
You can then read the value of your field or property using memberName.Value. More...
 
template<typename ForwardIterator >
void SetMemberParameterOptimizable (ForwardIterator beginIterator, ForwardIterator endIterator)
 Call this method in a constructor and pass references to a field stored in this object.
Marks a certain stored collection-type parameter as optimizable allowing it to be manipulated by the evolutionary algorithm (EA).
The collection will be typically defined to hold the EvoParam{type} specialization instances. More...
 

Protected Attributes

std::vector< std::reference_wrapper< BaseEvoParam > > parameters {}
 The encoding of the Individual – the collection of its optimizable parameters.
 

Friends

struct Mutation
 
struct Crossover
 
class EAOptimizer
 
class FitnessRepository
 

Detailed Description

Represents an individual for evolutionary algorithms (EA).
It stores the encoding consisting of optimizable parameters (represented by 'BaseEvoParam' and specialized 'EvoParam<Type>'.
The members (fields or properties) should be registered for optimization using 'SetMemberParameterOptimizable' methods in your implementation of the constructor.
In the optimization process, the EA will require assigning the fitness evaluation to the individual. Calculate it in your game environment based on the current values of Individual's parameters.

Member Function Documentation

◆ CalculateSimilarity()

float grail::evolution::Individual::CalculateSimilarity ( const Individual other) const

CalculateSimilarity - this function calculates the similarity between two Individuals in the form of a number of corresponding EvoParam pairs that share the same value divided by the total number of such pairs. Thus, the value is normalized to the [0,1] interval.

Parameters
other- instance of another Individual object to which similarity is computed.
Returns
the computed similarity.

◆ Clone()

std::unique_ptr< Individual > grail::evolution::Individual::Clone ( ) const

Performs a deep copy of the individual.

Returns
A copy of the individual that holds the same parameter values.

◆ CloneMany()

std::vector< std::unique_ptr< Individual > > grail::evolution::Individual::CloneMany ( int  cloneCount) const

Performs a given number of copies of the individual.

Parameters
cloneCount- the number of copies to make.
Returns
a list of copies of the individual. For example, you can use this method to create the initial population.

◆ EvoParamsCount()

std::size_t grail::evolution::Individual::EvoParamsCount ( ) const

EvoParamsCount - returns the number of parameters set as optimizable for evolutionary algorithms.

Returns
the size of collection of BaseEvoParams.

◆ GetEvoParamAt()

const BaseEvoParam & grail::evolution::Individual::GetEvoParamAt ( std::size_t  index) const

GetEvoParamAt - returns the i-th EvoParam of the collection of parameters stored in this Individual.

Parameters
index- the index of the returned EvoParam.
Returns
EvoParam.

◆ GetFitness()

double grail::evolution::Individual::GetFitness ( ) const

GetFitness - gets the fitness value (evaluation of how good the particular Individual object is).

Returns
fitness value.

◆ GetPossibleRealizationsCount()

long grail::evolution::Individual::GetPossibleRealizationsCount ( ) const
Returns
the maximum number of distinct parameterizations the individual may have.

◆ GetRandomRealizations()

std::vector< std::unique_ptr< Individual > > grail::evolution::Individual::GetRandomRealizations ( std::mt19937_64 &  randomGenerator,
std::size_t  count 
) const

Returns "count" number of copies of the individuals with randomized parameter values.

Parameters
randomGenerator- random numbers generator to use.
count- the number of individuals to return.
Returns
clones of the Individual with its parameters set to random possible values

◆ Randomize()

void grail::evolution::Individual::Randomize ( std::mt19937_64 &  randomGenerator)

Changes the values of each parameter in the encoding of the individual to random available ones.

Parameters
randomGenerator- random numbers generator to use.

◆ SetFitness()

void grail::evolution::Individual::SetFitness ( double  fitness)

SetFitness - sets the fitness value (evaluation of how good the particular Individual object is).

Parameters
fitness- the fitness value.

◆ SetMemberParameterOptimizable() [1/2]

void grail::evolution::Individual::SetMemberParameterOptimizable ( BaseEvoParam parameter)
protected

Call this method in a constructor and pass a reference to a field stored in this object.
Marks a certain stored parameter as optimizable allowing it to be manipulated by the evolutionary algorithm (EA).
This operation essentially binds your class member with the object used by EA. During optimization, EA might change its value.
You can then read the value of your field or property using memberName.Value.

Parameters
parameter- a member stored in your Individual class of type BaseEvoParam. Typically, you will be using the EvoParam{type} specialization instead of BaseEvoParam directly.

◆ SetMemberParameterOptimizable() [2/2]

template<typename ForwardIterator >
void grail::evolution::Individual::SetMemberParameterOptimizable ( ForwardIterator  beginIterator,
ForwardIterator  endIterator 
)
inlineprotected

Call this method in a constructor and pass references to a field stored in this object.
Marks a certain stored collection-type parameter as optimizable allowing it to be manipulated by the evolutionary algorithm (EA).
The collection will be typically defined to hold the EvoParam{type} specialization instances.

Parameters
begin- the begin() iterator of your collection type.
end- the end() iterator of your collection type.

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