GENEIAL  0.2=/
 All Classes Pages
ContinousMultiValueChromosomeFactory.hpp
1 #pragma once
2 
3 #include <geneial/core/population/builder/ContinousMultiValueChromosomeFactory.h>
4 #include <geneial/utility/Random.h>
5 
6 #include <algorithm>
7 #include <cassert>
8 
9 geneial_private_namespace(geneial)
10 {
11 geneial_private_namespace(population)
12 {
13 geneial_private_namespace(chromosome)
14 {
15 using ::geneial::population::chromosome::ContinousMultiValueBuilderSettings;
16 using ::geneial::population::chromosome::ContinousMultiValueChromosomeFactory;
17 
18 
19 geneial_export_namespace
20 {
21 
22 template<typename VALUE_TYPE, typename FITNESS_TYPE>
23 typename BaseChromosome<FITNESS_TYPE>::ptr ContinousMultiValueChromosomeFactory<VALUE_TYPE, FITNESS_TYPE>::doCreateChromosome(
24  typename BaseChromosomeFactory<FITNESS_TYPE>::PopulateBehavior populateValues)
25 {
26  using namespace geneial::utility;
27 
28  auto new_chromosome = this->allocateNewChromsome();
29 
30  if (populateValues == BaseChromosomeFactory<FITNESS_TYPE>::CREATE_VALUES)
31  {
32  const unsigned int amount = _settings.getNum();
33 
34  unsigned int i = 0;
35 
36  VALUE_TYPE lastVal = 0; //reference to last inserted value
37 
38  while (i<amount)
39  {
40 
41  if (i == 0)
42  {
43  if (_settings.hasStart())
44  {
45  lastVal = _settings.getStartValue();
46  }
47  else
48  {
49  lastVal = Random::generate<VALUE_TYPE>(_settings.getRandomMin(),
50  _settings.getRandomMax());
51  }
52  }
53 
54  const VALUE_TYPE lower_limited = std::max(_settings.getRandomMin(), lastVal - _settings.getEps());
55  const VALUE_TYPE upper_limited = std::min(_settings.getRandomMax(), lastVal + _settings.getEps());
56 
57  const VALUE_TYPE val = Random::generate<VALUE_TYPE>(lower_limited,
58  upper_limited);
59 
60  new_chromosome->getContainer()[i] = val;
61 
62  lastVal = upper_limited;
63  i++;
64  }
65  assert(new_chromosome->getSize() == _settings.getNum());
66 
67  }
68 
69  return std::move(new_chromosome);
70 }
71 
72 } /* geneial_export_namespace */
73 } /* private namespace chromosome */
74 } /* private namespace population */
75 } /* private namespace geneial */
76 
77