00001
00002
00003
00004
00005 #include <eo>
00006
00007 #include <eoInt.h>
00008 #include <eoShiftMutation.h>
00009 #include <set>
00010
00011
00012
00013 typedef eoInt<double> Chrom;
00014
00015
00016
00017
00018 double real_value(const Chrom & _chrom)
00019 {
00020 double sum = 0;
00021 for (unsigned i = 0; i < _chrom.size(); i++)
00022 sum += _chrom[i];
00023 return sum/_chrom.size();
00024 }
00025
00026
00027 bool check_permutation(const Chrom& _chrom){
00028 unsigned size= _chrom.size();
00029 std::set<unsigned> verif;
00030 for(unsigned i=0; i< size; i++){
00031 if(verif.insert(_chrom[i]).second==false){
00032 std::cout << " Error: Wrong permutation !" << std::endl;
00033 std::string s;
00034 s.append( " Wrong permutation in t-eoShiftMutation");
00035 throw std::runtime_error( s );
00036 return false;
00037 }
00038 }
00039 return true;
00040 }
00041
00042
00043 int main()
00044 {
00045 const unsigned POP_SIZE = 3, CHROM_SIZE = 8;
00046 unsigned i;
00047
00048
00049 eoInitPermutation <Chrom> random(CHROM_SIZE);
00050
00051
00052 eoPop<Chrom> pop;
00053
00054
00055 eoEvalFuncPtr<Chrom> eval( real_value );
00056
00057 for (i = 0; i < POP_SIZE; ++i)
00058 {
00059 Chrom chrom(CHROM_SIZE);
00060 random(chrom);
00061 eval(chrom);
00062 pop.push_back(chrom);
00063 }
00064
00065
00066 eoShiftMutation <Chrom> shift;
00067
00068 for (i = 0; i < POP_SIZE; ++i)
00069 {
00070 std::cout << " Initial chromosome n°" << i << " : " << pop[i] << "..." << std::endl;
00071 shift(pop[i]);
00072 std::cout << " ... becomes : " << pop[i] << " after shift mutation" << std::endl;
00073 check_permutation(pop[i]);
00074 }
00075
00076 return 0;
00077 }
00078
00079