minor cleanup of code and docs,
resolve some compiler warnings
This commit is contained in:
parent
4c76c89592
commit
bcaa0cf5e4
4 changed files with 87 additions and 82 deletions
|
|
@ -40,7 +40,7 @@ class eoExternalEO : public EO<Fit>, virtual public External
|
||||||
{
|
{
|
||||||
public :
|
public :
|
||||||
|
|
||||||
eoExternalEO(void)
|
eoExternalEO()
|
||||||
: EO<Fit>(), External()
|
: EO<Fit>(), External()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,8 @@ You should have received a copy of the GNU Lesser General Public
|
||||||
License along with this library; if not, write to the Free Software
|
License along with this library; if not, write to the Free Software
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
|
||||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
Contact: eodev-main@lists.sourceforge.net
|
||||||
|
Old contact information: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef EO_RANDOM_NUMBER_GENERATOR
|
#ifndef EO_RANDOM_NUMBER_GENERATOR
|
||||||
|
|
@ -26,18 +27,17 @@ Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||||
|
|
||||||
// uint32_t is an unsigned integer type capable of holding 32 bits.
|
// uint32_t is an unsigned integer type capable of holding 32 bits.
|
||||||
//
|
//
|
||||||
// In the applicatione here exactly 32 but should typically be fastest, but 64
|
// In the applicatione here exactly 32 are used.
|
||||||
// might be better on an Alpha with GCC at -O3 optimization so try your options
|
// 64 bits might be better on an Alpha or other 64 bit systems with GCC at high
|
||||||
// and see what's best for you.
|
// optimization levels so feel free to try your options and see what's best for
|
||||||
//
|
// you.
|
||||||
// The C99-standard defines uint32_t to be declared in stdint.h, but some
|
|
||||||
// systems don't have that and implement it in inttypes.h.
|
|
||||||
|
|
||||||
// first if check added for MSVC by Jeroen Eggermont 20-11-2006, needed for MSVC 2003 (and 2005)
|
|
||||||
# if (defined _MSC_VER)
|
# if (defined _MSC_VER)
|
||||||
typedef unsigned long uint32_t;
|
typedef unsigned long uint32_t;
|
||||||
#else
|
#else
|
||||||
#if (! defined __sun)
|
#if (! defined __sun)
|
||||||
|
// The C99-standard defines uint32_t to be declared in stdint.h, but some
|
||||||
|
// systems don't have that and implement it in inttypes.h.
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#else
|
#else
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
|
@ -125,7 +125,7 @@ public :
|
||||||
initialize(2*s);
|
initialize(2*s);
|
||||||
}
|
}
|
||||||
|
|
||||||
~eoRng(void)
|
~eoRng()
|
||||||
{
|
{
|
||||||
delete [] state;
|
delete [] state;
|
||||||
}
|
}
|
||||||
|
|
@ -188,7 +188,7 @@ public :
|
||||||
/**
|
/**
|
||||||
normal() zero mean gaussian deviate with standard deviation of 1
|
normal() zero mean gaussian deviate with standard deviation of 1
|
||||||
*/
|
*/
|
||||||
double normal(void); // gaussian mutation, stdev 1
|
double normal(); // gaussian mutation, stdev 1
|
||||||
|
|
||||||
/**
|
/**
|
||||||
normal(stdev) zero mean gaussian deviate with user defined standard deviation
|
normal(stdev) zero mean gaussian deviate with user defined standard deviation
|
||||||
|
|
@ -222,9 +222,10 @@ public :
|
||||||
/**
|
/**
|
||||||
rand_max() the maximum returned by rand()
|
rand_max() the maximum returned by rand()
|
||||||
*/
|
*/
|
||||||
uint32_t rand_max(void) const { return uint32_t(0xffffffff); }
|
uint32_t rand_max() const { return uint32_t(0xffffffff); }
|
||||||
|
|
||||||
|
/** Roulette wheel selection
|
||||||
|
|
||||||
/**
|
|
||||||
roulette_wheel(vec, total = 0) does a 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
|
on the input std::vector vec. If the total is not supplied, it is
|
||||||
calculated. It returns an integer denoting the selected argument.
|
calculated. It returns an integer denoting the selected argument.
|
||||||
|
|
@ -298,11 +299,11 @@ public :
|
||||||
_is >> cacheValue;
|
_is >> cacheValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string className(void) const { return "Mersenne-Twister"; }
|
std::string className() const { return "Mersenne-Twister"; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
uint32_t restart(void);
|
uint32_t restart();
|
||||||
|
|
||||||
|
|
||||||
/* @brief Initialize state
|
/* @brief Initialize state
|
||||||
|
|
@ -360,7 +361,7 @@ private:
|
||||||
|
|
||||||
int left;
|
int left;
|
||||||
|
|
||||||
// for normal distribution
|
/** @brief Are there cached values for normal distribution? */
|
||||||
bool cached;
|
bool cached;
|
||||||
|
|
||||||
double cacheValue;
|
double cacheValue;
|
||||||
|
|
@ -405,12 +406,12 @@ using eo::rng;
|
||||||
|
|
||||||
// Implementation of some eoRng members.... Don't mind the mess, it does work.
|
// Implementation of some eoRng members.... Don't mind the mess, it does work.
|
||||||
|
|
||||||
|
|
||||||
#define hiBit(u) ((u) & 0x80000000U) // mask all but highest bit of u
|
#define hiBit(u) ((u) & 0x80000000U) // mask all but highest bit of u
|
||||||
#define loBit(u) ((u) & 0x00000001U) // mask all but lowest bit of u
|
#define loBit(u) ((u) & 0x00000001U) // mask all but lowest bit of u
|
||||||
#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_t seed)
|
inline void eoRng::initialize(uint32_t seed)
|
||||||
{
|
{
|
||||||
left = -1;
|
left = -1;
|
||||||
|
|
@ -424,7 +425,7 @@ inline void eoRng::initialize(uint32_t seed)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
inline uint32_t eoRng::restart(void)
|
inline uint32_t eoRng::restart()
|
||||||
{
|
{
|
||||||
register uint32_t *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;
|
||||||
|
|
@ -446,15 +447,11 @@ inline uint32_t eoRng::restart(void)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
inline uint32_t eoRng::rand(void)
|
inline uint32_t eoRng::rand()
|
||||||
{
|
{
|
||||||
|
|
||||||
uint32_t y;
|
|
||||||
|
|
||||||
if(--left < 0)
|
if(--left < 0)
|
||||||
return(restart());
|
return(restart());
|
||||||
|
uint32_t y = *next++;
|
||||||
y = *next++;
|
|
||||||
y ^= (y >> 11);
|
y ^= (y >> 11);
|
||||||
y ^= (y << 7) & 0x9D2C5680U;
|
y ^= (y << 7) & 0x9D2C5680U;
|
||||||
y ^= (y << 15) & 0xEFC60000U;
|
y ^= (y << 15) & 0xEFC60000U;
|
||||||
|
|
@ -463,58 +460,38 @@ inline uint32_t eoRng::rand(void)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
inline double eoRng::normal(void)
|
inline double eoRng::normal()
|
||||||
{
|
|
||||||
if (cached)
|
|
||||||
{
|
{
|
||||||
|
if (cached) {
|
||||||
cached = false;
|
cached = false;
|
||||||
return cacheValue;
|
return cacheValue;
|
||||||
}
|
}
|
||||||
|
double rSquare, var1, var2;
|
||||||
double rSquare, factor, var1, var2;
|
do {
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
var1 = 2.0 * uniform() - 1.0;
|
var1 = 2.0 * uniform() - 1.0;
|
||||||
var2 = 2.0 * uniform() - 1.0;
|
var2 = 2.0 * uniform() - 1.0;
|
||||||
|
|
||||||
rSquare = var1 * var1 + var2 * var2;
|
rSquare = var1 * var1 + var2 * var2;
|
||||||
}
|
} while (rSquare >= 1.0 || rSquare == 0.0);
|
||||||
while (rSquare >= 1.0 || rSquare == 0.0);
|
double factor = sqrt(-2.0 * log(rSquare) / rSquare);
|
||||||
|
|
||||||
factor = sqrt(-2.0 * log(rSquare) / rSquare);
|
|
||||||
|
|
||||||
cacheValue = var1 * factor;
|
cacheValue = var1 * factor;
|
||||||
cached = true;
|
cached = true;
|
||||||
|
|
||||||
return (var2 * factor);
|
return (var2 * factor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace eo {
|
namespace eo
|
||||||
// a few convenience functions for generating numbers
|
{
|
||||||
|
|
||||||
/** @brief Random function
|
/** @brief Random function
|
||||||
|
|
||||||
Templatized random function, returns a random double in the range [0, max).
|
This is a convenience function for generating random numbers using the
|
||||||
|
global eo::rng object.
|
||||||
@param max Maximum for distribution
|
|
||||||
|
|
||||||
It works with most basic types such as:
|
|
||||||
- char
|
|
||||||
- int
|
|
||||||
- unsigned
|
|
||||||
- float
|
|
||||||
- double
|
|
||||||
*/
|
|
||||||
template <typename T>
|
|
||||||
inline T random(const T& max) {
|
|
||||||
return static_cast<T>(rng.uniform() * max); }
|
|
||||||
|
|
||||||
/** @brief Random function
|
|
||||||
|
|
||||||
Templatized random function, returns a random double in the range [min, max).
|
Templatized random function, returns a random double in the range [min, max).
|
||||||
|
It works with most basic types such as:
|
||||||
|
- char
|
||||||
|
- int (short, long, signed and unsigned)
|
||||||
|
- float, double
|
||||||
|
|
||||||
@param min Minimum for distribution
|
@param min Minimum for distribution
|
||||||
@param max Maximum for distribution
|
@param max Maximum for distribution
|
||||||
|
|
@ -525,7 +502,34 @@ namespace eo {
|
||||||
inline T random(const T& min, const T& max) {
|
inline T random(const T& min, const T& max) {
|
||||||
return static_cast<T>(rng.uniform() * (max-min)) + min; }
|
return static_cast<T>(rng.uniform() * (max-min)) + min; }
|
||||||
|
|
||||||
/** Normal distribution */
|
/** @brief Random function
|
||||||
|
|
||||||
|
@overload
|
||||||
|
|
||||||
|
This is a convenience function for generating random numbers using the
|
||||||
|
global eo::rng object.
|
||||||
|
|
||||||
|
Templatized random function, returns a random double in the range [0, max).
|
||||||
|
It works with most basic types such as:
|
||||||
|
- char
|
||||||
|
- int (short, long, signed and unsigned)
|
||||||
|
- float, double
|
||||||
|
|
||||||
|
@param max Maximum for distribution
|
||||||
|
|
||||||
|
@see random(const T& min, const T& max)
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
inline T random(const T& max) {
|
||||||
|
return static_cast<T>(rng.uniform() * max); }
|
||||||
|
|
||||||
|
/** Normal distribution
|
||||||
|
|
||||||
|
This is a convenience function for generating random numbers using the
|
||||||
|
global eo::rng object.
|
||||||
|
|
||||||
|
@return ormally distributed random number
|
||||||
|
*/
|
||||||
inline double normal() { return rng.normal(); }
|
inline double normal() { return rng.normal(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -536,6 +540,7 @@ namespace eo {
|
||||||
// Local Variables:
|
// Local Variables:
|
||||||
// coding: iso-8859-1
|
// coding: iso-8859-1
|
||||||
// mode: C++
|
// mode: C++
|
||||||
|
// c-file-offsets: ((c . 0))
|
||||||
// c-file-style: "Stroustrup"
|
// c-file-style: "Stroustrup"
|
||||||
// fill-column: 80
|
// fill-column: 80
|
||||||
// End:
|
// End:
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@
|
||||||
class eoTimedMonitor : public eoMonitor
|
class eoTimedMonitor : public eoMonitor
|
||||||
{
|
{
|
||||||
public :
|
public :
|
||||||
eoTimedMonitor(int seconds_) : seconds(seconds_), last_tick(0) {}
|
eoTimedMonitor(int seconds_) : last_tick(0), seconds(seconds_) {}
|
||||||
|
|
||||||
eoMonitor& operator()(void) {
|
eoMonitor& operator()(void) {
|
||||||
bool monitor = false;
|
bool monitor = false;
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ double f_sphere(const vector<double>& values) {
|
||||||
double f_rosen(const vector<double>& x) {
|
double f_rosen(const vector<double>& x) {
|
||||||
double sum =0.0;
|
double sum =0.0;
|
||||||
|
|
||||||
for (int i = 0; i < x.size()-1; ++i) {
|
for (unsigned i = 0; i < x.size()-1; ++i) {
|
||||||
sum += 100 * sqr(sqr(x[i])-x[i+1]) + sqr(1.-x[i]);
|
sum += 100 * sqr(sqr(x[i])-x[i+1]) + sqr(1.-x[i]);
|
||||||
}
|
}
|
||||||
++evals.value();
|
++evals.value();
|
||||||
|
|
|
||||||
Reference in a new issue