fixing bugs in replacers and moving some code between files

This commit is contained in:
gustavo 1999-09-21 18:14:39 +00:00
commit bc9638f53c
7 changed files with 89 additions and 103 deletions

View file

@ -27,21 +27,16 @@ template<class Chrom> class eoInclusion: public eoMerge<Chrom>
* @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()(eoPop<Chrom>& breeders, eoPop<Chrom>& pop)
{ {
eoPop<Chrom> all, tmp = breeders; unsigned target = min(static_cast<unsigned>(rint(pop.size() * rate())),
pop.size() + breeders.size());
sort(tmp.begin(), tmp.end()); copy(breeders.begin(), breeders.end(),
sort(pop.begin(), pop.end()); back_insert_iterator<eoPop<Chrom> >(pop));
partial_sort(pop.begin(), pop.begin() + target, pop.end(),
merge(tmp.begin(), tmp.end(), greater<Chrom>());
pop.begin(), pop.end(), pop.erase(pop.begin() + target, pop.end());
back_insert_iterator<eoPop<Chrom> >(all));
all.erase(all.begin(),
all.begin() + (unsigned)(all.size() - pop.size() * rate()));
pop.swap(all);
} }
}; };

View file

@ -7,7 +7,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <eo> #include <eoMerge.h>
/****************************************************************************** /******************************************************************************
* eoInsertion: A replacement algorithm. * eoInsertion: A replacement algorithm.
@ -26,7 +26,7 @@ template<class Chrom> class eoInsertion: public eoMerge<Chrom>
* @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()(eoPop<Chrom>& breeders, eoPop<Chrom>& pop)
{ {
int new_size = static_cast<int>(pop.size() * rate()); int new_size = static_cast<int>(pop.size() * rate());
@ -48,6 +48,28 @@ template<class Chrom> class eoInsertion: public eoMerge<Chrom>
copy(breeders.begin(), breeders.end(), copy(breeders.begin(), breeders.end(),
back_insert_iterator<eoPop<Chrom> >(pop)); back_insert_iterator<eoPop<Chrom> >(pop));
} }
}*/
void operator()(eoPop<Chrom>& breeders, eoPop<Chrom>& pop)
{
unsigned target = static_cast<unsigned>(rint(pop.size() * rate()));
pop.swap(breeders);
if (target < pop.size())
{
partial_sort(pop.begin(), pop.begin() + target, pop.end(),
greater<Chrom>());
pop.erase(pop.begin() + target, pop.end());
}
else
{
target = min(target - pop.size(), breeders.size());
partial_sort(breeders.begin(), breeders.begin() + target,
breeders.end(), greater<Chrom>());
copy(breeders.begin(), breeders.begin() + target,
back_insert_iterator<eoPop<Chrom> >(pop));
}
} }
}; };

View file

@ -7,55 +7,49 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <eoPopOps.h> // eoMerge #include <eoPop.h> // eoPop
//-----------------------------------------------------------------------------
// eoInsertion
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template<class Chrom> class Insertion: public eoMerge<Chrom> /** eoMerge involves three populations, that can be merged and transformed to
{ give a third
*/
template<class EOT>
class eoMerge: public eoObject{
public: public:
eoInsertion(const float& _rate = 1): eoMerge<Chrom>(rate) {} /// (Default) Constructor.
eoMerge(const float& _rate = 1.0): rep_rate(_rate) {}
bool compare(const Chrom& chrom1, const Chrom& chrom2) /// Dtor
{ virtual ~eoMerge() {}
return chrom1.fitness() < chrom2.fitness();
}
void operator()(const Pop& breeders, Pop& pop) /** Pure virtual transformation function. Extracts something from breeders
{ * and transfers it to the pop
sort(pop.begin(), pop.end() compare); * @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;
pop.erase(pop.end() + (int)(pop.size() * (rate - 1) - breeders.size()), /** @name Methods from eoObject */
pop.end()); //@{
/** readFrom and printOn are not overriden
*/
/** Inherited from eoObject. Returns the class name.
@see eoObject
*/
string className() const {return "eoMerge";};
//@}
copy(breeders.begin(), breeders.end(), /// Return the rate to be selected from the original population
back_insert_iterator<Pop>(pop)); 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; }
template<class Fitness> class Inclusion: public Replace<Fitness> private:
{ float rep_rate;
public:
Inclusion(const float& rate = 1): Replace<Fitness>(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);
}
}; };
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View file

@ -97,44 +97,5 @@ class eoSelect: public eoObject{
}; };
/** 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) {}
/// 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 () ( const 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;
};
#endif #endif

View file

@ -58,6 +58,7 @@ main()
breeder(pop); breeder(pop);
// reevaluation of fitness
for_each(pop.begin(), pop.end(), binary_value); for_each(pop.begin(), pop.end(), binary_value);
cout << "new population:" << endl; cout << "new population:" << endl;

View file

@ -3,6 +3,8 @@
// (c) GeNeura Team 1998 // (c) GeNeura Team 1998
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <time.h> // time
#include <stdlib.h> // srand, rand
#include <iostream> // cout #include <iostream> // cout
#include <eo> // eoFitness #include <eo> // eoFitness
@ -11,6 +13,9 @@
class eoFloat: public eoFitness class eoFloat: public eoFitness
{ {
public: public:
eoFloat(const float x) { fitness = x; }
eoFloat(const int x) { fitness = static_cast<float>(x); }
bool operator<(const eoFitness& other) const bool operator<(const eoFitness& other) const
{ {
const eoFloat& x = (const eoFloat&) other; const eoFloat& x = (const eoFloat&) other;
@ -40,7 +45,12 @@ private:
main() main()
{ {
eoFloat a, b; srand(time(0));
eoFloat a = static_cast<float>(rand()) / RAND_MAX,
b = static_cast<float>(rand()) / RAND_MAX;
cout.precision(2);
unsigned repeat = 2; unsigned repeat = 2;
while (repeat--) while (repeat--)

View file

@ -43,7 +43,7 @@ main()
for (unsigned POP2_SIZE = 4; POP2_SIZE <=6; POP2_SIZE++) for (unsigned POP2_SIZE = 4; POP2_SIZE <=6; POP2_SIZE++)
{ {
eoPop<Chrom> pop2, pop3, pop4, pop5; eoPop<Chrom> pop2, pop3, pop4, pop5, popx;
for (i = 0; i < POP2_SIZE; i++) for (i = 0; i < POP2_SIZE; i++)
{ {
@ -68,16 +68,19 @@ main()
} }
eoInsertion<Chrom> insertion(0.75); eoInsertion<Chrom> insertion(0.75);
popx = pop;
pop3 = pop2; pop3 = pop2;
insertion(pop, pop3); insertion(popx, pop3);
eoInsertion<Chrom> insertion2; eoInsertion<Chrom> insertion2;
popx = pop;
pop4 = pop2; pop4 = pop2;
insertion2(pop, pop4); insertion2(popx, pop4);
eoInsertion<Chrom> insertion3(1.5); eoInsertion<Chrom> insertion3(1.5);
popx = pop;
pop5 = pop2; pop5 = pop2;
insertion3(pop, pop5); insertion3(popx, pop5);
cout << endl cout << endl
<< "0.75 \t\t1.0 \t\t1.5" << endl << "0.75 \t\t1.0 \t\t1.5" << endl