Make sure uint32_t is defined correctly in eoRNG.h and use it where

appropriate.
This commit is contained in:
kuepper 2005-09-26 10:18:15 +00:00
commit 6e0c4a7264
13 changed files with 238 additions and 220 deletions

View file

@ -30,6 +30,7 @@ AC_CHECK_PROG(DOXYGEN, doxygen, doxygen, true)
dnl Checks for header files. dnl Checks for header files.
AC_HEADER_STDC AC_HEADER_STDC
AC_CHECK_HEADERS(inttypes.h)
AC_CHECK_HEADERS(limits.h) AC_CHECK_HEADERS(limits.h)
AC_CHECK_HEADERS(values.h) AC_CHECK_HEADERS(values.h)
AC_CXX_HAVE_NUMERIC_LIMITS AC_CXX_HAVE_NUMERIC_LIMITS
@ -39,6 +40,8 @@ AC_CXX_NAMESPACES
dnl Checks for typedefs, structures, and compiler characteristics. dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST AC_C_CONST
AC_C_INLINE AC_C_INLINE
AC_CHECK_SIZEOF([unsigned long])
AC_CHECK_TYPES(uint32_t)
AC_TYPE_SIZE_T AC_TYPE_SIZE_T
dnl Checks for libraries. dnl Checks for libraries.

View file

