refactor UF_random_generator to fit the new std::shuffle
This commit is contained in:
parent
009ef5e1d8
commit
4ee48e760b
7 changed files with 39 additions and 29 deletions
|
|
@ -193,17 +193,19 @@ class eoInitPermutation: public eoInit<EOT> // FIXME inherit from eoInitWithDim
|
|||
virtual void operator()(EOT& chrom)
|
||||
{
|
||||
chrom.resize(chromSize);
|
||||
for(unsigned idx=0;idx <chrom.size();idx++)
|
||||
chrom[idx]=idx+startFrom;
|
||||
for(unsigned idx=0; idx < chrom.size(); idx++) {
|
||||
chrom[idx] = idx+startFrom;
|
||||
}
|
||||
|
||||
std::shuffle(chrom.begin(), chrom.end(),gen);
|
||||
UF_random_generator<unsigned int> gen(chrom.size());
|
||||
std::shuffle(chrom.begin(), chrom.end(), gen);
|
||||
chrom.invalidate();
|
||||
}
|
||||
|
||||
private :
|
||||
unsigned chromSize;
|
||||
unsigned startFrom;
|
||||
UF_random_generator<unsigned int> gen;
|
||||
// UF_random_generator<unsigned int> gen;
|
||||
};
|
||||
/** @example t-eoInitPermutation.cpp
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -89,7 +89,8 @@ class boolean_generator
|
|||
either between [0, _max) if only one value (_max) is given to the ctor
|
||||
or in [_min,_max) if 2 values are given (_min, _max)
|
||||
*/
|
||||
template <class T = uint32_t> class random_generator
|
||||
template <class T = uint32_t>
|
||||
class random_generator
|
||||
{
|
||||
public :
|
||||
// added new ctor with 2 params, and modified the data to minim and range
|
||||
|
|
@ -123,16 +124,25 @@ 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}
|
||||
*/
|
||||
template <class T = uint32_t> class UF_random_generator
|
||||
template <class T = uint32_t>
|
||||
class UF_random_generator
|
||||
{
|
||||
public :
|
||||
UF_random_generator(eoRng& _rng = rng) :
|
||||
random(_rng) {}
|
||||
public :
|
||||
using result_type = T;
|
||||
|
||||
T operator()(T _t) { return (T) (random.random(_t)); }
|
||||
UF_random_generator(T max, eoRng& _rng = rng)
|
||||
: _max(max), _random(_rng)
|
||||
{}
|
||||
|
||||
private :
|
||||
eoRng& random;
|
||||
T operator()() { return _random.random(_max); }
|
||||
T operator()(T m) { return _random.random(m); }
|
||||
|
||||
T min() { return 0; }
|
||||
T max() { return _max; }
|
||||
|
||||
private :
|
||||
T _max;
|
||||
eoRng& _random;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ int main()
|
|||
unsigned i;
|
||||
|
||||
// a chromosome randomizer
|
||||
eoInitPermutation <Chrom> random(CHROM_SIZE);
|
||||
eoInitPermutation <Chrom> randomize(CHROM_SIZE);
|
||||
|
||||
// the population:
|
||||
eoPop<Chrom> pop;
|
||||
|
|
@ -55,7 +55,7 @@ int main()
|
|||
{
|
||||
Chrom chrom(CHROM_SIZE);
|
||||
std::cout << " Initial chromosome n°" << i << " : " << chrom << "..." << std::endl;
|
||||
random(chrom);
|
||||
randomize(chrom);
|
||||
eval(chrom);
|
||||
std::cout << " ... becomes : " << chrom << " after initialization" << std::endl;
|
||||
check_permutation(chrom);
|
||||
|
|
|
|||
|
|
@ -123,17 +123,17 @@ void main_function()
|
|||
<< chrom << " " << chrom2 << std::endl;
|
||||
}
|
||||
|
||||
for (i = 1; i < SIZE / 2; i++)
|
||||
for (j = 1; j < SIZE / 2; j++)
|
||||
{
|
||||
eoBitGxOver<Chrom> gxover(i, j);
|
||||
std::fill(chrom.begin(), chrom.end(), false);
|
||||
std::fill(chrom2.begin(), chrom2.end(), true);
|
||||
gxover(chrom, chrom2);
|
||||
chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
|
||||
std::cout << "eoBinGxOver(" << i << ", " << j << ") ..... "
|
||||
<< chrom << " " << chrom2 << std::endl;
|
||||
for (i = 1; i < SIZE / 2; i++) {
|
||||
for (j = 1; j < SIZE / 2; j++) {
|
||||
eoBitGxOver<Chrom> gxover(i, j);
|
||||
std::fill(chrom.begin(), chrom.end(), false);
|
||||
std::fill(chrom2.begin(), chrom2.end(), true);
|
||||
gxover(chrom, chrom2);
|
||||
chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
|
||||
std::cout << "eoBinGxOver(" << i << ", " << j << ") ..... "
|
||||
<< chrom << " " << chrom2 << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// test SGA algorithm
|
||||
eoGenContinue<Chrom> continuator1(50);
|
||||
|
|
|
|||
|
|
@ -86,6 +86,7 @@ public:
|
|||
for(unsigned int i = 0; i < LSvector.size(); i++)
|
||||
order.push_back(i);
|
||||
|
||||
UF_random_generator<unsigned int> gen(order.size());
|
||||
std::random_shuffle(order.begin(), order.end(), gen);
|
||||
|
||||
currentOrder = 0;
|
||||
|
|
@ -110,9 +111,6 @@ private:
|
|||
unsigned int currentOrder;
|
||||
// the index of heuristics in random order
|
||||
std::vector<unsigned int> order;
|
||||
// random generator
|
||||
UF_random_generator<unsigned int> gen;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -71,10 +71,10 @@ class moeoDetArchiveSelect : public eoSelect<MOEOT>
|
|||
_dest.push_back(archive[i]);
|
||||
}
|
||||
else if (archive_size > max){
|
||||
UF_random_generator<unsigned int> rndGen;
|
||||
std::vector <int> permutation;
|
||||
for(unsigned int i=0; i < archive_size; i++)
|
||||
permutation.push_back(i);
|
||||
UF_random_generator<unsigned int> rndGen(permutation.size());
|
||||
random_shuffle(permutation.begin(), permutation.end(), rndGen);
|
||||
for (unsigned int i=0; i<max; i++)
|
||||
_dest.push_back(archive[permutation[i]]);
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ public:
|
|||
res.push_back(i);
|
||||
}
|
||||
if(number < res.size()){
|
||||
UF_random_generator<unsigned int> rndGen;
|
||||
UF_random_generator<unsigned int> rndGen(res.size());
|
||||
std::random_shuffle(res.begin(), res.end(), rndGen);
|
||||
res.resize(number);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue