Grail (C++)  1.4.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
grail::evolution::RouletteSampler< T > Class Template Reference

Provides a functionality of a fitness proportional roulette sampling of any objects that implement a GetFitness() method.
In particular it is used by an evolutionary algorithm (EA).
Because the implementation is optimized for usage in EA, the sampled elements are removed unless the method ends with 'WithReturn'. More...

#include <RouletteSampler.hh>

Public Member Functions

 RouletteSampler ()
 RouletteSampler - Constructor. Note that if you use a parameterless constructor, call Initialize() to provide elements for sampling.
They must implement a public double GetFitness() method. More...
 
 RouletteSampler (const std::vector< std::unique_ptr< T >> &fitnessHavingElements)
 RouletteSampler - Constructor. More...
 
void Initialize (const std::vector< std::unique_ptr< T >> &fitnessHavingElements)
 Sets the elements that can be sampled using this RouletteSampler. They must implement a public "double GetFitness()" method. More...
 
T * Sample (std::mt19937_64 &randGen)
 Samples one element and removes it. More...
 
T * SampleWithReturn (std::mt19937_64 &randGen)
 Samples one element without removing it. More...
 
std::vector< T * > SampleMany (size_t count, std::mt19937_64 &randGen)
 Samples a given number of elements and removes them. More...
 
std::vector< T * > SampleManyWithReturn (size_t count, std::mt19937_64 &randGen)
 Samples a given number of elements without removing them. More...
 
std::vector< size_t > SampleManyIndices (size_t count, std::mt19937_64 &randGen)
 SampleManyIndices - samples a given number of indices of elements and removes them. More...
 

Detailed Description

template<class T>
class grail::evolution::RouletteSampler< T >

Provides a functionality of a fitness proportional roulette sampling of any objects that implement a GetFitness() method.
In particular it is used by an evolutionary algorithm (EA).
Because the implementation is optimized for usage in EA, the sampled elements are removed unless the method ends with 'WithReturn'.

Parameters
T- the type T must have a public method ef "double GetFitness()"

Constructor & Destructor Documentation

◆ RouletteSampler() [1/2]

template<class T >
grail::evolution::RouletteSampler< T >::RouletteSampler
inline

RouletteSampler - Constructor. Note that if you use a parameterless constructor, call Initialize() to provide elements for sampling.
They must implement a public double GetFitness() method.

◆ RouletteSampler() [2/2]

template<class T >
grail::evolution::RouletteSampler< T >::RouletteSampler ( const std::vector< std::unique_ptr< T >> &  fitnessHavingElements)
inline

RouletteSampler - Constructor.

Parameters
fitnessHavingElements- a vector of elements which will be sampled.

Member Function Documentation

◆ Initialize()

template<class T >
void grail::evolution::RouletteSampler< T >::Initialize ( const std::vector< std::unique_ptr< T >> &  fitnessHavingElements)
inline

Sets the elements that can be sampled using this RouletteSampler. They must implement a public "double GetFitness()" method.

Parameters
fitnessHavingElements- a vector of elements which will be sampled.

◆ Sample()

template<class T >
T * grail::evolution::RouletteSampler< T >::Sample ( std::mt19937_64 &  randGen)
inline

Samples one element and removes it.

Parameters
randGen- random numbers generator to use.
Returns
the sampled element.

◆ SampleMany()

template<class T >
std::vector< T * > grail::evolution::RouletteSampler< T >::SampleMany ( size_t  count,
std::mt19937_64 &  randGen 
)
inline

Samples a given number of elements and removes them.

Parameters
count- the number of elements to sample.
randGen- random numbers generator to use.
Returns
the sampled elements.

◆ SampleManyIndices()

template<class T >
std::vector< size_t > grail::evolution::RouletteSampler< T >::SampleManyIndices ( size_t  count,
std::mt19937_64 &  randGen 
)
inline

SampleManyIndices - samples a given number of indices of elements and removes them.

Parameters
count- the number of indices to sample.
randGen- random numbers generator to use.
Returns
the sampled indices of elements.

◆ SampleManyWithReturn()

template<class T >
std::vector< T * > grail::evolution::RouletteSampler< T >::SampleManyWithReturn ( size_t  count,
std::mt19937_64 &  randGen 
)
inline

Samples a given number of elements without removing them.

Parameters
count- the number of elements to sample.
randGen- random numbers generator to use.
Returns
the sampled elements.

◆ SampleWithReturn()

template<class T >
T * grail::evolution::RouletteSampler< T >::SampleWithReturn ( std::mt19937_64 &  randGen)
inline

Samples one element without removing it.

Parameters
randGen- random numbers generator to use.
Returns
the sampled element.

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