Samchon Framework for CPP  1.0.0
GAPopulation.hpp
1 #pragma once
2 #include <algorithm>
3 #include <functional>
4 #include <memory>
5 #include <vector>
6 
7 namespace samchon
8 {
9 namespace library
10 {
62  template <typename GeneArray, typename Compare = std::less<GeneArray>>
64  {
65  template <typename GeneArray, typename Compare, typename Gene>
66  friend class GeneticAlgorithm;
67 
68  private:
72  std::vector<std::shared_ptr<GeneArray>> children;
73 
84  GAPopulation(size_t size)
85  {
86  children.assign(size, nullptr);
87  };
88 
89  public:
103  GAPopulation(std::shared_ptr<GeneArray> geneArray, size_t size)
104  {
105  children.reserve(size);
106 
107  for (size_t i = 0; i < size; i++)
108  {
109  GeneArray *ptr = new GeneArray(*geneArray);
110 
111  if (i > 0) //FOR ELITICISM
112  std::random_shuffle(ptr->begin(), ptr->end());
113 
114  children.emplace_back(ptr);
115  }
116  };
117 
123  auto fitTest() const -> std::shared_ptr<GeneArray>
124  {
125  std::shared_ptr<GeneArray> best = children[0];
126 
127  for (size_t i = 1; i < children.size(); i++)
128  if (Compare()(*children[i], *best) == true)
129  best = children[i];
130 
131  return best;
132  };
133  };
134 };
135 };
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 of a generation in G.A.
Definition: RWMutex.hpp:4
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.
Top level namespace of products built from samchon.
Definition: ByteArray.hpp:7