NDSorting: changed exact ranking values to make check for front easier

eoRNG: changed nothing (did some debugging)
This commit is contained in:
maartenkeijzer 2001-03-26 10:09:40 +00:00
commit ebca71e228
2 changed files with 49 additions and 35 deletions

View file

@ -119,6 +119,8 @@ class eoNDSorting : public eoPerf2WorthCached<EOT, double>
} }
} }
unsigned first_front_size = current_front.size();
vector<unsigned> next_front; vector<unsigned> next_front;
next_front.reserve(_pop.size()); next_front.reserve(_pop.size());
@ -165,11 +167,21 @@ class eoNDSorting : public eoPerf2WorthCached<EOT, double>
// now all that's left to do is to transform lower rank into higher worth // now all that's left to do is to transform lower rank into higher worth
double max_fitness = *std::max_element(value().begin(), value().end()); double max_fitness = *std::max_element(value().begin(), value().end());
// but make sure it's an integer upper bound, so that all ranks inside the highest integer are the front
max_fitness = ceil(max_fitness);
unsigned nfirst = 0;
for (unsigned i = 0; i < value().size(); ++i) for (unsigned i = 0; i < value().size(); ++i)
{ {
value()[i] = max_fitness - value()[i]; value()[i] = max_fitness - value()[i];
assert(n[i] == 0); assert(n[i] == 0);
if (value()[i] > (max_fitness-1)) // this would be the test for 'front_ness'
nfirst++;
} }
assert(nfirst == first_front_size);
} }
}; };

View file

@ -116,21 +116,21 @@ public :
eoRng(uint32 s) : state(0), next(0), left(-1), cached(false), N(624), M(397), K(0x9908B0DFU) { eoRng(uint32 s) : state(0), next(0), left(-1), cached(false), N(624), M(397), K(0x9908B0DFU) {
state = new uint32[N+1]; state = new uint32[N+1];
initialize(s); initialize(s);
} }
~eoRng(void) ~eoRng(void)
{ {
delete [] state; delete [] state;
} }
/** /**
Re-initializes the Random Number Generator. Re-initializes the Random Number Generator.
*/ */
void reseed(uint32 s) void reseed(uint32 s)
{ {
initialize(s); initialize(s);
} }
/** /**
uniform(m = 1.0) returns a random double in the range [0, m) uniform(m = 1.0) returns a random double in the range [0, m)
*/ */
@ -146,7 +146,7 @@ public :
{ {
return uint32(uniform() * double(m)); return uint32(uniform() * double(m));
} }
/** /**
flip() tosses a biased coin such that flip(x/100.0) will flip() tosses a biased coin such that flip(x/100.0) will
returns true x% of the time returns true x% of the time
@ -160,7 +160,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(void); // 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
*/ */
@ -193,10 +193,10 @@ public :
/** /**
rand_max() the maximum returned by rand() rand_max() the maximum returned by rand()
*/ */
uint32 rand_max(void) const { return (uint32) 0xffffffff; } uint32 rand_max(void) const { return (uint32) 0xffffffff; }
/** /**
roulette_wheel(vec, total = 0) does a roulette wheel selection roulette_wheel(vec, total = 0) does a roulette wheel selection
on the input vector vec. If the total is not supplied, it is on the input 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.
*/ */
@ -210,14 +210,14 @@ public :
} }
float change = uniform() * total; float change = uniform() * total;
int i = 0; int i = 0;
while (change > 0) while (change > 0)
{ {
change -= vec[i++]; change -= vec[i++];
} }
return --i; return --i;
} }
@ -231,7 +231,7 @@ public :
_os << int(next - state) << ' '; _os << int(next - state) << ' ';
_os << left << ' ' << cached << ' ' << cacheValue; _os << left << ' ' << cached << ' ' << cacheValue;
} }
/// ///
void readFrom(istream& _is) void readFrom(istream& _is)
{ {
@ -239,11 +239,11 @@ public :
{ {
_is >> state[i]; _is >> state[i];
} }
int n; int n;
_is >> n; _is >> n;
next = state + n; next = state + n;
_is >> left; _is >> left;
_is >> cached; _is >> cached;
_is >> cacheValue; _is >> cacheValue;
@ -254,18 +254,20 @@ public :
private : private :
uint32 restart(void); uint32 restart(void);
void initialize(uint32 seed); void initialize(uint32 seed);
uint32* state; // the array for the state uint32* state; // the array for the state
uint32* next; uint32* next;
int left; int left;
// for normal distribution
bool cached; bool cached;
float cacheValue; float cacheValue;
const int N; const int N;
const int M; const int M;
const uint32 K; // a magic constant const uint32 K; // a magic constant
/** /**
Private copy ctor and assignment operator to make sure that Private copy ctor and assignment operator to make sure that
nobody accidentally copies the random number generator. nobody accidentally copies the random number generator.
@ -344,10 +346,10 @@ inline void eoRng::initialize(uint32 seed)
// //
left = -1; left = -1;
register uint32 x = (seed | 1U) & 0xFFFFFFFFU, *s = state; register uint32 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;
*s++ = (x*=69069U) & 0xFFFFFFFFU); *s++ = (x*=69069U) & 0xFFFFFFFFU);
} }
@ -357,15 +359,15 @@ inline uint32 eoRng::restart(void)
{ {
register uint32 *p0=state, *p2=state+2, *pM=state+M, s0, s1; register uint32 *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;
for(s0=state[0], s1=state[1], j=N-M+1; --j; s0=s1, s1=*p2++) for(s0=state[0], s1=state[1], j=N-M+1; --j; s0=s1, s1=*p2++)
*p0++ = *pM++ ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); *p0++ = *pM++ ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U);
for(pM=state, j=M; --j; s0=s1, s1=*p2++) for(pM=state, j=M; --j; s0=s1, s1=*p2++)
*p0++ = *pM++ ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); *p0++ = *pM++ ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U);
s1=state[0], *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); s1=state[0], *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U);
s1 ^= (s1 >> 11); s1 ^= (s1 >> 11);
s1 ^= (s1 << 7) & 0x9D2C5680U; s1 ^= (s1 << 7) & 0x9D2C5680U;
@ -373,14 +375,14 @@ inline uint32 eoRng::restart(void)
return(s1 ^ (s1 >> 18)); return(s1 ^ (s1 >> 18));
} }
inline uint32 eoRng::rand(void) inline uint32 eoRng::rand(void)
{ {
uint32 y; uint32 y;
if(--left < 0) if(--left < 0)
return(restart()); return(restart());
y = *next++; y = *next++;
y ^= (y >> 11); y ^= (y >> 11);
y ^= (y << 7) & 0x9D2C5680U; y ^= (y << 7) & 0x9D2C5680U;
@ -389,20 +391,20 @@ inline uint32 eoRng::rand(void)
} }
inline double eoRng::normal(void) inline double eoRng::normal(void)
{ {
if (cached) if (cached)
{ {
cached = false; cached = false;
return cacheValue; return cacheValue;
} }
float rSquare, factor, var1, var2; float 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);