EvolvingObjects
eoStarTopology.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoStarTopology.h
00005 // (c) OPAC 2007
00006 /*
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     Contact: thomas.legrand@lifl.fr
00022              clive.canape@inria.fr
00023  */
00024 //-----------------------------------------------------------------------------
00025 
00026 #ifndef EOSTARTOPOLOGY_H_
00027 #define EOSTARTOPOLOGY_H_
00028 
00029 //-----------------------------------------------------------------------------
00030 #include <eoTopology.h>
00031 #include <eoSocialNeighborhood.h>
00032 //-----------------------------------------------------------------------------
00033 
00034 
00042 template < class POT > class eoStarTopology:public eoTopology <POT>
00043 {
00044 
00045 public:
00046 
00050     eoStarTopology ():isSetup(false){}
00051 
00052 
00059     void setup(const eoPop<POT> & _pop)
00060     {
00061         if (!isSetup){
00062 
00063             // put all the particles in the only neighborhood
00064             for (unsigned i=0;i < _pop.size();i++)
00065                 neighborhood.put(i);
00066 
00067             // set the initial global best as the best initial particle
00068             neighborhood.best(_pop.best_element());
00069 
00070             isSetup=true;
00071         }
00072         else
00073         {
00074             // Should activate this part ?
00075             /*
00076                std::string s;
00077                s.append (" Linear topology already setup in eoStarTopology");
00078                throw std::runtime_error (s);
00079                */
00080         }
00081     }
00082 
00083    /*
00084      * Update the best fitness of the given particle if it's better.
00085      * Also replace the global best by the given particle if it's better.
00086      * @param _po - The particle to update
00087      * @param _indice - The indice of the given particle in the population
00088      */
00089     void updateNeighborhood(POT & _po,unsigned _indice)
00090     {
00091         // update the best fitness of the particle
00092         if (_po.fitness() > _po.best())
00093         {
00094           _po.best(_po.fitness());
00095           for(unsigned i=0;i<_po.size();i++)
00096             _po.bestPositions[i]=_po[i];
00097         }
00098         // update the global best if the given particle is "better"
00099         if (_po.fitness() > neighborhood.best().fitness())
00100         {
00101             neighborhood.best(_po);
00102         }
00103     }
00104 
00105 
00111     POT & best (unsigned  _indice) {return (neighborhood.best());}
00112 
00113     /*
00114          * Return the global best of the topology
00115          */
00116 
00117          virtual POT & globalBest(const eoPop<POT>& _pop)
00118     {
00119         return neighborhood.best();
00120     }
00121 
00122 
00126     void printOn()
00127     {
00128         std::cout << "{" ;
00129         for (unsigned i=0;i< neighborhood.size();i++)
00130             std::cout << neighborhood.get(i) << " ";
00131         std::cout << "}" << std::endl;
00132     }
00133 
00134 
00135 
00136 protected:
00137     eoSocialNeighborhood<POT> neighborhood; // the only neighborhood
00138     bool isSetup;
00139 };
00140 
00141 #endif /*EOSTARTOPOLOGY_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Friends