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 #ifndef EORINGTOPOLOGY_H_
00029 #define EORINGTOPOLOGY_H_
00030
00031
00032 #include <eoTopology.h>
00033 #include <eoSocialNeighborhood.h>
00034
00035
00036
00043 template < class POT > class eoRingTopology:public eoTopology <POT>
00044 {
00045
00046 public:
00047
00052 eoRingTopology (unsigned _neighborhoodSize):neighborhoodSize (_neighborhoodSize),isSetup(false){}
00053
00054
00061 void setup(const eoPop<POT> & _pop)
00062 {
00063 if (!isSetup){
00064
00065
00066 int k = neighborhoodSize/2;
00067 for (unsigned i=0;i < _pop.size();i++)
00068 {
00069 eoSocialNeighborhood<POT> currentNghd;
00070 currentNghd.best(_pop[i]);
00071 for (unsigned j=0; j < neighborhoodSize; j++)
00072 {
00073 currentNghd.put((_pop.size()+i-k+j)%_pop.size());
00074 if(_pop[(_pop.size()+i-k+j)%_pop.size()].fitness() > currentNghd.best().fitness())
00075 currentNghd.best(_pop[(_pop.size()+i-k+j)%_pop.size()]);
00076 }
00077 neighborhoods.push_back(currentNghd);
00078 }
00079 isSetup=true;
00080 }
00081 else
00082 {
00083
00084
00085
00086
00087
00088
00089 }
00090 }
00091
00096 unsigned retrieveNeighborhoodByIndice(unsigned _indice)
00097 {
00098 return _indice;
00099 }
00100
00101
00108 void updateNeighborhood(POT & _po,unsigned _indice)
00109 {
00110
00111
00112 if (_po.fitness() > _po.best())
00113 {
00114 _po.best(_po.fitness());
00115 for(unsigned i=0;i<_po.size();i++)
00116 _po.bestPositions[i]=_po[i];
00117 }
00118
00119 for (unsigned i=-neighborhoodSize+1; i < neighborhoodSize; i++)
00120 {
00121 unsigned indi = (_po.size()+_indice+i)%_po.size();
00122 if (_po.fitness() > neighborhoods[indi].best().fitness())
00123 neighborhoods[indi].best(_po);
00124 }
00125 }
00126
00127
00133 POT & best (unsigned _indice)
00134 {
00135 unsigned theGoodNhbd= retrieveNeighborhoodByIndice(_indice);
00136
00137 return (neighborhoods[theGoodNhbd].best());
00138 }
00139
00140
00146 void printOn()
00147 {
00148 for (unsigned i=0;i< neighborhoods.size();i++)
00149 {
00150 std::cout << "{ " ;
00151 for (unsigned j=0;j< neighborhoods[i].size();j++)
00152 {
00153 std::cout << neighborhoods[i].get(j) << " ";
00154 }
00155 std::cout << "}" << std::endl;
00156 }
00157 }
00158
00159
00160
00161
00162 virtual POT & globalBest()
00163 {
00164 POT gBest,tmp;
00165 unsigned indGlobalBest=0;
00166 if(neighborhoods.size()==1)
00167 return neighborhoods[0].best();
00168
00169 gBest=neighborhoods[0].best();
00170 for(unsigned i=1;i<neighborhoods.size();i++)
00171 {
00172 tmp=neighborhoods[i].best();
00173 if(gBest.best() < tmp.best())
00174 {
00175 gBest=tmp;
00176 indGlobalBest=i;
00177 }
00178
00179 }
00180 return neighborhoods[indGlobalBest].best();
00181 }
00182
00183
00184 protected:
00185 std::vector<eoSocialNeighborhood<POT> > neighborhoods;
00186 unsigned neighborhoodSize;
00187 bool isSetup;
00188 };
00189
00190 #endif