fix: used an unordered_map instead of vactor to save indexes

This commit is contained in:
Alessandro Sidero 2025-04-15 00:23:31 +02:00
commit df1ebb94dd

View file

@ -148,7 +148,6 @@ public:
*/ */
virtual void operator()(const eoPop<EOT> &_pop) virtual void operator()(const eoPop<EOT> &_pop)
{ {
unsigned pSize = _pop.size(); unsigned pSize = _pop.size();
if (pSize <= 1) if (pSize <= 1)
@ -170,16 +169,19 @@ public:
std::vector<const EOT *> rank; std::vector<const EOT *> rank;
_pop.sort(rank); _pop.sort(rank);
// vector of indices sorted by fitness // map of indices for the population
std::vector<size_t> indices(pSize); std::unordered_map<const EOT *, unsigned> indexMap;
for (size_t i = 0; i < pSize; ++i) for (unsigned i = 0; i < pSize; ++i)
indices[i] = i; {
indexMap[&_pop[i]] = i;
}
if (exponent == 1.0) // no need for exponetial then (linear case) if (exponent == 1.0) // no need for exponetial then (linear case)
{ {
for (unsigned i = 0; i < pSize; i++) for (unsigned i = 0; i < pSize; i++)
{ {
int which = indices[i]; const EOT *indiv = rank[i];
int which = indexMap[indiv];
value()[which] = cached_alpha * (pSize - i) + cached_beta; value()[which] = cached_alpha * (pSize - i) + cached_beta;
} }
} }
@ -187,7 +189,8 @@ public:
{ {
for (unsigned i = 0; i < pSize; i++) for (unsigned i = 0; i < pSize; i++)
{ {
int which = indices[i]; const EOT *indiv = rank[i];
int which = indexMap[indiv];
// value in in [0,1] // value in in [0,1]
double tmp = ((double)(pSize - i)) / pSize; double tmp = ((double)(pSize - i)) / pSize;
// to the exponent, and back to [m,M] // to the exponent, and back to [m,M]