3 #include <samchon/library/GAPopulation.hpp> 7 #include <samchon/library/Math.hpp> 59 template <
typename GeneArray,
typename Compare = std::less<GeneArray>>
114 inline auto evolveGeneArray(std::shared_ptr<GeneArray> individual,
size_t population,
size_t generation)
const -> std::shared_ptr<GeneArray>
116 std::shared_ptr<Population> myPopulation(
new Population(individual, population));
118 for (
size_t i = 0; i < generation; i++)
121 return myPopulation->fitTest();
129 auto evolvePopulation(std::shared_ptr<Population> population)
const -> std::shared_ptr<Population>
131 size_t size = population->children.size();
132 std::shared_ptr<Population> evolved(
new Population(size));
135 evolved->children[0] = population->fitTest();
137 #pragma omp parallel for 138 for (
int i = 1; i < size; i++)
140 std::shared_ptr<GeneArray> &gene1 =
selection(population);
141 std::shared_ptr<GeneArray> &gene2 =
selection(population);
143 std::shared_ptr<GeneArray> &child =
crossover(gene1, gene2);
146 evolved->children[i] = child;
187 auto selection(std::shared_ptr<Population> population)
const -> std::shared_ptr<GeneArray>
189 size_t size = population->children.size();
190 Population tornament(size);
192 for (
size_t i = 0; i < size; i++)
195 if (randomIndex == size)
198 tornament.
children[i] = population->children[randomIndex];
219 auto crossover(std::shared_ptr<GeneArray> &parent1, std::shared_ptr<GeneArray> &parent2)
const -> std::shared_ptr<GeneArray>
221 std::shared_ptr<GeneArray> individual(
new GeneArray(*parent1));
222 size_t size = parent1->size();
226 for (
size_t i = 0; i < size; i++)
228 individual->at(i) = parent2->at(i);
232 std::set<GeneArray::value_type> ptrSet;
233 std::set<size_t> indexSet;
240 std::swap(start, end);
243 for (
size_t i = 0; i < size; i++)
244 if (start <= i && i < end)
245 ptrSet.insert(parent1->at(i));
250 for (
size_t i = 0; i < size; i++)
252 GeneArray::value_type &ptr = parent2->at(i);
253 if (ptrSet.find(ptr) != ptrSet.end())
256 individual->at(*indexSet.begin()) = ptr;
257 indexSet.erase(indexSet.begin());
294 virtual void mutate(std::shared_ptr<GeneArray> individual)
const 296 for (
size_t i = 0; i < individual->size(); i++)
302 size_t j = (size_t)(
Math::random() * individual->size());
303 std::swap(individual->at(i), individual->at(j));
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.
auto fitTest() const -> std::shared_ptr< GeneArray >
Test fitness of each GeneArray in the population.
size_t tournament
Size of tournament in selection.
A population in a generation in G.A.
auto evolvePopulation(std::shared_ptr< Population > population) const -> std::shared_ptr< Population >
Evolve population, a mass of GeneArray(es)
A genetic algorithm class.
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 evolveGeneArray(std::shared_ptr< GeneArray > individual, size_t population, size_t generation) const -> std::shared_ptr< GeneArray >
Evolve a GeneArray.
double mutationRate
Rate of mutate ocurrence.
bool unique
Whether each element (Gene) is unique in their GeneArray.
virtual void mutate(std::shared_ptr< GeneArray > individual) const
Cause a mutation on the GeneArray.
std::vector< std::shared_ptr< GeneArray > > children
Genes representing the population.
static auto random() -> double
Get a random value.