00001
00002
00003
00004
00005
00006
00007
00008 #include <stdexcept>
00009 #include <iostream>
00010 #include <sstream>
00011
00012 #include <eo>
00013
00014
00015 using namespace std;
00016
00017
00018 typedef eoMinimizingFitness FitT;
00019 typedef eoBitParticle < FitT > Particle;
00020
00021
00022
00023
00024
00025
00026
00027 double binary_value (const Particle & _particle)
00028 {
00029 double sum = 0;
00030 for (unsigned i = 0; i < _particle.size(); i++)
00031 sum +=_particle[i];
00032 return (sum);
00033 }
00034
00035
00036
00037 void main_function(int argc, char **argv)
00038 {
00039
00040
00041 const unsigned int SEED = 42;
00042
00043 const unsigned int MAX_GEN=500;
00044 const unsigned int VEC_SIZE = 10;
00045 const unsigned int POP_SIZE = 20;
00046 const unsigned int NEIGHBORHOOD_SIZE= 3;
00047
00048 const double VELOCITY_INIT_MIN= -1;
00049 const double VELOCITY_INIT_MAX= 1;
00050
00051 const double VELOCITY_MIN= -1.5;
00052 const double VELOCITY_MAX= 1.5;
00053
00054 const double INERTIA= 1;
00055 const double LEARNING_FACTOR1= 1.7;
00056 const double LEARNING_FACTOR2= 2.3;
00057
00058
00060
00062
00063
00064 rng.reseed(SEED);
00065
00066
00068
00069 eoPop<Particle> pop;
00070
00072
00073 eoEvalFuncPtr<Particle, double, const Particle& > eval( binary_value );
00074
00075
00079
00080 eoRingTopology<Particle> topology(NEIGHBORHOOD_SIZE);
00081
00082
00084
00086
00087 eoUniformGenerator<bool> uGen;
00088 eoInitFixedLength < Particle > random (VEC_SIZE, uGen);
00089 pop.append (POP_SIZE, random);
00090
00091
00092 eoUniformGenerator < double >sGen (VELOCITY_INIT_MIN, VELOCITY_INIT_MAX);
00093 eoVelocityInitFixedLength < Particle > veloRandom (VEC_SIZE, sGen);
00094
00095
00096 eoFirstIsBestInit < Particle > localInit;
00097
00098
00099
00100
00101
00102
00103 eoInitializer <Particle> fullInit(eval,veloRandom,localInit,topology,pop);
00104
00105
00106
00107 fullInit();
00108
00110
00112
00113 pop.sort();
00114
00115
00116 cout << "INITIAL POPULATION:" << endl;
00117 for (unsigned i = 0; i < pop.size(); ++i)
00118 cout << "\t best fit=" << pop[i] << endl;
00119
00120
00124
00125 eoRealVectorBounds bnds(VEC_SIZE,VELOCITY_MIN,VELOCITY_MAX);
00126
00127
00128 eoStandardVelocity <Particle> velocity (topology,INERTIA,LEARNING_FACTOR1,LEARNING_FACTOR2,bnds);
00129
00130
00134
00135 eoSigBinaryFlight <Particle> flight;
00136
00137
00141
00142 eoGenContinue <Particle> genCont (MAX_GEN);
00143
00144
00145
00147
00149
00150
00151
00152 eoEasyPSO<Particle> pso(genCont, eval, velocity, flight);
00153
00154
00155 pso(pop);
00156
00157
00158
00159 pop.sort();
00160 cout << "FINAL POPULATION:" << endl;
00161 for (unsigned i = 0; i < pop.size(); ++i)
00162 cout << "\t best fit=" << pop[i] << endl;
00163
00164 }
00165
00166
00167
00168
00169 int main(int argc, char **argv)
00170 {
00171 try
00172 {
00173 main_function(argc, argv);
00174 }
00175 catch(exception& e)
00176 {
00177 cout << "Exception: " << e.what() << '\n';
00178 }
00179
00180 return 1;
00181 }
00182