GENEIAL  0.2=/
 All Classes Pages
SimpleCouplingOperation.hpp
1 #pragma once
2 
3 #include <geneial/core/operations/coupling/SimpleCouplingOperation.h>
4 
5 geneial_private_namespace(geneial)
6 {
7 geneial_private_namespace(operation)
8 {
9 geneial_private_namespace(coupling)
10 {
11 using ::geneial::population::Population;
12 using ::geneial::operation::crossover::BaseCrossoverOperation;
13 using ::geneial::operation::selection::BaseSelectionOperation;
14 using ::geneial::population::management::BaseManager;
15 
16 geneial_export_namespace
17 {
18 
19 template<typename FITNESS_TYPE>
20 typename BaseCouplingOperation<FITNESS_TYPE>::offspring_result_set SimpleCouplingOperation<FITNESS_TYPE>::doCopulate(
21  const typename BaseSelectionOperation<FITNESS_TYPE>::selection_result_set &mating_pool,
22  const BaseCrossoverOperation<FITNESS_TYPE> &crossoverOperation, BaseManager<FITNESS_TYPE> &manager)
23 {
24 
25  typedef typename BaseCouplingOperation<FITNESS_TYPE>::offspring_result_set offspring_container;
26  typedef typename BaseCrossoverOperation<FITNESS_TYPE>::crossover_result_set children_container;
27  typedef typename BaseSelectionOperation<FITNESS_TYPE>::selection_result_set mating_container;
28  typedef typename BaseChromosome<FITNESS_TYPE>::ptr chrom_ptr;
29 
30  unsigned int offspring_left = this->getSettings().getNumberOfOffspring();
31 
32  offspring_container offspring; //A small container for all the little children :-)
33  offspring.reserve(offspring_left);
34 
35 
36  assert(mating_pool.size() > 1);
37  //iterate over mating candidates and create a pairwise mapping.
53  for (typename mating_container::const_iterator it = mating_pool.begin(); offspring_left > 0;)
54  {
55  //TODO (bewo) REFACTOR: research cyclic iterators to avoid all those ugly case distinctions
56  chrom_ptr parent1(*it);
57  it++;
58  //wrap around if necessary
59  if (it == mating_pool.end())
60  {
61  it = mating_pool.begin();
62  }
63 
64  chrom_ptr parent2(*it);
65  it++;
66  if (it == mating_pool.end())
67  {
68  it = mating_pool.begin();
69  }
70 
71  const children_container children(crossoverOperation.doCrossover(parent1, parent2));
72  offspring_left -= this->copyUnlessMaximumReached(offspring,children,offspring_left);
73 
74  if (offspring_left > 0 && !crossoverOperation.isSymmetric())
75  {
76  const children_container assymetricChildren(crossoverOperation.doCrossover(parent2, parent1));
77  offspring_left -= this->copyUnlessMaximumReached(offspring,assymetricChildren,offspring_left);
78  }
79  }
80 
81  return offspring;
82 }
83 
84 } /* geneial_export_namespace */
85 } /* private namespace coupling */
86 } /* private namespace operation */
87 } /* private namespace geneial */
88