00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef peoPSO_h
00037 #define peoPSO_h
00038
00039 #include <eoPop.h>
00040 #include <utils/eoRNG.h>
00041 #include <eoFunctor.h>
00042 #include <eoMerge.h>
00043 #include <eoReduce.h>
00044 #include <eoReplacement.h>
00045 #include <utils/eoHowMany.h>
00046 #include <eoSelectOne.h>
00047
00048
00054 template <class POT> class peoPSOSelect: public eoSelectOne<POT>
00055 {
00056 public:
00057
00060 peoPSOSelect(eoTopology < POT > & _topology):topology(_topology)
00061 {}
00062
00064 typedef typename PO < POT >::Fitness Fitness;
00065
00069 virtual const POT& operator()(const eoPop<POT>& _pop)
00070 {
00071 return topology.globalBest(_pop);
00072 }
00073
00074 private:
00076 eoTopology < POT > & topology;
00077 };
00078
00084 template <class POT>
00085 class peoGlobalBestVelocity : public eoReplacement<POT>
00086 {
00087 public:
00088
00090 typedef typename POT::ParticleVelocityType VelocityType;
00091
00095 peoGlobalBestVelocity( const double & _c3, eoVelocity < POT > &_velocity): c3 (_c3),velocity (_velocity)
00096 {}
00097
00101 void operator()(eoPop<POT>& _dest, eoPop<POT>& _source)
00102 {
00103
00104 VelocityType newVelocity,r3;
00105 r3 = (VelocityType) rng.uniform (1) * c3;
00106 for (unsigned i=0;i<_dest.size();i++)
00107 for (unsigned j=0;j<_dest[i].size();j++)
00108 {
00109 newVelocity= _dest[i].velocities[j] + r3 * (_source[0].bestPositions[j] - _dest[i][j]);
00110 _dest[i].velocities[j]=newVelocity;
00111 }
00112
00113 }
00114
00115 protected:
00118 const double & c3;
00119 eoVelocity < POT > & velocity;
00120 };
00121
00127 template <class POT> class peoWorstPositionReplacement : public eoReplacement<POT>
00128 {
00129 public:
00131 peoWorstPositionReplacement()
00132 {}
00133
00137 void operator()(eoPop<POT>& _dest, eoPop<POT>& _source)
00138 {
00139 unsigned ind=0;
00140 double best=_dest[0].best();
00141 for (unsigned j=1;j<_dest.size();j++)
00142 if (_dest[j].best() < best)
00143 {
00144 ind=j;
00145 best=_dest[j].best();
00146 }
00147 if (_dest[ind].best() < _source[0].best())
00148 {
00149 _dest[ind].best(_source[0].best());
00150 for (unsigned j=0;j<_dest[ind].size();j++)
00151 _dest[ind].bestPositions[j]=_source[0].bestPositions[j];
00152 }
00153 }
00154 };
00155
00156 #endif