From 8f42508ff163cdf06c3fcc815a053da57ae20d42 Mon Sep 17 00:00:00 2001 From: Francesco Biscani Date: Thu, 18 Dec 2008 16:32:42 +0100 Subject: [PATCH] Use Mersenne twister from Boost as uint32_t random generator. --- Functions/rng/PkRandom.h | 10 ++++++--- GOclasses/algorithms/ASA.cpp | 8 ++++---- GOclasses/algorithms/ASA.h | 6 +++--- GOclasses/algorithms/DE.cpp | 4 ++-- GOclasses/algorithms/DE.h | 4 ++-- GOclasses/algorithms/MPSO.cpp | 4 ++-- GOclasses/algorithms/MPSO.h | 4 ++-- GOclasses/algorithms/PSO.cpp | 4 ++-- GOclasses/algorithms/PSO.h | 4 ++-- GOclasses/algorithms/SGA.cpp | 8 ++++---- GOclasses/algorithms/SGA.h | 4 ++-- GOclasses/basic/individual.cpp | 4 ++-- GOclasses/basic/individual.h | 4 ++-- GOclasses/basic/population.cpp | 6 +++--- GOclasses/basic/population.h | 7 ++++--- SolversThreads/SolversThreads.cpp | 32 ++++++++++++++--------------- SolversThreads/SolversThreads.h | 2 +- main.cpp | 43 ++++++++++++++++++++------------------- 18 files changed, 82 insertions(+), 76 deletions(-) diff --git a/Functions/rng/PkRandom.h b/Functions/rng/PkRandom.h index 4c5c046..d439602 100644 --- a/Functions/rng/PkRandom.h +++ b/Functions/rng/PkRandom.h @@ -67,7 +67,11 @@ #ifndef PKRANDOM_H #define PKRANDOM_H -#include +#include + +#include + +typedef boost::mt19937 rng_type; // Mersenne Twister @@ -101,8 +105,8 @@ typedef Random Random64; // Added and implemented by MaRu. // This is the algorithm used in Java Standard Library, but I'm not convinced if it's the correct way to do it. // IMHO The correct way should be based on IEEE 754 floating point number encoding standard. -inline double nextDouble(Random32& rng) { - unsigned long long l = ((((unsigned long long)rng.next()) & 0x03FFFFFFLL) << 27) | (((unsigned long long)rng.next()) & 0x7FFFFFF); +inline double nextDouble(rng_type &rng) { + unsigned long long l = ((((unsigned long long)rng()) & 0x03FFFFFFLL) << 27) | (((unsigned long long)rng()) & 0x7FFFFFF); return l / (double)(1LL << 53); } diff --git a/GOclasses/algorithms/ASA.cpp b/GOclasses/algorithms/ASA.cpp index 4fa85c3..31fca04 100644 --- a/GOclasses/algorithms/ASA.cpp +++ b/GOclasses/algorithms/ASA.cpp @@ -112,7 +112,7 @@ Population ASAalgorithm::evolve(Individual x0, GOProblem& problem) { return newpop; } - void ASAalgorithm::initASA(int niterTotInit, int niterTempInit, int niterRangeInit, int SolDimInit, double T0Init, double TcoeffInit, double StartStepInit, unsigned long randomSeed){ + void ASAalgorithm::initASA(int niterTotInit, int niterTempInit, int niterRangeInit, int SolDimInit, double T0Init, double TcoeffInit, double StartStepInit, uint32_t randomSeed){ niterTot=niterTotInit; niterTemp=niterTempInit; niterRange=niterRangeInit; @@ -121,10 +121,10 @@ Population ASAalgorithm::evolve(Individual x0, GOProblem& problem) { Tcoeff=TcoeffInit; StartStep=StartStepInit; niterOuter = niterTot / (niterTemp * niterRange * SolDim); - rng = Pk::Random32(randomSeed); + rng.seed(randomSeed); } - void ASAalgorithm::initASA(int niterTotInit,int SolDimInit, double Ts, double Tf, unsigned long randomSeed){ + void ASAalgorithm::initASA(int niterTotInit,int SolDimInit, double Ts, double Tf, uint32_t randomSeed){ niterTot=niterTotInit; niterTemp=1; niterRange=20; @@ -133,5 +133,5 @@ Population ASAalgorithm::evolve(Individual x0, GOProblem& problem) { T0=Ts; Tcoeff=pow(Tf/Ts,1.0/(double)(niterOuter)); StartStep=1; - rng = Pk::Random32(randomSeed); + rng.seed(randomSeed); } diff --git a/GOclasses/algorithms/ASA.h b/GOclasses/algorithms/ASA.h index 028bbd3..0ac9c61 100644 --- a/GOclasses/algorithms/ASA.h +++ b/GOclasses/algorithms/ASA.h @@ -31,7 +31,7 @@ public: double T0Init, double TcoeffInit, double StartStepInit, - unsigned long randomSeed); + uint32_t randomSeed); //This method initialise the SA-AN algorithm starting and final temperature setting deafult values for //the StartStep, the niterTemp and the niterRange. Tcoeff is evaluated accordingly @@ -39,7 +39,7 @@ public: int SolDimInit, double Ts, double Tf, - unsigned long randomSeed); + uint32_t randomSeed); private: int niterTot; @@ -50,7 +50,7 @@ private: double Tcoeff; double StartStep; int niterOuter; - Pk::Random32 rng; + rng_type rng; }; #endif diff --git a/GOclasses/algorithms/DE.cpp b/GOclasses/algorithms/DE.cpp index eca4e88..8a053cb 100644 --- a/GOclasses/algorithms/DE.cpp +++ b/GOclasses/algorithms/DE.cpp @@ -15,13 +15,13 @@ using namespace std; -void DEalgorithm::initDE(int generationsInit, int SolDimInit, double FInit, double CRInit, int strategyInit, unsigned long randomSeed){ +void DEalgorithm::initDE(int generationsInit, int SolDimInit, double FInit, double CRInit, int strategyInit, uint32_t randomSeed){ generations = generationsInit; strategy = strategyInit; F = FInit; CR = CRInit; SolDim = SolDimInit; - rng = Pk::Random32(randomSeed); + rng.seed(randomSeed); } Population DEalgorithm::evolve(Population deme, GOProblem& problem){ diff --git a/GOclasses/algorithms/DE.h b/GOclasses/algorithms/DE.h index f8b3824..d35dc18 100644 --- a/GOclasses/algorithms/DE.h +++ b/GOclasses/algorithms/DE.h @@ -26,7 +26,7 @@ public: double FInit, double CRInit, int strategyInit, - unsigned long randomSeed); + uint32_t randomSeed); private: int generations; @@ -34,7 +34,7 @@ private: double F; double CR; int strategy; - Pk::Random32 rng; + rng_type rng; }; #endif diff --git a/GOclasses/algorithms/MPSO.cpp b/GOclasses/algorithms/MPSO.cpp index 5cc8577..bf1b40f 100644 --- a/GOclasses/algorithms/MPSO.cpp +++ b/GOclasses/algorithms/MPSO.cpp @@ -12,7 +12,7 @@ using namespace std; -void MPSOalgorithm::initMPSO(int generationsInit, int SolDimInit, double omegaInit, double eta1Init, double eta2Init,double vcoeffInit, int nswarmsInit, unsigned long randomSeed){ +void MPSOalgorithm::initMPSO(int generationsInit, int SolDimInit, double omegaInit, double eta1Init, double eta2Init,double vcoeffInit, int nswarmsInit, uint32_t randomSeed){ generations = generationsInit; SolDim = SolDimInit; omega = omegaInit; @@ -20,7 +20,7 @@ void MPSOalgorithm::initMPSO(int generationsInit, int SolDimInit, double omegaIn eta2 = eta2Init; vcoeff = vcoeffInit; nswarms = nswarmsInit; - rng = Pk::Random32(randomSeed); + rng.seed(randomSeed); } Population MPSOalgorithm::evolve(Population deme, GOProblem& problem){ diff --git a/GOclasses/algorithms/MPSO.h b/GOclasses/algorithms/MPSO.h index abb62fa..09cef26 100644 --- a/GOclasses/algorithms/MPSO.h +++ b/GOclasses/algorithms/MPSO.h @@ -28,7 +28,7 @@ void initMPSO(int generationsInit, double eta2Init, double vcoeffInit, int nswarmsInit, - unsigned long randomSeed); + uint32_t randomSeed); private: int generations; @@ -38,7 +38,7 @@ private: double eta2; double vcoeff; int nswarms; - Pk::Random32 rng; + rng_type rng; }; #endif diff --git a/GOclasses/algorithms/PSO.cpp b/GOclasses/algorithms/PSO.cpp index 2a5243c..f943b40 100644 --- a/GOclasses/algorithms/PSO.cpp +++ b/GOclasses/algorithms/PSO.cpp @@ -12,14 +12,14 @@ using namespace std; -void PSOalgorithm::initPSO(int generationsInit, int SolDimInit, double omegaInit, double eta1Init, double eta2Init,double vcoeffInit, unsigned long randomSeed){ +void PSOalgorithm::initPSO(int generationsInit, int SolDimInit, double omegaInit, double eta1Init, double eta2Init,double vcoeffInit, uint32_t randomSeed){ generations = generationsInit; SolDim = SolDimInit; omega = omegaInit; eta1 = eta1Init; eta2 = eta2Init; vcoeff = vcoeffInit; - rng = Pk::Random32(randomSeed); + rng.seed(randomSeed); } Population PSOalgorithm::evolve(Population deme, GOProblem& problem){ diff --git a/GOclasses/algorithms/PSO.h b/GOclasses/algorithms/PSO.h index 38b4811..d20c024 100644 --- a/GOclasses/algorithms/PSO.h +++ b/GOclasses/algorithms/PSO.h @@ -26,7 +26,7 @@ void initPSO(int generationsInit, double eta1Init, double eta2Init, double vcoeffInit, - unsigned long randomSeed); + uint32_t randomSeed); private: int generations; @@ -35,7 +35,7 @@ private: double eta1; double eta2; double vcoeff; - Pk::Random32 rng; + rng_type rng; }; #endif diff --git a/GOclasses/algorithms/SGA.cpp b/GOclasses/algorithms/SGA.cpp index d4f78e0..fc4be91 100644 --- a/GOclasses/algorithms/SGA.cpp +++ b/GOclasses/algorithms/SGA.cpp @@ -12,13 +12,13 @@ using namespace std; -void SGAalgorithm::initSGA(int generationsInit, int SolDimInit, double CRInit, double MInit, int insert_bestInit, unsigned long randomSeed){ +void SGAalgorithm::initSGA(int generationsInit, int SolDimInit, double CRInit, double MInit, int insert_bestInit, uint32_t randomSeed){ generations = generationsInit; SolDim = SolDimInit; CR = CRInit; M = MInit; insert_best = insert_bestInit; - rng = Pk::Random32(randomSeed); + rng.seed(randomSeed); } Population SGAalgorithm::evolve(Population deme, GOProblem& problem){ @@ -116,11 +116,11 @@ Population SGAalgorithm::evolve(Population deme, GOProblem& problem){ member1 = Xnew[i]; //we select a mating patner (different from the self (i.e. no masturbation)) do { //FIXME: [MaRu] YOU DON'T DO IT THAT WAY, MAN!!! - r1 = rng.next() % NP; + r1 = rng() % NP; } while ( r1 == i ); member2 = Xnew[r1]; //and we operate crossover - n = rng.next() % D; + n = rng() % D; L = 0; do { member1[n] = member2[n]; diff --git a/GOclasses/algorithms/SGA.h b/GOclasses/algorithms/SGA.h index 5c989c3..b8d3ca3 100644 --- a/GOclasses/algorithms/SGA.h +++ b/GOclasses/algorithms/SGA.h @@ -26,7 +26,7 @@ void initSGA(int generationsInit, double CRInit, double MInit, int insert_bestInit, - unsigned long randomSeed + uint32_t randomSeed ); private: @@ -35,7 +35,7 @@ private: double CR; //crossover double M; //mutation int insert_best; - Pk::Random32 rng; + rng_type rng; }; #endif diff --git a/GOclasses/basic/individual.cpp b/GOclasses/basic/individual.cpp index 454b65c..a3adc62 100644 --- a/GOclasses/basic/individual.cpp +++ b/GOclasses/basic/individual.cpp @@ -10,7 +10,7 @@ #include "individual.h" #include "PkRandom.h" - void Individual::createRandomIndividual(std::vector LB, std::vector UB, Pk::Random32& rng){ + void Individual::createRandomIndividual(std::vector LB, std::vector UB, rng_type &rng){ //We first delete the vector content if any x.clear(); @@ -29,7 +29,7 @@ } };//createRandomIndividual - void Individual::resetVelocity(std::vector LB, std::vector UB, Pk::Random32& rng){ + void Individual::resetVelocity(std::vector LB, std::vector UB, rng_type &rng){ v.clear(); double dummy; for (unsigned int i=0; i < LB.size(); i++){ diff --git a/GOclasses/basic/individual.h b/GOclasses/basic/individual.h index ea0e63a..c5c39a8 100644 --- a/GOclasses/basic/individual.h +++ b/GOclasses/basic/individual.h @@ -20,7 +20,7 @@ class Individual{ public: //methods - void createRandomIndividual(std::vector LB, std::vector UB, Pk::Random32& rng); + void createRandomIndividual(std::vector LB, std::vector UB, rng_type &rng); double evaluateFitness(GOProblem&); double getFitness() const; void setFitness(double fitnessnew); @@ -28,7 +28,7 @@ public: void setDecisionVector(std::vector xnew); std::vector getVelocity() const; void setVelocity(std::vector xnew); - void resetVelocity(std::vector LB, std::vector UB, Pk::Random32& rng); + void resetVelocity(std::vector LB, std::vector UB, rng_type &rng); //operators double& operator[](int index); diff --git a/GOclasses/basic/population.cpp b/GOclasses/basic/population.cpp index f1a1f93..48da43b 100644 --- a/GOclasses/basic/population.cpp +++ b/GOclasses/basic/population.cpp @@ -11,7 +11,7 @@ - void Population::createRandomPopulation(std::vector LB, std::vector UB, int N, Pk::Random32& rng){ + void Population::createRandomPopulation(std::vector LB, std::vector UB, int N, rng_type &rng){ Individual x; pop.clear(); @@ -21,7 +21,7 @@ }//for };//createRandomPopulation - void Population::resetVelocities(std::vector LB, std::vector UB, Pk::Random32& rng){ + void Population::resetVelocities(std::vector LB, std::vector UB, rng_type &rng){ for (unsigned int j=0 ;j &picks, Pk::Random32& rng){ + Population Population::extractRandomDeme(int N, std::vector &picks, rng_type &rng){ Population deme; std::vector PossiblePicks; int Pick; diff --git a/GOclasses/basic/population.h b/GOclasses/basic/population.h index 183a27d..ab15cde 100644 --- a/GOclasses/basic/population.h +++ b/GOclasses/basic/population.h @@ -19,8 +19,9 @@ class Population{ public: //Methods - void createRandomPopulation(std::vector LB, std::vector UB, int N, Pk::Random32& rng); - void resetVelocities(std::vector LB, std::vector UB, Pk::Random32& rng); + // TODO: pass by reference here, why the copies? + void createRandomPopulation(std::vector LB, std::vector UB, int N, rng_type &rng); + void resetVelocities(std::vector LB, std::vector UB, rng_type &rng); void evaluatePopulation(GOProblem&); void addIndividual(Individual x); void substituteIndividual(const Individual x, const int n); @@ -29,7 +30,7 @@ public: unsigned int size (); Individual extractBestIndividual(); Individual extractWorstIndividual(); - Population extractRandomDeme(int N, std::vector &picks, Pk::Random32& rng); + Population extractRandomDeme(int N, std::vector &picks, rng_type &rng); void insertDeme(Population deme, std::vector picks); void insertBestInDeme(Population deme, std::vector picks); void insertDemeForced(Population deme, std::vector picks); diff --git a/SolversThreads/SolversThreads.cpp b/SolversThreads/SolversThreads.cpp index 9aaa17d..6aa7104 100644 --- a/SolversThreads/SolversThreads.cpp +++ b/SolversThreads/SolversThreads.cpp @@ -39,7 +39,7 @@ void *DEthread(void *data) GOProblem* problem; vector LB,UB; DEalgorithm DE; - Pk::Random32 rng; + rng_type rng; clock_t start,end; @@ -47,11 +47,11 @@ void *DEthread(void *data) { lock_type lock(*PtrTP->TPmutex); - rng = Pk::Random32(PtrTP->randomSeed); + rng.seed(PtrTP->randomSeed); deme = PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, rng); problem = PtrTP->problem; problem->getBounds(LB, UB); - DE.initDE(PtrTP->generations,LB.size(),PtrTP->F,PtrTP->CR,PtrTP->strategy, rng.next()); + DE.initDE(PtrTP->generations,LB.size(),PtrTP->F,PtrTP->CR,PtrTP->strategy, rng()); } oldfitness = deme.extractBestIndividual().getFitness(); @@ -99,18 +99,18 @@ void *MPSOthread(void *data) GOProblem* problem; vector LB, UB; MPSOalgorithm MPSO; - Pk::Random32 rng; + rng_type rng; clock_t start,end; double dif; { lock_type lock(*PtrTP->TPmutex); - rng = Pk::Random32(PtrTP->randomSeed); + rng.seed(PtrTP->randomSeed); deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, rng); problem = PtrTP->problem; problem->getBounds(LB, UB); - MPSO.initMPSO(PtrTP->generations,LB.size(),PtrTP->omega,PtrTP->eta1,PtrTP->eta2,PtrTP->vcoeff, PtrTP->nswarms, rng.next()); + MPSO.initMPSO(PtrTP->generations,LB.size(),PtrTP->omega,PtrTP->eta1,PtrTP->eta2,PtrTP->vcoeff, PtrTP->nswarms, rng()); } oldfitness = deme.extractBestIndividual().getFitness(); @@ -154,18 +154,18 @@ void *PSOthread(void *data) GOProblem* problem; vector LB,UB; PSOalgorithm PSO; - Pk::Random32 rng; + rng_type rng; clock_t start,end; double dif; { lock_type lock(*PtrTP->TPmutex); - rng = Pk::Random32(PtrTP->randomSeed); + rng.seed(PtrTP->randomSeed); deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, rng); problem = PtrTP->problem; problem->getBounds(LB, UB); - PSO.initPSO(PtrTP->generations,LB.size(),PtrTP->omega,PtrTP->eta1,PtrTP->eta2,PtrTP->vcoeff, rng.next()); + PSO.initPSO(PtrTP->generations,LB.size(),PtrTP->omega,PtrTP->eta1,PtrTP->eta2,PtrTP->vcoeff, rng()); } oldfitness = deme.extractBestIndividual().getFitness(); @@ -210,7 +210,7 @@ void *SGAthread(void *data) vector picks; vector LB,UB; SGAalgorithm SGA; - Pk::Random32 rng; + rng_type rng; GOProblem* problem; clock_t start,end; @@ -218,11 +218,11 @@ void *SGAthread(void *data) { lock_type lock(*PtrTP->TPmutex); - rng = Pk::Random32(PtrTP->randomSeed); + rng.seed(PtrTP->randomSeed); deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, rng); problem = PtrTP->problem; problem->getBounds(LB, UB); - SGA.initSGA(PtrTP->generations,LB.size(),PtrTP->CRsga,PtrTP->M,PtrTP->insert_best, rng.next()); + SGA.initSGA(PtrTP->generations,LB.size(),PtrTP->CRsga,PtrTP->M,PtrTP->insert_best, rng()); } oldfitness = deme.extractBestIndividual().getFitness(); @@ -266,7 +266,7 @@ void *ASAthread(void *data) vector picks; vector LB,UB; ASAalgorithm ASA; - Pk::Random32 rng; + rng_type rng; GOProblem* problem; @@ -275,13 +275,13 @@ void *ASAthread(void *data) { lock_type lock(*PtrTP->TPmutex); - rng = Pk::Random32(PtrTP->randomSeed); + rng.seed(PtrTP->randomSeed); deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, rng); problem = PtrTP->problem; problem->getBounds(LB, UB); unsigned int temp; - temp = rng.next(); - ASA.initASA(PtrTP->generations,LB.size(),PtrTP->Ts,PtrTP->Tf, rng.next()); + temp = rng(); + ASA.initASA(PtrTP->generations,LB.size(),PtrTP->Ts,PtrTP->Tf, rng()); } oldfitness = deme.extractBestIndividual().getFitness(); diff --git a/SolversThreads/SolversThreads.h b/SolversThreads/SolversThreads.h index 8973109..ba653d7 100644 --- a/SolversThreads/SolversThreads.h +++ b/SolversThreads/SolversThreads.h @@ -46,7 +46,7 @@ struct threadParam{ boost::condition_variable *exit; Population *Ptr_pop; std::ofstream *Ptr_log; - unsigned long randomSeed; + uint32_t randomSeed; }; //Here we define the protoypes for each type of thread we may want to open diff --git a/main.cpp b/main.cpp index fa77e2d..4940d04 100644 --- a/main.cpp +++ b/main.cpp @@ -21,7 +21,9 @@ using namespace std; +// Useful typedefs. typedef boost::unique_lock lock_type; +typedef messengerfullProb problem_type; int main(){ @@ -31,10 +33,9 @@ int main(){ //We prepare the pseudorandom sequence (TODO: check the randomnumbers of different threads are different) - Pk::Random32 rng(time(0)); + rng_type rng(time(0)); //we set the problem - typedef messengerfullProb problem_type; problem_type problem; //we extract its information into local variables const vector& LB = problem.getLB(); @@ -85,8 +86,8 @@ while (choice != -1) { //Instanciate the algorithm //Adaptive Simulated Annealing ASAalgorithm ASA; - //ASA.initASA(niterTot,niterTemp,niterRange,LB.size(),T0,Tcoeff,StartStep, rng.next()); - ASA.initASA(niterTot,LB.size(),T0,Tf, rng.next()); + //ASA.initASA(niterTot,niterTemp,niterRange,LB.size(),T0,Tcoeff,StartStep, rng()); + ASA.initASA(niterTot,LB.size(),T0,Tf, rng()); //Pruned bounds @@ -105,7 +106,7 @@ while (choice != -1) { //we evolve it start1=clock(); if (pop.extractBestIndividual().getFitness() < 5){ - ASA.initASA(niterTot,LB.size(),1,0.01, rng.next()); + ASA.initASA(niterTot,LB.size(),1,0.01, rng()); } pop = ASA.evolve(pop[0],problem); end1=clock(); @@ -175,7 +176,7 @@ while (choice != -1) { //Instanciate the algorithm PSOalgorithm PSO; - PSO.initPSO(gen,LB.size(),omega,eta1,eta2,vcoeff, rng.next()); + PSO.initPSO(gen,LB.size(),omega,eta1,eta2,vcoeff, rng()); for (int i=0;i