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:
parent
55b2f57d19
commit
6f7d505a2a
4 changed files with 25 additions and 19 deletions
|
|
@ -29,11 +29,12 @@
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
#include "eoOp.h"
|
#include "eoOp.h"
|
||||||
#include "eoSTLFunctor.h"
|
#include "eoSTLFunctor.h"
|
||||||
#include "utils/eoRndGenerators.h"
|
#include "utils/eoRndGenerators.h"
|
||||||
#include "utils/rnd_generators.h" // for shuffle method
|
// #include "utils/rnd_generators.h" // for shuffle method
|
||||||
#include "eoExceptions.h"
|
#include "eoExceptions.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -197,7 +198,8 @@ class eoInitPermutation: public eoInit<EOT> // FIXME inherit from eoInitWithDim
|
||||||
chrom[idx] = idx+startFrom;
|
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);
|
std::shuffle(chrom.begin(), chrom.end(), gen);
|
||||||
chrom.invalidate();
|
chrom.invalidate();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ Authors:
|
||||||
#include "eoOp.h" // for eoInit
|
#include "eoOp.h" // for eoInit
|
||||||
#include "eoPersistent.h"
|
#include "eoPersistent.h"
|
||||||
#include "eoInit.h"
|
#include "eoInit.h"
|
||||||
#include "utils/rnd_generators.h" // for shuffle method
|
// #include "utils/rnd_generators.h" // for shuffle method
|
||||||
#include "eoExceptions.h"
|
#include "eoExceptions.h"
|
||||||
|
|
||||||
/** A std::vector of EO object, to be used in all algorithms
|
/** A std::vector of EO object, to be used in all algorithms
|
||||||
|
|
|
||||||
|
|
@ -124,6 +124,7 @@ inline bool random_generator<bool>::operator()(void)
|
||||||
function (see eoPop::shuffle): its operator() takes an unsigned argument m
|
function (see eoPop::shuffle): its operator() takes an unsigned argument m
|
||||||
and must return an unsigned uniformly distributed in [0,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>
|
template <class T = uint32_t>
|
||||||
class UF_random_generator
|
class UF_random_generator
|
||||||
{
|
{
|
||||||
|
|
@ -134,11 +135,11 @@ class UF_random_generator
|
||||||
: _max(max), _random(_rng)
|
: _max(max), _random(_rng)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
T operator()() { return _random.random(_max); }
|
T operator()() const { return _random.random(_max); }
|
||||||
T operator()(T m) { return _random.random(m); }
|
T operator()(T m) const { return _random.random(m); }
|
||||||
|
|
||||||
T min() { return 0; }
|
T min() const { return 0; }
|
||||||
T max() { return _max; }
|
T max() const { return _max; }
|
||||||
|
|
||||||
private :
|
private :
|
||||||
T _max;
|
T _max;
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,8 @@
|
||||||
#ifndef _MOEONUMBERUNVISITEDSELECT_H
|
#ifndef _MOEONUMBERUNVISITEDSELECT_H
|
||||||
#define _MOEONUMBERUNVISITEDSELECT_H
|
#define _MOEONUMBERUNVISITEDSELECT_H
|
||||||
|
|
||||||
|
#include <random>
|
||||||
|
|
||||||
#include <eoPop.h>
|
#include <eoPop.h>
|
||||||
#include <selection/moeoUnvisitedSelect.h>
|
#include <selection/moeoUnvisitedSelect.h>
|
||||||
|
|
||||||
|
|
@ -51,10 +53,10 @@ class moeoNumberUnvisitedSelect : public moeoUnvisitedSelect < MOEOT >
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
* @param _number the number of individuals to select
|
* @param _number the number of individuals to select
|
||||||
*/
|
*/
|
||||||
moeoNumberUnvisitedSelect(unsigned int _number): number(_number){}
|
moeoNumberUnvisitedSelect(unsigned int _number): number(_number){}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -64,24 +66,25 @@ public:
|
||||||
*/
|
*/
|
||||||
std::vector <unsigned int> operator()(eoPop < MOEOT > & _src)
|
std::vector <unsigned int> operator()(eoPop < MOEOT > & _src)
|
||||||
{
|
{
|
||||||
std::vector <unsigned int> res;
|
std::vector <unsigned int> res;
|
||||||
res.resize(0);
|
res.resize(0);
|
||||||
for (unsigned int i=0; i<_src.size(); i++)
|
for (unsigned int i=0; i<_src.size(); i++)
|
||||||
{
|
{
|
||||||
if (_src[i].flag() == 0)
|
if (_src[i].flag() == 0)
|
||||||
res.push_back(i);
|
res.push_back(i);
|
||||||
}
|
}
|
||||||
if(number < res.size()){
|
if(number < res.size()){
|
||||||
UF_random_generator<unsigned int> rndGen(res.size());
|
std::random_device rd;
|
||||||
std::random_shuffle(res.begin(), res.end(), rndGen);
|
std::mt19937 gen(rd());
|
||||||
res.resize(number);
|
std::shuffle(res.begin(), res.end(), gen);
|
||||||
|
res.resize(number);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/** number of individuals to select */
|
/** number of individuals to select */
|
||||||
unsigned int number;
|
unsigned int number;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue