00001
00002
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
00016 using namespace std;
00017
00018
00019
00020
00021 typedef eoRealParticle < eoMinimizingFitness >Indi;
00022
00023
00024
00025
00026
00027
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
00037
00038 void main_function (int __argc, char *__argv[])
00039 {
00040
00041
00042 const unsigned int VEC_SIZE = 5;
00043 const unsigned int POP_SIZE = 30;
00044 const unsigned int MAX_GEN = 100;
00045
00046 const double C1 = 1.3;
00047 const double C2 = 2;
00048 const double POS_MIN = -2.0;
00049 const double POS_MAX = 2.0;
00050 const double VELO_MIN = -2;
00051 const double VELO_MAX = 2;
00052 const double WEIGHT = 0.7;
00053
00054
00055
00056 rng.reseed (time(0));
00057
00058
00059
00061
00063
00064 eoEvalFuncPtr<Indi, double, const Indi& > eval( real_value );
00065
00066
00067
00068 eoPop < Indi > pop;
00069
00071
00073
00074 eoSingleParticleArchive < Indi > archive;
00075
00076
00078
00080
00081
00082 eoUniformGenerator < double >uGen (POS_MIN, POS_MAX);
00083 eoInitFixedLength < Indi > random (VEC_SIZE, uGen);
00084 pop.append (POP_SIZE, random);
00085
00086
00087 eoUniformGenerator < double >sGen (VELO_MIN, VELO_MAX);
00088 eoVelocityInitFixedLength < Indi > speedRandom (VEC_SIZE, sGen);
00089 apply < Indi > (speedRandom, pop);
00090
00091
00092
00093 apply < Indi > (eval, pop);
00094
00095
00096 eoFirstIsBestInit < Indi > localInit;
00097 apply < Indi > (localInit, pop);
00098
00099
00100 eoBestOfAllInit < Indi > globalInit (archive);
00101 psoApply < Indi > (globalInit, pop);
00102
00104
00105
00106
00107
00108 pop.sort();
00109
00110 cout << "Initial Population" << endl;
00111 cout << pop;
00112
00113
00114
00115 eoInertiaFixedWeightedVelocity < Indi > velocity (archive,WEIGHT,C1, C2);
00116 eoRealVectorBounds bnds(VEC_SIZE,VELO_MIN,VELO_MAX);
00117
00118
00119 eoStandardFlight < Indi > flight(bnds);
00120
00121
00122
00123 eoUpdateIfBetter < Indi > updater;
00124
00125
00126
00127 eoUpdateGlobalIfBetter < Indi > globalUpdater (archive);
00128
00129
00130
00131
00133
00135
00136 eoGenContinue<Indi> continuator(MAX_GEN);
00137
00138
00139
00141
00143
00144
00145
00146 eoEasyPSO < Indi > psa (continuator, eval, velocity, flight, updater, globalUpdater);
00147
00148
00149
00150 psa(pop);
00151
00152
00153
00154 pop.sort();
00155 cout << "FINAL Population\n" << pop << endl;
00156
00157 }
00158
00159
00160
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