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 eoRealParticle < FitT > Particle;
00020
00021
00022
00023
00024
00025
00026 FitT real_value (const Particle & _particle)
00027 {
00028 double sum = 0;
00029 for (unsigned i = 0; i < _particle.size(); i++)
00030 sum += pow(_particle[i],2);
00031 return (sqrt(sum));
00032 }
00033
00034
00035
00036 void main_function(int argc, char **argv)
00037 {
00038
00039
00040 const unsigned int SEED = 42;
00041
00042 const unsigned int MAX_GEN=100;
00043 const unsigned int VEC_SIZE = 2;
00044 const unsigned int POP_SIZE = 20;
00045 const unsigned int NEIGHBORHOOD_SIZE= 5;
00046
00047 const double POS_INIT_MIN= -2;
00048 const double POS_INIT_MAX= 2;
00049
00050 const double VELOCITY_INIT_MIN= -1;
00051 const double VELOCITY_INIT_MAX= 1;
00052
00053 const double VELOCITY_MIN= -1.5;
00054 const double VELOCITY_MAX= 1.5;
00055
00056 const double INERTIA= 1;
00057 const double LEARNING_FACTOR1= 1.7;
00058 const double LEARNING_FACTOR2= 2.3;
00059
00061
00063
00064
00065 rng.reseed(SEED);
00066
00067
00069
00070 eoPop<Particle> pop;
00071
00073
00074 eoEvalFuncPtr<Particle, FitT, const Particle& > eval( real_value );
00075
00076
00080
00081 eoLinearTopology<Particle> topology(NEIGHBORHOOD_SIZE);
00082
00083
00085
00087
00088 eoUniformGenerator < double >uGen (POS_INIT_MIN, POS_INIT_MAX);
00089 eoInitFixedLength < Particle > random (VEC_SIZE, uGen);
00090 pop.append (POP_SIZE, random);
00091
00092
00093 eoUniformGenerator < double >sGen (VELOCITY_INIT_MIN, VELOCITY_INIT_MAX);
00094 eoVelocityInitFixedLength < Particle > veloRandom (VEC_SIZE, sGen);
00095
00096
00097 eoFirstIsBestInit < Particle > localInit;
00098
00099
00100
00101
00102
00103
00104 eoInitializer <Particle> fullInit(eval,veloRandom,localInit,topology,pop);
00105
00106
00107
00108 fullInit();
00109
00111
00113
00114 pop.sort();
00115
00116
00117 cout << "INITIAL POPULATION:" << endl;
00118 for (unsigned i = 0; i < pop.size(); ++i)
00119 cout << "\t best fit=" << pop[i] << endl;
00120
00121
00125
00126 eoRealVectorBounds bnds(VEC_SIZE,VELOCITY_MIN,VELOCITY_MAX);
00127
00128
00129 eoStandardVelocity <Particle> velocity (topology,INERTIA,LEARNING_FACTOR1,LEARNING_FACTOR2,bnds);
00130
00131
00135
00136 eoStandardFlight <Particle> flight;
00137
00138
00142
00143 eoGenContinue <Particle> genCont (MAX_GEN);
00144
00145
00146
00148
00150
00151
00152
00153 eoEasyPSO<Particle> pso(genCont, eval, velocity, flight);
00154
00155
00156 pso(pop);
00157
00158
00159
00160 pop.sort();
00161 cout << "FINAL POPULATION:" << endl;
00162 for (unsigned i = 0; i < pop.size(); ++i)
00163 cout << "\t best fit=" << pop[i] << endl;
00164
00165 }
00166
00167
00168
00169
00170 int main(int argc, char **argv)
00171 {
00172 try
00173 {
00174 main_function(argc, argv);
00175 }
00176 catch(exception& e)
00177 {
00178 cout << "Exception: " << e.what() << '\n';
00179 }
00180
00181 return 1;
00182 }
00183