GENEIAL  0.2=/
 All Classes Pages
Smoothing.hpp
1 #pragma once
2 
3 #include <geneial/utility/Smoothing.h>
4 
5 geneial_private_namespace(geneial)
6 {
7 geneial_private_namespace(utility)
8 {
9 using ::geneial::population::chromosome::MultiValueChromosome;
10 geneial_export_namespace
11 {
12 
13 //TODO
14 template<typename VALUE_TYPE, typename FITNESS_TYPE>
15 void Smoothing::restoreSmoothness(typename MultiValueChromosome<VALUE_TYPE, FITNESS_TYPE>::ptr chromosome,
16  VALUE_TYPE maxAbsElevation, VALUE_TYPE min, VALUE_TYPE max, bool hasStart, VALUE_TYPE start)
17 {
18  VALUE_TYPE lastVal;
19 
20  if(hasStart)
21  {
22  lastVal = *(chromosome->getContainer().begin());
23  }else{
24  lastVal = start;
25  }
26 
27  for (auto& it: chromosome->getContainer())
28  {
29 
30  //Restore Limits if needed
31  const VALUE_TYPE currentValue = std::max(min,std::min(max,it));
32  it = currentValue;
33 
34  if (std::abs(currentValue-lastVal) < maxAbsElevation)
35  {
36  //We are within the boundaries of the allowed elevation..
37  lastVal = currentValue;
38  }
39  else
40  {
41  //We have exceeded the boundaries of the allowed elevation
42  if (currentValue < lastVal)
43  {
44  //Case descending
45  it = std::max(lastVal - maxAbsElevation, min);
46  }
47  else
48  {
49  //Case ascending
50  it = std::min(lastVal + maxAbsElevation, max);
51  }
52 
53  }
54  lastVal = it;
55  }
56 
57 }
58 
59 template<typename VALUE_TYPE, typename FITNESS_TYPE>
60 void Smoothing::peakAt(unsigned int pos, unsigned int epsLeft, unsigned int epsRight, VALUE_TYPE elevation,
61  typename MultiValueChromosome<VALUE_TYPE, FITNESS_TYPE>::ptr chromosome)
62 {
63 
64  const unsigned int chromSize = chromosome->getSize();
65 
66  assert(pos < chromSize);
67  assert(0 <= pos);
68 
69  //Look at all the value to the LEFT of pos
70  //Determine target left position (avoid underflow)
71  unsigned int leftEpsPos = pos - epsLeft;
72  if (pos < epsLeft)
73  {
74  leftEpsPos = 0;
75  }
76  assert(leftEpsPos >= 0);
77  assert(leftEpsPos <= chromSize - 1);
78 
79  for (int i = pos; i >= static_cast<int>(leftEpsPos); i--)
80  {
81  assert(i >= 0);
82  assert(i <= static_cast<signed>(chromSize - 1));
83  //How many pct have we advanced to the left?
84  double pctElevated;
85  if (leftEpsPos != 0 && epsLeft != 0.0)
86  {
87  //pctElevated = 1.0 - (static_cast<double>(i - leftEpsPos)) / static_cast<double>(leftEpsPos);
88  pctElevated = 1.0 - (static_cast<double>(pos -i)) / static_cast<double>(epsLeft);
89  }
90  else
91  {
92  pctElevated = 1.0;
93  }
94  const VALUE_TYPE toModify = static_cast<VALUE_TYPE>(static_cast<double>(pctElevated) * elevation);
95  chromosome->getContainer()[i] += toModify;
96  }
97 
98  //Look at all the values to the RIGHT of pos
99  //avoid overflow.
100  unsigned int rightEpsPos = std::min(pos + epsRight, chromSize - 1);
101  assert(rightEpsPos >= 0);
102  assert(rightEpsPos <= chromSize - 1);
103 
104  for (unsigned int i = pos + 1; i < rightEpsPos; i++)
105  {
106  assert(i >= 0);
107  assert(i <= chromSize - 1);
108  //How many pct have we advanced to the right?
109  double pctElevated;
110  if (rightEpsPos != 0 && epsRight != 0.0)
111  {
112  //pctElevated = (static_cast<double>(i)) / static_cast<double>(rightEpsPos);
113  pctElevated = 1.0 - (static_cast<double>(i -pos)) / static_cast<double>(epsRight);
114  }
115  else
116  {
117  pctElevated = 0;
118  }
119  //const VALUE_TYPE toModify = static_cast<VALUE_TYPE>((1.0 - static_cast<double>(pctElevated)) * elevation);
120  const VALUE_TYPE toModify = static_cast<VALUE_TYPE>((static_cast<double>(pctElevated)) * elevation);
121  chromosome->getContainer()[i] += toModify;
122  }
123 
124 }
125 
126 } /* geneial_export_namespace */
127 } /* private namespace utility */
128 } /* private namespace geneial */
129