Samchon Framework for CPP  1.0.0
GAPopulation.hpp
1 #pragma once
2 
3 #include <algorithm>
4 #include <functional>
5 #include <memory>
6 #include <vector>
7 
8 namespace samchon
9 {
10 namespace library
11 {
56  template <typename GeneArray, typename Compare = std::less<GeneArray>>
58  {
59  template <typename GeneArray, typename Compare>
60  friend class GeneticAlgorithm;
61 
62  private:
66  std::vector<std::shared_ptr<GeneArray>> children;
67 
78  GAPopulation(size_t size)
79  {
80  children.assign(size, nullptr);
81  };
82 
83  public:
97  GAPopulation(std::shared_ptr<GeneArray> geneArray, size_t size)
98  {
99  children.reserve(size);
100 
101  for (size_t i = 0; i < size; i++)
102  {
103  GeneArray *ptr = new GeneArray(*geneArray);
104 
105  if (i > 0) //FOR ELITICISM
106  std::random_shuffle(ptr->begin(), ptr->end());
107 
108  children.emplace_back(ptr);
109  }
110  };
111 
117  auto fitTest() const -> std::shared_ptr<GeneArray>
118  {
119  std::shared_ptr<GeneArray> best = children[0];
120 
121  for (size_t i = 1; i < children.size(); i++)
122  if (Compare()(*children[i], *best) == true)
123  best = children[i];
124 
125  return best;
126  };
127  };
128 };
129 };
auto fitTest() const -> std::shared_ptr< GeneArray >
Test fitness of each GeneArray in the population.
GAPopulation(size_t size)
Private Constructor with population.
A population in a generation in G.A.
A genetic algorithm class.
std::vector< std::shared_ptr< GeneArray > > children
Genes representing the population.
GAPopulation(std::shared_ptr< GeneArray > geneArray, size_t size)
Construct from a GeneArray and size of the population.