From a1ee120589c7df7270a42027ed4cc2af6fcd5c5e Mon Sep 17 00:00:00 2001 From: tlegrand Date: Fri, 18 Apr 2008 08:58:42 +0000 Subject: [PATCH] imrproved PSO topology/velocity tests --- eo/test/t-eoExtendedVelocity.cpp | 10 +++++----- eo/test/t-eoOrderXover.cpp | 9 +++++---- eo/test/t-eoSwapMutation.cpp | 30 ++++++++++++++++-------------- eo/test/t-eoTwoOptMutation.cpp | 30 ++++++++++++++++-------------- 4 files changed, 42 insertions(+), 37 deletions(-) diff --git a/eo/test/t-eoExtendedVelocity.cpp b/eo/test/t-eoExtendedVelocity.cpp index c019bf695..d40d28817 100644 --- a/eo/test/t-eoExtendedVelocity.cpp +++ b/eo/test/t-eoExtendedVelocity.cpp @@ -43,20 +43,20 @@ int main_function(int argc, char **argv) eoExtendedVelocity velocity (topology,1,1,1,1); // the test itself - for (unsigned int i = 0; i < POP_SIZE; ++i) + for (unsigned int i = 0; i < POP_SIZE; i++) { std::cout << " Initial particle n°" << i << " velocity: " << std::endl; - for (unsigned int j = 0; j < VEC_SIZE; ++j) + for (unsigned int j = 0; j < VEC_SIZE; j++) std::cout << " v" << j << "=" << pop[i].velocities[j] << std::endl; } - for (unsigned int i = 0; i < POP_SIZE; ++i) + for (unsigned int i = 0; i < POP_SIZE; i++) velocity (pop[i],i); - for (unsigned int i = 0; i < POP_SIZE; ++i) + for (unsigned int i = 0; i < POP_SIZE; i++) { std::cout << " Final particle n°" << i << " velocity: " << std::endl; - for (unsigned int j = 0; j < VEC_SIZE; ++j) + for (unsigned int j = 0; j < VEC_SIZE; j++) std::cout << " v" << j << "=" << pop[i].velocities[j] << std::endl; } diff --git a/eo/test/t-eoOrderXover.cpp b/eo/test/t-eoOrderXover.cpp index 1280cf84d..f44b8c69d 100644 --- a/eo/test/t-eoOrderXover.cpp +++ b/eo/test/t-eoOrderXover.cpp @@ -2,11 +2,12 @@ // t-eoOrderXover.cpp //----------------------------------------------------------------------------- -#include +#include +#include #include #include -#include + //----------------------------------------------------------------------------- @@ -56,13 +57,13 @@ int main() eoOrderXover cross; for (i = 0; i < POP_SIZE; ++i) - std::cout << " Initial chromosome n°" << i << " : " << pop[i] << "..." << std::endl; + std::cout << " Initial chromosome n�" << i << " : " << pop[i] << "..." << std::endl; cross(pop[0],pop[1]); cross(pop[1],pop[2]); for (i = 0; i < POP_SIZE; ++i) { - std::cout << " Initial chromosome n°" << i << " becomes : " << pop[i] << " after orderXover" << std::endl; + std::cout << " Initial chromosome n�" << i << " becomes : " << pop[i] << " after orderXover" << std::endl; check_permutation(pop[i]); } return 0; diff --git a/eo/test/t-eoSwapMutation.cpp b/eo/test/t-eoSwapMutation.cpp index af35b42fd..f2c33789e 100644 --- a/eo/test/t-eoSwapMutation.cpp +++ b/eo/test/t-eoSwapMutation.cpp @@ -2,8 +2,9 @@ // t-eoSwapMutation.cpp //----------------------------------------------------------------------------- -#include +#include +#include #include #include @@ -22,20 +23,21 @@ double real_value(const Chrom & _chrom) return sum/_chrom.size(); } +//----------------------------------------------------------------------------- // Return true if the given chromosome corresponds to a permutation -// There must be an nicer way to do it (set?) ... -bool check_permutation(const Chrom & _chrom) -{ - for (unsigned i = 0; i < _chrom.size(); ++i) - for (unsigned j = 0; j < _chrom.size(); ++j) - if(i!=j) - if(_chrom[i]==_chrom[j]){ - std::cout << " Error: Wrong permutation !" << std::endl; - std::string s; - s.append( " Wrong permutation in t-eoSwapMutation"); - throw std::runtime_error( s ); - } - return true; +bool check_permutation(const Chrom& _chrom){ + unsigned size= _chrom.size(); + std::set verif; + for(unsigned i=0; i< size; i++){ + if(verif.insert(_chrom[i]).second==false){ + std::cout << " Error: Wrong permutation !" << std::endl; + std::string s; + s.append( " Wrong permutation in t-eoShiftMutation"); + throw std::runtime_error( s ); + return false; + } + } + return true; } diff --git a/eo/test/t-eoTwoOptMutation.cpp b/eo/test/t-eoTwoOptMutation.cpp index bfc68fe6e..95e713a6c 100644 --- a/eo/test/t-eoTwoOptMutation.cpp +++ b/eo/test/t-eoTwoOptMutation.cpp @@ -2,8 +2,9 @@ // t-eoTwoOptMutation.cpp //----------------------------------------------------------------------------- -#include +#include +#include #include #include @@ -22,20 +23,21 @@ double real_value(const Chrom & _chrom) return sum/_chrom.size(); } +//----------------------------------------------------------------------------- // Return true if the given chromosome corresponds to a permutation -// There must be an nicer way to do it (set?) ... -bool check_permutation(const Chrom & _chrom) -{ - for (unsigned i = 0; i < _chrom.size(); ++i) - for (unsigned j = 0; j < _chrom.size(); ++j) - if(i!=j) - if(_chrom[i]==_chrom[j]){ - std::cout << " Error: Wrong permutation !" << std::endl; - std::string s; - s.append( " Wrong permutation in t-eoTwoOptMutation"); - throw std::runtime_error( s ); - } - return true; +bool check_permutation(const Chrom& _chrom){ + unsigned size= _chrom.size(); + std::set verif; + for(unsigned i=0; i< size; i++){ + if(verif.insert(_chrom[i]).second==false){ + std::cout << " Error: Wrong permutation !" << std::endl; + std::string s; + s.append( " Wrong permutation in t-eoShiftMutation"); + throw std::runtime_error( s ); + return false; + } + } + return true; }