From cd4c12e735900404c2ad09ecb66ff4e37624bebb Mon Sep 17 00:00:00 2001 From: legrand Date: Tue, 29 May 2007 10:01:58 +0000 Subject: [PATCH] start topology for global best strategies added git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@297 331e1502-861f-0410-8da2-ba01fb791d7f --- branches/pso/eoStarTopology.h | 103 ++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 branches/pso/eoStarTopology.h diff --git a/branches/pso/eoStarTopology.h b/branches/pso/eoStarTopology.h new file mode 100644 index 000000000..d94a970d3 --- /dev/null +++ b/branches/pso/eoStarTopology.h @@ -0,0 +1,103 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoStarTopology.h +// (c) OPAC 2007 +/* + This library... + + Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef EOSTARTOPOLOGY_H_ +#define EOSTARTOPOLOGY_H_ + +//----------------------------------------------------------------------------- +#include +#include +//----------------------------------------------------------------------------- + + +/** + * Topology dedicated to "globest best" strategy. + * All the particles of the swarm belong to the same and only one neighborhood. + * The global best is stored as a protected member and updated by using the "update" method. + */ +template < class POT > class eoStarTopology:public eoTopology +{ + +public: + + /** + * The only Ctor. No parameter required. + */ + eoStarTopology (){} + + + /** + * Builds the only neighborhood that contains all the particles of the given population. + * Also initializes the global best particle with the best particle of the given population. + * @param _pop - The population used to build the only neighborhood. + */ + void setup(const eoPop & _pop) + { + // put all the particles in the only neighborhood + for(unsigned i=0;i < _pop.size();i++) + neighborhood.put(i); + + // set the initial global best as the best initial particle + neighborhood.best(_pop.best_element()); + } + + /* + * Update the best fitness of the given particle if it's better. + * Also replace the global best by the given particle if it's better. + */ + void update(POT & _po,unsigned _indice) + { + // update the best fitness of the particle + if(_po.fitness() > _po.best()) + { + _po.best(_po.fitness()); + } + // update the global best if the given particle is "better" + if(_po.fitness() > neighborhood.best().fitness()) + { + neighborhood.best(_po); + } + } + + + /** + * Return the global best particle. + */ + POT & best (unsigned _indice) {return (neighborhood.best());} + + + /** + * Print the structure of the topology on the standard output. + */ + void printOn() + { + std::cout << "{" ; + for(unsigned i=0;i< neighborhood.size();i++) + std::cout << neighborhood.get(i) << " "; + std::cout << "}" << std::endl; + } + + +protected: + eoNeighborhood neighborhood; // the only neighborhood + +}; + +#endif /*EOSTARTOPOLOGY_H_ */ + + + + + + + +