Added Royal Road, MGE now work with it, small modifications to eoPopStat

This commit is contained in:
jmerelo 2001-05-11 10:44:01 +00:00
commit 34cb430bd7
3 changed files with 84 additions and 17 deletions

View file

@ -53,8 +53,10 @@ class eoPopStat : public eoStat<EOT, string>
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<EOT, string>("", _desc) {}
is meaningless there. _howMany defaults to 0, that is, the whole
population*/
eoPopStat(unsigned _howMany = 0, string _desc ="")
: eoStat<EOT, string>("", _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<EOT>& _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<EOT>& _pop)
value() += buffer;
}
}
private:
unsigned combien;
};
/** Thanks to MS/VC++, eoParam mechanism is unable to handle vectors of stats.

60
eo/test/RoyalRoad.h Normal file
View file

@ -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 EOT>
class RoyalRoad: public eoEvalFunc<EOT> {
public:
typedef typename EOT::Fitness FitT;
/// Ctor: takes a length, and divides that length in equal parts
RoyalRoad( unsigned _div ): eoEvalFunc<EOT >(), 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

View file

@ -10,7 +10,7 @@
#include <eo>
#include <ga/eoBitOp.h>
#include "binary_value.h"
#include "RoyalRoad.h"
// Viri
#include <MGE/VirusOp.h>
@ -25,21 +25,21 @@ typedef eoVirus<float> 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<Chrom> pop;
// Evaluation
eoEvalFuncPtr<Chrom> eval( binary_value );
// Evaluation
RoyalRoad<Chrom> rr( 4 );
eoInitVirus<float> 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<Chrom> lottery( 3) ;
eoStochTournamentSelect<Chrom> lottery(0.9 );
// breeder
VirusMutation<float> vm;
@ -58,27 +58,29 @@ int main()
eoUBitXover<Chrom> xover;
eoProportionalOp<Chrom> propSel;
eoGeneralBreeder<Chrom> 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<Chrom> replace;
eoCommaReplacement<Chrom> replace;
// Terminators
eoGenContinue<Chrom> continuator1(50);
eoFitContinue<Chrom> continuator2(65535.f);
eoGenContinue<Chrom> continuator1(500);
eoFitContinue<Chrom> continuator2(128);
eoCombinedContinue<Chrom> continuator(continuator1, continuator2);
eoCheckPoint<Chrom> checkpoint(continuator);
eoStdoutMonitor monitor;
checkpoint.add(monitor);
eoSecondMomentStats<Chrom> stats;
eoPopStat<Chrom> dumper( 10 );
monitor.add(stats);
checkpoint.add(dumper);
checkpoint.add(stats);
// GA generation
eoEasyEA<Chrom> ea(checkpoint, eval, breeder, replace );
eoEasyEA<Chrom> ea(checkpoint, rr, breeder, replace );
// evolution
try