new replacer

This commit is contained in:
gustavo 1999-02-09 17:27:22 +00:00
commit a86873dcbf
3 changed files with 70 additions and 16 deletions

View file

@ -43,7 +43,7 @@
#include <eoLottery.h>
#include <eoInsertion.h>
#include <eoInclusion.h>
//-----------------------------------------------------------------------------

49
eo/src/eoInclusion.h Normal file
View file

@ -0,0 +1,49 @@
//-----------------------------------------------------------------------------
// eoInclusion.h
//-----------------------------------------------------------------------------
#ifndef eoInclusion_h
#define eoInclusion_h
//-----------------------------------------------------------------------------
#include <eo>
/******************************************************************************
* eoInclusion: A replacement algorithm.
* Creates a new population by selecting the best individuals from the
* breeders and original populations
*****************************************************************************/
template<class Chrom> class eoInclusion: public eoMerge<Chrom>
{
public:
/// (Default) Constructor.
eoInclusion(const float& _rate = 1.0): eoMerge(_rate) {}
/**
* Creates a new population based on breeders and original populations.
* @param breeders The population of breeders.
* @param pop The original population.
*/
void operator()(const eoPop<Chrom>& breeders, eoPop<Chrom>& pop)
{
eoPop<Chrom> all, tmp = breeders;
sort(tmp.begin(), tmp.end());
sort(pop.begin(), pop.end());
merge(tmp.begin(), tmp.end(),
pop.begin(), pop.end(),
back_insert_iterator<eoPop<Chrom> >(all));
all.erase(all.begin(),
all.begin() + (unsigned)(all.size() - pop.size() * rate()));
pop.swap(all);
}
};
//-----------------------------------------------------------------------------
#endif eoInclusion_h

View file

@ -11,10 +11,8 @@
/******************************************************************************
* eoInsertion: A replacement algorithm.
* Takes two populations: breeders and original populations. At the en of the
* process, the original population has chenge in the followin way:
* (1) the worst individuals haa been erased
* (2) the best individuals from the breeders has been added
* Creates a new population with all the breeders and the best individuals
* from the original population.
*****************************************************************************/
template<class Chrom> class eoInsertion: public eoMerge<Chrom>
@ -24,26 +22,33 @@ template<class Chrom> class eoInsertion: public eoMerge<Chrom>
eoInsertion(const float& _rate = 1.0): eoMerge(_rate) {}
/**
* Creates a new population based on breeders and original population
* Creates a new population based on breeders and original populations.
* @param breeders The population of breeders.
* @param pop The original population.
*/
void operator()(const eoPop<Chrom>& breeders, eoPop<Chrom>& pop)
{
sort(pop.begin(), pop.end());
int new_size = static_cast<int>(pop.size() * rate());
cout << "new_size = " << new_size << endl;
if (rated() > 1)
pop.erase(pop.end() +
(int)(pop.size() * (rate() - 1) - breeders.size()),
pop.end());
if (new_size == breeders.size())
{
pop = breeders;
}
else if (new_size < breeders.size())
{
pop = breeders;
sort(pop.begin(), pop.end());
pop.erase(pop.begin(), pop.begin() - new_size + pop.size());
}
else
{
cout << "eoInsertion no funciona con rate < 1"
exit(1);
sort(pop.begin(), pop.end());
pop.erase(pop.begin(),
pop.begin() + breeders.size() + pop.size() - new_size);
copy(breeders.begin(), breeders.end(),
back_insert_iterator<eoPop<Chrom> >(pop));
}
copy(breeders.begin(), breeders.end(),
back_insert_iterator<eoPop<Chrom> >(pop));
}
};