eoLinearTopology.h

00001 
00002 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00003 
00004 //-----------------------------------------------------------------------------
00005 // eoLinearTopology.h
00006 // (c) OPAC 2007
00007 /*
00008     This library...
00009 
00010     Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
00011  */
00012 //-----------------------------------------------------------------------------
00013 
00014 #ifndef EOLINEARTOPOLOGY_H_
00015 #define EOLINEARTOPOLOGY_H_
00016 
00017 //-----------------------------------------------------------------------------
00018 #include <eoPop.h>
00019 #include <eoTopology.h>
00020 #include <eoSocialNeighborhood.h>
00021 //-----------------------------------------------------------------------------
00022 
00023 
00029 template < class POT > class eoLinearTopology:public eoTopology <
00030             POT >
00031 {
00032 
00033 public:
00034 
00039     eoLinearTopology (unsigned _neighborhoodSize):neighborhoodSize (_neighborhoodSize),isSetup(false){}
00040 
00041 
00048     void setup(const eoPop<POT> & _pop)
00049     {
00050         if (!isSetup)
00051         {
00052             // consitency check
00053             if (neighborhoodSize >= _pop.size()){
00054                 std::string s;
00055                 s.append (" Invalid neighborhood size in eoLinearTopology ");
00056                 throw std::runtime_error (s);
00057             }
00058 
00059             unsigned howManyNeighborhood=_pop.size()/ neighborhoodSize;
00060 
00061             // build all the neighborhoods
00062             for (unsigned i=0;i< howManyNeighborhood;i++)
00063             {
00064                 eoSocialNeighborhood<POT> currentNghd;
00065 
00066                 currentNghd.best(_pop[i*neighborhoodSize]);
00067                 for (unsigned k=i*neighborhoodSize;k < neighborhoodSize*(i+1);k++)
00068                 {
00069                     currentNghd.put(k);
00070                     if (_pop[k].fitness() > currentNghd.best().fitness())
00071                         currentNghd.best(_pop[k]);
00072                 }
00073                 neighborhoods.push_back(currentNghd);
00074             }
00075 
00076             // assign the last neighborhood to the remaining particles
00077             if (_pop.size()%neighborhoodSize !=0)
00078             {
00079                 for (unsigned z=_pop.size()-1;z >= (_pop.size()-_pop.size()%neighborhoodSize);z--){
00080                     neighborhoods.back().put(z);
00081 
00082                     if (_pop[z].fitness() > neighborhoods.back().best().fitness())
00083                         neighborhoods.back().best(_pop[z]);
00084                 }
00085             }
00086 
00087             isSetup=true;
00088         }
00089         else
00090         {
00091             // Should activate this part ?
00092             /*
00093                std::string s;
00094                s.append (" Linear topology already setup in eoLinearTopology");
00095                throw std::runtime_error (s);
00096                */
00097         }
00098 
00099     }
00100 
00101 
00106     unsigned retrieveNeighborhoodByIndice(unsigned _indice)
00107     {
00108         unsigned i=0;
00109         for (i=0;i< neighborhoods.size();i++)
00110         {
00111             if (neighborhoods[i].contains(_indice))
00112             {
00113                 return i;
00114             }
00115         }
00116         return i;
00117     }
00118 
00119 
00124     void updateNeighborhood(POT & _po,unsigned _indice)
00125     {
00126         // update the best fitness of the particle
00127         if (_po.fitness() > _po.best())
00128         {
00129             _po.best(_po.fitness());
00130         }
00131 
00132         // update the best in its neighborhood
00133         unsigned theGoodNhbd= retrieveNeighborhoodByIndice(_indice);
00134         if (_po.fitness() > neighborhoods[theGoodNhbd].best().fitness())
00135         {
00136             neighborhoods[theGoodNhbd].best(_po);
00137         }
00138     }
00139 
00140 
00146     POT & best (unsigned  _indice)
00147     {
00148         unsigned theGoodNhbd= retrieveNeighborhoodByIndice(_indice);
00149 
00150         return (neighborhoods[theGoodNhbd].best());
00151     }
00152 
00153 
00157     void printOn()
00158     {
00159         for (unsigned i=0;i< neighborhoods.size();i++)
00160         {
00161             std::cout << "{ " ;
00162             for (unsigned j=0;j< neighborhoods[i].size();j++)
00163             {
00164                 std::cout << neighborhoods[i].get(j) << " ";
00165             }
00166             std::cout << "}" << std::endl;
00167         }
00168     }
00169 
00170 
00171 protected:
00172         std::vector<eoSocialNeighborhood<POT> >  neighborhoods;
00173     unsigned neighborhoodSize; // the size of each neighborhood
00174     
00175     bool isSetup;
00176 
00177 };
00178 
00179 #endif /*EOLINEARTOPOLOGY_H_ */
00180 
00181 
00182 
00183 
00184 
00185 
00186 
00187 

Generated on Fri Jun 22 10:17:02 2007 for EO-PSO by  doxygen 1.4.7