00001
00002
00003
00004
00005 #ifndef __GNUG__
00006
00007 #pragma warning(disable:4786)
00008 #endif // __GNUG__
00009
00010 #include <eo>
00011
00012
00013 typedef eoMinimizingFitness FitT;
00014 typedef eoRealParticle < FitT > Particle;
00015
00016
00017
00018 double real_value (const Particle & _particle)
00019 {
00020 double sum = 0;
00021 for (unsigned i = 0; i < _particle.size ()-1; i++)
00022 sum += pow(_particle[i],2);
00023 return (sum);
00024 }
00025
00026
00027 int main()
00028 {
00029 const unsigned int VEC_SIZE = 2;
00030 const unsigned int POP_SIZE = 20;
00031 const unsigned int NEIGHBORHOOD_SIZE= 5;
00032 unsigned i;
00033
00034
00035 eoPop<Particle> pop;
00036
00037
00038 eoEvalFuncPtr<Particle, double, const Particle& > eval( real_value );
00039
00040
00041 eoUniformGenerator < double >uGen (-3, 3);
00042 eoInitFixedLength < Particle > random (VEC_SIZE, uGen);
00043
00044
00045 eoUniformGenerator < double >sGen (-2, 2);
00046 eoVelocityInitFixedLength < Particle > veloRandom (VEC_SIZE, sGen);
00047
00048
00049 eoFirstIsBestInit < Particle > localInit;
00050
00051
00052 pop.append (POP_SIZE, random);
00053
00054
00055 eoLinearTopology<Particle> topology(NEIGHBORHOOD_SIZE);
00056
00057
00058 eoInitializer <Particle> init(eval,veloRandom,localInit,topology,pop);
00059 init();
00060
00061
00062
00063 eoRealVectorBounds bnds(VEC_SIZE,-1.5,1.5);
00064
00065
00066 eoStandardVelocity <Particle> velocity (topology,1,1.6,2,bnds);
00067
00068
00069 eoStandardFlight <Particle> flight;
00070
00071
00072 eoGenContinue <Particle> genCont1 (50);
00073 eoGenContinue <Particle> genCont2 (50);
00074
00075
00076 eoSyncEasyPSO<Particle> pso1(genCont1, eval, velocity, flight);
00077
00078 eoSyncEasyPSO<Particle> pso2(init,genCont2, eval, velocity, flight);
00079
00080
00081 try
00082 {
00083 pso1(pop);
00084 std::cout << "FINAL POPULATION AFTER SYNC PSO n°1:" << std::endl;
00085 for (i = 0; i < pop.size(); ++i)
00086 std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
00087
00088 pso2(pop);
00089 std::cout << "FINAL POPULATION AFTER SYNC PSO n°2:" << std::endl;
00090 for (i = 0; i < pop.size(); ++i)
00091 std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl;
00092 }
00093 catch (std::exception& e)
00094 {
00095 std::cout << "exception: " << e.what() << std::endl;;
00096 exit(EXIT_FAILURE);
00097 }
00098
00099
00100
00101 return 0;
00102 }
00103
00104