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 #ifndef EOLINEARTOPOLOGY_H_
00027 #define EOLINEARTOPOLOGY_H_
00028
00029
00030 #include <eoPop.h>
00031 #include <eoTopology.h>
00032 #include <eoSocialNeighborhood.h>
00033
00034
00035
00041 template < class POT > class eoLinearTopology:public eoTopology <
00042 POT >
00043 {
00044
00045 public:
00046
00051 eoLinearTopology (unsigned _neighborhoodSize):neighborhoodSize (_neighborhoodSize),isSetup(false){}
00052
00053
00060 void setup(const eoPop<POT> & _pop)
00061 {
00062 if (!isSetup)
00063 {
00064
00065 if (neighborhoodSize >= _pop.size()){
00066 std::string s;
00067 s.append (" Invalid neighborhood size in eoLinearTopology ");
00068 throw std::runtime_error (s);
00069 }
00070
00071 unsigned howManyNeighborhood=_pop.size()/ neighborhoodSize;
00072
00073
00074 for (unsigned i=0;i< howManyNeighborhood;i++)
00075 {
00076 eoSocialNeighborhood<POT> currentNghd;
00077
00078 currentNghd.best(_pop[i*neighborhoodSize]);
00079 for (unsigned k=i*neighborhoodSize;k < neighborhoodSize*(i+1);k++)
00080 {
00081 currentNghd.put(k);
00082 if (_pop[k].fitness() > currentNghd.best().fitness())
00083 currentNghd.best(_pop[k]);
00084 }
00085 neighborhoods.push_back(currentNghd);
00086 }
00087
00088
00089 if (_pop.size()%neighborhoodSize !=0)
00090 {
00091 for (unsigned z=_pop.size()-1;z >= (_pop.size()-_pop.size()%neighborhoodSize);z--){
00092 neighborhoods.back().put(z);
00093
00094 if (_pop[z].fitness() > neighborhoods.back().best().fitness())
00095 neighborhoods.back().best(_pop[z]);
00096 }
00097 }
00098
00099 isSetup=true;
00100 }
00101 else
00102 {
00103
00104
00105
00106
00107
00108
00109 }
00110
00111 }
00112
00113
00118 unsigned retrieveNeighborhoodByIndice(unsigned _indice)
00119 {
00120 unsigned i=0;
00121 for (i=0;i< neighborhoods.size();i++)
00122 {
00123 if (neighborhoods[i].contains(_indice))
00124 {
00125 return i;
00126 }
00127 }
00128 return i;
00129 }
00130
00135 void updateNeighborhood(POT & _po,unsigned _indice)
00136 {
00137
00138 if (_po.fitness() > _po.best())
00139 {
00140 _po.best(_po.fitness());
00141 for(unsigned i=0;i<_po.size();i++)
00142 _po.bestPositions[i]=_po[i];
00143 }
00144
00145
00146 unsigned theGoodNhbd= retrieveNeighborhoodByIndice(_indice);
00147 if (_po.fitness() > neighborhoods[theGoodNhbd].best().fitness())
00148 {
00149 neighborhoods[theGoodNhbd].best(_po);
00150 }
00151 }
00152
00158 POT & best (unsigned _indice)
00159 {
00160 unsigned theGoodNhbd= retrieveNeighborhoodByIndice(_indice);
00161
00162 return (neighborhoods[theGoodNhbd].best());
00163 }
00164
00165
00166
00167
00168
00169 virtual POT & globalBest()
00170 {
00171 POT gBest,tmp;
00172 unsigned indGlobalBest=0;
00173 if(neighborhoods.size()==1)
00174 return neighborhoods[0].best();
00175
00176 gBest=neighborhoods[0].best();
00177 for(unsigned i=1;i<neighborhoods.size();i++)
00178 {
00179 tmp=neighborhoods[i].best();
00180 if(gBest.best() < tmp.best())
00181 {
00182 gBest=tmp;
00183 indGlobalBest=i;
00184 }
00185
00186 }
00187 return neighborhoods[indGlobalBest].best();
00188 }
00189
00193 void printOn()
00194 {
00195 for (unsigned i=0;i< neighborhoods.size();i++)
00196 {
00197 std::cout << "{ " ;
00198 for (unsigned j=0;j< neighborhoods[i].size();j++)
00199 {
00200 std::cout << neighborhoods[i].get(j) << " ";
00201 }
00202 std::cout << "}" << std::endl;
00203 }
00204 }
00205
00206
00207 protected:
00208 std::vector<eoSocialNeighborhood<POT> > neighborhoods;
00209 unsigned neighborhoodSize;
00210
00211 bool isSetup;
00212
00213 };
00214
00215 #endif
00216
00217
00218
00219
00220
00221
00222
00223