fixing bugs in replacers and moving some code between files
This commit is contained in:
parent
265c5671bf
commit
bc9638f53c
7 changed files with 89 additions and 103 deletions
|
|
@ -7,55 +7,49 @@
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoPopOps.h> // eoMerge
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoInsertion
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Chrom> class Insertion: public eoMerge<Chrom>
|
||||
{
|
||||
public:
|
||||
eoInsertion(const float& _rate = 1): eoMerge<Chrom>(rate) {}
|
||||
|
||||
bool compare(const Chrom& chrom1, const Chrom& chrom2)
|
||||
{
|
||||
return chrom1.fitness() < chrom2.fitness();
|
||||
}
|
||||
|
||||
void operator()(const Pop& breeders, Pop& pop)
|
||||
{
|
||||
sort(pop.begin(), pop.end() compare);
|
||||
|
||||
pop.erase(pop.end() + (int)(pop.size() * (rate - 1) - breeders.size()),
|
||||
pop.end());
|
||||
|
||||
copy(breeders.begin(), breeders.end(),
|
||||
back_insert_iterator<Pop>(pop));
|
||||
}
|
||||
};
|
||||
#include <eoPop.h> // eoPop
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
template<class Fitness> class Inclusion: public Replace<Fitness>
|
||||
{
|
||||
public:
|
||||
Inclusion(const float& rate = 1): Replace<Fitness>(rate) {}
|
||||
/** eoMerge involves three populations, that can be merged and transformed to
|
||||
give a third
|
||||
*/
|
||||
template<class EOT>
|
||||
class eoMerge: public eoObject{
|
||||
|
||||
public:
|
||||
/// (Default) Constructor.
|
||||
eoMerge(const float& _rate = 1.0): rep_rate(_rate) {}
|
||||
|
||||
void operator()(Pop& breeders, Pop& pop)
|
||||
{
|
||||
Pop temp;
|
||||
|
||||
sort(breeders.begin(), breeders.end(), compare);
|
||||
sort(pop.begin(), pop.end(), compare);
|
||||
|
||||
merge(breeders.begin(), breeders.end(),
|
||||
pop.begin(), pop.end(),
|
||||
back_insert_iterator<Pop>(temp), compare);
|
||||
|
||||
temp.erase(temp.begin() + (unsigned)(rate * pop.size()), temp.end());
|
||||
pop.swap(temp);
|
||||
}
|
||||
/// Dtor
|
||||
virtual ~eoMerge() {}
|
||||
|
||||
/** Pure virtual transformation function. Extracts something from breeders
|
||||
* and transfers it to the pop
|
||||
* @param breeders Tranformed individuals.
|
||||
* @param pop The original population at the begining, the result at the end
|
||||
*/
|
||||
virtual void operator () ( eoPop<EOT>& breeders, eoPop<EOT>& pop ) = 0;
|
||||
|
||||
/** @name Methods from eoObject */
|
||||
//@{
|
||||
/** readFrom and printOn are not overriden
|
||||
*/
|
||||
/** Inherited from eoObject. Returns the class name.
|
||||
@see eoObject
|
||||
*/
|
||||
string className() const {return "eoMerge";};
|
||||
//@}
|
||||
|
||||
/// Return the rate to be selected from the original population
|
||||
float rate() const { return rep_rate; }
|
||||
|
||||
/// Set the rate to be obtained after replacement.
|
||||
/// @param _rate The rate.
|
||||
void rate(const float& _rate) { rep_rate = _rate; }
|
||||
|
||||
private:
|
||||
float rep_rate;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
|
|
|||
Reference in a new issue