added PSO ring topology test
This commit is contained in:
parent
9ff1e99715
commit
9174a37355
1 changed files with 68 additions and 0 deletions
68
eo/test/t-eoRingTopology.cpp
Normal file
68
eo/test/t-eoRingTopology.cpp
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// t-eoRingTopology.cpp
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#include <eo>
|
||||||
|
|
||||||
|
typedef eoRealParticle < double >Indi;
|
||||||
|
|
||||||
|
//Evaluation function
|
||||||
|
double f (const Indi & _indi)
|
||||||
|
{
|
||||||
|
double sum = 0;
|
||||||
|
for (unsigned i = 0; i < _indi.size (); i++)
|
||||||
|
sum += pow(_indi[i],2);
|
||||||
|
return (-sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
int the_main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
//Parameters
|
||||||
|
const unsigned int VEC_SIZE = 2;
|
||||||
|
const unsigned int POP_SIZE = 10;
|
||||||
|
const unsigned int NEIGHBORHOOD_SIZE= 3;
|
||||||
|
|
||||||
|
rng.reseed (33);
|
||||||
|
eoEvalFuncPtr<Indi, double, const Indi& > plainEval(f);
|
||||||
|
eoEvalFuncCounter < Indi > eval (plainEval);
|
||||||
|
eoUniformGenerator < double >uGen (0., 5.);
|
||||||
|
eoInitFixedLength < Indi > random (VEC_SIZE, uGen);
|
||||||
|
eoUniformGenerator < double >sGen (-1., 1.);
|
||||||
|
eoVelocityInitFixedLength < Indi > veloRandom (VEC_SIZE, sGen);
|
||||||
|
eoFirstIsBestInit < Indi > localInit;
|
||||||
|
eoStandardFlight < Indi > flight;
|
||||||
|
eoPop < Indi > pop;
|
||||||
|
pop.append (POP_SIZE, random);
|
||||||
|
apply(eval, pop);
|
||||||
|
apply < Indi > (veloRandom, pop);
|
||||||
|
apply < Indi > (localInit, pop);
|
||||||
|
eoRingTopology<Indi> topology(NEIGHBORHOOD_SIZE);
|
||||||
|
topology.setup(pop);
|
||||||
|
std::cout<<"\n\n\nPopulation :\n\n"<<pop;
|
||||||
|
std::cout<<"\n\nNeighborhood :\n\n";
|
||||||
|
topology.printOn();
|
||||||
|
int k = NEIGHBORHOOD_SIZE/2;
|
||||||
|
for(unsigned i=0;i<pop.size();i++)
|
||||||
|
{
|
||||||
|
std::cout<<"\nBetween : ";
|
||||||
|
for(unsigned j=0;j<NEIGHBORHOOD_SIZE;j++)
|
||||||
|
std::cout<<"\n"<<pop[((pop.size()+i-k+j)%pop.size())];
|
||||||
|
std::cout<<"\nThe best is : \n"<<topology.best(i)<<"\n";
|
||||||
|
}
|
||||||
|
std::cout<<"\n\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
the_main(argc, argv);
|
||||||
|
}
|
||||||
|
catch(std::exception& e)
|
||||||
|
{
|
||||||
|
std::cout << "Exception: " << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in a new issue