3 #include <geneial/utility/Smoothing.h>
5 geneial_private_namespace(geneial)
7 geneial_private_namespace(utility)
9 using ::geneial::population::chromosome::MultiValueChromosome;
10 geneial_export_namespace
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)
22 lastVal = *(chromosome->getContainer().begin());
27 for (
auto& it: chromosome->getContainer())
31 const VALUE_TYPE currentValue = std::max(min,std::min(max,it));
34 if (std::abs(currentValue-lastVal) < maxAbsElevation)
37 lastVal = currentValue;
42 if (currentValue < lastVal)
45 it = std::max(lastVal - maxAbsElevation, min);
50 it = std::min(lastVal + maxAbsElevation, max);
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)
64 const unsigned int chromSize = chromosome->getSize();
66 assert(pos < chromSize);
71 unsigned int leftEpsPos = pos - epsLeft;
76 assert(leftEpsPos >= 0);
77 assert(leftEpsPos <= chromSize - 1);
79 for (
int i = pos; i >=
static_cast<int>(leftEpsPos); i--)
82 assert(i <= static_cast<signed>(chromSize - 1));
85 if (leftEpsPos != 0 && epsLeft != 0.0)
88 pctElevated = 1.0 - (
static_cast<double>(pos -i)) /
static_cast<double>(epsLeft);
94 const VALUE_TYPE toModify =
static_cast<VALUE_TYPE
>(
static_cast<double>(pctElevated) * elevation);
95 chromosome->getContainer()[i] += toModify;
100 unsigned int rightEpsPos = std::min(pos + epsRight, chromSize - 1);
101 assert(rightEpsPos >= 0);
102 assert(rightEpsPos <= chromSize - 1);
104 for (
unsigned int i = pos + 1; i < rightEpsPos; i++)
107 assert(i <= chromSize - 1);
110 if (rightEpsPos != 0 && epsRight != 0.0)
113 pctElevated = 1.0 - (
static_cast<double>(i -pos)) /
static_cast<double>(epsRight);
120 const VALUE_TYPE toModify =
static_cast<VALUE_TYPE
>((
static_cast<double>(pctElevated)) * elevation);
121 chromosome->getContainer()[i] += toModify;