* indentations + whitespace cleanup
This commit is contained in:
parent
8457e39efe
commit
56c6edab04
285 changed files with 6068 additions and 6223 deletions
151
eo/src/eoPop.h
151
eo/src/eoPop.h
|
|
@ -1,9 +1,9 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoPop.h
|
||||
// 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
|
||||
|
|
@ -36,16 +36,16 @@
|
|||
#include <eoInit.h>
|
||||
#include <utils/rnd_generators.h> // for shuffle method
|
||||
|
||||
/** A std::vector of EO object, to be used in all algorithms
|
||||
/** A std::vector of EO object, to be used in all algorithms
|
||||
* (selectors, operators, replacements, ...).
|
||||
*
|
||||
* We have no idea if a population can be
|
||||
* some other thing that a std::vector, but if somebody thinks of it, this concrete
|
||||
* implementation can be moved to "generic" and an abstract Population
|
||||
* implementation can be moved to "generic" and an abstract Population
|
||||
* interface be provided.
|
||||
*
|
||||
* The template can be instantiated with anything that accepts a "size"
|
||||
* and eoInit derived object. in the ctor.
|
||||
* The template can be instantiated with anything that accepts a "size"
|
||||
* and eoInit derived object. in the ctor.
|
||||
* EOT must also have a copy ctor, since temporaries are created and then
|
||||
* passed to the eoInit object
|
||||
*
|
||||
|
|
@ -55,62 +55,62 @@ template<class EOT>
|
|||
class eoPop: public std::vector<EOT>, public eoObject, public eoPersistent
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
using std::vector<EOT>::size;
|
||||
using std::vector<EOT>::resize;
|
||||
using std::vector<EOT>::operator[];
|
||||
using std::vector<EOT>::begin;
|
||||
using std::vector<EOT>::end;
|
||||
|
||||
|
||||
typedef typename EOT::Fitness Fitness;
|
||||
#if defined(__CUDACC__)
|
||||
typedef typename std::vector<EOT>::iterator iterator;
|
||||
typedef typename std::vector<EOT>::const_iterator const_iterator;
|
||||
#endif
|
||||
|
||||
/** Default ctor. Creates empty pop
|
||||
*/
|
||||
eoPop() : std::vector<EOT>(), eoObject(), eoPersistent() {};
|
||||
|
||||
/** Ctor for the initialization of chromosomes
|
||||
|
||||
@param _popSize total population size
|
||||
@param _chromInit Initialization routine, produces EO's, needs to be an eoInit
|
||||
*/
|
||||
/** Default ctor. Creates empty pop
|
||||
*/
|
||||
eoPop() : std::vector<EOT>(), eoObject(), eoPersistent() {};
|
||||
|
||||
/** Ctor for the initialization of chromosomes
|
||||
|
||||
@param _popSize total population size
|
||||
@param _chromInit Initialization routine, produces EO's, needs to be an eoInit
|
||||
*/
|
||||
eoPop( unsigned _popSize, eoInit<EOT>& _chromInit )
|
||||
:std::vector<EOT>()
|
||||
:std::vector<EOT>()
|
||||
{
|
||||
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.
|
||||
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
|
||||
*/
|
||||
/** appends random guys at end of pop.
|
||||
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
|
||||
*/
|
||||
void append( unsigned _newPopSize, eoInit<EOT>& _chromInit )
|
||||
{
|
||||
unsigned oldSize = size();
|
||||
if (_newPopSize < oldSize)
|
||||
{
|
||||
throw std::runtime_error("New size smaller than old size in pop.append");
|
||||
return;
|
||||
}
|
||||
{
|
||||
throw std::runtime_error("New size smaller than old size in pop.append");
|
||||
return;
|
||||
}
|
||||
if (_newPopSize == oldSize)
|
||||
return;
|
||||
resize(_newPopSize); // adjust the size
|
||||
return;
|
||||
resize(_newPopSize); // adjust the size
|
||||
for ( unsigned i = oldSize; i < _newPopSize; i++ )
|
||||
{
|
||||
_chromInit(operator[](i));
|
||||
}
|
||||
{
|
||||
_chromInit(operator[](i));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
/** Ctor from an std::istream; reads the population from a stream,
|
||||
each element should be in different lines
|
||||
@param _is the stream
|
||||
|
|
@ -118,9 +118,9 @@ public:
|
|||
eoPop( std::istream& _is ) :std::vector<EOT>() {
|
||||
readFrom( _is );
|
||||
}
|
||||
|
||||
|
||||
/** Empty Dtor */
|
||||
virtual ~eoPop() {}
|
||||
virtual ~eoPop() {}
|
||||
|
||||
|
||||
/// helper struct for getting a pointer
|
||||
|
|
@ -130,16 +130,16 @@ public:
|
|||
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
|
||||
{
|
||||
return b.operator<(a);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/// 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
|
||||
|
|
@ -185,10 +185,10 @@ public:
|
|||
#if defined(__CUDACC__)
|
||||
eoPop<EOT>::iterator it_best_element()
|
||||
{
|
||||
eoPop<EOT>:: iterator it = std::max_element(begin(), end());
|
||||
eoPop<EOT>:: iterator it = std::max_element(begin(), end());
|
||||
#else
|
||||
typename eoPop<EOT>::iterator it_best_element() {
|
||||
typename eoPop<EOT>::iterator it = std::max_element(begin(), end());
|
||||
typename eoPop<EOT>::iterator it_best_element() {
|
||||
typename eoPop<EOT>::iterator it = std::max_element(begin(), end());
|
||||
#endif
|
||||
return it;
|
||||
}
|
||||
|
|
@ -199,7 +199,7 @@ public:
|
|||
#if defined(__CUDACC__)
|
||||
eoPop<EOT>::const_iterator it = std::max_element(begin(), end());
|
||||
#else
|
||||
typename eoPop<EOT>::const_iterator it = std::max_element(begin(), end());
|
||||
typename eoPop<EOT>::const_iterator it = std::max_element(begin(), end());
|
||||
#endif
|
||||
return (*it);
|
||||
}
|
||||
|
|
@ -210,7 +210,7 @@ public:
|
|||
#if defined(__CUDACC__)
|
||||
eoPop<EOT>::const_iterator it = std::min_element(begin(), end());
|
||||
#else
|
||||
typename eoPop<EOT>::const_iterator it = std::min_element(begin(), end());
|
||||
typename eoPop<EOT>::const_iterator it = std::min_element(begin(), end());
|
||||
#endif
|
||||
return (*it);
|
||||
}
|
||||
|
|
@ -221,9 +221,9 @@ public:
|
|||
{
|
||||
eoPop<EOT>::iterator it = std::min_element(begin(), end());
|
||||
#else
|
||||
typename eoPop<EOT>::iterator it_worse_element()
|
||||
typename eoPop<EOT>::iterator it_worse_element()
|
||||
{
|
||||
typename eoPop<EOT>::iterator it = std::min_element(begin(), end());
|
||||
typename eoPop<EOT>::iterator it = std::min_element(begin(), end());
|
||||
#endif
|
||||
return it;
|
||||
}
|
||||
|
|
@ -232,25 +232,25 @@ public:
|
|||
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__)
|
||||
#if defined(__CUDACC__)
|
||||
eoPop<EOT>::iterator nth_element(int nth)
|
||||
{
|
||||
eoPop<EOT>::iterator it = begin() + nth;
|
||||
#else
|
||||
typename eoPop<EOT>::iterator nth_element(int nth)
|
||||
typename eoPop<EOT>::iterator nth_element(int nth)
|
||||
{
|
||||
typename eoPop<EOT>::iterator it = begin() + nth;
|
||||
typename eoPop<EOT>::iterator it = begin() + nth;
|
||||
#endif
|
||||
std::nth_element(begin(), it, end(), std::greater<EOT>());
|
||||
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
|
||||
|
||||
|
||||
std::vector<Fitness> fitness(size());
|
||||
std::transform(begin(), end(), fitness.begin(), GetFitness());
|
||||
|
||||
|
|
@ -259,15 +259,15 @@ public:
|
|||
return *it;
|
||||
}
|
||||
|
||||
/** const nth_element function, returns pointers to sorted individuals
|
||||
* up the the nth
|
||||
/** const nth_element function, returns pointers to sorted individuals
|
||||
* up the the nth
|
||||
*/
|
||||
void nth_element(int which, std::vector<const EOT*>& result) const
|
||||
{
|
||||
|
||||
result.resize(size());
|
||||
std::transform(begin(), end(), result.begin(), Ref());
|
||||
|
||||
|
||||
typename std::vector<const EOT*>::iterator it = result.begin() + which;
|
||||
|
||||
std::nth_element(result.begin(), it, result.end(), Cmp());
|
||||
|
|
@ -278,28 +278,28 @@ public:
|
|||
{
|
||||
std::swap(static_cast<std::vector<EOT>& >(*this), static_cast<std::vector<EOT>& >(other));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prints sorted pop but does NOT modify it!
|
||||
*
|
||||
* @param _os A std::ostream.
|
||||
* @param _os A std::ostream.
|
||||
*/
|
||||
virtual void sortedPrintOn(std::ostream& _os) const
|
||||
virtual void sortedPrintOn(std::ostream& _os) const
|
||||
{
|
||||
std::vector<const EOT*> result;
|
||||
sort(result);
|
||||
_os << size() << '\n';
|
||||
for (unsigned i = 0; i < size(); ++i)
|
||||
{
|
||||
_os << *result[i] << std::endl;
|
||||
_os << *result[i] << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write object. It's called printOn since it prints the object _on_ a stream.
|
||||
* @param _os A std::ostream.
|
||||
* @param _os A std::ostream.
|
||||
*/
|
||||
virtual void printOn(std::ostream& _os) const
|
||||
virtual void printOn(std::ostream& _os) const
|
||||
{
|
||||
_os << size() << '\n';
|
||||
std::copy( begin(), end(), std::ostream_iterator<EOT>( _os, "\n") );
|
||||
|
|
@ -309,16 +309,16 @@ public:
|
|||
//@{
|
||||
/**
|
||||
* Read object. The EOT class must have a ctor from a stream;
|
||||
* @param _is A std::istream.
|
||||
* @param _is A std::istream.
|
||||
*/
|
||||
virtual void readFrom(std::istream& _is)
|
||||
virtual void readFrom(std::istream& _is)
|
||||
{
|
||||
size_t sz;
|
||||
_is >> sz;
|
||||
|
||||
resize(sz);
|
||||
|
||||
for (size_t i = 0; i < sz; ++i) {
|
||||
for (size_t i = 0; i < sz; ++i) {
|
||||
operator[](i).readFrom( _is );
|
||||
}
|
||||
}
|
||||
|
|
@ -337,4 +337,3 @@ public:
|
|||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
|||
Reference in a new issue