added test for permutation components
This commit is contained in:
parent
141ccae1dd
commit
0aa67ac17b
5 changed files with 327 additions and 0 deletions
68
eo/test/t-eoInitPermutation.cpp
Normal file
68
eo/test/t-eoInitPermutation.cpp
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// t-eoInitPermutation.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eo>
|
||||
|
||||
#include <eoInt.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
|
||||
// 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 <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);
|
||||
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;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
24
eo/test/t-eoInt.cpp
Normal file
24
eo/test/t-eoInt.cpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// t-eoInt.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eo>
|
||||
#include <eoInt.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
typedef eoInt<double> Chrom;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
int main()
|
||||
{
|
||||
Chrom chrom1, chrom2;
|
||||
|
||||
std::cout << "chrom1 = " << chrom1 << std::endl
|
||||
<< "chrom2 = " << chrom2 << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
78
eo/test/t-eoShiftMutation.cpp
Normal file
78
eo/test/t-eoShiftMutation.cpp
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// t-eoShiftMutation.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eo>
|
||||
|
||||
#include <eoInt.h>
|
||||
#include <eoShiftMutation.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
|
||||
// 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 <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 shift mutation
|
||||
eoShiftMutation <Chrom> 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;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
79
eo/test/t-eoSwapMutation.cpp
Normal file
79
eo/test/t-eoSwapMutation.cpp
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// t-eoSwapMutation.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#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
|
||||
// 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 <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;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
78
eo/test/t-eoTwoOptMutation.cpp
Normal file
78
eo/test/t-eoTwoOptMutation.cpp
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// t-eoTwoOptMutation.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eo>
|
||||
|
||||
#include <eoInt.h>
|
||||
#include <eoTwoOptMutation.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
|
||||
// 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 <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 twoOpt mutation
|
||||
eoTwoOptMutation <Chrom> 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;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
Reference in a new issue