From ca8948486f4b465754390889d2ce2e12fb994326 Mon Sep 17 00:00:00 2001 From: nojhan Date: Sun, 1 Apr 2012 12:09:17 +0200 Subject: [PATCH] cleaning the indentation and formating of eoPop.h, added the authors --- eo/src/eoPop.h | 487 ++++++++++++++++++++++++++----------------------- 1 file changed, 260 insertions(+), 227 deletions(-) diff --git a/eo/src/eoPop.h b/eo/src/eoPop.h index 9c4c6bac5..068498aa9 100644 --- a/eo/src/eoPop.h +++ b/eo/src/eoPop.h @@ -4,26 +4,35 @@ // eoPop.h // (c) GeNeura Team, 1998 /* - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - Contact: todos@geneura.ugr.es, http://geneura.ugr.es - */ +Authors: + todos@geneura.ugr.es, http://geneura.ugr.es + jmerelo + gustavoromero + mac + maartenkeijzer + kuepper + okoenig + evomarc + Johann Dréo +*/ //----------------------------------------------------------------------------- -#ifndef _EOPOP_H -#define _EOPOP_H +#ifndef _EOPOP_H_ +#define _EOPOP_H_ #include #include @@ -54,18 +63,18 @@ template class eoPop: public std::vector, public eoObject, public eoPersistent { -public: + public: - using std::vector::size; - using std::vector::resize; - using std::vector::operator[]; - using std::vector::begin; - using std::vector::end; + using std::vector::size; + using std::vector::resize; + using std::vector::operator[]; + using std::vector::begin; + using std::vector::end; - typedef typename EOT::Fitness Fitness; + typedef typename EOT::Fitness Fitness; #if defined(__CUDACC__) - typedef typename std::vector::iterator iterator; - typedef typename std::vector::const_iterator const_iterator; + typedef typename std::vector::iterator iterator; + typedef typename std::vector::const_iterator const_iterator; #endif /** Default ctor. Creates empty pop @@ -74,272 +83,296 @@ public: /** Ctor for the initialization of chromosomes - @param _popSize total population size - @param _chromInit Initialization routine, produces EO's, needs to be an eoInit + @param _popSize total population size + @param _chromInit Initialization routine, produces EO's, needs to be an eoInit */ - eoPop( unsigned _popSize, eoInit& _chromInit ) - :std::vector() - { - resize(_popSize); - for ( unsigned i = 0; i < _popSize; i++ ) - { + eoPop( unsigned _popSize, eoInit& _chromInit ) + : std::vector() + { + resize(_popSize); + for ( unsigned i = 0; i < _popSize; i++ ) + { _chromInit(operator[](i)); - } - }; + } + } /** appends random guys at end of pop. - Can be used to initialize it pop is empty + Can be used to initialize it pop is empty - @param _newPopSize total population size - @param _chromInit Initialization routine, produces EO's, needs to be an eoInit + @param _newPopSize total population size + @param _chromInit Initialization routine, produces EO's, needs to be an eoInit */ - void append( unsigned _newPopSize, eoInit& _chromInit ) - { - unsigned oldSize = size(); - if (_newPopSize < oldSize) + void append( unsigned _newPopSize, eoInit& _chromInit ) { - throw std::runtime_error("New size smaller than old size in pop.append"); - return; + unsigned oldSize = size(); + if (_newPopSize < oldSize) + { + throw std::runtime_error("New size smaller than old size in pop.append"); + return; + } + if (_newPopSize == oldSize) + return; + resize(_newPopSize); // adjust the size + for ( unsigned i = oldSize; i < _newPopSize; i++ ) + { + _chromInit(operator[](i)); + } } - if (_newPopSize == oldSize) - return; - resize(_newPopSize); // adjust the size - for ( unsigned i = oldSize; i < _newPopSize; i++ ) + + + /** Ctor from an std::istream; reads the population from a stream, + each element should be in different lines + @param _is the stream + */ + eoPop( std::istream& _is ) :std::vector() { - _chromInit(operator[](i)); + readFrom( _is ); } - }; - /** Ctor from an std::istream; reads the population from a stream, - each element should be in different lines - @param _is the stream - */ - eoPop( std::istream& _is ) :std::vector() { - readFrom( _is ); - } - - /** Empty Dtor */ + /** Empty Dtor */ virtual ~eoPop() {} - /// helper struct for getting a pointer - struct Ref { const EOT* operator()(const EOT& eot) { return &eot;}}; - /// helper struct for comparing on pointers - struct Cmp { - bool operator()(const EOT* a, const EOT* b) const + /// helper struct for getting a pointer + struct Ref { const EOT* operator()(const EOT& eot) { return &eot;}}; + + /// helper struct for comparing on pointers + struct Cmp { + bool operator()(const EOT* a, const EOT* b) const { return b->operator<(*a); } - }; - /// helper struct for comparing (EA or PSO) - struct Cmp2 - { - bool operator()(const EOT & a,const EOT & b) const + }; + + /// helper struct for comparing (EA or PSO) + struct Cmp2 { + bool operator()(const EOT & a,const EOT & b) const + { return b.operator<(a); + } + }; + + + /** + sort the population. Use this member to sort in order + of descending Fitness, so the first individual is the best! + */ + void sort(void) + { + std::sort(begin(), end(), Cmp2()); } - }; + /** creates a std::vector pointing to the individuals in descending order */ + void sort(std::vector& result) const + { + result.resize(size()); - /** - sort the population. Use this member to sort in order - of descending Fitness, so the first individual is the best! - */ - void sort(void) - { - std::sort(begin(), end(), Cmp2()); - } + std::transform(begin(), end(), result.begin(), Ref()); - /** creates a std::vector pointing to the individuals in descending order */ - void sort(std::vector& result) const - { - result.resize(size()); + std::sort(result.begin(), result.end(), Cmp()); + } - std::transform(begin(), end(), result.begin(), Ref()); - std::sort(result.begin(), result.end(), Cmp()); - } + /** + shuffle the population. Use this member to put the population + in random order + */ + void shuffle(void) + { + UF_random_generator gen; + std::random_shuffle(begin(), end(), gen); + } - /** - shuffle the population. Use this member to put the population - in random order - */ - void shuffle(void) - { - UF_random_generator gen; - std::random_shuffle(begin(), end(), gen); - } - /** creates a std::vector pointing to the individuals in random order */ - void shuffle(std::vector& result) const - { - result.resize(size()); + /** creates a std::vector pointing to the individuals in random order */ + void shuffle(std::vector& result) const + { + result.resize(size()); - std::transform(begin(), end(), result.begin(), Ref()); + std::transform(begin(), end(), result.begin(), Ref()); - UF_random_generator gen; - std::random_shuffle(result.begin(), result.end(), gen); - } + UF_random_generator gen; + std::random_shuffle(result.begin(), result.end(), gen); + } - /** returns an iterator to the best individual DOES NOT MOVE ANYBODY */ + + /** returns an iterator to the best individual DOES NOT MOVE ANYBODY */ #if defined(__CUDACC__) - eoPop::iterator it_best_element() - { - eoPop:: iterator it = std::max_element(begin(), end()); + eoPop::iterator it_best_element() + { + eoPop:: iterator it = std::max_element(begin(), end()); #else - typename eoPop::iterator it_best_element() - { - assert( this->size() > 0 ); - typename eoPop::iterator it = std::max_element(begin(), end()); + typename eoPop::iterator it_best_element() + { + assert( this->size() > 0 ); + typename eoPop::iterator it = std::max_element(begin(), end()); #endif - return it; - } + return it; + } - /** returns an iterator to the best individual DOES NOT MOVE ANYBODY */ - const EOT & best_element() const - { + + /** returns an iterator to the best individual DOES NOT MOVE ANYBODY */ + const EOT & best_element() const + { #if defined(__CUDACC__) - eoPop::const_iterator it = std::max_element(begin(), end()); + eoPop::const_iterator it = std::max_element(begin(), end()); #else - typename eoPop::const_iterator it = std::max_element(begin(), end()); + typename eoPop::const_iterator it = std::max_element(begin(), end()); #endif - return (*it); - } + return (*it); + } - /** returns a const reference to the worse individual DOES NOT MOVE ANYBODY */ - const EOT & worse_element() const - { + + /** returns a const reference to the worse individual DOES NOT MOVE ANYBODY */ + const EOT & worse_element() const + { #if defined(__CUDACC__) - eoPop::const_iterator it = std::min_element(begin(), end()); + eoPop::const_iterator it = std::min_element(begin(), end()); #else - assert( this->size() > 0 ); - typename eoPop::const_iterator it = std::min_element(begin(), end()); + assert( this->size() > 0 ); + typename eoPop::const_iterator it = std::min_element(begin(), end()); #endif - return (*it); - } + return (*it); + } - /** returns an iterator to the worse individual DOES NOT MOVE ANYBODY */ + + /** returns an iterator to the worse individual DOES NOT MOVE ANYBODY */ #if defined(__CUDACC__) - eoPop::iterator it_worse_element() - { - eoPop::iterator it = std::min_element(begin(), end()); + eoPop::iterator it_worse_element() + { + eoPop::iterator it = std::min_element(begin(), end()); #else - typename eoPop::iterator it_worse_element() - { - assert( this->size() > 0 ); - typename eoPop::iterator it = std::min_element(begin(), end()); + typename eoPop::iterator it_worse_element() + { + assert( this->size() > 0 ); + typename eoPop::iterator it = std::min_element(begin(), end()); #endif - return it; - } + return it; + } - /** - slightly faster algorithm than sort to find all individuals that are better - than the nth individual. INDIVIDUALS ARE MOVED AROUND in the pop. - */ + + /** + slightly faster algorithm than sort to find all individuals that are better + than the nth individual. INDIVIDUALS ARE MOVED AROUND in the pop. + */ #if defined(__CUDACC__) - eoPop::iterator nth_element(int nth) - { - eoPop::iterator it = begin() + nth; + eoPop::iterator nth_element(int nth) + { + eoPop::iterator it = begin() + nth; #else - typename eoPop::iterator nth_element(int nth) - { - assert( this->size() > 0 ); - typename eoPop::iterator it = begin() + nth; + typename eoPop::iterator nth_element(int nth) + { + assert( this->size() > 0 ); + typename eoPop::iterator it = begin() + nth; #endif - std::nth_element(begin(), it, end(), std::greater()); - return it; - } + std::nth_element(begin(), it, end(), std::greater()); + return it; + } - struct GetFitness { Fitness operator()(const EOT& _eo) const { return _eo.fitness(); } }; - /** returns the fitness of the nth element */ - Fitness nth_element_fitness(int which) const - { // probably not the fastest way to do this, but what the heck + struct GetFitness { Fitness operator()(const EOT& _eo) const { return _eo.fitness(); } }; - std::vector fitness(size()); - std::transform(begin(), end(), fitness.begin(), GetFitness()); - typename std::vector::iterator it = fitness.begin() + which; - std::nth_element(fitness.begin(), it, fitness.end(), std::greater()); - return *it; - } + /** returns the fitness of the nth element */ + Fitness nth_element_fitness(int which) const + { // probably not the fastest way to do this, but what the heck - /** const nth_element function, returns pointers to sorted individuals - * up the the nth - */ - void nth_element(int which, std::vector& result) const - { + std::vector fitness(size()); + std::transform(begin(), end(), fitness.begin(), GetFitness()); - assert( this->size() > 0 ); - result.resize(size()); - std::transform(begin(), end(), result.begin(), Ref()); + typename std::vector::iterator it = fitness.begin() + which; + std::nth_element(fitness.begin(), it, fitness.end(), std::greater()); + return *it; + } - typename std::vector::iterator it = result.begin() + which; - std::nth_element(result.begin(), it, result.end(), Cmp()); - } + /** const nth_element function, returns pointers to sorted individuals + * up the the nth + */ + void nth_element(int which, std::vector& result) const + { - /** does STL swap with other pop */ - void swap(eoPop& other) - { - std::swap(static_cast& >(*this), static_cast& >(other)); - } + assert( this->size() > 0 ); + result.resize(size()); + std::transform(begin(), end(), result.begin(), Ref()); - /** - * Prints sorted pop but does NOT modify it! - * - * @param _os A std::ostream. - */ - virtual void sortedPrintOn(std::ostream& _os) const - { - std::vector result; - sort(result); - _os << size() << '\n'; - for (unsigned i = 0; i < size(); ++i) - { - _os << *result[i] << std::endl; - } - } + typename std::vector::iterator it = result.begin() + which; - /** - * Write object. It's called printOn since it prints the object _on_ a stream. - * @param _os A std::ostream. - */ - virtual void printOn(std::ostream& _os) const - { - _os << size() << '\n'; - std::copy( begin(), end(), std::ostream_iterator( _os, "\n") ); - } + std::nth_element(result.begin(), it, result.end(), Cmp()); + } - /** @name Methods from eoObject */ - //@{ - /** - * Read object. The EOT class must have a ctor from a stream; - * @param _is A std::istream. - */ - virtual void readFrom(std::istream& _is) - { - size_t sz; - _is >> sz; - resize(sz); + /** does STL swap with other pop */ + void swap(eoPop& other) + { + std::swap(static_cast& >(*this), static_cast& >(other)); + } - for (size_t i = 0; i < sz; ++i) { - operator[](i).readFrom( _is ); - } - } - /** Inherited from eoObject. Returns the class name. - @see eoObject - */ - virtual std::string className() const {return "eoPop";}; - //@} + /** + * Prints sorted pop but does NOT modify it! + * + * @param _os A std::ostream. + */ + virtual void sortedPrintOn(std::ostream& _os) const + { + std::vector result; + sort(result); + _os << size() << '\n'; + for (unsigned i = 0; i < size(); ++i) + { + _os << *result[i] << std::endl; + } + } - virtual void invalidate() - { - for (unsigned i=0; ioperator[](i).invalidate(); - } -}; -#endif + /** + * Write object. It's called printOn since it prints the object _on_ a stream. + * @param _os A std::ostream. + */ + virtual void printOn(std::ostream& _os) const + { + _os << size() << '\n'; + std::copy( begin(), end(), std::ostream_iterator( _os, "\n") ); + } + + + /** @name Methods from eoObject */ + //@{ + /** + * Read object. The EOT class must have a ctor from a stream; + * @param _is A std::istream. + */ + virtual void readFrom(std::istream& _is) + { + size_t sz; + _is >> sz; + + resize(sz); + + for (size_t i = 0; i < sz; ++i) { + operator[](i).readFrom( _is ); + } + } + + + /** Inherited from eoObject. Returns the class name. + @see eoObject + */ + virtual std::string className() const {return "eoPop";}; + //@} + + + /** Invalidate the whole population + */ + virtual void invalidate() + { + for (unsigned i=0; ioperator[](i).invalidate(); + } + +}; // class eoPop + +#endif // _EOPOP_H_ +