new replacer
This commit is contained in:
parent
e721603cf2
commit
a86873dcbf
3 changed files with 70 additions and 16 deletions
|
|
@ -43,7 +43,7 @@
|
||||||
#include <eoLottery.h>
|
#include <eoLottery.h>
|
||||||
|
|
||||||
#include <eoInsertion.h>
|
#include <eoInsertion.h>
|
||||||
|
#include <eoInclusion.h>
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
|
||||||
49
eo/src/eoInclusion.h
Normal file
49
eo/src/eoInclusion.h
Normal 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
|
||||||
|
|
@ -11,10 +11,8 @@
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* eoInsertion: A replacement algorithm.
|
* eoInsertion: A replacement algorithm.
|
||||||
* Takes two populations: breeders and original populations. At the en of the
|
* Creates a new population with all the breeders and the best individuals
|
||||||
* process, the original population has chenge in the followin way:
|
* from the original population.
|
||||||
* (1) the worst individuals haa been erased
|
|
||||||
* (2) the best individuals from the breeders has been added
|
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
template<class Chrom> class eoInsertion: public eoMerge<Chrom>
|
template<class Chrom> class eoInsertion: public eoMerge<Chrom>
|
||||||
|
|
@ -24,27 +22,34 @@ template<class Chrom> class eoInsertion: public eoMerge<Chrom>
|
||||||
eoInsertion(const float& _rate = 1.0): eoMerge(_rate) {}
|
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 breeders The population of breeders.
|
||||||
* @param pop The original population.
|
* @param pop The original population.
|
||||||
*/
|
*/
|
||||||
void operator()(const eoPop<Chrom>& breeders, eoPop<Chrom>& pop)
|
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)
|
if (new_size == breeders.size())
|
||||||
pop.erase(pop.end() +
|
{
|
||||||
(int)(pop.size() * (rate() - 1) - breeders.size()),
|
pop = breeders;
|
||||||
pop.end());
|
}
|
||||||
|
else if (new_size < breeders.size())
|
||||||
|
{
|
||||||
|
pop = breeders;
|
||||||
|
sort(pop.begin(), pop.end());
|
||||||
|
pop.erase(pop.begin(), pop.begin() - new_size + pop.size());
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cout << "eoInsertion no funciona con rate < 1"
|
sort(pop.begin(), pop.end());
|
||||||
exit(1);
|
pop.erase(pop.begin(),
|
||||||
}
|
pop.begin() + breeders.size() + pop.size() - new_size);
|
||||||
|
|
||||||
copy(breeders.begin(), breeders.end(),
|
copy(breeders.begin(), breeders.end(),
|
||||||
back_insert_iterator<eoPop<Chrom> >(pop));
|
back_insert_iterator<eoPop<Chrom> >(pop));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
|
||||||
Reference in a new issue