FirstRealPSO.cpp

00001 //--------------------------------------------------------------------------------
00002 //      FirstRealPSO.cpp
00003 //--------------------------------------------------------------------------------
00004 
00005 #include <stdexcept>
00006 #include <iostream>
00007 #include <sstream>
00008 
00009 #include <eo>
00010 #include <es.h>
00011 #include <eoScalarFitness.h>
00012 #include <utils/eoRealVectorBounds.h>
00013 #include <es/eoRealParticle.h>
00014 
00015 // Use functions from namespace std
00016 using namespace std;
00017 
00018 // REPRESENTATION
00019 //-----------------------------------------------------------------------------
00020 // define your particles
00021 typedef eoRealParticle < eoMinimizingFitness >Indi;
00022 
00023 
00024 // EVAL
00025 //-----------------------------------------------------------------------------
00026 // a simple fitness function that computes the euclidian norm of a real vector
00027 //    @param _indi A real-valued individual
00028 double real_value (const Indi & _indi)
00029 {
00030   double sum = 0;
00031   for (unsigned i = 0; i < _indi.size ()-1; i++)
00032     sum += pow(_indi[i],2);
00033   return (sum);
00034 }
00035 
00036 // GENERAL
00037 //-----------------------------------------------------------------------------
00038 void main_function (int __argc, char *__argv[])
00039 {
00040 // PARAMETRES
00041   // all parameters are hard-coded!
00042   const unsigned int VEC_SIZE = 5;      // Number of object variables in genotypes
00043   const unsigned int POP_SIZE = 30;     // Size of population
00044   const unsigned int MAX_GEN = 100;             // Maximum number of generations
00045   
00046   const double C1 = 1.3;                // Velocity learning factor 1
00047   const double C2 = 2;          // Velocity learning factor 2
00048   const double POS_MIN = -2.0;  // Minimum value for the position
00049   const double POS_MAX = 2.0;   // Maximum value for the position
00050   const double VELO_MIN = -2;   // Minimum value for the velocity
00051   const double VELO_MAX = 2;    // Maximum value for the velocity
00052   const double WEIGHT = 0.7;    // Maximum value for the velocity
00053   
00054 // SEED
00055   // Initialize the seed with the current date.
00056   rng.reseed (time(0));
00057 
00058 
00059 // EVAL
00061   // Fitness function
00063   // Evaluation: from a plain C++ fn to an EvalFunc Object
00064   eoEvalFuncPtr<Indi, double, const Indi& > eval(  real_value );
00065 
00066 
00067 // POPULATION (swarm)
00068   eoPop < Indi > pop;
00069 
00071 // PARTICLE ARCHIVE
00073   // The global best is stored in an archive
00074   eoSingleParticleArchive < Indi > archive; 
00075 
00076 
00078 // INITIALIZE THE POPULATION
00080 
00081   // Initialize the positions
00082   eoUniformGenerator < double >uGen (POS_MIN, POS_MAX); 
00083   eoInitFixedLength < Indi > random (VEC_SIZE, uGen);
00084   pop.append (POP_SIZE, random);  
00085 
00086   // Initialize the velocities
00087   eoUniformGenerator < double >sGen (VELO_MIN, VELO_MAX);
00088   eoVelocityInitFixedLength < Indi > speedRandom (VEC_SIZE, sGen);
00089   apply < Indi > (speedRandom, pop);
00090 
00091   // FIRST EVAL REQUIRED 
00092   // We must have a first fitness to compute the first "bests"
00093   apply < Indi > (eval, pop);
00094   
00095   // Initialize the particle's best
00096   eoFirstIsBestInit < Indi > localInit;
00097   apply < Indi > (localInit, pop);
00098 
00099   // Initialize the global best
00100   eoBestOfAllInit < Indi > globalInit (archive); // initialize the global besr
00101   psoApply < Indi > (globalInit, pop); // apply the initialization
00102 
00104 
00105 
00106 // OUTPUT
00107   // sort pop before printing it!
00108   pop.sort();
00109   // Print (sorted) intial population (raw printout)
00110   cout << "Initial Population" << endl;
00111   cout << pop;
00112   
00113   
00114 // VELOCITY
00115   eoInertiaFixedWeightedVelocity < Indi > velocity (archive,WEIGHT,C1, C2);
00116   eoRealVectorBounds bnds(VEC_SIZE,VELO_MIN,VELO_MAX);
00117 
00118 // FLIGHT
00119   eoStandardFlight < Indi > flight(bnds);
00120 
00121 
00122 // PARTICLE BEST UPDATER
00123   eoUpdateIfBetter < Indi > updater;
00124 
00125 
00126 // GLOBAL BEST UPDATER
00127   eoUpdateGlobalIfBetter < Indi > globalUpdater (archive);
00128 
00129 
00130   // STOP
00131 // CHECKPOINT
00133   // termination condition
00135   // stop after MAX_GEN generations
00136   eoGenContinue<Indi> continuator(MAX_GEN);
00137   
00138 
00139 // GENERATION
00141   // the algorithm
00143   // standard PSO requires
00144   // evaluation, velocity, particle best updater, global best updater and flight, stopping criterion
00145   
00146   eoEasyPSO < Indi > psa (continuator, eval, velocity, flight, updater, globalUpdater);
00147  
00148  
00149   // Apply algo to pop - that's it!
00150   psa(pop);
00151 
00152 // OUTPUT
00153   // Print (sorted) final population
00154   pop.sort();
00155   cout << "FINAL Population\n" << pop << endl;
00156 // GENERAL
00157 }
00158 
00159 
00160 // A main that catches the exceptions
00161 
00162 int main(int argc, char **argv)
00163 {
00164     try
00165     {
00166         main_function(argc, argv);
00167     }
00168     catch(exception& e)
00169     {
00170         cout << "Exception: " << e.what() << '\n';
00171     }
00172 
00173     return 1;
00174 }
00175 

Generated on Thu Apr 19 11:02:28 2007 for EO by  doxygen 1.4.7