adjust eol style

This commit is contained in:
swaits committed 2005-05-20 18:12:44 +00:00
1 parent 70650ba753
commit 6e2d4bf3ca
7 files changed
+838 -838

No files matched your search

+80 -80
View File
@@ -1,80 +1,80 @@
#ifndef __de_h__ #ifndef __de_h__
#define __de_h__ #define __de_h__
#include "devec.h" #include "devec.h"
#include "derand.h" #include "derand.h"
namespace DE namespace DE
{ {
template <unsigned int DIM, unsigned int POP=(DIM*10)> template <unsigned int DIM, unsigned int POP=(DIM*10)>
class Engine class Engine
{ {
public: public:
Engine(); Engine();
virtual ~Engine(); virtual ~Engine();
void Reset(); void Reset();
const double* GetBest() const; const double* GetBest() const;
void SetRange(double minimum, double maximum); void SetRange(double minimum, double maximum);
void SetRange(const Vector<DIM>& minimum, const Vector<DIM>& maximum); void SetRange(const Vector<DIM>& minimum, const Vector<DIM>& maximum);
virtual double CalculateError(const double testcase[DIM], bool& stop) = 0; virtual double CalculateError(const double testcase[DIM], bool& stop) = 0;
bool Solve(unsigned int maxgenerations = 1000000000); bool Solve(unsigned int maxgenerations = 1000000000);
private: private:
void Select(unsigned int candidate, unsigned int *r1 = 0, unsigned int *r2 = 0, unsigned int *r3 = 0, unsigned int *r4 = 0, unsigned int *r5 = 0); void Select(unsigned int candidate, unsigned int *r1 = 0, unsigned int *r2 = 0, unsigned int *r3 = 0, unsigned int *r4 = 0, unsigned int *r5 = 0);
void Dump(const double& fitness, Vector<DIM>& individual) const; void Dump(const double& fitness, Vector<DIM>& individual) const;
void MakeTrial_best1exp(unsigned int candidate, Vector<DIM>& trial); void MakeTrial_best1exp(unsigned int candidate, Vector<DIM>& trial);
void MakeTrial_rand1exp(unsigned int candidate, Vector<DIM>& trial); void MakeTrial_rand1exp(unsigned int candidate, Vector<DIM>& trial);
void MakeTrial_randtobest1exp(unsigned int candidate, Vector<DIM>& trial); void MakeTrial_randtobest1exp(unsigned int candidate, Vector<DIM>& trial);
void MakeTrial_best2exp(unsigned int candidate, Vector<DIM>& trial); void MakeTrial_best2exp(unsigned int candidate, Vector<DIM>& trial);
void MakeTrial_rand2exp(unsigned int candidate, Vector<DIM>& trial); void MakeTrial_rand2exp(unsigned int candidate, Vector<DIM>& trial);
void MakeTrial_best1bin(unsigned int candidate, Vector<DIM>& trial); void MakeTrial_best1bin(unsigned int candidate, Vector<DIM>& trial);
void MakeTrial_rand1bin(unsigned int candidate, Vector<DIM>& trial); void MakeTrial_rand1bin(unsigned int candidate, Vector<DIM>& trial);
void MakeTrial_randtobest1bin(unsigned int candidate, Vector<DIM>& trial); void MakeTrial_randtobest1bin(unsigned int candidate, Vector<DIM>& trial);
void MakeTrial_best2bin(unsigned int candidate, Vector<DIM>& trial); void MakeTrial_best2bin(unsigned int candidate, Vector<DIM>& trial);
void MakeTrial_rand2bin(unsigned int candidate, Vector<DIM>& trial); void MakeTrial_rand2bin(unsigned int candidate, Vector<DIM>& trial);
private: private:
static const double DEFAULTSCALE; static const double DEFAULTSCALE;
static const double DEFAULTCROSSOVER; static const double DEFAULTCROSSOVER;
static const double BIGDOUBLE; static const double BIGDOUBLE;
private: private:
Random prng; Random prng;
Vector<DIM> best; Vector<DIM> best;
Vector<DIM> minimum; Vector<DIM> minimum;
Vector<DIM> maximum; Vector<DIM> maximum;
Vector<DIM> population[POP]; Vector<DIM> population[POP];
double fitness[POP]; double fitness[POP];
double scale; double scale;
double crossover; double crossover;
double bestfitness; double bestfitness;
}; };
}; };
#include "de.inl" #include "de.inl"
#endif // __de_h__ #endif // __de_h__
+407 -407
View File
@@ -1,407 +1,407 @@
#ifndef __de_inl__ #ifndef __de_inl__
#define __de_inl__ #define __de_inl__
#include <cstdio> #include <cstdio>
#if !(defined(__de_h__)) #if !(defined(__de_h__))
# error "de.inl should only be included by de.h" # error "de.inl should only be included by de.h"
#endif #endif
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
const double DE::Engine<DIM,POP>::DEFAULTSCALE = 0.85; const double DE::Engine<DIM,POP>::DEFAULTSCALE = 0.85;
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
const double DE::Engine<DIM,POP>::DEFAULTCROSSOVER = 1.0; const double DE::Engine<DIM,POP>::DEFAULTCROSSOVER = 1.0;
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
const double DE::Engine<DIM,POP>::BIGDOUBLE = 1.0e30; const double DE::Engine<DIM,POP>::BIGDOUBLE = 1.0e30;
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline DE::Engine<DIM,POP>::Engine() inline DE::Engine<DIM,POP>::Engine()
{ {
scale = DEFAULTSCALE; scale = DEFAULTSCALE;
crossover = DEFAULTCROSSOVER; crossover = DEFAULTCROSSOVER;
for ( unsigned int i=0;i<DIM;i++ ) for ( unsigned int i=0;i<DIM;i++ )
{ {
this->minimum[i] = -BIGDOUBLE; this->minimum[i] = -BIGDOUBLE;
this->maximum[i] = -(this->minimum[i]); this->maximum[i] = -(this->minimum[i]);
} }
Reset(); Reset();
return; return;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline DE::Engine<DIM,POP>::~Engine() inline DE::Engine<DIM,POP>::~Engine()
{ {
return; return;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline void DE::Engine<DIM,POP>::Reset() inline void DE::Engine<DIM,POP>::Reset()
{ {
// reset our best // reset our best
bestfitness = BIGDOUBLE; bestfitness = BIGDOUBLE;
for ( unsigned int i=0;i<DIM;i++ ) for ( unsigned int i=0;i<DIM;i++ )
{ {
best[i] = 0.0; best[i] = 0.0;
} }
// reset all vectors // reset all vectors
for ( unsigned int i=0;i<POP;i++ ) for ( unsigned int i=0;i<POP;i++ )
{ {
// energy tied for worst // energy tied for worst
fitness[i] = bestfitness; fitness[i] = bestfitness;
// make a new individual // make a new individual
for ( unsigned int j=0;j<DIM;j++ ) for ( unsigned int j=0;j<DIM;j++ )
{ {
population[i][j] = prng.RandDouble(minimum[j],maximum[j]); population[i][j] = prng.RandDouble(minimum[j],maximum[j]);
} }
} }
return; return;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline void DE::Engine<DIM,POP>::SetRange(double minimum, double maximum) inline void DE::Engine<DIM,POP>::SetRange(double minimum, double maximum)
{ {
for (unsigned int i=0;i<DIM;i++) for (unsigned int i=0;i<DIM;i++)
{ {
this->minimum[i] = minimum; this->minimum[i] = minimum;
this->maximum[i] = maximum; this->maximum[i] = maximum;
} }
return; return;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline void DE::Engine<DIM,POP>::SetRange(const Vector<DIM>& minimum, const Vector<DIM>& maximum) inline void DE::Engine<DIM,POP>::SetRange(const Vector<DIM>& minimum, const Vector<DIM>& maximum)
{ {
this->minimum = minimum; this->minimum = minimum;
this->maximum = maximum; this->maximum = maximum;
return; return;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline const double* DE::Engine<DIM,POP>::GetBest() const inline const double* DE::Engine<DIM,POP>::GetBest() const
{ {
return best; return best;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline void DE::Engine<DIM,POP>::Select(unsigned int candidate, unsigned int *r1, unsigned int *r2, unsigned int *r3, unsigned int *r4, unsigned int *r5) inline void DE::Engine<DIM,POP>::Select(unsigned int candidate, unsigned int *r1, unsigned int *r2, unsigned int *r3, unsigned int *r4, unsigned int *r5)
{ {
if ( r1 ) if ( r1 )
{ {
do do
{ {
*r1 = prng.RandU32(0,POP-1); *r1 = prng.RandU32(0,POP-1);
} }
while ( *r1 == candidate ); while ( *r1 == candidate );
} }
if ( r2 ) if ( r2 )
{ {
do do
{ {
*r2 = prng.RandU32(0,POP-1); *r2 = prng.RandU32(0,POP-1);
} }
while ( (*r2 == candidate) || (*r2 == *r1) ); while ( (*r2 == candidate) || (*r2 == *r1) );
} }
if ( r3 ) if ( r3 )
{ {
do do
{ {
*r3 = prng.RandU32(0,POP-1); *r3 = prng.RandU32(0,POP-1);
} }
while ( (*r3 == candidate) || (*r3 == *r2) || (*r3 == *r1) ); while ( (*r3 == candidate) || (*r3 == *r2) || (*r3 == *r1) );
} }
if ( r4 ) if ( r4 )
{ {
do do
{ {
*r4 = prng.RandU32(0,POP-1); *r4 = prng.RandU32(0,POP-1);
} }
while ( (*r4 == candidate) || (*r4 == *r3) || (*r4 == *r2) || (*r4 == *r1) ); while ( (*r4 == candidate) || (*r4 == *r3) || (*r4 == *r2) || (*r4 == *r1) );
} }
if ( r5 ) if ( r5 )
{ {
do do
{ {
*r5 = prng.RandU32(0,POP-1); *r5 = prng.RandU32(0,POP-1);
} }
while ( (*r5 == candidate) || (*r5 == *r4) || (*r5 == *r3) || (*r5 == *r2) || (*r5 == *r1) ); while ( (*r5 == candidate) || (*r5 == *r4) || (*r5 == *r3) || (*r5 == *r2) || (*r5 == *r1) );
} }
return; return;
} }
// TODO: change this to run one generation at a time??? // TODO: change this to run one generation at a time???
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline bool DE::Engine<DIM,POP>::Solve(unsigned int maxgenerations) inline bool DE::Engine<DIM,POP>::Solve(unsigned int maxgenerations)
{ {
bool success = false; bool success = false;
Reset(); Reset();
for (unsigned int generation=0;generation<maxgenerations && !success;generation++) for (unsigned int generation=0;generation<maxgenerations && !success;generation++)
{ {
printf("====== GENERATION %07d =============================================\n\n",generation); printf("====== GENERATION %07d =============================================\n\n",generation);
for (int candidate=0;candidate<POP && !success;candidate++) for (int candidate=0;candidate<POP && !success;candidate++)
{ {
Vector<DIM> trial; Vector<DIM> trial;
MakeTrial_randtobest1bin(candidate,trial); MakeTrial_randtobest1bin(candidate,trial);
double trialfitness = CalculateError(trial,success); double trialfitness = CalculateError(trial,success);
if ( trialfitness < fitness[candidate] ) if ( trialfitness < fitness[candidate] )
{ {
fitness[candidate] = trialfitness; fitness[candidate] = trialfitness;
population[candidate] = trial; population[candidate] = trial;
if ( trialfitness < bestfitness ) if ( trialfitness < bestfitness )
{ {
bestfitness = trialfitness; bestfitness = trialfitness;
best = trial; best = trial;
Dump(bestfitness,best); Dump(bestfitness,best);
} }
} }
} }
} }
printf("====== RUN COMPLETE =============================================\n\n"); printf("====== RUN COMPLETE =============================================\n\n");
Dump(bestfitness,best); Dump(bestfitness,best);
return success; return success;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline void DE::Engine<DIM,POP>::Dump(const double& fitness, DE::Vector<DIM>& individual) const inline void DE::Engine<DIM,POP>::Dump(const double& fitness, DE::Vector<DIM>& individual) const
{ {
printf(" fitness = %60.30f\n",fitness); printf(" fitness = %60.30f\n",fitness);
for (unsigned int i=0;i<DIM;i++) for (unsigned int i=0;i<DIM;i++)
{ {
printf(" [%03d] = %60.30f\n",i,individual[i]); printf(" [%03d] = %60.30f\n",i,individual[i]);
} }
printf("\n"); printf("\n");
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline void DE::Engine<DIM,POP>::MakeTrial_best1exp(unsigned int candidate, DE::Vector<DIM>& trial) inline void DE::Engine<DIM,POP>::MakeTrial_best1exp(unsigned int candidate, DE::Vector<DIM>& trial)
{ {
unsigned int r1, r2; unsigned int r1, r2;
unsigned int n; unsigned int n;
Select(candidate, &r1, &r2); Select(candidate, &r1, &r2);
n = prng.RandU32(0,DIM-1); n = prng.RandU32(0,DIM-1);
trial = population[candidate]; trial = population[candidate];
for (unsigned int i=0; (prng.RandDouble(0.0,1.0) < crossover) && (i < DIM); i++) for (unsigned int i=0; (prng.RandDouble(0.0,1.0) < crossover) && (i < DIM); i++)
{ {
trial[n] = best[n] + scale * (population[r1][n] - population[r2][n]); trial[n] = best[n] + scale * (population[r1][n] - population[r2][n]);
n = (n + 1) % DIM; n = (n + 1) % DIM;
} }
return; return;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline void DE::Engine<DIM,POP>::MakeTrial_rand1exp(unsigned int candidate, DE::Vector<DIM>& trial) inline void DE::Engine<DIM,POP>::MakeTrial_rand1exp(unsigned int candidate, DE::Vector<DIM>& trial)
{ {
unsigned int r1, r2, r3; unsigned int r1, r2, r3;
unsigned int n; unsigned int n;
Select(candidate, &r1, &r2, &r3); Select(candidate, &r1, &r2, &r3);
n = prng.RandU32(0,DIM-1); n = prng.RandU32(0,DIM-1);
trial = population[candidate]; trial = population[candidate];
for (unsigned int i=0; (prng.RandDouble(0.0,1.0) <= crossover) && (i < DIM); i++) for (unsigned int i=0; (prng.RandDouble(0.0,1.0) <= crossover) && (i < DIM); i++)
{ {
trial[n] = population[r1][n] + scale * (population[r2][n] - population[r3][n]); trial[n] = population[r1][n] + scale * (population[r2][n] - population[r3][n]);
n = (n + 1) % DIM; n = (n + 1) % DIM;
} }
return; return;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline void DE::Engine<DIM,POP>::MakeTrial_randtobest1exp(unsigned int candidate, DE::Vector<DIM>& trial) inline void DE::Engine<DIM,POP>::MakeTrial_randtobest1exp(unsigned int candidate, DE::Vector<DIM>& trial)
{ {
unsigned int r1, r2; unsigned int r1, r2;
unsigned int n; unsigned int n;
Select(candidate, &r1, &r2); Select(candidate, &r1, &r2);
n = prng.RandU32(0,DIM-1); n = prng.RandU32(0,DIM-1);
trial = population[candidate]; trial = population[candidate];
for (unsigned int i=0; (prng.RandDouble(0.0,1.0) <= crossover) && (i < DIM); i++) for (unsigned int i=0; (prng.RandDouble(0.0,1.0) <= crossover) && (i < DIM); i++)
{ {
trial[n] += scale * (best[n] - trial[n]) + scale * (population[r1][n] - population[r2][n]); trial[n] += scale * (best[n] - trial[n]) + scale * (population[r1][n] - population[r2][n]);
n = (n + 1) % DIM; n = (n + 1) % DIM;
} }
return; return;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline void DE::Engine<DIM,POP>::MakeTrial_best2exp(unsigned int candidate, DE::Vector<DIM>& trial) inline void DE::Engine<DIM,POP>::MakeTrial_best2exp(unsigned int candidate, DE::Vector<DIM>& trial)
{ {
unsigned int r1, r2, r3, r4; unsigned int r1, r2, r3, r4;
unsigned int n; unsigned int n;
Select(candidate, &r1, &r2, &r3, &r4); Select(candidate, &r1, &r2, &r3, &r4);
n = prng.RandU32(0,DIM-1); n = prng.RandU32(0,DIM-1);
trial = population[candidate]; trial = population[candidate];
for (unsigned int i=0; (prng.RandDouble(0.0,1.0) <= crossover) && (i < DIM); i++) for (unsigned int i=0; (prng.RandDouble(0.0,1.0) <= crossover) && (i < DIM); i++)
{ {
trial[n] = best[n] + scale * (population[r1][n] + population[r2][n] - population[r3][n] - population[r4][n]); trial[n] = best[n] + scale * (population[r1][n] + population[r2][n] - population[r3][n] - population[r4][n]);
n = (n + 1) % DIM; n = (n + 1) % DIM;
} }
return; return;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline void DE::Engine<DIM,POP>::MakeTrial_rand2exp(unsigned int candidate, DE::Vector<DIM>& trial) inline void DE::Engine<DIM,POP>::MakeTrial_rand2exp(unsigned int candidate, DE::Vector<DIM>& trial)
{ {
unsigned int r1, r2, r3, r4, r5; unsigned int r1, r2, r3, r4, r5;
unsigned int n; unsigned int n;
Select(candidate, &r1, &r2, &r3 ,&r4, &r5); Select(candidate, &r1, &r2, &r3 ,&r4, &r5);
n = prng.RandU32(0,DIM-1); n = prng.RandU32(0,DIM-1);
trial = population[candidate]; trial = population[candidate];
for (unsigned int i=0; (prng.RandDouble(0.0,1.0) <= crossover) && (i < DIM); i++) for (unsigned int i=0; (prng.RandDouble(0.0,1.0) <= crossover) && (i < DIM); i++)
{ {
trial[n] = population[r1][n] + scale * (population[r2][n] + population[r3][n] - population[r4][n] - population[r5][n]); trial[n] = population[r1][n] + scale * (population[r2][n] + population[r3][n] - population[r4][n] - population[r5][n]);
n = (n + 1) % DIM; n = (n + 1) % DIM;
} }
return; return;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline void DE::Engine<DIM,POP>::MakeTrial_best1bin(unsigned int candidate, DE::Vector<DIM>& trial) inline void DE::Engine<DIM,POP>::MakeTrial_best1bin(unsigned int candidate, DE::Vector<DIM>& trial)
{ {
unsigned int r1, r2; unsigned int r1, r2;
unsigned int n; unsigned int n;
Select(candidate, &r1, &r2); Select(candidate, &r1, &r2);
n = prng.RandU32(0,DIM-1); n = prng.RandU32(0,DIM-1);
trial = population[candidate]; trial = population[candidate];
for (unsigned int i=0; i < DIM; i++) for (unsigned int i=0; i < DIM; i++)
{ {
if ( (prng.RandDouble(0.0,1.0) <= crossover) || (i == (DIM - 1)) ) if ( (prng.RandDouble(0.0,1.0) <= crossover) || (i == (DIM - 1)) )
{ {
trial[n] = best[n] + scale * (population[r1][n] - population[r2][n]); trial[n] = best[n] + scale * (population[r1][n] - population[r2][n]);
} }
n = (n + 1) % DIM; n = (n + 1) % DIM;
} }
return; return;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline void DE::Engine<DIM,POP>::MakeTrial_rand1bin(unsigned int candidate, DE::Vector<DIM>& trial) inline void DE::Engine<DIM,POP>::MakeTrial_rand1bin(unsigned int candidate, DE::Vector<DIM>& trial)
{ {
unsigned int r1, r2, r3; unsigned int r1, r2, r3;
unsigned int n; unsigned int n;
Select(candidate, &r1, &r2, &r3); Select(candidate, &r1, &r2, &r3);
n = prng.RandU32(0,DIM-1); n = prng.RandU32(0,DIM-1);
trial = population[candidate]; trial = population[candidate];
for (unsigned int i=0; i < DIM; i++) for (unsigned int i=0; i < DIM; i++)
{ {
if ( (prng.RandDouble(0.0,1.0) <= crossover) || (i == (DIM - 1)) ) if ( (prng.RandDouble(0.0,1.0) <= crossover) || (i == (DIM - 1)) )
{ {
trial[n] = population[r1][n] + scale * (population[r2][n] - population[r3][n]); trial[n] = population[r1][n] + scale * (population[r2][n] - population[r3][n]);
} }
n = (n + 1) % DIM; n = (n + 1) % DIM;
} }
return; return;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline void DE::Engine<DIM,POP>::MakeTrial_randtobest1bin(unsigned int candidate, DE::Vector<DIM>& trial) inline void DE::Engine<DIM,POP>::MakeTrial_randtobest1bin(unsigned int candidate, DE::Vector<DIM>& trial)
{ {
unsigned int r1, r2; unsigned int r1, r2;
unsigned int n; unsigned int n;
Select(candidate, &r1, &r2); Select(candidate, &r1, &r2);
n = prng.RandU32(0,DIM-1); n = prng.RandU32(0,DIM-1);
trial = population[candidate]; trial = population[candidate];
for (unsigned int i=0; i < DIM; i++) for (unsigned int i=0; i < DIM; i++)
{ {
if ( (prng.RandDouble(0.0,1.0) <= crossover) || (i == (DIM - 1)) ) if ( (prng.RandDouble(0.0,1.0) <= crossover) || (i == (DIM - 1)) )
{ {
trial[n] += scale * (best[n] - trial[n]) + scale * (population[r1][n] - population[r2][n]); trial[n] += scale * (best[n] - trial[n]) + scale * (population[r1][n] - population[r2][n]);
} }
n = (n + 1) % DIM; n = (n + 1) % DIM;
} }
return; return;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline void DE::Engine<DIM,POP>::MakeTrial_best2bin(unsigned int candidate, DE::Vector<DIM>& trial) inline void DE::Engine<DIM,POP>::MakeTrial_best2bin(unsigned int candidate, DE::Vector<DIM>& trial)
{ {
unsigned int r1, r2, r3, r4; unsigned int r1, r2, r3, r4;
unsigned int n; unsigned int n;
Select(candidate, &r1, &r2, &r3, &r4); Select(candidate, &r1, &r2, &r3, &r4);
n = prng.RandU32(0,DIM-1); n = prng.RandU32(0,DIM-1);
trial = population[candidate]; trial = population[candidate];
for (unsigned int i=0; i < DIM; i++) for (unsigned int i=0; i < DIM; i++)
{ {
if ( (prng.RandDouble(0.0,1.0) <= crossover) || (i == (DIM - 1)) ) if ( (prng.RandDouble(0.0,1.0) <= crossover) || (i == (DIM - 1)) )
{ {
trial[n] = best[n] + scale * (population[r1][n] + population[r2][n] - population[r3][n] - population[r4][n]); trial[n] = best[n] + scale * (population[r1][n] + population[r2][n] - population[r3][n] - population[r4][n]);
} }
n = (n + 1) % DIM; n = (n + 1) % DIM;
} }
return; return;
} }
template <unsigned int DIM, unsigned int POP> template <unsigned int DIM, unsigned int POP>
inline void DE::Engine<DIM,POP>::MakeTrial_rand2bin(unsigned int candidate, DE::Vector<DIM>& trial) inline void DE::Engine<DIM,POP>::MakeTrial_rand2bin(unsigned int candidate, DE::Vector<DIM>& trial)
{ {
unsigned int r1, r2, r3, r4, r5; unsigned int r1, r2, r3, r4, r5;
unsigned int n; unsigned int n;
Select(candidate, &r1, &r2, &r3, &r4, &r5); Select(candidate, &r1, &r2, &r3, &r4, &r5);
n = prng.RandU32(0,DIM-1); n = prng.RandU32(0,DIM-1);
trial = population[candidate]; trial = population[candidate];
for (unsigned int i=0; i < DIM; i++) for (unsigned int i=0; i < DIM; i++)
{ {
if ( (prng.RandDouble(0.0,1.0) <= crossover) || (i == (DIM - 1)) ) if ( (prng.RandDouble(0.0,1.0) <= crossover) || (i == (DIM - 1)) )
{ {
trial[n] = population[r1][n] + scale * (population[r2][n] + population[r3][n] - population[r4][n] - population[r5][n]); trial[n] = population[r1][n] + scale * (population[r2][n] + population[r3][n] - population[r4][n] - population[r5][n]);
} }
n = (n + 1) % DIM; n = (n + 1) % DIM;
} }
return; return;
} }
#endif // __de_inl__ #endif // __de_inl__
+1 -1
View File
@@ -1 +1 @@
+76 -76
View File
@@ -1,76 +1,76 @@
#ifndef __derand_h__ #ifndef __derand_h__
#define __derand_h__ #define __derand_h__
namespace DE namespace DE
{ {
typedef unsigned int u32; typedef unsigned int u32;
typedef signed int s32; typedef signed int s32;
class Random class Random
{ {
public: public:
void Seed(u32 seed); void Seed(u32 seed);
u32 Rand(); u32 Rand();
u32 operator()(); u32 operator()();
float RandFloat(); float RandFloat();
float RandFloat(float min, float max); float RandFloat(float min, float max);
double RandDouble(); double RandDouble();
double RandDouble(double min, double max); double RandDouble(double min, double max);
u32 RandU32(); u32 RandU32();
u32 RandU32(u32 min, u32 max); u32 RandU32(u32 min, u32 max);
s32 RandS32(); s32 RandS32();
s32 RandS32(s32 min, s32 max); s32 RandS32(s32 min, s32 max);
private: private:
static const u32 U32_MAX = (u32)0xffffffff; static const u32 U32_MAX = (u32)0xffffffff;
/** /**
* LCG multiplier by M. Lavaux and F. Janssens * LCG multiplier by M. Lavaux and F. Janssens
*/ */
static const u32 MULTIPLIER = 1664525UL; static const u32 MULTIPLIER = 1664525UL;
/** /**
* LCG increment by M. Lavaux and F. Janssens * LCG increment by M. Lavaux and F. Janssens
*/ */
static const u32 INCREMENT = 1013904223UL; static const u32 INCREMENT = 1013904223UL;
/** /**
* LCG modulus set to 2^32, but not used due to MODULUS_ASSUMED * LCG modulus set to 2^32, but not used due to MODULUS_ASSUMED
* above (since we're using a 32 bit word here) * above (since we're using a 32 bit word here)
* *
* Set this to 0 if the modulus falls out automatically * Set this to 0 if the modulus falls out automatically
* due to your data type (i.e. "addition modulo". For * due to your data type (i.e. "addition modulo". For
* instance, if you're using a 32 bit integer, and your * instance, if you're using a 32 bit integer, and your
* modulus is 2^32, then there's no need to perform the * modulus is 2^32, then there's no need to perform the
* modulo since a == a % (2^32). (so the modulo is "free") * modulo since a == a % (2^32). (so the modulo is "free")
* *
*/ */
static const u32 MODULUS = 0xffffffff; static const u32 MODULUS = 0xffffffff;
/** /**
* LCG state * LCG state
*/ */
u32 seed; u32 seed;
}; };
}; };
#include "derand.inl" #include "derand.inl"
#endif // __derand_h__ #endif // __derand_h__
+69 -69
View File
@@ -1,69 +1,69 @@
#ifndef __derand_inl__ #ifndef __derand_inl__
#define __derand_inl__ #define __derand_inl__
#if !(defined(__derand_h__)) #if !(defined(__derand_h__))
# error "derand.inl should only be included by derand.h" # error "derand.inl should only be included by derand.h"
#endif #endif
inline void DE::Random::Seed(u32 seed) inline void DE::Random::Seed(u32 seed)
{ {
this->seed = seed; this->seed = seed;
} }
inline DE::u32 DE::Random::Rand() inline DE::u32 DE::Random::Rand()
{ {
seed = ( (seed * DE::Random::MULTIPLIER) + DE::Random::INCREMENT ); seed = ( (seed * DE::Random::MULTIPLIER) + DE::Random::INCREMENT );
seed %= DE::Random::MODULUS; seed %= DE::Random::MODULUS;
return seed; return seed;
} }
inline DE::u32 DE::Random::operator()() inline DE::u32 DE::Random::operator()()
{ {
return this->Rand(); return this->Rand();
} }
inline float DE::Random::RandFloat() inline float DE::Random::RandFloat()
{ {
return (float)Rand() / (float)DE::Random::U32_MAX; return (float)Rand() / (float)DE::Random::U32_MAX;
} }
inline float DE::Random::RandFloat(float min, float max) inline float DE::Random::RandFloat(float min, float max)
{ {
return ( RandFloat() * (max - min) + min ); return ( RandFloat() * (max - min) + min );
} }
inline double DE::Random::RandDouble() inline double DE::Random::RandDouble()
{ {
return (double)Rand() / (double)DE::Random::U32_MAX; return (double)Rand() / (double)DE::Random::U32_MAX;
} }
inline double DE::Random::RandDouble(double min, double max) inline double DE::Random::RandDouble(double min, double max)
{ {
return ( RandDouble() * (max - min) + min ); return ( RandDouble() * (max - min) + min );
} }
inline DE::u32 DE::Random::RandU32() inline DE::u32 DE::Random::RandU32()
{ {
return Rand(); return Rand();
} }
inline DE::u32 DE::Random::RandU32(DE::u32 min, DE::u32 max) inline DE::u32 DE::Random::RandU32(DE::u32 min, DE::u32 max)
{ {
return ( ( Rand() % (max - min) ) + min ); return ( ( Rand() % (max - min) ) + min );
} }
inline DE::s32 DE::Random::RandS32() inline DE::s32 DE::Random::RandS32()
{ {
return (DE::s32)Rand(); return (DE::s32)Rand();
} }
inline DE::s32 DE::Random::RandS32(DE::s32 min, DE::s32 max) inline DE::s32 DE::Random::RandS32(DE::s32 min, DE::s32 max)
{ {
return (DE::s32)( ( Rand() % (max - min) ) + min ); return (DE::s32)( ( Rand() % (max - min) ) + min );
} }
#endif // __derand_inl__ #endif // __derand_inl__
+63 -63
View File
@@ -1,63 +1,63 @@
#ifndef __devec_h__ #ifndef __devec_h__
#define __devec_h__ #define __devec_h__
namespace DE namespace DE
{ {
template <unsigned int DIM> template <unsigned int DIM>
class Vector class Vector
{ {
public: public:
// default constructor // default constructor
Vector(); Vector();
// destructor // destructor
~Vector(); ~Vector();
// copy constructor // copy constructor
Vector(const Vector& other); Vector(const Vector& other);
// assignment // assignment
Vector& operator= (const Vector& other); Vector& operator= (const Vector& other);
// addition // addition
Vector& operator+= (const Vector& other); Vector& operator+= (const Vector& other);
Vector operator+ (const Vector& other); Vector operator+ (const Vector& other);
// subtraction // subtraction
Vector& operator-= (const Vector& other); Vector& operator-= (const Vector& other);
Vector operator- (const Vector& other); Vector operator- (const Vector& other);
// multiply by scalar // multiply by scalar
Vector& operator*= (const double& multiplier); Vector& operator*= (const double& multiplier);
Vector operator* (const double& multiplier); Vector operator* (const double& multiplier);
// divide by scalar // divide by scalar
Vector& operator/= (const double& divisor); Vector& operator/= (const double& divisor);
Vector operator/ (const double& divisor); Vector operator/ (const double& divisor);
// element access // element access
const double& operator[] (unsigned int i) const; const double& operator[] (unsigned int i) const;
double& operator[] (unsigned int i); double& operator[] (unsigned int i);
// data access // data access
operator const double* () const; operator const double* () const;
operator double* (); operator double* ();
private: private:
// data lives here // data lives here
double tuple[DIM]; double tuple[DIM];
}; };
}; };
#include "devec.inl" #include "devec.inl"
#endif // __devec_h__ #endif // __devec_h__
+142 -142
View File
@@ -1,142 +1,142 @@
#ifndef __devec_inl__ #ifndef __devec_inl__
#define __devec_inl__ #define __devec_inl__
#if !(defined(__devec_h__)) #if !(defined(__devec_h__))
# error "devec.inl should only be included by devec.h" # error "devec.inl should only be included by devec.h"
#endif #endif
template <unsigned int DIM> template <unsigned int DIM>
inline DE::Vector<DIM>::Vector() inline DE::Vector<DIM>::Vector()
{ {
for (unsigned int i=0;i<DIM;i++) for (unsigned int i=0;i<DIM;i++)
{ {
tuple[i] = 0.0; tuple[i] = 0.0;
} }
return; return;
} }
template <unsigned int DIM> template <unsigned int DIM>
inline DE::Vector<DIM>::~Vector() inline DE::Vector<DIM>::~Vector()
{ {
// empty // empty
return; return;
} }
template <unsigned int DIM> template <unsigned int DIM>
inline DE::Vector<DIM>::Vector(const Vector& other) inline DE::Vector<DIM>::Vector(const Vector& other)
{ {
*this = other; *this = other;
return; return;
} }
template <unsigned int DIM> template <unsigned int DIM>
inline DE::Vector<DIM>& DE::Vector<DIM>::operator= (const Vector& other) inline DE::Vector<DIM>& DE::Vector<DIM>::operator= (const Vector& other)
{ {
for (unsigned int i=0;i<DIM;i++) for (unsigned int i=0;i<DIM;i++)
{ {
tuple[i] = other.tuple[i]; tuple[i] = other.tuple[i];
} }
return *this; return *this;
} }
template <unsigned int DIM> template <unsigned int DIM>
inline DE::Vector<DIM>& DE::Vector<DIM>::operator+= (const Vector& other) inline DE::Vector<DIM>& DE::Vector<DIM>::operator+= (const Vector& other)
{ {
for (unsigned int i=0;i<DIM;i++) for (unsigned int i=0;i<DIM;i++)
{ {
tuple[i] += other.tuple[i]; tuple[i] += other.tuple[i];
} }
return *this; return *this;
} }
template <unsigned int DIM> template <unsigned int DIM>
inline DE::Vector<DIM> DE::Vector<DIM>::operator+ (const Vector& other) inline DE::Vector<DIM> DE::Vector<DIM>::operator+ (const Vector& other)
{ {
Vector tmp(*this); Vector tmp(*this);
tmp += other; tmp += other;
return tmp; return tmp;
} }
template <unsigned int DIM> template <unsigned int DIM>
inline DE::Vector<DIM>& DE::Vector<DIM>::operator-= (const Vector& other) inline DE::Vector<DIM>& DE::Vector<DIM>::operator-= (const Vector& other)
{ {
for (unsigned int i=0;i<DIM;i++) for (unsigned int i=0;i<DIM;i++)
{ {
tuple[i] -= other.tuple[i]; tuple[i] -= other.tuple[i];
} }
return *this; return *this;
} }
template <unsigned int DIM> template <unsigned int DIM>
inline DE::Vector<DIM> DE::Vector<DIM>::operator- (const Vector& other) inline DE::Vector<DIM> DE::Vector<DIM>::operator- (const Vector& other)
{ {
Vector tmp(*this); Vector tmp(*this);
tmp -= other; tmp -= other;
return tmp; return tmp;
} }
template <unsigned int DIM> template <unsigned int DIM>
inline DE::Vector<DIM>& DE::Vector<DIM>::operator*= (const double& multiplier) inline DE::Vector<DIM>& DE::Vector<DIM>::operator*= (const double& multiplier)
{ {
for (unsigned int i=0;i<DIM;i++) for (unsigned int i=0;i<DIM;i++)
{ {
tuple[i] *= multiplier; tuple[i] *= multiplier;
} }
return *this; return *this;
} }
template <unsigned int DIM> template <unsigned int DIM>
inline DE::Vector<DIM> DE::Vector<DIM>::operator* (const double& multiplier) inline DE::Vector<DIM> DE::Vector<DIM>::operator* (const double& multiplier)
{ {
Vector tmp(*this); Vector tmp(*this);
tmp *= multiplier; tmp *= multiplier;
return tmp; return tmp;
} }
template <unsigned int DIM> template <unsigned int DIM>
inline DE::Vector<DIM>& DE::Vector<DIM>::operator/= (const double& divisor) inline DE::Vector<DIM>& DE::Vector<DIM>::operator/= (const double& divisor)
{ {
for (unsigned int i=0;i<DIM;i++) for (unsigned int i=0;i<DIM;i++)
{ {
tuple[i] /= divisor; tuple[i] /= divisor;
} }
return *this; return *this;
} }
template <unsigned int DIM> template <unsigned int DIM>
inline DE::Vector<DIM> DE::Vector<DIM>::operator/ (const double& divisor) inline DE::Vector<DIM> DE::Vector<DIM>::operator/ (const double& divisor)
{ {
Vector tmp(*this); Vector tmp(*this);
tmp /= divisor; tmp /= divisor;
return tmp; return tmp;
} }
template <unsigned int DIM> template <unsigned int DIM>
inline const double& DE::Vector<DIM>::operator[] (unsigned int i) const inline const double& DE::Vector<DIM>::operator[] (unsigned int i) const
{ {
return tuple[i]; return tuple[i];
} }
template <unsigned int DIM> template <unsigned int DIM>
inline double& DE::Vector<DIM>::operator[] (unsigned int i) inline double& DE::Vector<DIM>::operator[] (unsigned int i)
{ {
return tuple[i]; return tuple[i];
} }
template <unsigned int DIM> template <unsigned int DIM>
inline DE::Vector<DIM>::operator const double* () const inline DE::Vector<DIM>::operator const double* () const
{ {
return tuple; return tuple;
} }
template <unsigned int DIM> template <unsigned int DIM>
inline DE::Vector<DIM>::operator double* () inline DE::Vector<DIM>::operator double* ()
{ {
return tuple; return tuple;
} }
#endif // __devec_inl__ #endif // __devec_inl__