Added Royal Road, MGE now work with it, small modifications to eoPopStat
This commit is contained in:
parent
88f281b606
commit
34cb430bd7
3 changed files with 84 additions and 17 deletions
|
|
@ -53,8 +53,10 @@ class eoPopStat : public eoStat<EOT, string>
|
||||||
public :
|
public :
|
||||||
/** default Ctor, void string by default, as it appears
|
/** default Ctor, void string by default, as it appears
|
||||||
on the description line once at beginning of evolution. and
|
on the description line once at beginning of evolution. and
|
||||||
is meaningless there */
|
is meaningless there. _howMany defaults to 0, that is, the whole
|
||||||
eoPopStat(string _desc ="") : eoStat<EOT, string>("", _desc) {}
|
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.
|
/** 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
|
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
|
char buffer[1023]; // about one K of space per member
|
||||||
value() = "\n====== Pop dump =====\n";
|
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
|
std::ostrstream os(buffer, 1022); // leave space for emergency terminate
|
||||||
os << _pop[i] << endl << ends;
|
os << _pop[i] << endl << ends;
|
||||||
|
|
@ -74,6 +77,8 @@ void operator()(const eoPop<EOT>& _pop)
|
||||||
value() += buffer;
|
value() += buffer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private:
|
||||||
|
unsigned combien;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Thanks to MS/VC++, eoParam mechanism is unable to handle vectors of stats.
|
/** Thanks to MS/VC++, eoParam mechanism is unable to handle vectors of stats.
|
||||||
|
|
|
||||||
60
eo/test/RoyalRoad.h
Normal file
60
eo/test/RoyalRoad.h
Normal 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
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
#include <eo>
|
#include <eo>
|
||||||
#include <ga/eoBitOp.h>
|
#include <ga/eoBitOp.h>
|
||||||
|
|
||||||
#include "binary_value.h"
|
#include "RoyalRoad.h"
|
||||||
|
|
||||||
// Viri
|
// Viri
|
||||||
#include <MGE/VirusOp.h>
|
#include <MGE/VirusOp.h>
|
||||||
|
|
@ -25,21 +25,21 @@ typedef eoVirus<float> Chrom;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
const unsigned POP_SIZE = 100, CHROM_SIZE = 16;
|
const unsigned POP_SIZE = 1000, CHROM_SIZE = 128;
|
||||||
unsigned i;
|
unsigned i;
|
||||||
eoBooleanGenerator gen;
|
eoBooleanGenerator gen;
|
||||||
|
|
||||||
// the populations:
|
// the populations:
|
||||||
eoPop<Chrom> pop;
|
eoPop<Chrom> pop;
|
||||||
|
|
||||||
// Evaluation
|
// Evaluation
|
||||||
eoEvalFuncPtr<Chrom> eval( binary_value );
|
RoyalRoad<Chrom> rr( 4 );
|
||||||
|
|
||||||
eoInitVirus<float> random(CHROM_SIZE, gen);
|
eoInitVirus<float> random(CHROM_SIZE, gen);
|
||||||
for (i = 0; i < POP_SIZE; ++i) {
|
for (i = 0; i < POP_SIZE; ++i) {
|
||||||
Chrom chrom;
|
Chrom chrom;
|
||||||
random(chrom);
|
random(chrom);
|
||||||
eval(chrom);
|
rr(chrom);
|
||||||
pop.push_back(chrom);
|
pop.push_back(chrom);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -49,7 +49,7 @@ int main()
|
||||||
|
|
||||||
|
|
||||||
// selection
|
// selection
|
||||||
eoDetTournamentSelect<Chrom> lottery( 3) ;
|
eoStochTournamentSelect<Chrom> lottery(0.9 );
|
||||||
|
|
||||||
// breeder
|
// breeder
|
||||||
VirusMutation<float> vm;
|
VirusMutation<float> vm;
|
||||||
|
|
@ -58,27 +58,29 @@ int main()
|
||||||
eoUBitXover<Chrom> xover;
|
eoUBitXover<Chrom> xover;
|
||||||
eoProportionalOp<Chrom> propSel;
|
eoProportionalOp<Chrom> propSel;
|
||||||
eoGeneralBreeder<Chrom> breeder( lottery, propSel );
|
eoGeneralBreeder<Chrom> breeder( lottery, propSel );
|
||||||
propSel.add(vm, 0.25);
|
propSel.add(vm, 0.4);
|
||||||
propSel.add(vf, 0.25);
|
propSel.add(vf, 0.4);
|
||||||
propSel.add(vt, 0.25);
|
propSel.add(vt, 0.1);
|
||||||
propSel.add(xover, 0.25);
|
propSel.add(xover, 0.1);
|
||||||
|
|
||||||
// Replace a single one
|
// Replace a single one
|
||||||
eoPlusReplacement<Chrom> replace;
|
eoCommaReplacement<Chrom> replace;
|
||||||
|
|
||||||
// Terminators
|
// Terminators
|
||||||
eoGenContinue<Chrom> continuator1(50);
|
eoGenContinue<Chrom> continuator1(500);
|
||||||
eoFitContinue<Chrom> continuator2(65535.f);
|
eoFitContinue<Chrom> continuator2(128);
|
||||||
eoCombinedContinue<Chrom> continuator(continuator1, continuator2);
|
eoCombinedContinue<Chrom> continuator(continuator1, continuator2);
|
||||||
eoCheckPoint<Chrom> checkpoint(continuator);
|
eoCheckPoint<Chrom> checkpoint(continuator);
|
||||||
eoStdoutMonitor monitor;
|
eoStdoutMonitor monitor;
|
||||||
checkpoint.add(monitor);
|
checkpoint.add(monitor);
|
||||||
eoSecondMomentStats<Chrom> stats;
|
eoSecondMomentStats<Chrom> stats;
|
||||||
|
eoPopStat<Chrom> dumper( 10 );
|
||||||
monitor.add(stats);
|
monitor.add(stats);
|
||||||
|
checkpoint.add(dumper);
|
||||||
checkpoint.add(stats);
|
checkpoint.add(stats);
|
||||||
|
|
||||||
// GA generation
|
// GA generation
|
||||||
eoEasyEA<Chrom> ea(checkpoint, eval, breeder, replace );
|
eoEasyEA<Chrom> ea(checkpoint, rr, breeder, replace );
|
||||||
|
|
||||||
// evolution
|
// evolution
|
||||||
try
|
try
|
||||||
|
|
|
||||||
Reference in a new issue