cleaning the indentation and formating of eoPop.h, added the authors

This commit is contained in:
nojhan 2012-04-01 12:09:17 +02:00
commit ca8948486f

View file

@ -18,12 +18,21 @@
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 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 <johann.dreo@thalesgroup.com>
*/
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef _EOPOP_H #ifndef _EOPOP_H_
#define _EOPOP_H #define _EOPOP_H_
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
@ -54,7 +63,7 @@
template<class EOT> template<class EOT>
class eoPop: public std::vector<EOT>, public eoObject, public eoPersistent class eoPop: public std::vector<EOT>, public eoObject, public eoPersistent
{ {
public: public:
using std::vector<EOT>::size; using std::vector<EOT>::size;
using std::vector<EOT>::resize; using std::vector<EOT>::resize;
@ -78,14 +87,14 @@ public:
@param _chromInit Initialization routine, produces EO's, needs to be an eoInit @param _chromInit Initialization routine, produces EO's, needs to be an eoInit
*/ */
eoPop( unsigned _popSize, eoInit<EOT>& _chromInit ) eoPop( unsigned _popSize, eoInit<EOT>& _chromInit )
:std::vector<EOT>() : std::vector<EOT>()
{ {
resize(_popSize); resize(_popSize);
for ( unsigned i = 0; i < _popSize; i++ ) for ( unsigned i = 0; i < _popSize; i++ )
{ {
_chromInit(operator[](i)); _chromInit(operator[](i));
} }
}; }
/** appends random guys at end of pop. /** 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
@ -108,28 +117,32 @@ public:
{ {
_chromInit(operator[](i)); _chromInit(operator[](i));
} }
}; }
/** Ctor from an std::istream; reads the population from a stream, /** Ctor from an std::istream; reads the population from a stream,
each element should be in different lines each element should be in different lines
@param _is the stream @param _is the stream
*/ */
eoPop( std::istream& _is ) :std::vector<EOT>() { eoPop( std::istream& _is ) :std::vector<EOT>()
{
readFrom( _is ); readFrom( _is );
} }
/** Empty Dtor */ /** Empty Dtor */
virtual ~eoPop() {} virtual ~eoPop() {}
/// helper struct for getting a pointer /// helper struct for getting a pointer
struct Ref { const EOT* operator()(const EOT& eot) { return &eot;}}; struct Ref { const EOT* operator()(const EOT& eot) { return &eot;}};
/// helper struct for comparing on pointers /// helper struct for comparing on pointers
struct Cmp { struct Cmp {
bool operator()(const EOT* a, const EOT* b) const bool operator()(const EOT* a, const EOT* b) const
{ return b->operator<(*a); } { return b->operator<(*a); }
}; };
/// helper struct for comparing (EA or PSO) /// helper struct for comparing (EA or PSO)
struct Cmp2 struct Cmp2
{ {
@ -140,7 +153,6 @@ public:
}; };
/** /**
sort the population. Use this member to sort in order sort the population. Use this member to sort in order
of descending Fitness, so the first individual is the best! of descending Fitness, so the first individual is the best!
@ -150,6 +162,7 @@ public:
std::sort(begin(), end(), Cmp2()); std::sort(begin(), end(), Cmp2());
} }
/** creates a std::vector<EOT*> pointing to the individuals in descending order */ /** creates a std::vector<EOT*> pointing to the individuals in descending order */
void sort(std::vector<const EOT*>& result) const void sort(std::vector<const EOT*>& result) const
{ {
@ -160,6 +173,7 @@ public:
std::sort(result.begin(), result.end(), Cmp()); std::sort(result.begin(), result.end(), Cmp());
} }
/** /**
shuffle the population. Use this member to put the population shuffle the population. Use this member to put the population
in random order in random order
@ -170,6 +184,7 @@ public:
std::random_shuffle(begin(), end(), gen); std::random_shuffle(begin(), end(), gen);
} }
/** creates a std::vector<EOT*> pointing to the individuals in random order */ /** creates a std::vector<EOT*> pointing to the individuals in random order */
void shuffle(std::vector<const EOT*>& result) const void shuffle(std::vector<const EOT*>& result) const
{ {
@ -181,6 +196,7 @@ public:
std::random_shuffle(result.begin(), result.end(), 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__) #if defined(__CUDACC__)
eoPop<EOT>::iterator it_best_element() eoPop<EOT>::iterator it_best_element()
@ -195,6 +211,7 @@ public:
return it; return it;
} }
/** returns an iterator to the best individual DOES NOT MOVE ANYBODY */ /** returns an iterator to the best individual DOES NOT MOVE ANYBODY */
const EOT & best_element() const const EOT & best_element() const
{ {
@ -206,6 +223,7 @@ public:
return (*it); return (*it);
} }
/** returns a const reference to the worse individual DOES NOT MOVE ANYBODY */ /** returns a const reference to the worse individual DOES NOT MOVE ANYBODY */
const EOT & worse_element() const const EOT & worse_element() const
{ {
@ -218,6 +236,7 @@ public:
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__) #if defined(__CUDACC__)
eoPop<EOT>::iterator it_worse_element() eoPop<EOT>::iterator it_worse_element()
@ -232,6 +251,7 @@ public:
return it; return it;
} }
/** /**
slightly faster algorithm than sort to find all individuals that are better slightly faster algorithm than sort to find all individuals that are better
than the nth individual. INDIVIDUALS ARE MOVED AROUND in the pop. than the nth individual. INDIVIDUALS ARE MOVED AROUND in the pop.
@ -250,8 +270,10 @@ public:
return it; return it;
} }
struct GetFitness { Fitness operator()(const EOT& _eo) const { return _eo.fitness(); } }; struct GetFitness { Fitness operator()(const EOT& _eo) const { return _eo.fitness(); } };
/** returns the fitness of the nth element */ /** returns the fitness of the nth element */
Fitness nth_element_fitness(int which) const Fitness nth_element_fitness(int which) const
{ // probably not the fastest way to do this, but what the heck { // probably not the fastest way to do this, but what the heck
@ -264,6 +286,7 @@ public:
return *it; return *it;
} }
/** const nth_element function, returns pointers to sorted individuals /** const nth_element function, returns pointers to sorted individuals
* up the the nth * up the the nth
*/ */
@ -279,12 +302,14 @@ public:
std::nth_element(result.begin(), it, result.end(), Cmp()); std::nth_element(result.begin(), it, result.end(), Cmp());
} }
/** does STL swap with other pop */ /** does STL swap with other pop */
void swap(eoPop<EOT>& other) void swap(eoPop<EOT>& other)
{ {
std::swap(static_cast<std::vector<EOT>& >(*this), static_cast<std::vector<EOT>& >(other)); std::swap(static_cast<std::vector<EOT>& >(*this), static_cast<std::vector<EOT>& >(other));
} }
/** /**
* Prints sorted pop but does NOT modify it! * Prints sorted pop but does NOT modify it!
* *
@ -301,6 +326,7 @@ public:
} }
} }
/** /**
* Write object. It's called printOn since it prints the object _on_ a stream. * Write object. It's called printOn since it prints the object _on_ a stream.
* @param _os A std::ostream. * @param _os A std::ostream.
@ -311,6 +337,7 @@ public:
std::copy( begin(), end(), std::ostream_iterator<EOT>( _os, "\n") ); std::copy( begin(), end(), std::ostream_iterator<EOT>( _os, "\n") );
} }
/** @name Methods from eoObject */ /** @name Methods from eoObject */
//@{ //@{
/** /**
@ -329,17 +356,23 @@ public:
} }
} }
/** Inherited from eoObject. Returns the class name. /** Inherited from eoObject. Returns the class name.
@see eoObject @see eoObject
*/ */
virtual std::string className() const {return "eoPop";}; virtual std::string className() const {return "eoPop";};
//@} //@}
/** Invalidate the whole population
*/
virtual void invalidate() virtual void invalidate()
{ {
for (unsigned i=0; i<size(); i++) for (unsigned i=0; i<size(); i++)
this->operator[](i).invalidate(); this->operator[](i).invalidate();
} }
};
#endif }; // class eoPop
#endif // _EOPOP_H_