Added new lesson (6) dedicated to the PSO. Also changed a few things into the PSO-dedicated components (constructors)

This commit is contained in:
tlegrand 2008-03-04 14:01:29 +00:00
commit 4ad79a9148
17 changed files with 771 additions and 115 deletions

View file

@ -76,6 +76,7 @@ SET (TEST_LIST t-eoParetoFitness
t-eoShiftMutation
t-eoTwoOptMutation
t-eoRingTopology
t-eoSyncEasyPSO
# t-eoFrontSorter
# t-eoEpsMOEA
)

View file

@ -16,14 +16,21 @@
avoid compiler warnings.
2006-07-02 Thomas Legrand <thomas.legrand@lifl.fr>
2006-07-02 Thomas Legrand <thomas.legrand@inria.fr>
* test/t-eoEasyPSO.cpp: add PSO test
* test/t-eoEasyPSO.cpp: added PSO test
* test/Makefile.am: add PSO test
* test/Makefile.am: added PSO test
2006-02-27 Thomas Legrand <thomas.legrand@inria.fr>
* test/t-eoSyncEasyPSO.cpp: added synchronous PSO test
* test/t-eoEasyPSO.cpp: customized PSO test (initialization)
* test/Makefile.am: added synchronous PSO test
* Local Variables:
* coding: iso-8859-1
* mode: flyspell
* fill-column: 80
* End:
* End:

View file

@ -50,7 +50,8 @@ check_PROGRAMS = t-eoParetoFitness \
t-eoSwapMutation \
t-eoShiftMutation \
t-eoTwoOptMutation \
t-eoRingTopology
t-eoRingTopology \
t-eoSyncEasyPSO
TESTS = $(check_PROGRAMS) \
run_tests # This script can be used to check command-line arguments
@ -114,4 +115,4 @@ t_eoSwapMutation_SOURCES = t-eoSwapMutation.cpp
t_eoShiftMutation_SOURCES = t-eoShiftMutation.cpp
t_eoTwoOptMutation_SOURCES = t-eoTwoOptMutation.cpp
t_eoRingTopology_SOURCES = t-eoRingTopology.cpp
t_eoSyncEasyPSO_SOURCES = t-eoSyncEasyPSO.cpp

View file

@ -48,23 +48,17 @@ int main()
// local best init
eoFirstIsBestInit < Particle > localInit;
// perform initialization
// perform position initialization
pop.append (POP_SIZE, random);
apply < Particle > (eval, pop);
apply < Particle > (veloRandom, pop);
apply < Particle > (localInit, pop);
std::cout << "population:" << std::endl;
for (i = 0; i < pop.size(); ++i)
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
// topology
eoLinearTopology<Particle> topology(NEIGHBORHOOD_SIZE);
topology.setup(pop);
// the full initializer
eoInitializer <Particle> init(eval,veloRandom,localInit,topology,pop);
init();
// bounds
eoRealVectorBounds bnds(VEC_SIZE,-1.5,1.5);
@ -75,19 +69,26 @@ int main()
eoStandardFlight <Particle> flight;
// Terminators
eoGenContinue <Particle> genCont (50);
// the full initializer
eoInitializer <Particle> init(eval,veloRandom,localInit,topology,pop);
eoGenContinue <Particle> genCont1 (50);
eoGenContinue <Particle> genCont2 (50);
// PS flight
eoEasyPSO<Particle> pso(init,genCont, eval, velocity, flight);
eoEasyPSO<Particle> pso1(genCont1, eval, velocity, flight);
eoEasyPSO<Particle> pso2(init,genCont2, eval, velocity, flight);
// flight
try
{
pso(pop);
pso1(pop);
std::cout << "FINAL POPULATION AFTER PSO n°1:" << std::endl;
for (i = 0; i < pop.size(); ++i)
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
pso2(pop);
std::cout << "FINAL POPULATION AFTER PSO n°2:" << std::endl;
for (i = 0; i < pop.size(); ++i)
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
}
catch (std::exception& e)
{
@ -95,9 +96,7 @@ int main()
exit(EXIT_FAILURE);
}
std::cout << "pop" << std::endl;
for (i = 0; i < pop.size(); ++i)
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
return 0;
}

104
eo/test/t-eoSyncEasyPSO.cpp Normal file
View file

@ -0,0 +1,104 @@
//-----------------------------------------------------------------------------
// t-eoEasySyncPSO.cpp
//-----------------------------------------------------------------------------
#ifndef __GNUG__
// to avoid long name warnings
#pragma warning(disable:4786)
#endif // __GNUG__
#include <eo>
//-----------------------------------------------------------------------------
typedef eoMinimizingFitness FitT;
typedef eoRealParticle < FitT > Particle;
//-----------------------------------------------------------------------------
// the objective function
double real_value (const Particle & _particle)
{
double sum = 0;
for (unsigned i = 0; i < _particle.size ()-1; i++)
sum += pow(_particle[i],2);
return (sum);
}
int main()
{
const unsigned int VEC_SIZE = 2;
const unsigned int POP_SIZE = 20;
const unsigned int NEIGHBORHOOD_SIZE= 5;
unsigned i;
// the population:
eoPop<Particle> pop;
// Evaluation
eoEvalFuncPtr<Particle, double, const Particle& > eval( real_value );
// position init
eoUniformGenerator < double >uGen (-3, 3);
eoInitFixedLength < Particle > random (VEC_SIZE, uGen);
// velocity init
eoUniformGenerator < double >sGen (-2, 2);
eoVelocityInitFixedLength < Particle > veloRandom (VEC_SIZE, sGen);
// local best init
eoFirstIsBestInit < Particle > localInit;
// perform position initialization
pop.append (POP_SIZE, random);
// topology
eoLinearTopology<Particle> topology(NEIGHBORHOOD_SIZE);
// the full initializer
eoInitializer <Particle> init(eval,veloRandom,localInit,topology,pop);
init();
// bounds
eoRealVectorBounds bnds(VEC_SIZE,-1.5,1.5);
// velocity
eoStandardVelocity <Particle> velocity (topology,1,1.6,2,bnds);
// flight
eoStandardFlight <Particle> flight;
// Terminators
eoGenContinue <Particle> genCont1 (50);
eoGenContinue <Particle> genCont2 (50);
// PS flight
eoSyncEasyPSO<Particle> pso1(genCont1, eval, velocity, flight);
eoSyncEasyPSO<Particle> pso2(init,genCont2, eval, velocity, flight);
// flight
try
{
pso1(pop);
std::cout << "FINAL POPULATION AFTER SYNC PSO n°1:" << std::endl;
for (i = 0; i < pop.size(); ++i)
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
pso2(pop);
std::cout << "FINAL POPULATION AFTER SYNC PSO n°2:" << std::endl;
for (i = 0; i < pop.size(); ++i)
std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
}
catch (std::exception& e)
{
std::cout << "exception: " << e.what() << std::endl;;
exit(EXIT_FAILURE);
}
return 0;
}
//-----------------------------------------------------------------------------