fix(rnd): use STL's rand gen for shuffles

Previous implementation used Paradiseo's own random generator system,
now superseeded by the STL's one.
This commit is contained in:
Johann Dreo 2024-08-19 11:02:15 +02:00
commit 6f7d505a2a
4 changed files with 25 additions and 19 deletions

View file

@ -29,11 +29,12 @@
#include <algorithm>
#include <cassert>
#include <random>
#include "eoOp.h"
#include "eoSTLFunctor.h"
#include "utils/eoRndGenerators.h"
#include "utils/rnd_generators.h" // for shuffle method
// #include "utils/rnd_generators.h" // for shuffle method
#include "eoExceptions.h"
@ -197,7 +198,8 @@ class eoInitPermutation: public eoInit<EOT> // FIXME inherit from eoInitWithDim
chrom[idx] = idx+startFrom;
}
UF_random_generator<unsigned int> gen(chrom.size());
std::random_device rd;
std::mt19937 gen(rd());
std::shuffle(chrom.begin(), chrom.end(), gen);
chrom.invalidate();
}

View file

@ -45,7 +45,7 @@ Authors:
#include "eoOp.h" // for eoInit
#include "eoPersistent.h"
#include "eoInit.h"
#include "utils/rnd_generators.h" // for shuffle method
// #include "utils/rnd_generators.h" // for shuffle method
#include "eoExceptions.h"
/** A std::vector of EO object, to be used in all algorithms

View file

@ -124,6 +124,7 @@ inline bool random_generator<bool>::operator()(void)
function (see eoPop::shuffle): its operator() takes an unsigned argument m
and must return an unsigned uniformly distributed in [0,m}
*/
// FIXME this is probably deprecated by the new STL way of managing random generators.
template <class T = uint32_t>
class UF_random_generator
{
@ -134,11 +135,11 @@ class UF_random_generator
: _max(max), _random(_rng)
{}
T operator()() { return _random.random(_max); }
T operator()(T m) { return _random.random(m); }
T operator()() const { return _random.random(_max); }
T operator()(T m) const { return _random.random(m); }
T min() { return 0; }
T max() { return _max; }
T min() const { return 0; }
T max() const { return _max; }
private :
T _max;

View file

@ -39,6 +39,8 @@
#ifndef _MOEONUMBERUNVISITEDSELECT_H
#define _MOEONUMBERUNVISITEDSELECT_H
#include <random>
#include <eoPop.h>
#include <selection/moeoUnvisitedSelect.h>
@ -51,10 +53,10 @@ class moeoNumberUnvisitedSelect : public moeoUnvisitedSelect < MOEOT >
public:
/**
* Constructor
* @param _number the number of individuals to select
*/
/**
* Constructor
* @param _number the number of individuals to select
*/
moeoNumberUnvisitedSelect(unsigned int _number): number(_number){}
/**
@ -64,24 +66,25 @@ public:
*/
std::vector <unsigned int> operator()(eoPop < MOEOT > & _src)
{
std::vector <unsigned int> res;
res.resize(0);
std::vector <unsigned int> res;
res.resize(0);
for (unsigned int i=0; i<_src.size(); i++)
{
if (_src[i].flag() == 0)
res.push_back(i);
res.push_back(i);
}
if(number < res.size()){
UF_random_generator<unsigned int> rndGen(res.size());
std::random_shuffle(res.begin(), res.end(), rndGen);
res.resize(number);
std::random_device rd;
std::mt19937 gen(rd());
std::shuffle(res.begin(), res.end(), gen);
res.resize(number);
}
return res;
}
private:
/** number of individuals to select */
unsigned int number;
/** number of individuals to select */
unsigned int number;
};