diff --git a/eo/src/eoInit.h b/eo/src/eoInit.h index a73310778..58eef63e0 100644 --- a/eo/src/eoInit.h +++ b/eo/src/eoInit.h @@ -193,17 +193,19 @@ class eoInitPermutation: public eoInit // FIXME inherit from eoInitWithDim virtual void operator()(EOT& chrom) { chrom.resize(chromSize); - for(unsigned idx=0;idx gen(chrom.size()); + std::shuffle(chrom.begin(), chrom.end(), gen); chrom.invalidate(); } private : unsigned chromSize; unsigned startFrom; - UF_random_generator gen; + // UF_random_generator gen; }; /** @example t-eoInitPermutation.cpp */ diff --git a/eo/src/utils/rnd_generators.h b/eo/src/utils/rnd_generators.h index 60ff04b3f..01b682b67 100644 --- a/eo/src/utils/rnd_generators.h +++ b/eo/src/utils/rnd_generators.h @@ -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 random_generator +template +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::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 UF_random_generator +template +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; }; diff --git a/eo/test/t-eoInitPermutation.cpp b/eo/test/t-eoInitPermutation.cpp index 4b64f3576..44af6f6b0 100644 --- a/eo/test/t-eoInitPermutation.cpp +++ b/eo/test/t-eoInitPermutation.cpp @@ -43,7 +43,7 @@ int main() unsigned i; // a chromosome randomizer - eoInitPermutation random(CHROM_SIZE); + eoInitPermutation randomize(CHROM_SIZE); // the population: eoPop 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); diff --git a/eo/test/t-eobin.cpp b/eo/test/t-eobin.cpp index 2a36d0ee1..4d3445faa 100644 --- a/eo/test/t-eobin.cpp +++ b/eo/test/t-eobin.cpp @@ -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 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 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 continuator1(50); diff --git a/mo/src/neighborhood/moRndVectorVNSelection.h b/mo/src/neighborhood/moRndVectorVNSelection.h index 3debb85d4..a460f2d45 100644 --- a/mo/src/neighborhood/moRndVectorVNSelection.h +++ b/mo/src/neighborhood/moRndVectorVNSelection.h @@ -86,6 +86,7 @@ public: for(unsigned int i = 0; i < LSvector.size(); i++) order.push_back(i); + UF_random_generator 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 order; - // random generator - UF_random_generator gen; - }; #endif diff --git a/moeo/src/selection/moeoDetArchiveSelect.h b/moeo/src/selection/moeoDetArchiveSelect.h index 81e09e36c..78b69a06e 100644 --- a/moeo/src/selection/moeoDetArchiveSelect.h +++ b/moeo/src/selection/moeoDetArchiveSelect.h @@ -71,10 +71,10 @@ class moeoDetArchiveSelect : public eoSelect _dest.push_back(archive[i]); } else if (archive_size > max){ - UF_random_generator rndGen; std::vector permutation; for(unsigned int i=0; i < archive_size; i++) permutation.push_back(i); + UF_random_generator rndGen(permutation.size()); random_shuffle(permutation.begin(), permutation.end(), rndGen); for (unsigned int i=0; i rndGen; + UF_random_generator rndGen(res.size()); std::random_shuffle(res.begin(), res.end(), rndGen); res.resize(number); }