From 5bcb8e319798562fdf2b86936d306d34bee6bc5f Mon Sep 17 00:00:00 2001 From: Francesco Biscani Date: Fri, 19 Dec 2008 11:54:55 +0100 Subject: [PATCH] Removed last uses of Pk random generators, removed unused files and introduced the rng.h header. --- CMakeLists.txt | 1 - Functions/rng/PkRandom.cpp | 131 -------------------------------------- Functions/rng/PkRandom.h | 122 ----------------------------------- Functions/rng/PkRandom.inl | 88 ------------------------- Functions/rng/rng.h | 12 ++++ GOclasses/algorithms/ASA.cpp | 5 +- GOclasses/algorithms/ASA.h | 8 ++- GOclasses/algorithms/DE.cpp | 5 +- GOclasses/algorithms/DE.h | 7 +- GOclasses/algorithms/MPSO.cpp | 3 +- GOclasses/algorithms/MPSO.h | 10 +-- GOclasses/algorithms/PSO.cpp | 3 +- GOclasses/algorithms/PSO.h | 7 +- GOclasses/algorithms/SGA.cpp | 3 +- GOclasses/algorithms/SGA.h | 9 +-- GOclasses/basic/individual.cpp | 12 ++-- GOclasses/basic/individual.h | 9 +-- GOclasses/basic/population.cpp | 17 ++--- GOclasses/basic/population.h | 11 ++-- SolversThreads/SolversThreads.cpp | 30 ++++++--- main.cpp | 17 ++--- 21 files changed, 103 insertions(+), 407 deletions(-) delete mode 100644 Functions/rng/PkRandom.cpp delete mode 100644 Functions/rng/PkRandom.h delete mode 100644 Functions/rng/PkRandom.inl create mode 100644 Functions/rng/rng.h diff --git a/CMakeLists.txt b/CMakeLists.txt index bb78f1c..4975e43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,7 +37,6 @@ SET(PAGMO_LIB_SRC_LIST ${CMAKE_SOURCE_DIR}/AstroToolbox/PowSwingByInv.cpp ${CMAKE_SOURCE_DIR}/AstroToolbox/propagateKEP.cpp ${CMAKE_SOURCE_DIR}/AstroToolbox/time2distance.cpp - ${CMAKE_SOURCE_DIR}/Functions/rng/PkRandom.cpp ${CMAKE_SOURCE_DIR}/Functions/objfuns/trajobjfuns.cpp ${CMAKE_SOURCE_DIR}/GOclasses/algorithms/ASA.cpp ${CMAKE_SOURCE_DIR}/GOclasses/algorithms/DE.cpp diff --git a/Functions/rng/PkRandom.cpp b/Functions/rng/PkRandom.cpp deleted file mode 100644 index a78062b..0000000 --- a/Functions/rng/PkRandom.cpp +++ /dev/null @@ -1,131 +0,0 @@ - -//Mersenne Twister pseudorandom number generator of 32 and 64-bit. -//2005, Diego Park -// -//A C-program for MT19937, with initialization improved 2002/1/26. -//Coded by Takuji Nishimura and Makoto Matsumoto. -// -//Before using, initialize the state by using init_genrand(seed) -//or init_by_array(init_key, key_length). -// -//Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, -//All rights reserved. -// -//A C-program for MT19937-64 (2004/9/29 version). -//Coded by Takuji Nishimura and Makoto Matsumoto. -// -//This is a 64-bit version of Mersenne Twister pseudorandom number -//generator. -// -//Before using, initialize the state by using init_genrand64(seed) -//or init_by_array64(init_key, key_length). -// -//Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura, -//All rights reserved. -// -//Redistribution and use in source and binary forms, with or without -//modification, are permitted provided that the following conditions -//are met: -// -//1. Redistributions of source code must retain the above copyright -//notice, this list of conditions and the following disclaimer. -// -//2. Redistributions in binary form must reproduce the above copyright -//notice, this list of conditions and the following disclaimer in the -//documentation and/or other materials provided with the distribution. -// -//3. The names of its contributors may not be used to endorse or promote -//products derived from this software without specific prior written -//permission. -// -//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -//A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR -//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -//EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -//PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -//PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -//LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -//References: -//T. Nishimura, ``Tables of 64-bit Mersenne Twisters'' -//ACM Transactions on Modeling and -//Computer Simulation 10. (2000) 348--357. -//M. Matsumoto and T. Nishimura, -//``Mersenne Twister: a 623-dimensionally equidistributed -//uniform pseudorandom number generator'' -//ACM Transactions on Modeling and -//Computer Simulation 8. (Jan. 1998) 3--30. -// -//Any feedback is very welcome. -//http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html -//email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces) - -#include "PkRandom.h" -namespace Pk -{ - -template <> const size_t Random::Period = 397; - -template <> -Random::Random(unsigned long aSeed) { - state[0] = aSeed & 0xffffffffUL; - for (index = 1; index < MaxState; index++) { - state[index] = - (1812433253UL * (state[index - 1] ^ (state[index - 1] >> 30)) + index) & 0xffffffffUL; - } - - seed(); -} - -template <> -unsigned long Random::hashFunction(unsigned long anInteger) { - anInteger ^= (anInteger >> 11); - anInteger ^= (anInteger << 7) & 0x9d2c5680UL; - anInteger ^= (anInteger << 15) & 0xefc60000UL; - anInteger ^= (anInteger >> 18); - return anInteger; -} - -template <> -unsigned long Random::twist(unsigned long a, unsigned long b, unsigned long c) { - return - a ^ - (((b & 0x80000000UL) | (c & 0x7fffffffUL)) >> 1) ^ - (((c & 1) * 0xffffffffUL) & 0x9908b0dfUL); -} - -template <> const size_t Random::Period = 156; - -template <> -Random::Random(unsigned long long aSeed) { - state[0] = aSeed; - for (index = 1; index < MaxState; index++) { - state[index] = - 6364136223846793005ULL * (state[index - 1] ^ (state[index - 1] >> 62)) + index; - } - - seed(); -} - -template <> -unsigned long long Random::hashFunction(unsigned long long anInteger) { - anInteger ^= (anInteger >> 29) & 0x5555555555555555ULL; - anInteger ^= (anInteger << 17) & 0x71D67FFFEDA60000ULL; - anInteger ^= (anInteger << 37) & 0xFFF7EEE000000000ULL; - anInteger ^= (anInteger >> 43); - return anInteger; -} - -template <> -unsigned long long Random::twist(unsigned long long a, unsigned long long b, unsigned long long c) { - return - a ^ - (((b & 0xffffffff80000000ULL) | (c & 0x7fffffffULL)) >> 1) ^ - (((c & 1) * 0xffffffffffffffffULL) & 0xb5026f5aa96619e9ULL); -} - -} diff --git a/Functions/rng/PkRandom.h b/Functions/rng/PkRandom.h deleted file mode 100644 index c2abe24..0000000 --- a/Functions/rng/PkRandom.h +++ /dev/null @@ -1,122 +0,0 @@ - -//Mersenne Twister pseudorandom number generator of 32 and 64-bit. -//2005, Diego Park -// -//A C-program for MT19937, with initialization improved 2002/1/26. -//Coded by Takuji Nishimura and Makoto Matsumoto. -// -//Before using, initialize the state by using init_genrand(seed) -//or init_by_array(init_key, key_length). -// -//Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, -//All rights reserved. -// -//A C-program for MT19937-64 (2004/9/29 version). -//Coded by Takuji Nishimura and Makoto Matsumoto. -// -//This is a 64-bit version of Mersenne Twister pseudorandom number -//generator. -// -//Before using, initialize the state by using init_genrand64(seed) -//or init_by_array64(init_key, key_length). -// -//Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura, -//All rights reserved. -// -//Redistribution and use in source and binary forms, with or without -//modification, are permitted provided that the following conditions -//are met: -// -//1. Redistributions of source code must retain the above copyright -//notice, this list of conditions and the following disclaimer. -// -//2. Redistributions in binary form must reproduce the above copyright -//notice, this list of conditions and the following disclaimer in the -//documentation and/or other materials provided with the distribution. -// -//3. The names of its contributors may not be used to endorse or promote -//products derived from this software without specific prior written -//permission. -// -//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -//A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR -//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -//EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -//PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -//PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -//LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -//References: -//T. Nishimura, ``Tables of 64-bit Mersenne Twisters'' -//ACM Transactions on Modeling and -//Computer Simulation 10. (2000) 348--357. -//M. Matsumoto and T. Nishimura, -//``Mersenne Twister: a 623-dimensionally equidistributed -//uniform pseudorandom number generator'' -//ACM Transactions on Modeling and -//Computer Simulation 8. (Jan. 1998) 3--30. -// -//Any feedback is very welcome. -//http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html -//email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces) - -#ifndef PKRANDOM_H -#define PKRANDOM_H - -#include - -#include -#include - -typedef boost::mt19937 rng_uint32_type; -typedef boost::lagged_fibonacci607 rng_double_type; - -// Mersenne Twister - -namespace Pk { - -template -class Random { -public: - Random(Integer aSeed = 5489); - - Integer next(); - -private: - static const size_t MaxState = 624 * sizeof(unsigned long) / sizeof(Integer); - static const size_t Period; - - void seed(); - - static Integer hashFunction(Integer anInteger); - static Integer twist(Integer a, Integer b, Integer c); - - Integer state[MaxState]; - Integer index; -}; - -typedef Random Random32; -typedef Random Random64; - -#include "PkRandom.inl" - -// 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(rng_uint32_type &rng) { - unsigned long long l = ((((unsigned long long)rng()) & 0x03FFFFFFLL) << 27) | (((unsigned long long)rng()) & 0x7FFFFFF); - return l / (double)(1LL << 53); -} - -inline double nextDouble(Random64& rng) { - unsigned long long l = (rng.next() & 0x1FFFFFFFFFFFFFLL); - return l / (double)(1LL << 53); -} - -} - -#endif diff --git a/Functions/rng/PkRandom.inl b/Functions/rng/PkRandom.inl deleted file mode 100644 index 467ac9c..0000000 --- a/Functions/rng/PkRandom.inl +++ /dev/null @@ -1,88 +0,0 @@ - -//Mersenne Twister pseudorandom number generator of 32 and 64-bit. -//2005, Diego Park -// -//A C-program for MT19937, with initialization improved 2002/1/26. -//Coded by Takuji Nishimura and Makoto Matsumoto. -// -//Before using, initialize the state by using init_genrand(seed) -//or init_by_array(init_key, key_length). -// -//Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, -//All rights reserved. -// -//A C-program for MT19937-64 (2004/9/29 version). -//Coded by Takuji Nishimura and Makoto Matsumoto. -// -//This is a 64-bit version of Mersenne Twister pseudorandom number -//generator. -// -//Before using, initialize the state by using init_genrand64(seed) -//or init_by_array64(init_key, key_length). -// -//Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura, -//All rights reserved. -// -//Redistribution and use in source and binary forms, with or without -//modification, are permitted provided that the following conditions -//are met: -// -//1. Redistributions of source code must retain the above copyright -//notice, this list of conditions and the following disclaimer. -// -//2. Redistributions in binary form must reproduce the above copyright -//notice, this list of conditions and the following disclaimer in the -//documentation and/or other materials provided with the distribution. -// -//3. The names of its contributors may not be used to endorse or promote -//products derived from this software without specific prior written -//permission. -// -//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -//A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR -//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -//EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -//PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -//PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -//LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -//References: -//T. Nishimura, ``Tables of 64-bit Mersenne Twisters'' -//ACM Transactions on Modeling and -//Computer Simulation 10. (2000) 348--357. -//M. Matsumoto and T. Nishimura, -//``Mersenne Twister: a 623-dimensionally equidistributed -//uniform pseudorandom number generator'' -//ACM Transactions on Modeling and -//Computer Simulation 8. (Jan. 1998) 3--30. -// -//Any feedback is very welcome. -//http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html -//email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces) - -template -Integer Random::next() { - if (index >= MaxState) { - seed(); - } - - return hashFunction(state[index++]); -} - -template -void Random::seed() { - index = 0; - - size_t i; - for (i = 0; i < MaxState - Period; i++) { - state[i] = twist(state[i + Period], state[i], state[i + 1]); - } - for (; i < MaxState - 1; i++) { - state[i] = twist(state[i + (Period - MaxState)], state[i], state[i + 1]); - } - state[MaxState - 1] = twist(state[Period - 1], state[MaxState - 1], state[0]); -} diff --git a/Functions/rng/rng.h b/Functions/rng/rng.h new file mode 100644 index 0000000..46f9e4f --- /dev/null +++ b/Functions/rng/rng.h @@ -0,0 +1,12 @@ +#ifndef PAGMO_RNG_H +#define PAGMO_RNG_H + +#include +#include + +// This rng returns an unsigned integer in the [0,2**32-1] range. +typedef boost::mt19937 rng_uint32_type; +// This rng returns a double in the [0,1] range. +typedef boost::lagged_fibonacci607 rng_double_type; + +#endif diff --git a/GOclasses/algorithms/ASA.cpp b/GOclasses/algorithms/ASA.cpp index 670f485..cdfd009 100644 --- a/GOclasses/algorithms/ASA.cpp +++ b/GOclasses/algorithms/ASA.cpp @@ -7,10 +7,11 @@ * */ -#include "ASA.h" -#include "PkRandom.h" #include +#include "ASA.h" +#include "rng.h" + Population ASAalgorithm::evolve(Individual x0, GOProblem& problem) { const std::vector& LB = problem.getLB(); diff --git a/GOclasses/algorithms/ASA.h b/GOclasses/algorithms/ASA.h index 0dfa838..75e1470 100644 --- a/GOclasses/algorithms/ASA.h +++ b/GOclasses/algorithms/ASA.h @@ -10,12 +10,14 @@ #ifndef ASA_H #define ASA_H -#include "population.h" #include #include -#include "constants.h" -#include "PkRandom.h" + #include "GOproblem.h" +#include "constants.h" +#include "individual.h" +#include "population.h" +#include "rng.h" class ASAalgorithm{ diff --git a/GOclasses/algorithms/DE.cpp b/GOclasses/algorithms/DE.cpp index 53c5d25..c8a580f 100644 --- a/GOclasses/algorithms/DE.cpp +++ b/GOclasses/algorithms/DE.cpp @@ -9,9 +9,10 @@ * from http://www.icsi.berkeley.edu/~storn/code.html#csou */ -#include "DE.h" -#include "vector" #include +#include + +#include "DE.h" using namespace std; diff --git a/GOclasses/algorithms/DE.h b/GOclasses/algorithms/DE.h index d8976a0..3473551 100644 --- a/GOclasses/algorithms/DE.h +++ b/GOclasses/algorithms/DE.h @@ -10,11 +10,12 @@ #ifndef DE_H #define DE_H -#include "population.h" #include -#include +#include + #include "constants.h" -#include "PkRandom.h" +#include "population.h" +#include "rng.h" class DEalgorithm{ public: diff --git a/GOclasses/algorithms/MPSO.cpp b/GOclasses/algorithms/MPSO.cpp index 72123d5..a3b4f51 100644 --- a/GOclasses/algorithms/MPSO.cpp +++ b/GOclasses/algorithms/MPSO.cpp @@ -7,8 +7,9 @@ * */ +#include + #include "MPSO.h" -#include "vector" using namespace std; diff --git a/GOclasses/algorithms/MPSO.h b/GOclasses/algorithms/MPSO.h index 526c520..4792e4a 100644 --- a/GOclasses/algorithms/MPSO.h +++ b/GOclasses/algorithms/MPSO.h @@ -10,11 +10,13 @@ #ifndef MPSO_H #define MPSO_H -#include "population.h" +#include #include -#include -#include "PkRandom.h" -#include "GOproblem.h" + +#include "GOproblem.h" +#include "population.h" +#include "rng.h" + class MPSOalgorithm{ public: diff --git a/GOclasses/algorithms/PSO.cpp b/GOclasses/algorithms/PSO.cpp index 10061c6..2f35b6b 100644 --- a/GOclasses/algorithms/PSO.cpp +++ b/GOclasses/algorithms/PSO.cpp @@ -7,8 +7,9 @@ * */ +#include + #include "PSO.h" -#include "vector" using namespace std; diff --git a/GOclasses/algorithms/PSO.h b/GOclasses/algorithms/PSO.h index 7c26202..aef7f3f 100644 --- a/GOclasses/algorithms/PSO.h +++ b/GOclasses/algorithms/PSO.h @@ -10,10 +10,11 @@ #ifndef PSO_H #define PSO_H -#include "population.h" +#include #include -#include -#include "PkRandom.h" + +#include "population.h" +#include "rng.h" class PSOalgorithm{ public: diff --git a/GOclasses/algorithms/SGA.cpp b/GOclasses/algorithms/SGA.cpp index 358a881..ee7349f 100644 --- a/GOclasses/algorithms/SGA.cpp +++ b/GOclasses/algorithms/SGA.cpp @@ -7,8 +7,9 @@ * */ +#include + #include "SGA.h" -#include "vector" using namespace std; diff --git a/GOclasses/algorithms/SGA.h b/GOclasses/algorithms/SGA.h index da0b113..678e455 100644 --- a/GOclasses/algorithms/SGA.h +++ b/GOclasses/algorithms/SGA.h @@ -10,11 +10,12 @@ #ifndef SGA_H #define SGA_H +#include +#include + +#include "GOproblem.h" #include "population.h" -#include -#include -#include "PkRandom.h" -#include "GOproblem.h" +#include "rng.h" class SGAalgorithm{ public: diff --git a/GOclasses/basic/individual.cpp b/GOclasses/basic/individual.cpp index 3c6dfde..10b0768 100644 --- a/GOclasses/basic/individual.cpp +++ b/GOclasses/basic/individual.cpp @@ -8,9 +8,9 @@ */ #include "individual.h" -#include "PkRandom.h" +#include "rng.h" - void Individual::createRandomIndividual(std::vector LB, std::vector UB, rng_uint32_type &rng){ + void Individual::createRandomIndividual(std::vector LB, std::vector UB, rng_double_type &drng){ //We first delete the vector content if any x.clear(); @@ -19,21 +19,21 @@ //We then push back random numbers to fill a new random chromosome double dummy; for (unsigned int i=0; i < LB.size(); i++){ - dummy = LB[i] + Pk::nextDouble(rng) * (UB[i] - LB[i]); + dummy = LB[i] + drng() * (UB[i] - LB[i]); x.push_back(dummy); } //And a random velocity for (unsigned int i=0; i < LB.size(); i++){ - dummy = Pk::nextDouble(rng) * (UB[i] - LB[i]); //initial velocity range + dummy = drng() * (UB[i] - LB[i]); //initial velocity range v.push_back(dummy); } };//createRandomIndividual - void Individual::resetVelocity(std::vector LB, std::vector UB, rng_uint32_type &rng){ + void Individual::resetVelocity(std::vector LB, std::vector UB, rng_double_type &drng){ v.clear(); double dummy; for (unsigned int i=0; i < LB.size(); i++){ - dummy = (2*Pk::nextDouble(rng) - 1) * (UB[i] - LB[i]); + dummy = (2*drng() - 1) * (UB[i] - LB[i]); v.push_back(dummy); } } diff --git a/GOclasses/basic/individual.h b/GOclasses/basic/individual.h index 16e1d1f..50aaf08 100644 --- a/GOclasses/basic/individual.h +++ b/GOclasses/basic/individual.h @@ -12,15 +12,16 @@ #include #include -#include "constants.h" -#include "PkRandom.h" + #include "GOproblem.h" +#include "constants.h" +#include "rng.h" class Individual{ public: //methods - void createRandomIndividual(std::vector LB, std::vector UB, rng_uint32_type &rng); + void createRandomIndividual(std::vector LB, std::vector UB, rng_double_type &); double evaluateFitness(GOProblem&); double getFitness() const; void setFitness(double fitnessnew); @@ -28,7 +29,7 @@ public: void setDecisionVector(std::vector xnew); std::vector getVelocity() const; void setVelocity(std::vector xnew); - void resetVelocity(std::vector LB, std::vector UB, rng_uint32_type &rng); + void resetVelocity(std::vector LB, std::vector UB, rng_double_type &); //operators double& operator[](int index); diff --git a/GOclasses/basic/population.cpp b/GOclasses/basic/population.cpp index ec73b31..d21b5b8 100644 --- a/GOclasses/basic/population.cpp +++ b/GOclasses/basic/population.cpp @@ -6,24 +6,25 @@ * Copyright 2008 ¿dvanced Concepts Team (European Space Agency). All rights reserved. * */ -#include "population.h" -#include +#include +#include "population.h" +#include "rng.h" - void Population::createRandomPopulation(std::vector LB, std::vector UB, int N, rng_uint32_type &rng){ + void Population::createRandomPopulation(std::vector LB, std::vector UB, int N, rng_double_type &drng){ Individual x; pop.clear(); for (int i=0; i < N; i++){ - x.createRandomIndividual(LB,UB, rng); + x.createRandomIndividual(LB,UB, drng); pop.push_back(x); }//for };//createRandomPopulation - void Population::resetVelocities(std::vector LB, std::vector UB, rng_uint32_type &rng){ + void Population::resetVelocities(std::vector LB, std::vector UB, rng_double_type &drng){ for (unsigned int j=0 ;j &picks, rng_uint32_type &rng){ + Population Population::extractRandomDeme(int N, std::vector &picks, rng_double_type &drng){ Population deme; std::vector PossiblePicks; int Pick; @@ -87,7 +88,7 @@ for (int i=0; i < N; i++){ //we pick a random position between 0 and popsize-1 - Pick = (int)(Pk::nextDouble(rng) * PossiblePicks.size()); + Pick = (int)(drng() * PossiblePicks.size()); //and store it picks.push_back(PossiblePicks[Pick]); //we insert the corresponding individual in the deme diff --git a/GOclasses/basic/population.h b/GOclasses/basic/population.h index 20c5afe..f23908a 100644 --- a/GOclasses/basic/population.h +++ b/GOclasses/basic/population.h @@ -11,17 +11,18 @@ #define POPULATION_H #include + +#include "GOproblem.h" #include "constants.h" #include "individual.h" -#include "PkRandom.h" -#include "GOproblem.h" +#include "rng.h" class Population{ public: //Methods // TODO: pass by reference here, why the copies? - void createRandomPopulation(std::vector LB, std::vector UB, int N, rng_uint32_type &rng); - void resetVelocities(std::vector LB, std::vector UB, rng_uint32_type &rng); + void createRandomPopulation(std::vector LB, std::vector UB, int N, rng_double_type &); + void resetVelocities(std::vector LB, std::vector UB, rng_double_type &); void evaluatePopulation(GOProblem&); void addIndividual(Individual x); void substituteIndividual(const Individual x, const int n); @@ -30,7 +31,7 @@ public: unsigned int size (); Individual extractBestIndividual(); Individual extractWorstIndividual(); - Population extractRandomDeme(int N, std::vector &picks, rng_uint32_type &rng); + Population extractRandomDeme(int N, std::vector &picks, rng_double_type &); 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 aeb3475..2e3bbc1 100644 --- a/SolversThreads/SolversThreads.cpp +++ b/SolversThreads/SolversThreads.cpp @@ -12,14 +12,14 @@ #include #include -#include "SolversThreads.h" -#include "population.h" +#include "ASA.h" #include "DE.h" -#include "PSO.h" #include "MPSO.h" +#include "PSO.h" #include "SGA.h" -#include "ASA.h" -#include "PkRandom.h" +#include "SolversThreads.h" +#include "population.h" +#include "rng.h" using namespace std; @@ -40,6 +40,7 @@ void *DEthread(void *data) vector LB,UB; DEalgorithm DE; rng_uint32_type rng; + rng_double_type drng; clock_t start,end; @@ -48,7 +49,8 @@ void *DEthread(void *data) { lock_type lock(*PtrTP->TPmutex); rng.seed(PtrTP->randomSeed); - deme = PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, rng); + drng.seed(PtrTP->randomSeed); + deme = PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, drng); problem = PtrTP->problem; problem->getBounds(LB, UB); DE.initDE(PtrTP->generations,LB.size(),PtrTP->F,PtrTP->CR,PtrTP->strategy, rng()); @@ -100,6 +102,7 @@ void *MPSOthread(void *data) vector LB, UB; MPSOalgorithm MPSO; rng_uint32_type rng; + rng_double_type drng; clock_t start,end; double dif; @@ -107,7 +110,8 @@ void *MPSOthread(void *data) { lock_type lock(*PtrTP->TPmutex); rng.seed(PtrTP->randomSeed); - deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, rng); + drng.seed(PtrTP->randomSeed); + deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, drng); problem = PtrTP->problem; problem->getBounds(LB, UB); MPSO.initMPSO(PtrTP->generations,LB.size(),PtrTP->omega,PtrTP->eta1,PtrTP->eta2,PtrTP->vcoeff, PtrTP->nswarms, rng()); @@ -155,6 +159,7 @@ void *PSOthread(void *data) vector LB,UB; PSOalgorithm PSO; rng_uint32_type rng; + rng_double_type drng; clock_t start,end; double dif; @@ -162,7 +167,8 @@ void *PSOthread(void *data) { lock_type lock(*PtrTP->TPmutex); rng.seed(PtrTP->randomSeed); - deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, rng); + drng.seed(PtrTP->randomSeed); + deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, drng); problem = PtrTP->problem; problem->getBounds(LB, UB); PSO.initPSO(PtrTP->generations,LB.size(),PtrTP->omega,PtrTP->eta1,PtrTP->eta2,PtrTP->vcoeff, rng()); @@ -211,6 +217,7 @@ void *SGAthread(void *data) vector LB,UB; SGAalgorithm SGA; rng_uint32_type rng; + rng_double_type drng; GOProblem* problem; clock_t start,end; @@ -219,7 +226,8 @@ void *SGAthread(void *data) { lock_type lock(*PtrTP->TPmutex); rng.seed(PtrTP->randomSeed); - deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, rng); + drng.seed(PtrTP->randomSeed); + deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, drng); problem = PtrTP->problem; problem->getBounds(LB, UB); SGA.initSGA(PtrTP->generations,LB.size(),PtrTP->CRsga,PtrTP->M,PtrTP->insert_best, rng()); @@ -267,6 +275,7 @@ void *ASAthread(void *data) vector LB,UB; ASAalgorithm ASA; rng_uint32_type rng; + rng_double_type drng; GOProblem* problem; @@ -276,7 +285,8 @@ void *ASAthread(void *data) { lock_type lock(*PtrTP->TPmutex); rng.seed(PtrTP->randomSeed); - deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, rng); + drng.seed(PtrTP->randomSeed); + deme=PtrTP->Ptr_pop->extractRandomDeme(PtrTP->NP,picks, drng); problem = PtrTP->problem; problem->getBounds(LB, UB); unsigned int temp; diff --git a/main.cpp b/main.cpp index d66a7ab..0eb96ce 100644 --- a/main.cpp +++ b/main.cpp @@ -17,7 +17,7 @@ #include "ClassicProblems.h" #include #include "SolversThreads.h" -#include "PkRandom.h" +#include "rng.h" using namespace std; @@ -34,6 +34,7 @@ int main(){ //We prepare the pseudorandom sequence (TODO: check the randomnumbers of different threads are different) rng_uint32_type rng(time(0)); + rng_double_type drng(time(0)); //we set the problem problem_type problem; @@ -94,7 +95,7 @@ while (choice != -1) { for (int i=0;i parallelProblems(islandsN); for (int i=0;i