RC
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@2710 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
parent
6f384f4a59
commit
e3a610506b
1731 changed files with 105122 additions and 63920 deletions
81
branches/rc2.0/eo/test/t-eoSwapMutation.cpp
Normal file
81
branches/rc2.0/eo/test/t-eoSwapMutation.cpp
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// t-eoSwapMutation.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <set>
|
||||
|
||||
#include <eo>
|
||||
#include <eoInt.h>
|
||||
#include <eoSwapMutation.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
typedef eoInt<double> 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
|
||||
bool check_permutation(const Chrom& _chrom){
|
||||
unsigned size= _chrom.size();
|
||||
std::set<unsigned> 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;
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
const unsigned POP_SIZE = 8, CHROM_SIZE = 16;
|
||||
unsigned i;
|
||||
|
||||
// a chromosome randomizer
|
||||
eoInitPermutation <Chrom> random(CHROM_SIZE);
|
||||
|
||||
// the population:
|
||||
eoPop<Chrom> pop;
|
||||
|
||||
// Evaluation
|
||||
eoEvalFuncPtr<Chrom> 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 <Chrom> 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;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
Loading…
Add table
Add a link
Reference in a new issue