EvolvingObjects
|
Random Number Generator. More...
#include <utils/eoRNG.h>
Public Member Functions | |
eoRng (uint32_t s) | |
Constructor. | |
~eoRng () | |
Destructor. | |
void | reseed (uint32_t s) |
Re-initializes the Random Number Generator. | |
double | uniform (double m=1.0) |
Random number from unifom distribution. | |
double | uniform (double min, double max) |
Random number from unifom distribution. | |
uint32_t | random (uint32_t m) |
Random integer number from unifom distribution. | |
bool | flip (double bias=0.5) |
Biased coin toss. | |
double | normal () |
Gaussian deviate. | |
double | normal (double stdev) |
Gaussian deviate. | |
double | normal (double mean, double stdev) |
Gaussian deviate. | |
double | negexp (double mean) |
Random numbers using a negative exponential distribution. | |
uint32_t | rand () |
rand() returns a random number in the range [0, rand_max) | |
uint32_t | rand_max () const |
rand_max() the maximum returned by rand() | |
template<typename TYPE > | |
int | roulette_wheel (const std::vector< TYPE > &vec, TYPE total=0) |
Roulette wheel selection. | |
template<typename TYPE > | |
const TYPE & | choice (const std::vector< TYPE > &vec) |
Randomly select element from vector. | |
template<typename TYPE > | |
TYPE & | choice (std::vector< TYPE > &vec) |
Randomly select element from vector. | |
void | printOn (std::ostream &_os) const |
Print RNG. | |
void | readFrom (std::istream &_is) |
Read RNG. | |
std::string | className () const |
Return the class id. | |
Private Member Functions | |
uint32_t | restart () |
void | initialize (uint32_t seed) |
eoRng (const eoRng &) | |
Copy constructor. | |
eoRng & | operator= (const eoRng &) |
Assignment operator. | |
Private Attributes | |
uint32_t * | state |
Array for the state. | |
uint32_t * | next |
Pointer to next available random number. | |
int | left |
Number of random numbers currently left. | |
bool | cached |
Is there a valid cached value for the normal distribution? | |
double | cacheValue |
Cached value for normal distribution? | |
Static Private Attributes | |
static const int | N |
Size of the state-vector. | |
static const int | M |
Internal constant. | |
static const uint32_t | K |
Magic constant. |
Random Number Generator.
eoRng is a persistent class that uses the ``Mersenne Twister'' random number generator MT19937 for generating random numbers. The various member functions implement useful functions for evolutionary algorithms. Included are: rand(), random(), flip() and normal().
EO provides a global random number generator rng
that is seeded by the current UNIX time at program start. Moreover some global convenience functions are provided that use the global random number generator: random
, normal
.
This is the ``Mersenne Twister'' random number generator MT19937, which generates pseudorandom integers uniformly distributed in 0..(2^32 - 1) starting from any odd seed in 0..(2^32 - 1). This version is a recode by Shawn Cokus (Cokus@math.washington.edu) on March 8, 1998 of a version by Takuji Nishimura (who had suggestions from Topher Cooper and Marc Rieffel in July-August 1997).
Effectiveness of the recoding (on Goedel2.math.washington.edu, a DEC Alpha running OSF/1) using GCC -O3 as a compiler: before recoding: 51.6 sec. to generate 300 million random numbers; after recoding: 24.0 sec. for the same (i.e., 46.5% of original time), so speed is now about 12.5 million random number generations per second on this machine.
According to the URL <http://www.math.keio.ac.jp/~matumoto/emt.html> (and paraphrasing a bit in places), the Mersenne Twister is ``designed with consideration of the flaws of various existing generators,'' has a period of 2^19937 - 1, gives a sequence that is 623-dimensionally equidistributed, and ``has passed many std::stringent tests, including the die-hard test of G. Marsaglia and the load test of P. Hellekalek and S. Wegenkittl.'' It is efficient in memory usage (typically using 2506 to 5012 bytes of static data, depending on data type sizes, and the code is quite short as well). It generates random numbers in batches of 624 at a time, so the caching and pipelining of modern systems is exploited. It is also divide- and mod-free.
The code as Shawn received it included the following notice: Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. When you use this, send an e-mail to <matumoto@math.keio.ac.jp> with an appropriate reference to your work.
It would be nice to Cc: <Cokus@math.washington.edu> and <eodev-main@lists.sourceforge.net> when you write.
Note for people porting EO to other platforms: please make sure that the type uint32_t in the file eoRNG.h is exactly 32 bits long. It may in principle be longer, but not shorter. If it is longer, file compatibility between EO on different platforms may be broken.
eoRng::eoRng | ( | uint32_t | s | ) | [inline] |
eoRng::eoRng | ( | const eoRng & | ) | [private] |
Copy constructor.
Private copy ctor and assignment operator to make sure that nobody accidentally copies the random number generator. If you want similar RNG's, make two RNG's and initialize them with the same seed.
As it cannot be called, we do not provide an implementation.
void eoRng::reseed | ( | uint32_t | s | ) | [inline] |
Re-initializes the Random Number Generator.
WARNING: Jeroen Eggermont <jeggermo@liacs.nl> noticed that initialize does not differentiate between odd and even numbers, therefore the argument to reseed is now doubled before being passed on.
Manually divide the seed by 2 if you want to re-run old runs
Definition at line 148 of file eoRNG.h.
Referenced by do_make_pop().
double eoRng::uniform | ( | double | m = 1.0 | ) | [inline] |
Random number from unifom distribution.
m | Define interval for random number to [0, m) |
Definition at line 175 of file eoRNG.h.
References rand(), and rand_max().
Referenced by eoEsChromInit< EOT >::create_self_adapt(), flip(), normal(), eoRandomRealWeightUp::operator()(), eoProportionalSelect< EOT >::operator()(), eoSigBinaryFlight< POT >::operator()(), eoOneToOneBreeder< EOT >::operator()(), eoUniformGenerator< T >::operator()(), eoDoubleIntermediate::operator()(), eoUniformInit< T >::operator()(), eoSBXCrossover< EOT >::operator()(), eoUniformMutation< EOT >::operator()(), eoFixedInertiaWeightedVelocity< POT >::operator()(), eoStandardVelocity< POT >::operator()(), eoIntegerVelocity< POT >::operator()(), eoConstrictedVelocity< POT >::operator()(), eoVariableInertiaWeightedVelocity< POT >::operator()(), eoExtendedVelocity< POT >::operator()(), eoConstrictedVariableWeightVelocity< POT >::operator()(), eoDetUniformMutation< EOT >::operator()(), eoSegmentCrossover< EOT >::operator()(), eoHypercubeCrossover< EOT >::operator()(), random(), roulette_wheel(), eoStochasticUniversalSelect< EOT >::setup(), and uniform().
double eoRng::uniform | ( | double | min, |
double | max | ||
) | [inline] |
uint32_t eoRng::random | ( | uint32_t | m | ) | [inline] |
Random integer number from unifom distribution.
m | Define interval for random number to [0, m) |
Definition at line 196 of file eoRNG.h.
References uniform().
Referenced by choice(), eoOrderXover< Chrom >::operator()(), eoRandomSelect< EOT >::operator()(), eoTwoOptMutation< EOT >::operator()(), eoOneBitFlip< Chrom >::operator()(), eoShiftMutation< EOT >::operator()(), eoSwapMutation< Chrom >::operator()(), eoSubtreeXOver< FType, Node >::operator()(), eoVlAddMutation< EOT >::operator()(), eoStSubtreeXOver< FType, Node >::operator()(), eoDetBitFlip< Chrom >::operator()(), eoFlOrKMutation< EOT >::operator()(), eoFlOrKAtomQuadOp< EOT >::operator()(), eoUniformGeneChooser< EOT >::operator()(), eoFlOrKAtomBinOp< EOT >::operator()(), eoBranchMutation< FType, Node >::operator()(), eoEPReduce< EOT >::operator()(), eoDetSingleBitFlip< Chrom >::operator()(), eoVelocityInitVariableLength< POT >::operator()(), eoInitVariableLength< EOT >::operator()(), eoStBranchMutation< FType, Node >::operator()(), eoPointMutation< FType, Node >::operator()(), eoFlOr1ptQuadOp< EOT >::operator()(), eoFlOr1ptBinOp< EOT >::operator()(), eoDetUniformMutation< EOT >::operator()(), eoBitInversion< Chrom >::operator()(), eoExpansionMutation< FType, Node >::operator()(), eoStPointMutation< FType, Node >::operator()(), eoStHoistMutation< FType, Node >::operator()(), eoCollapseSubtreeMutation< FType, Node >::operator()(), eo1PtBitXover< Chrom >::operator()(), eoHoistMutation< FType, Node >::operator()(), eoBitGxOver< Chrom >::operator()(), and eoStochasticUniversalSelect< EOT >::setup().
bool eoRng::flip | ( | double | bias = 0.5 | ) | [inline] |
Biased coin toss.
This tosses a biased coin such that flip(x/100.0) will true x% of the time
bias | The coins' bias (the x above) |
Definition at line 213 of file eoRNG.h.
References uniform().
Referenced by eoOrderXover< Chrom >::operator()(), eoFlOrAllAtomQuadOp< EOT >::operator()(), eoFlOrAllAtomBinOp< EOT >::operator()(), eoSGATransform< EOT >::operator()(), eoSGA< EOT >::operator()(), eoNormalVecMutation< EOT >::operator()(), eoUniformMutation< EOT >::operator()(), eoBooleanGenerator::operator()(), eoBooleanInit::operator()(), eoDynSGATransform< EOT >::operator()(), eoNormalMutation< EOT >::operator()(), eoInnerExchangeQuadOp< EOT >::operator()(), eoHypercubeCrossover< EOT >::operator()(), and eoRealUXover< EOT >::operator()().
double eoRng::normal | ( | ) | [inline] |
Gaussian deviate.
Zero mean Gaussian deviate with standard deviation 1. Note: Use the Marsaglia polar method.
Definition at line 517 of file eoRNG.h.
References cached, cacheValue, and uniform().
Referenced by eoGaussRealWeightUp::operator()(), eoNormalVecMutation< EOT >::operator()(), eoEsMutate< EOT >::operator()(), eoNormalGenerator< T >::operator()(), eoNormalInit< T >::operator()(), and eoNormalMutation< EOT >::operator()().
double eoRng::normal | ( | double | stdev | ) | [inline] |
double eoRng::normal | ( | double | mean, |
double | stdev | ||
) | [inline] |
double eoRng::negexp | ( | double | mean | ) | [inline] |
Random numbers using a negative exponential distribution.
mean | Mean value of distribution |
Definition at line 253 of file eoRNG.h.
References rand(), and rand_max().
Referenced by eoNegExpGenerator< T >::operator()(), and eoNegExpInit< T >::operator()().
int eoRng::roulette_wheel | ( | const std::vector< TYPE > & | vec, |
TYPE | total = 0 |
||
) | [inline] |
Roulette wheel selection.
roulette_wheel(vec, total = 0) does a roulette wheel selection on the input std::vector vec. If the total is not supplied, it is calculated. It returns an integer denoting the selected argument.
Definition at line 275 of file eoRNG.h.
References uniform().
Referenced by eoProportionalOp< EOT >::apply(), eoCombinedInit< EOT >::operator()(), eoPropCombinedMonOp< EOT >::operator()(), eoPropCombinedBinOp< EOT >::operator()(), and eoPropCombinedQuadOp< EOT >::operator()().
const TYPE& eoRng::choice | ( | const std::vector< TYPE > & | vec | ) | [inline] |
TYPE& eoRng::choice | ( | std::vector< TYPE > & | vec | ) | [inline] |
Randomly select element from vector.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Provide a version returning a non-const element reference.
Definition at line 312 of file eoRNG.h.
References random().
void eoRng::printOn | ( | std::ostream & | _os | ) | const [inline, virtual] |
Print RNG.
_os | Stream to print RNG on |
Implements eoPrintable.
void eoRng::readFrom | ( | std::istream & | _is | ) | [inline, virtual] |
Read RNG.
_is | Stream to read RNG from |
Implements eoPersistent.
std::string eoRng::className | ( | ) | const [inline, virtual] |
Return the class id.
This should be redefined in each class. Only "leaf" classes can be non-virtual.
Maarten: removed the default implementation as this proved to be too error-prone: I found several classes that had a typo in className (like classname), which would print eoObject instead of their own. Having it pure will force the implementor to provide a name.
Implements eoObject.
Assignment operator.