@ -49,7 +49,7 @@ template <class EOT>
eoPop<EOT>& do_make_pop(eoParser & _parser, eoState& _state, eoInit<EOT> & _init) eoPop<EOT>& do_make_pop(eoParser & _parser, eoState& _state, eoInit<EOT> & _init)
{ {
// random seed // random seed
eoValueParam<uint32>& seedParam = _parser.createParam(uint32(0), "seed", "Random number seed", 'S'); eoValueParam<uint32_t>& seedParam = _parser.createParam(uint32_t(0), "seed", "Random number seed", 'S');
if (seedParam.value() == 0) if (seedParam.value() == 0)
seedParam.value() = time(0); seedParam.value() = time(0);
eoValueParam<unsigned>& popSize = _parser.createParam(unsigned(20), "popSize", "Population Size", 'P', "Evolution Engine"); eoValueParam<unsigned>& popSize = _parser.createParam(unsigned(20), "popSize", "Population Size", 'P', "Evolution Engine");

View file

@ -49,7 +49,7 @@ template <class EOT>
eoPBILDistrib<EOT> & do_make_PBILdistrib(eoParser & _parser, eoState& _state, EOT) eoPBILDistrib<EOT> & do_make_PBILdistrib(eoParser & _parser, eoState& _state, EOT)
{ {
// First the random seed // First the random seed
eoValueParam<uint32>& seedParam = _parser.createParam(uint32(0), "seed", "Random number seed", 'S'); eoValueParam<uint32_t>& seedParam = _parser.createParam(uint32_t(0), "seed", "Random number seed", 'S');
if (seedParam.value() == 0) if (seedParam.value() == 0)
seedParam.value() = time(0); seedParam.value() = time(0);

View file

@ -70,7 +70,7 @@
// //
// //
// uint32 must be an unsigned integer type capable of holding at least 32 // uint32_t must be an unsigned integer type capable of holding at least 32
// bits; exactly 32 should be fastest, but 64 is better on an Alpha with // bits; exactly 32 should be fastest, but 64 is better on an Alpha with
// GCC at -O3 optimization so try your options and see what's best for you // GCC at -O3 optimization so try your options and see what's best for you
// //
@ -81,6 +81,12 @@
#ifndef EO_RANDOM_NUMBER_GENERATOR #ifndef EO_RANDOM_NUMBER_GENERATOR
#define EO_RANDOM_NUMBER_GENERATOR #define EO_RANDOM_NUMBER_GENERATOR
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#include "../eoPersistent.h" #include "../eoPersistent.h"
#include "../eoObject.h" #include "../eoObject.h"
@ -89,7 +95,14 @@
// Unfortunately MSVC's preprocessor does not comprehend sizeof() // Unfortunately MSVC's preprocessor does not comprehend sizeof()
// so neat preprocessing tricks will not work // so neat preprocessing tricks will not work
typedef unsigned long uint32; // Compiler and platform dependent! #if(! (defined HAVE_UINT32_T))
#if(SIZEOF_UNSIGNED_LONG == 4)
typedef unsigned long uint32_t;
#else
#error Need to provide a type for uint32_t in eoRNG.h.
#endif
#endif
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoRng // eoRng
@ -101,7 +114,7 @@ for generating random numbers. The various member functions implement useful fun
for evolutionary algorithms. Included are: rand(), random(), flip() and normal(). for evolutionary algorithms. Included are: rand(), random(), flip() and normal().
Note for people porting EO to other platforms: please make sure that the typedef Note for people porting EO to other platforms: please make sure that the typedef
uint32 in the file eoRng.h is exactly 32 bits long. It may be longer, but not uint32_t in the file eoRng.h is exactly 32 bits long. It may be longer, but not
shorter. If it is longer, file compatibility between EO on different platforms shorter. If it is longer, file compatibility between EO on different platforms
may be broken. may be broken.
*/ */
@ -113,8 +126,8 @@ public :
@see reseed to see why the parameter to initialize is doubled @see reseed to see why the parameter to initialize is doubled
*/ */
eoRng(uint32 s) : state(0), next(0), left(-1), cached(false), N(624), M(397), K(0x9908B0DFU) { eoRng(uint32_t s) : state(0), next(0), left(-1), cached(false), N(624), M(397), K(0x9908B0DFU) {
state = new uint32[N+1]; state = new uint32_t[N+1];
initialize(2*s); initialize(2*s);
} }
@ -133,7 +146,7 @@ public :
* *
* MS. 5 Oct. 2001 * MS. 5 Oct. 2001
*/ */
void reseed(uint32 s) void reseed(uint32_t s)
{ {
initialize(2*s); initialize(2*s);
} }
@ -141,7 +154,7 @@ public :
/** /**
Re-initializes the Random Number Generator - old version Re-initializes the Random Number Generator - old version
*/ */
void oldReseed(uint32 s) void oldReseed(uint32_t s)
{ {
initialize(s); initialize(s);
} }
@ -157,9 +170,9 @@ public :
/** /**
random() returns a random integer in the range [0, m) random() returns a random integer in the range [0, m)
*/ */
uint32 random(uint32 m) uint32_t random(uint32_t m)
{ {
return uint32(uniform() * double(m)); return uint32_t(uniform() * double(m));
} }
/** /**
@ -203,12 +216,12 @@ public :
/** /**
rand() returns a random number in the range [0, rand_max) rand() returns a random number in the range [0, rand_max)
*/ */
uint32 rand(); uint32_t rand();
/** /**
rand_max() the maximum returned by rand() rand_max() the maximum returned by rand()
*/ */
uint32 rand_max(void) const { return (uint32) 0xffffffff; } uint32_t rand_max(void) const { return (uint32_t) 0xffffffff; }
/** /**
roulette_wheel(vec, total = 0) does a roulette wheel selection roulette_wheel(vec, total = 0) does a roulette wheel selection
@ -275,11 +288,11 @@ public :
std::string className(void) const { return "Mersenne-Twister"; } std::string className(void) const { return "Mersenne-Twister"; }
private : private :
uint32 restart(void); uint32_t restart(void);
void initialize(uint32 seed); void initialize(uint32_t seed);
uint32* state; // the array for the state uint32_t* state; // the array for the state
uint32* next; uint32_t* next;
int left; int left;
// for normal distribution // for normal distribution
@ -288,7 +301,7 @@ private :
const int N; const int N;
const int M; const int M;
const uint32 K; // a magic constant const uint32_t K; // a magic constant
/** /**
@ -319,7 +332,7 @@ using eo::rng;
#define loBits(u) ((u) & 0x7FFFFFFFU) // mask the highest bit of u #define loBits(u) ((u) & 0x7FFFFFFFU) // mask the highest bit of u
#define mixBits(u, v) (hiBit(u)|loBits(v)) // move hi bit of u to hi bit of v #define mixBits(u, v) (hiBit(u)|loBits(v)) // move hi bit of u to hi bit of v
inline void eoRng::initialize(uint32 seed) inline void eoRng::initialize(uint32_t seed)
{ {
// //
// We initialize state[0..(N-1)] via the generator // We initialize state[0..(N-1)] via the generator
@ -369,7 +382,7 @@ inline void eoRng::initialize(uint32 seed)
left = -1; left = -1;
register uint32 x = (seed | 1U) & 0xFFFFFFFFU, *s = state; register uint32_t x = (seed | 1U) & 0xFFFFFFFFU, *s = state;
register int j; register int j;
for(left=0, *s++=x, j=N; --j; for(left=0, *s++=x, j=N; --j;
@ -377,9 +390,9 @@ inline void eoRng::initialize(uint32 seed)
} }
inline uint32 eoRng::restart(void) inline uint32_t eoRng::restart(void)
{ {
register uint32 *p0=state, *p2=state+2, *pM=state+M, s0, s1; register uint32_t *p0=state, *p2=state+2, *pM=state+M, s0, s1;
register int j; register int j;
left=N-1, next=state+1; left=N-1, next=state+1;
@ -397,10 +410,10 @@ inline uint32 eoRng::restart(void)
return(s1 ^ (s1 >> 18)); return(s1 ^ (s1 >> 18));
} }
inline uint32 eoRng::rand(void) inline uint32_t eoRng::rand(void)
{ {
uint32 y; uint32_t y;
if(--left < 0) if(--left < 0)
return(restart()); return(restart());

View file

@ -84,7 +84,7 @@ class boolean_generator
either between [0, _max) if only one value (_max) is given to the ctor 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) or in [_min,_max) if 2 values are given (_min, _max)
*/ */
template <class T = uint32> class random_generator template <class T = uint32_t> class random_generator
{ {
public : public :
// added new ctor with 2 params, and modified the data to minim and range // added new ctor with 2 params, and modified the data to minim and range
@ -118,7 +118,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}
*/ */
template <class T = uint32> class UF_random_generator template <class T = uint32_t> class UF_random_generator
{ {
public : public :
UF_random_generator(eoRng& _rng = rng) : UF_random_generator(eoRng& _rng = rng) :

View file

@ -48,7 +48,7 @@ int the_main(int argc, char **argv)
// Define Parameters // Define Parameters
eoValueParam<double> rate(0.01, "mutationRatePerBit", "Initial value for mutation rate per bit"); eoValueParam<double> rate(0.01, "mutationRatePerBit", "Initial value for mutation rate per bit");
eoValueParam<double> factor(0.99, "mutationFactor", "Decrease factor for mutation rate"); eoValueParam<double> factor(0.99, "mutationFactor", "Decrease factor for mutation rate");
eoValueParam<uint32> seed(time(0), "seed", "Random number seed"); eoValueParam<uint32_t> seed(time(0), "seed", "Random number seed");
eoValueParam<std::string> load_name("", "Load","Load",'L'); eoValueParam<std::string> load_name("", "Load","Load",'L');
eoValueParam<std::string> save_name("", "Save","Save",'S'); eoValueParam<std::string> save_name("", "Save","Save",'S');

View file

@ -33,7 +33,8 @@ int main_function(int argc, char *argv[])
eoParser parser( argc, argv, "Basic EA for vector<float> with adaptive mutations"); eoParser parser( argc, argv, "Basic EA for vector<float> with adaptive mutations");
// Define Parameters and load them // Define Parameters and load them
eoValueParam<uint32>& seed = parser.createParam(static_cast<uint32>(time(0)), "seed", "Random number seed"); eoValueParam<uint32_t>& seed = parser.createParam(static_cast<uint32_t>(time(0)),
"seed", "Random number seed");
eoValueParam<string>& load_name = parser.createParam(string(), "Load","Load a state file",'L'); eoValueParam<string>& load_name = parser.createParam(string(), "Load","Load a state file",'L');
eoValueParam<string>& save_name = parser.createParam(string(), "Save","Saves a state file",'S'); eoValueParam<string>& save_name = parser.createParam(string(), "Save","Saves a state file",'S');
eoValueParam<bool>& stdevs = parser.createParam(false, "Stdev", "Use adaptive mutation rates", 's'); eoValueParam<bool>& stdevs = parser.createParam(false, "Stdev", "Use adaptive mutation rates", 's');

View file

@ -197,7 +197,7 @@ int the_main(int argc, char **argv)
eoValueParam<unsigned int> parentSizeParam = parser.createParam(unsigned(10), "parentSize", "Parent size",'P'); eoValueParam<unsigned int> parentSizeParam = parser.createParam(unsigned(10), "parentSize", "Parent size",'P');
pSize = parentSizeParam.value(); // global variable pSize = parentSizeParam.value(); // global variable
eoValueParam<uint32> seedParam(time(0), "seed", "Random number seed", 'S'); eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam ); parser.processParam( seedParam );
eo::rng.reseed(seedParam.value()); eo::rng.reseed(seedParam.value());

View file

@ -156,7 +156,8 @@ eoValueParam<unsigned> tournamentSizeParam = parser.createParam(unsigned(2), "to
std::cout << "Initial parents (odd)\n" << parentsOrg << std::endl; std::cout << "Initial parents (odd)\n" << parentsOrg << std::endl;
// random seed // random seed
eoValueParam<uint32>& seedParam = parser.createParam(uint32(0), "seed", "Random number seed", 'S'); eoValueParam<uint32_t>& seedParam = parser.createParam(uint32_t(0), "seed",
"Random number seed", 'S');
if (seedParam.value() == 0) if (seedParam.value() == 0)
seedParam.value() = time(0); seedParam.value() = time(0);
rng.reseed(seedParam.value()); rng.reseed(seedParam.value());

View file

@ -121,7 +121,7 @@ int the_main(int argc, char **argv)
eoParser parser(argc, argv); eoParser parser(argc, argv);
// random seed // random seed
eoValueParam<uint32>& seedParam = parser.createParam(uint32(0), "seed", "Random number seed", 'S'); eoValueParam<uint32_t>& seedParam = parser.createParam(uint32_t(0), "seed", "Random number seed", 'S');
if (seedParam.value() == 0) if (seedParam.value() == 0)
seedParam.value() = time(0); seedParam.value() = time(0);
rng.reseed(seedParam.value()); rng.reseed(seedParam.value());

View file

@ -45,7 +45,7 @@ int the_main(int argc, char **argv)
eoValueParam<unsigned int> dimParam((unsigned int)(5), "dimension", "dimension"); eoValueParam<unsigned int> dimParam((unsigned int)(5), "dimension", "dimension");
eoValueParam<double> rate(0.01, "mutationRatePerBit", "Initial value for mutation rate per bit"); eoValueParam<double> rate(0.01, "mutationRatePerBit", "Initial value for mutation rate per bit");
eoValueParam<double> factor(0.99, "mutationFactor", "Decrease factor for mutation rate"); eoValueParam<double> factor(0.99, "mutationFactor", "Decrease factor for mutation rate");
eoValueParam<uint32> seed(time(0), "seed", "Random number seed"); eoValueParam<uint32_t> seed(time(0), "seed", "Random number seed");
// test if user entered or if default value used // test if user entered or if default value used
if (parser.isItThere(seed)) if (parser.isItThere(seed))
std::cout << "YES\n"; std::cout << "YES\n";

View file

@ -52,7 +52,7 @@ void main_function(int argc, char **argv)
// For each parameter, define Parameter, read it through the parser, // For each parameter, define Parameter, read it through the parser,
// and assign the value to the variable // and assign the value to the variable
eoValueParam<uint32> seedParam(time(0), "seed", "Random number seed", 'S'); eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam ); parser.processParam( seedParam );
unsigned seed = seedParam.value(); unsigned seed = seedParam.value();

View file

@ -52,7 +52,7 @@ void main_function(int argc, char **argv)
// For each parameter, define Parameter, read it through the parser, // For each parameter, define Parameter, read it through the parser,
// and assign the value to the variable // and assign the value to the variable
eoValueParam<uint32> seedParam(time(0), "seed", "Random number seed", 'S'); eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam ); parser.processParam( seedParam );
unsigned seed = seedParam.value(); unsigned seed = seedParam.value();