Samchon Framework for CPP  1.0.0
samchon::library::GAPopulation< GeneArray, Compare > Class Template Reference

A population in a generation in G.A. More...

#include <GAPopulation.hpp>

Public Member Functions

 GAPopulation (std::shared_ptr< GeneArray > geneArray, size_t size)
 Construct from a GeneArray and size of the population. More...
 
auto fitTest () const -> std::shared_ptr< GeneArray >
 Test fitness of each GeneArray in the population. More...
 

Private Member Functions

 GAPopulation (size_t size)
 Private Constructor with population. More...
 

Private Attributes

std::vector< std::shared_ptr< GeneArray > > children
 Genes representing the population. More...
 

Detailed Description

template<typename GeneArray, typename Compare = std::less<GeneArray>>
class samchon::library::GAPopulation< GeneArray, Compare >

A population in a generation in G.A.

Template Parameters
GeneArray

An array(std::vector) containing genes as elments; sequnce listing

The GeneArray must be a type of std::vector.

Template Parameters
CompareA comparison class (or struct) returns whether left gene is more optimal.

Default template parameter of Compare is std::less<GeneArray>. It means to compare two std::vector (GeneArray must be a std::vector). Thus, you've to keep follwing rules.

  • GeneArray is inherited from std::vector
  • GeneArray has custom auto operator<(const GeneArray &) const -> bool

If you don't want to follow the rules or want a custom comparison class, you have to realize a comparison class. The following code is an example realizing the comparison class.

template <typename _Ty>
struct MyCompare
{
auto operator()(const _Ty &newObj, const _Ty &prevObj) const -> bool;
};

GAPopulation is a class representing population of candidate genes(sequence listing) having an array of GeneArray as a member. GAPopulation also manages initial set of genes and handles fitting test direclty by the method fitTest.

The success of evolution of genetic algorithm is depend on the GAPopulation's initial set and fitting test. (GeneArray and Compare.)

Class Diagram
Warning

Be careful for the mistakes of direction or position of Compare.

Most of logical errors failed to access optimal solution are occured from those mistakes.

Library - Genetic Algorithm

Author
Jeonghoh Nam

Definition at line 57 of file GAPopulation.hpp.

Constructor & Destructor Documentation

template<typename GeneArray , typename Compare = std::less<GeneArray>>
samchon::library::GAPopulation< GeneArray, Compare >::GAPopulation ( size_t  size)
inlineprivate

Private Constructor with population.

Private constructor of GAPopulation does not create children (candidate genes) but fills only nullptr repeatedly following the size.

The private constructor is only for and handled by GeneticAlgorithm.

Parameters
sizeSize of the population

Definition at line 78 of file GAPopulation.hpp.

template<typename GeneArray , typename Compare = std::less<GeneArray>>
samchon::library::GAPopulation< GeneArray, Compare >::GAPopulation ( std::shared_ptr< GeneArray >  geneArray,
size_t  size 
)
inline

Construct from a GeneArray and size of the population.

Constructor with initial set of genes and size of the population.

The public constructor creates GeneArray(s) as population (size) having shuffled genes which are came from the initial set of genes (geneArray).

Note
The success of genetic algorithm is depend on the initial genes(geneArray).
Parameters
geneArrayAn initial sequence listing
sizeThe size of population to have as children

Definition at line 97 of file GAPopulation.hpp.

Member Function Documentation

template<typename GeneArray , typename Compare = std::less<GeneArray>>
auto samchon::library::GAPopulation< GeneArray, Compare >::fitTest ( ) const -> std::shared_ptr<GeneArray>
inline

Test fitness of each GeneArray in the population.

Returns
The best GeneArray in the population

Definition at line 117 of file GAPopulation.hpp.

Referenced by samchon::library::GeneticAlgorithm< GeneArray, Compare >::selection().

Here is the caller graph for this function:

Member Data Documentation

template<typename GeneArray , typename Compare = std::less<GeneArray>>
std::vector<std::shared_ptr<GeneArray> > samchon::library::GAPopulation< GeneArray, Compare >::children
private

Genes representing the population.

Definition at line 66 of file GAPopulation.hpp.

Referenced by samchon::library::GeneticAlgorithm< GeneArray, Compare >::selection().


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