diff --git a/eo/src/utils/eoPopStat.h b/eo/src/utils/eoPopStat.h index 23708e17..8907ded7 100644 --- a/eo/src/utils/eoPopStat.h +++ b/eo/src/utils/eoPopStat.h @@ -53,8 +53,10 @@ class eoPopStat : public eoStat public : /** default Ctor, void string by default, as it appears on the description line once at beginning of evolution. and - is meaningless there */ - eoPopStat(string _desc ="") : eoStat("", _desc) {} + is meaningless there. _howMany defaults to 0, that is, the whole + population*/ + eoPopStat(unsigned _howMany = 0, string _desc ="") + : eoStat("", _desc), combien( _howMany) {} /** Fills the value() of the eoParam with the dump of the population. Adds a \n before so it does not get mixed up with the rest of the stats @@ -64,7 +66,8 @@ void operator()(const eoPop& _pop) { char buffer[1023]; // about one K of space per member value() = "\n====== Pop dump =====\n"; - for (unsigned i = 0; i < _pop.size(); ++i) + unsigned howMany=combien?combien:_pop.size(); + for (unsigned i = 0; i < howMany; ++i) { std::ostrstream os(buffer, 1022); // leave space for emergency terminate os << _pop[i] << endl << ends; @@ -74,6 +77,8 @@ void operator()(const eoPop& _pop) value() += buffer; } } +private: + unsigned combien; }; /** Thanks to MS/VC++, eoParam mechanism is unable to handle vectors of stats. diff --git a/eo/test/RoyalRoad.h b/eo/test/RoyalRoad.h new file mode 100644 index 00000000..35b217b9 --- /dev/null +++ b/eo/test/RoyalRoad.h @@ -0,0 +1,60 @@ +/* + RoyalRoad.h + -- Implementation of the Royal Road function for any length and block size + (c) GeNeura Team 2001, Marc Schoenauer 2000 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Contact: todos@geneura.ugr.es, http://geneura.ugr.es + Marc.Schoenauer@polytechnique.fr +CVS Info: $Date: 2001-05-11 10:44:01 $ $Header: /home/nojhan/dev/eodev/eodev_cvs/eo/test/RoyalRoad.h,v 1.1 2001-05-11 10:44:01 jmerelo Exp $ $Author: jmerelo $ +*/ + +#ifndef RoyalRoad_h +#define RoyalRoad_h + +template +class RoyalRoad: public eoEvalFunc { + + public: + + typedef typename EOT::Fitness FitT; + + /// Ctor: takes a length, and divides that length in equal parts + RoyalRoad( unsigned _div ): eoEvalFunc(), div( _div ) {}; + + // Applies the function + virtual void operator() ( EOT & _eo ) { + FitT fitness; + if (_eo.invalid()) { + for ( unsigned i = 0; i < _eo.size()/div; i ++ ) { + bool block = true; + for ( unsigned j = 0; j < div; j ++ ) { + block &= _eo[i*div+j]; + } + if (block) { + fitness += div; + } + _eo.fitness( fitness ); + } + } + }; + + private: + unsigned div; + +}; + +#endif diff --git a/eo/test/t-MGE.cpp b/eo/test/t-MGE.cpp index 21c1c57f..0ddfd72b 100644 --- a/eo/test/t-MGE.cpp +++ b/eo/test/t-MGE.cpp @@ -10,7 +10,7 @@ #include #include -#include "binary_value.h" +#include "RoyalRoad.h" // Viri #include @@ -25,21 +25,21 @@ typedef eoVirus Chrom; int main() { - const unsigned POP_SIZE = 100, CHROM_SIZE = 16; + const unsigned POP_SIZE = 1000, CHROM_SIZE = 128; unsigned i; eoBooleanGenerator gen; // the populations: eoPop pop; - // Evaluation - eoEvalFuncPtr eval( binary_value ); + // Evaluation + RoyalRoad rr( 4 ); eoInitVirus random(CHROM_SIZE, gen); for (i = 0; i < POP_SIZE; ++i) { Chrom chrom; random(chrom); - eval(chrom); + rr(chrom); pop.push_back(chrom); } @@ -49,7 +49,7 @@ int main() // selection - eoDetTournamentSelect lottery( 3) ; + eoStochTournamentSelect lottery(0.9 ); // breeder VirusMutation vm; @@ -58,27 +58,29 @@ int main() eoUBitXover xover; eoProportionalOp propSel; eoGeneralBreeder breeder( lottery, propSel ); - propSel.add(vm, 0.25); - propSel.add(vf, 0.25); - propSel.add(vt, 0.25); - propSel.add(xover, 0.25); + propSel.add(vm, 0.4); + propSel.add(vf, 0.4); + propSel.add(vt, 0.1); + propSel.add(xover, 0.1); // Replace a single one - eoPlusReplacement replace; + eoCommaReplacement replace; // Terminators - eoGenContinue continuator1(50); - eoFitContinue continuator2(65535.f); + eoGenContinue continuator1(500); + eoFitContinue continuator2(128); eoCombinedContinue continuator(continuator1, continuator2); eoCheckPoint checkpoint(continuator); eoStdoutMonitor monitor; checkpoint.add(monitor); eoSecondMomentStats stats; + eoPopStat dumper( 10 ); monitor.add(stats); + checkpoint.add(dumper); checkpoint.add(stats); // GA generation - eoEasyEA ea(checkpoint, eval, breeder, replace ); + eoEasyEA ea(checkpoint, rr, breeder, replace ); // evolution try