GENEIAL  0.2=/
 All Classes Pages
UniformRandomSelection.hpp
1 #pragma once
2 
3 #include <geneial/core/operations/selection/UniformRandomSelection.h>
4 #include <geneial/core/population/chromosome/BaseChromosome.h>
5 #include <geneial/utility/Random.h>
6 
7 #include <map>
8 #include <cassert>
9 
10 geneial_private_namespace(geneial)
11 {
12 geneial_private_namespace(operation)
13 {
14 geneial_private_namespace(selection)
15 {
16 using ::geneial::utility::Random;
17 
18 geneial_export_namespace
19 {
20 
21 //TODO (bewo) check whether all this will work with negative fitness values
22 
23 template<typename FITNESS_TYPE>
24 typename BaseSelectionOperation<FITNESS_TYPE>::selection_result_set UniformRandomSelection<FITNESS_TYPE>::doSelect(
25  const Population<FITNESS_TYPE> &population, BaseManager<FITNESS_TYPE> &manager) const
26 {
27 
28  //shorthands for type mess
29  typedef typename BaseSelectionOperation<FITNESS_TYPE>::selection_result_set result_set;
30  typedef typename Population<FITNESS_TYPE>::fitnessmap_const_it const_pop_itr;
31 
32  result_set result;
33 
34  unsigned int left_select = this->getSettings().getNumberOfParents();
35 
36  //TODO (bewo) allow parameter for the best chromosomes to be selected (and skipped here)
37  assert(population.getSize() >= left_select);
38 
39  while (left_select > 0)
40  {
41  //TODO (bewo) make this a setting:
42  const bool allowDuplicates = false;
43  const_pop_itr rnditer;
44  do
45  {
46  rnditer = population.getFitnessMap().begin();
47 
48  std::advance(rnditer, Random::generate<int>(0, population.getFitnessMap().size() - 1));
49 
50  } while (allowDuplicates || std::find(result.begin(), result.end(), rnditer->second) != result.end());
51 
52  left_select--;
53 
54  result.emplace_back(rnditer->second);
55 
56  }
57  return result;
58 }
59 
60 } /* geneial_export_namespace */
61 } /* private namespace selection */
62 } /* private namespace operation */
63 } /* private namespace geneial */
64