2 #include <samchon/library/GAPopulation.hpp> 6 #include <samchon/library/Math.hpp> 65 template <
typename GeneArray,
typename Compare = std::less<GeneArray>,
typename Gene = GeneArray::value_type>
72 std::vector<Gene> candidates;
107 GeneticAlgorithm(
const std::vector<Gene> &candidates,
bool unique,
double mutationRate = 0.015,
size_t tournament = 10)
109 this->candidates = candidates;
141 inline auto evolveGeneArray(std::shared_ptr<GeneArray> geneArray,
size_t population,
size_t generation)
const -> std::shared_ptr<GeneArray>
143 std::shared_ptr<Population> myPopulation(
new Population(geneArray, population));
145 for (
size_t i = 0; i < generation; i++)
148 return myPopulation->fitTest();
157 auto evolvePopulation(std::shared_ptr<Population> population)
const -> std::shared_ptr<Population>
159 size_t size = population->children.size();
160 std::shared_ptr<Population> evolved(
new Population(size));
163 evolved->children[0] = population->fitTest();
165 #pragma omp parallel for 166 for (
int i = 1; i < size; i++)
168 std::shared_ptr<GeneArray> &gene1 =
selection(population);
169 std::shared_ptr<GeneArray> &gene2 =
selection(population);
171 std::shared_ptr<GeneArray> &child =
crossover(gene1, gene2);
174 evolved->children[i] = child;
215 auto selection(std::shared_ptr<Population> population)
const -> std::shared_ptr<GeneArray>
217 size_t size = population->children.size();
218 Population tornament(size);
220 for (
size_t i = 0; i < size; i++)
223 if (randomIndex == size)
226 tornament.
children[i] = population->children[randomIndex];
247 auto crossover(std::shared_ptr<GeneArray> &parent1, std::shared_ptr<GeneArray> &parent2)
const -> std::shared_ptr<GeneArray>
249 std::shared_ptr<GeneArray> geneArray(
new GeneArray(*parent1));
250 size_t size = parent1->size();
254 for (
size_t i = 0; i < size; i++)
256 geneArray->at(i) = parent2->at(i);
260 std::set<GeneArray::value_type> ptrSet;
261 std::set<size_t> indexSet;
267 for (
size_t i = 0; i < size; i++)
268 if (start < i && i < end)
269 ptrSet.insert(parent1->at(i));
274 for (
size_t i = 0; i < size; i++)
276 GeneArray::value_type &ptr = parent2->at(i);
277 if (ptrSet.find(ptr) != ptrSet.end())
280 geneArray->at(*indexSet.begin()) = ptr;
281 indexSet.erase(indexSet.begin());
318 virtual void mutate(std::shared_ptr<GeneArray> geneArray)
const 320 for (
size_t i = 0; i < geneArray->size(); i++)
325 if (candidates.empty() ==
true)
328 size_t j = (size_t)(
Math::random() * geneArray->size());
329 std::swap(geneArray->at(i), geneArray->at(j));
331 else if (unique ==
false)
334 geneArray->at(i) = candidates.at((
size_t)(
Math::random() * candidates.size()));
339 bool duplicated =
true;
342 while (duplicated ==
true)
344 item = candidates.at((
size_t)(
Math::random() * candidates.size()));
348 for (
size_t j = 0; j < geneArray->size(); j++)
349 if (i != j && geneArray->at(i) == geneArray->at(j))
356 geneArray->at(i) = item;
static auto random() -> double
Get a random value.
double mutationRate
Rate of mutate ocurrence.
auto fitTest() const -> std::shared_ptr< GeneArray >
Test fitness of each GeneArray in the population.
GeneticAlgorithm(bool unique, double mutationRate=0.015, size_t tournament=10)
Construct from parameters of Genetic Algorithm.
auto selection(std::shared_ptr< Population > population) const -> std::shared_ptr< GeneArray >
Select the best GeneArray in population from tournament.
A population of a generation in G.A.
A genetic algorithm class.
bool unique
Whether each element (Gene) is unique in their GeneArray.
virtual void mutate(std::shared_ptr< GeneArray > geneArray) const
Cause a mutation on the GeneArray.
std::vector< std::shared_ptr< GeneArray > > children
Genes representing the population.
auto crossover(std::shared_ptr< GeneArray > &parent1, std::shared_ptr< GeneArray > &parent2) const -> std::shared_ptr< GeneArray >
Create a new GeneArray by crossing over two GeneArray(s)
auto evolvePopulation(std::shared_ptr< Population > population) const -> std::shared_ptr< Population >
Evolve population, a mass of GeneArray(es)
GeneticAlgorithm(const std::vector< Gene > &candidates, bool unique, double mutationRate=0.015, size_t tournament=10)
Construct from parameters of Genetic Algorithm.
size_t tournament
Size of tournament in selection.
Top level namespace of products built from samchon.
auto evolveGeneArray(std::shared_ptr< GeneArray > geneArray, size_t population, size_t generation) const -> std::shared_ptr< GeneArray >
Evolve a GeneArray.