From 9303c4b53dd49954f2445fba73afcbe542af3172 Mon Sep 17 00:00:00 2001 From: gustavo Date: Fri, 12 Feb 1999 17:43:28 +0000 Subject: [PATCH] eoBreeder almost finished --- eo/mkinstalldirs | 2 +- eo/src/eo | 1 + eo/src/eoBitOp.h | 125 +++++++++++++++++++++++-------------------- eo/src/eoBreeder.h | 73 ++++++++++++------------- eo/src/eoData.h | 9 ++-- eo/src/eoInsertion.h | 37 +++++++------ eo/src/eoLottery.h | 7 ++- 7 files changed, 137 insertions(+), 117 deletions(-) diff --git a/eo/mkinstalldirs b/eo/mkinstalldirs index cb1c70a4..42b58cdb 100755 --- a/eo/mkinstalldirs +++ b/eo/mkinstalldirs @@ -4,7 +4,7 @@ # Created: 1993-05-16 # Public domain -# $Id: mkinstalldirs,v 1.1 1999-01-29 12:23:53 gustavo Exp $ +# $Id: mkinstalldirs,v 5.1 1999-02-12 17:39:01 gustavo Exp $ errstatus=0 diff --git a/eo/src/eo b/eo/src/eo index 43e26d82..9f21e4dd 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -8,6 +8,7 @@ //----------------------------------------------------------------------------- +#include #include #include #include diff --git a/eo/src/eoBitOp.h b/eo/src/eoBitOp.h index 7f37ae52..50b0bba5 100644 --- a/eo/src/eoBitOp.h +++ b/eo/src/eoBitOp.h @@ -24,17 +24,18 @@ have a factory that knows how to build them from a description //@{ -/** - eoBinRandom --> mofify a chromosome in a random way -*/ +/** eoBinRandom --> mofify a chromosome in a random way */ + template class eoBinRandom: public eoMonOp { public: /// The class name. string className() const { return "eoBinRandom"; } - /// Randomizes a cromosome. - /// @param chrom The cromosome to be randomize. + /** + * Randomizes a cromosome. + * @param chrom The cromosome to be randomize. + */ void operator()(Chrom& chrom) const { eoUniform uniform(0.0, 1.0); @@ -44,9 +45,7 @@ template class eoBinRandom: public eoMonOp }; -//----------------------------------------------------------------------------- -// eoBinBitFlip --> chages a bit -//----------------------------------------------------------------------------- +/** eoBinBitFlip --> chages a bit */ template class eoBinBitFlip: public eoMonOp { @@ -54,8 +53,10 @@ template class eoBinBitFlip: public eoMonOp /// The class name. string className() const { return "eoBinBitFlip"; } - /// Change one bit. - /// @param chrom The cromosome which one bit is going to be changed. + /** + * Change one bit. + * @param chrom The cromosome which one bit is going to be changed. + */ void operator()(Chrom& chrom) const { eoUniform uniform(0.0, 1.0); @@ -64,22 +65,25 @@ template class eoBinBitFlip: public eoMonOp } }; -//----------------------------------------------------------------------------- -// eoBinMutation --> classical mutation -//----------------------------------------------------------------------------- + +/** eoBinMutation --> classical mutation */ template class eoBinMutation: public eoMonOp { public: - /// (Default) Constructor. - /// @param _rate Rate of mutation. + /** + * (Default) Constructor. + * @param _rate Rate of mutation. + */ eoBinMutation(const double& _rate = 0.01): rate(_rate), uniform(0.0, 1.0) {} /// The class name. string className() const { return "eoBinMutation"; } - /// Mutate a chromosome. - /// @param chrom The chromosome to be mutated. + /** + * Mutate a chromosome. + * @param chrom The chromosome to be mutated. + */ void operator()(Chrom& chrom) const { for (unsigned i = 0; i < chrom.size(); i++) @@ -92,9 +96,8 @@ template class eoBinMutation: public eoMonOp mutable eoUniform uniform; }; -//----------------------------------------------------------------------------- -// eoBinInversion --> inverts the bits of the chromosome between an interval -//----------------------------------------------------------------------------- + +/** eoBinInversion: inverts the bits of the chromosome between an interval */ template class eoBinInversion: public eoMonOp { @@ -102,8 +105,10 @@ template class eoBinInversion: public eoMonOp /// The class name. string className() const { return "eoBinInversion"; } - /// Inverts a range of bits in a binary chromosome. - /// @param chrom The chromosome whos bits are going to be inverted (a range). + /** + * Inverts a range of bits in a binary chromosome. + * @param chrom The chromosome whos bits are going to be inverted (a range). + */ void operator()(Chrom& chrom) const { eoUniform uniform(0, chrom.size() + 1); @@ -116,9 +121,8 @@ template class eoBinInversion: public eoMonOp } }; -//----------------------------------------------------------------------------- -// eoBinNext --> next binary value -//----------------------------------------------------------------------------- + +/** eoBinNext --> next binary value */ template class eoBinNext: public eoMonOp { @@ -126,8 +130,10 @@ template class eoBinNext: public eoMonOp /// The class name. string className() const { return "eoBinNext"; } - /// Change the bit string x to be x+1. - /// @param chrom The chromosome to be added one. + /** + * Change the bit string x to be x+1. + * @param chrom The chromosome to be added one. + */ void operator()(Chrom& chrom) const { for (int i = chrom.size() - 1; i >= 0; i--) @@ -144,18 +150,19 @@ template class eoBinNext: public eoMonOp } }; -//----------------------------------------------------------------------------- -// eoBinPrev --> previos binary value -//----------------------------------------------------------------------------- + +/** eoBinPrev --> previos binary value */ template class eoBinPrev: public eoMonOp { public: /// The class name. string className() const { return "eoBinPrev"; } - - /// Change the bit string x to be x-1. - /// @param chrom The chromosome to be substracted one. + + /** + * Change the bit string x to be x-1. + * @param chrom The chromosome to be substracted one. + */ void operator()(Chrom& chrom) const { for (int i = chrom.size() - 1; i >= 0; i--) @@ -172,9 +179,8 @@ template class eoBinPrev: public eoMonOp } }; -//----------------------------------------------------------------------------- -// eoBinCrossover --> classic crossover -//----------------------------------------------------------------------------- + +/** eoBinCrossover --> classic crossover */ template class eoBinCrossover: public eoBinOp { @@ -182,9 +188,11 @@ template class eoBinCrossover: public eoBinOp /// The class name. string className() const { return "eoBinCrossover"; } - /// 2-point crossover for binary chromosomes. - /// @param chrom1 The first chromosome. - /// @param chrom2 The first chromosome. + /** + * 2-point crossover for binary chromosomes. + * @param chrom1 The first chromosome. + * @param chrom2 The first chromosome. + */ void operator()(Chrom& chrom1, Chrom& chrom2) const { eoUniform uniform(1, min(chrom1.size(), chrom2.size())); @@ -192,9 +200,8 @@ template class eoBinCrossover: public eoBinOp } }; -//----------------------------------------------------------------------------- -// eoBinNxOver --> n-point crossover -//----------------------------------------------------------------------------- + +/** eoBinNxOver --> n-point crossover */ template class eoBinNxOver: public eoBinOp { @@ -212,9 +219,11 @@ template class eoBinNxOver: public eoBinOp /// The class name. string className() const { return "eoBinNxOver"; } - /// n-point crossover for binary chromosomes. - /// @param chrom1 The first chromosome. - /// @param chrom2 The first chromosome. + /** + * n-point crossover for binary chromosomes. + * @param chrom1 The first chromosome. + * @param chrom2 The first chromosome. + */ void operator()(Chrom& chrom1, Chrom& chrom2) const { unsigned max_size = min(chrom1.size(), chrom2.size()); @@ -252,9 +261,8 @@ template class eoBinNxOver: public eoBinOp unsigned num_points; }; -//----------------------------------------------------------------------------- -// eoBinGxOver --> gene crossover -//----------------------------------------------------------------------------- + +/** eoBinGxOver --> gene crossover */ template class eoBinGxOver: public eoBinOp { @@ -278,9 +286,11 @@ template class eoBinGxOver: public eoBinOp /// The class name string className() const { return "eoBinGxOver"; } - /// Gene crossover for binary chromosomes. - /// @param chrom1 The first chromosome. - /// @param chrom2 The first chromosome. + /** + * Gene crossover for binary chromosomes. + * @param chrom1 The first chromosome. + * @param chrom2 The first chromosome. + */ void operator()(Chrom& chrom1, Chrom& chrom2) const { unsigned max_genes = min(chrom1.size(), chrom2.size()) / gene_size; @@ -314,9 +324,8 @@ template class eoBinGxOver: public eoBinOp unsigned num_points; }; -//----------------------------------------------------------------------------- -// eoBinUxOver --> uniform crossover -//----------------------------------------------------------------------------- + +/** eoBinUxOver --> uniform crossover */ template class eoBinUxOver: public eoBinOp { @@ -334,9 +343,11 @@ template class eoBinUxOver: public eoBinOp /// The class name. string className() const { return "eoBinUxOver"; } - /// Uniform crossover for binary chromosomes. - /// @param chrom1 The first chromosome. - /// @param chrom2 The first chromosome. + /** + * Uniform crossover for binary chromosomes. + * @param chrom1 The first chromosome. + * @param chrom2 The first chromosome. + */ void operator()(Chrom& chrom1, Chrom& chrom2) const { unsigned min_size = min(chrom1.size(), chrom2.size()); diff --git a/eo/src/eoBreeder.h b/eo/src/eoBreeder.h index fe13352d..b2191685 100644 --- a/eo/src/eoBreeder.h +++ b/eo/src/eoBreeder.h @@ -22,59 +22,60 @@ template class eoBreeder: public eoTransform /// Default constructor. eoBreeder(): uniform(0.0, 1.0) {} - /// Destructor. - ~eoBreeder() - { - for (Operators::iterator op = operators.begin(); - op != operators.end(); - op++) - delete op->second; - } - /** * Adds a genetic operator. * @param operator The operator. * @param rate Rate to apply the operator. */ - void add(eoOp* operator, float rate = 1.0) + void add(eoOp& op, float rate = 1.0) { - operators.push_back(pair * >(operator, rate)); + operators.push_back(pair*>(rate, &op)); } /** * Transforms a population. * @param pop The population to be transformed. */ - void operator()(eoPop& pop) const + void operator()(eoPop& pop) { for (Operators::const_iterator op = operators.begin(); op != operators.end(); op++) - if (op->first < uniform()) - switch (op->second->readArity()) - { - case unary: - { - eoMonOp& monop = - dinamic_cast >(*(op->second)); - for_each(pop.begin(), pop.end(), monop); - break; - } - case binary: - { - eoBinOp& binop = - dinamic_cast >(*(op->second)); - for (unsigned i = 1; i < pop.size(); i += 2) - binop(pop[i - 1], pop[i]); - break; - } - default: - { - cerr << "eoBreeder:operator() Not implemented yet!" << endl; - exit(EXIT_FAILURE); - break; - } + switch (op->second->readArity()) + { + case unary: + { + eoMonOp& monop = (eoMonOp&)*(op->second); + for (unsigned i = 0; i < pop.size(); i++) + if (uniform() < op->first) + monop(pop[i]); + break; } + case binary: + { + vector pos(pop.size(), 1); + pos[0] = 0; + partial_sum(pos.begin(), pos.end(), pos.begin()); + random_shuffle(pos.begin(), pos.end()); + + cout << "pos = "; + copy(pos.begin(), pos.end(), + ostream_iterator(cout, " ")); + cout << endl; + + eoBinOp& binop = (eoBinOp&)*(op->second); + for (unsigned i = 1; i < pop.size(); i += 2) + if (uniform() < op->first) + binop(pop[pos[i - 1]], pop[pos[i]]); + break; + } + default: + { + cerr << "eoBreeder:operator() Not implemented yet!" << endl; + exit(EXIT_FAILURE); + break; + } + } } /// The class name. diff --git a/eo/src/eoData.h b/eo/src/eoData.h index 1b31eab7..d2493824 100644 --- a/eo/src/eoData.h +++ b/eo/src/eoData.h @@ -22,8 +22,11 @@ using namespace std; #define MAXINT numeric_limits::max() #define min _MIN #define max _MAX -#else - #include +#else + #include + #include + #include + #ifndef MAXFLOAT #define MAXFLOAT (float)1e127 #define MAXDOUBLE (double)1.79769313486231570e+308 @@ -38,4 +41,4 @@ using namespace std; //----------------------------------------------------------------------------- -#endif npi_DATATYPES_H +#endif EODATA_H diff --git a/eo/src/eoInsertion.h b/eo/src/eoInsertion.h index 17440d39..3c6d90a9 100644 --- a/eo/src/eoInsertion.h +++ b/eo/src/eoInsertion.h @@ -11,39 +11,44 @@ /****************************************************************************** * 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 eoInsertion: public eoMerge { public: /// (Default) Constructor. - 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 pop The original population. */ void operator()(const eoPop& breeders, eoPop& pop) { - sort(pop.begin(), pop.end()); + int new_size = static_cast(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 >(pop)); } - - copy(breeders.begin(), breeders.end(), - back_insert_iterator >(pop)); } }; diff --git a/eo/src/eoLottery.h b/eo/src/eoLottery.h index 20c4eadf..ffbecade 100644 --- a/eo/src/eoLottery.h +++ b/eo/src/eoLottery.h @@ -6,11 +6,10 @@ #define eoLottery_h //----------------------------------------------------------------------------- - -#include -#include // accumulate -#include // eoPop eoSelect +#include // +#include // accumulate +#include // eoPop eoSelect MINFLOAT //----------------------------------------------------------------------------- /// eoLottery: a selection method.