diff --git a/eo/test/t-eoInitPermutation.cpp b/eo/test/t-eoInitPermutation.cpp new file mode 100644 index 000000000..4090128be --- /dev/null +++ b/eo/test/t-eoInitPermutation.cpp @@ -0,0 +1,68 @@ +//----------------------------------------------------------------------------- +// t-eoInitPermutation.cpp +//----------------------------------------------------------------------------- + +#include + +#include + +//----------------------------------------------------------------------------- + +typedef eoInt Chrom; + +//----------------------------------------------------------------------------- + + +double real_value(const Chrom & _chrom) +{ + double sum = 0; + for (unsigned i = 0; i < _chrom.size(); i++) + sum += _chrom[i]; + 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-eoInitPermutation"); + throw std::runtime_error( s ); + } + return true; +} + +int main() +{ + const unsigned POP_SIZE = 8, CHROM_SIZE = 16; + unsigned i; + + // a chromosome randomizer + eoInitPermutation random(CHROM_SIZE); + + // the population: + eoPop pop; + + // Evaluation + eoEvalFuncPtr eval( real_value ); + + for (i = 0; i < POP_SIZE; ++i) + { + Chrom chrom(CHROM_SIZE); + std::cout << " Initial chromosome n°" << i << " : " << chrom << "..." << std::endl; + random(chrom); + eval(chrom); + std::cout << " ... becomes : " << chrom << " after initialization" << std::endl; + check_permutation(chrom); + pop.push_back(chrom); + } + + return 0; +} + +//----------------------------------------------------------------------------- diff --git a/eo/test/t-eoInt.cpp b/eo/test/t-eoInt.cpp new file mode 100644 index 000000000..279803ce3 --- /dev/null +++ b/eo/test/t-eoInt.cpp @@ -0,0 +1,24 @@ +//----------------------------------------------------------------------------- +// t-eoInt.cpp +//----------------------------------------------------------------------------- + +#include +#include + +//----------------------------------------------------------------------------- + +typedef eoInt Chrom; + +//----------------------------------------------------------------------------- + +int main() +{ + Chrom chrom1, chrom2; + + std::cout << "chrom1 = " << chrom1 << std::endl + << "chrom2 = " << chrom2 << std::endl; + + return 0; +} + +//----------------------------------------------------------------------------- diff --git a/eo/test/t-eoShiftMutation.cpp b/eo/test/t-eoShiftMutation.cpp new file mode 100644 index 000000000..928cdf306 --- /dev/null +++ b/eo/test/t-eoShiftMutation.cpp @@ -0,0 +1,78 @@ +//----------------------------------------------------------------------------- +// t-eoShiftMutation.cpp +//----------------------------------------------------------------------------- + +#include + +#include +#include + +//----------------------------------------------------------------------------- + +typedef eoInt Chrom; + +//----------------------------------------------------------------------------- + + +double real_value(const Chrom & _chrom) +{ + double sum = 0; + for (unsigned i = 0; i < _chrom.size(); i++) + sum += _chrom[i]; + 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-eoShiftMutation"); + throw std::runtime_error( s ); + } + return true; +} + + +int main() +{ + const unsigned POP_SIZE = 3, CHROM_SIZE = 8; + unsigned i; + + // a chromosome randomizer + eoInitPermutation random(CHROM_SIZE); + + // the population: + eoPop pop; + + // Evaluation + eoEvalFuncPtr eval( real_value ); + + for (i = 0; i < POP_SIZE; ++i) + { + Chrom chrom(CHROM_SIZE); + random(chrom); + eval(chrom); + pop.push_back(chrom); + } + + // a shift mutation + eoShiftMutation shift; + + for (i = 0; i < POP_SIZE; ++i) + { + std::cout << " Initial chromosome n°" << i << " : " << pop[i] << "..." << std::endl; + shift(pop[i]); + std::cout << " ... becomes : " << pop[i] << " after shift mutation" << std::endl; + check_permutation(pop[i]); + } + + return 0; +} + +//----------------------------------------------------------------------------- diff --git a/eo/test/t-eoSwapMutation.cpp b/eo/test/t-eoSwapMutation.cpp new file mode 100644 index 000000000..af35b42fd --- /dev/null +++ b/eo/test/t-eoSwapMutation.cpp @@ -0,0 +1,79 @@ +//----------------------------------------------------------------------------- +// t-eoSwapMutation.cpp +//----------------------------------------------------------------------------- + +#include + +#include +#include + +//----------------------------------------------------------------------------- + +typedef eoInt Chrom; + +//----------------------------------------------------------------------------- + + +double real_value(const Chrom & _chrom) +{ + double sum = 0; + for (unsigned i = 0; i < _chrom.size(); i++) + sum += _chrom[i]; + 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; +} + + +int main() +{ + const unsigned POP_SIZE = 8, CHROM_SIZE = 16; + unsigned i; + + // a chromosome randomizer + eoInitPermutation random(CHROM_SIZE); + + // the population: + eoPop pop; + + // Evaluation + eoEvalFuncPtr eval( real_value ); + + for (i = 0; i < POP_SIZE; ++i) + { + Chrom chrom(CHROM_SIZE); + random(chrom); + eval(chrom); + pop.push_back(chrom); + } + + + // a swap mutation + eoSwapMutation swap; + + for (i = 0; i < POP_SIZE; ++i) + { + std::cout << " Initial chromosome n°" << i << " : " << pop[i] << "..." << std::endl; + swap(pop[i]); + std::cout << " ... becomes : " << pop[i] << " after swap mutation" << std::endl; + check_permutation(pop[i]); + } + + return 0; +} + +//----------------------------------------------------------------------------- diff --git a/eo/test/t-eoTwoOptMutation.cpp b/eo/test/t-eoTwoOptMutation.cpp new file mode 100644 index 000000000..bfc68fe6e --- /dev/null +++ b/eo/test/t-eoTwoOptMutation.cpp @@ -0,0 +1,78 @@ +//----------------------------------------------------------------------------- +// t-eoTwoOptMutation.cpp +//----------------------------------------------------------------------------- + +#include + +#include +#include + +//----------------------------------------------------------------------------- + +typedef eoInt Chrom; + +//----------------------------------------------------------------------------- + + +double real_value(const Chrom & _chrom) +{ + double sum = 0; + for (unsigned i = 0; i < _chrom.size(); i++) + sum += _chrom[i]; + 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; +} + + +int main() +{ + const unsigned POP_SIZE = 3, CHROM_SIZE = 8; + unsigned i; + + // a chromosome randomizer + eoInitPermutation random(CHROM_SIZE); + + // the population: + eoPop pop; + + // Evaluation + eoEvalFuncPtr eval( real_value ); + + for (i = 0; i < POP_SIZE; ++i) + { + Chrom chrom(CHROM_SIZE); + random(chrom); + eval(chrom); + pop.push_back(chrom); + } + + // a twoOpt mutation + eoTwoOptMutation twoOpt; + + for (i = 0; i < POP_SIZE; ++i) + { + std::cout << " Initial chromosome n°" << i << " : " << pop[i] << "..." << std::endl; + twoOpt(pop[i]); + std::cout << " ... becomes : " << pop[i] << " after twoOpt mutation" << std::endl; + check_permutation(pop[i]); + } + + return 0; +} + +//-----------------------------------------------------------------------------