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 00040 template < class POT > class eoStarTopology:public eoTopology <POT> 00041 { 00042 00043 public: 00044 00048 eoStarTopology ():isSetup(false){} 00049 00050 00057 void setup(const eoPop<POT> & _pop) 00058 { 00059 if (!isSetup){ 00060 00061 // put all the particles in the only neighborhood 00062 for (unsigned i=0;i < _pop.size();i++) 00063 neighborhood.put(i); 00064 00065 // set the initial global best as the best initial particle 00066 neighborhood.best(_pop.best_element()); 00067 00068 isSetup=true; 00069 } 00070 else 00071 { 00072 // Should activate this part ? 00073 /* 00074 std::string s; 00075 s.append (" Linear topology already setup in eoStarTopology"); 00076 throw std::runtime_error (s); 00077 */ 00078 } 00079 } 00080 00081 /* 00082 * Update the best fitness of the given particle if it's better. 00083 * Also replace the global best by the given particle if it's better. 00084 * @param _po - The particle to update 00085 * @param _indice - The indice of the given particle in the population 00086 */ 00087 void updateNeighborhood(POT & _po,unsigned _indice) 00088 { 00089 // update the best fitness of the particle 00090 if (_po.fitness() > _po.best()) 00091 { 00092 _po.best(_po.fitness()); 00093 for(unsigned i=0;i<_po.size();i++) 00094 _po.bestPositions[i]=_po[i]; 00095 } 00096 // update the global best if the given particle is "better" 00097 if (_po.fitness() > neighborhood.best().fitness()) 00098 { 00099 neighborhood.best(_po); 00100 } 00101 } 00102 00103 00109 POT & best (unsigned _indice) {return (neighborhood.best());} 00110 00111 /* 00112 * Return the global best of the topology 00113 */ 00114 00115 virtual POT & globalBest(const eoPop<POT>& _pop) 00116 { 00117 return neighborhood.best(); 00118 } 00119 00120 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_ */ 00142 00143 00144 00145 00146 00147 00148 00149