EvolvingObjects
eoRingTopology.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoRingTopology.h
00005 // (c) INRIA Futurs DOLPHIN 2007
00006 /*
00007     Clive Canape
00008         Thomas Legrand
00009 
00010     This library is free software; you can redistribute it and/or
00011     modify it under the terms of the GNU Lesser General Public
00012     License as published by the Free Software Foundation; either
00013     version 2 of the License, or (at your option) any later version.
00014 
00015     This library is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018     Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public
00021     License along with this library; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 
00024     Contact: clive.canape@inria.fr
00025  */
00026 //-----------------------------------------------------------------------------
00027 
00028 #ifndef EORINGTOPOLOGY_H_
00029 #define EORINGTOPOLOGY_H_
00030 
00031 //-----------------------------------------------------------------------------
00032 #include <eoTopology.h>
00033 #include <eoSocialNeighborhood.h>
00034 //-----------------------------------------------------------------------------
00035 
00036 
00045 template < class POT > class eoRingTopology:public eoTopology <POT>
00046 {
00047 
00048 public:
00049 
00054     eoRingTopology (unsigned _neighborhoodSize):neighborhoodSize (_neighborhoodSize),isSetup(false){}
00055 
00056 
00063     void setup(const eoPop<POT> & _pop)
00064     {
00065         if (!isSetup){
00066 
00067             // put in the neighborhood
00068             int k = neighborhoodSize/2;
00069             for (unsigned i=0;i < _pop.size();i++)
00070             {
00071                 eoSocialNeighborhood<POT> currentNghd;
00072                 currentNghd.best(_pop[i]);
00073                 for (unsigned j=0; j < neighborhoodSize; j++)
00074                 {
00075                         currentNghd.put((_pop.size()+i-k+j)%_pop.size());
00076                         if(_pop[(_pop.size()+i-k+j)%_pop.size()].fitness() > currentNghd.best().fitness())
00077                         currentNghd.best(_pop[(_pop.size()+i-k+j)%_pop.size()]);
00078                 }
00079                 neighborhoods.push_back(currentNghd);
00080             }
00081             isSetup=true;
00082         }
00083         else
00084         {
00085             // Should activate this part ?
00086             /*
00087                std::string s;
00088                s.append (" Linear topology already setup in eoRingTopology");
00089                throw std::runtime_error (s);
00090                */
00091         }
00092     }
00093 
00098     unsigned retrieveNeighborhoodByIndice(unsigned _indice)
00099     {
00100         return _indice;
00101     }
00102 
00103 
00110     void updateNeighborhood(POT & _po,unsigned _indice)
00111     {
00112         //this->printOn();exit(0);
00113         // update the best fitness of the particle
00114         if (_po.fitness() > _po.best())
00115         {
00116             _po.best(_po.fitness());
00117             for(unsigned i=0;i<_po.size();i++)
00118                         _po.bestPositions[i]=_po[i];
00119         }
00120         // update the global best if the given particle is "better"
00121         for (unsigned i=-neighborhoodSize+1; i < neighborhoodSize; i++)
00122                 {
00123                         unsigned indi = (_po.size()+_indice+i)%_po.size();
00124                         if (_po.fitness() > neighborhoods[indi].best().fitness())
00125                                 neighborhoods[indi].best(_po);
00126                 }
00127      }
00128 
00129 
00135     POT & best (unsigned  _indice)
00136     {
00137         unsigned theGoodNhbd= retrieveNeighborhoodByIndice(_indice);
00138 
00139         return (neighborhoods[theGoodNhbd].best());
00140     }
00141 
00142 
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          * Return the global best of the topology
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 };
00192 #endif /*EORINGTOPOLOGY_H_*/
 All Classes Namespaces Files Functions Variables Typedefs Friends