From 6a7102f5b620e025f0cb7399c24db5d6b94ac5da Mon Sep 17 00:00:00 2001 From: gustavoromero Date: Fri, 24 Nov 2000 12:33:44 +0000 Subject: [PATCH] changing genetic operators --- eo/app/gprop/gprop.cc | 39 +++++++++++++++++++++++---------------- eo/app/gprop/gprop.h | 32 ++++++++++++++++---------------- eo/src/eoProportional.h | 40 +++++++++++++++++++--------------------- 3 files changed, 58 insertions(+), 53 deletions(-) diff --git a/eo/app/gprop/gprop.cc b/eo/app/gprop/gprop.cc index d2b11a3a..4b4c3bd7 100644 --- a/eo/app/gprop/gprop.cc +++ b/eo/app/gprop/gprop.cc @@ -11,6 +11,7 @@ #include // eoPop #include // eoGenContinue #include // eoProportional +#include #include // eoSGA #include "gprop.h" // Chrom eoChromInit eoChromMutation eoChromXover eoChromEvaluator @@ -24,12 +25,12 @@ unsigned in, out, hidden; // parameters //----------------------------------------------------------------------------- -eoValueParam pop_size(10, "pop_size", "default population size", 'p'); -eoValueParam generations(10, "generations", "default generation number", 'g'); -eoValueParam mut_rate(0.1, "mut_rate", "default mutation rate", 'm'); +eoValueParam pop_size(10, "pop_size", "population size", 'p'); +eoValueParam generations(10, "generations", "number of generation", 'g'); +eoValueParam mut_rate(0.1, "mut_rate", "mutation rate", 'm'); eoValueParam xover_rate(0.1, "xover_rate", "default crossover rate", 'x'); -eoValueParam file("", "file", "common part of patterns filenames *.trn *.val and *.tst", 'f'); -eoValueParam hiddenp(8, "hidden", "default number of neurons in hidden layer", 'h'); +eoValueParam file("", "file", "common start of patterns filenames *.trn *.val and *.tst", 'f'); +eoValueParam hiddenp(0, "hidden", "number of neurons in hidden layer", 'd'); //----------------------------------------------------------------------------- // auxiliar functions @@ -108,8 +109,6 @@ void load_file(mlp::set& set, const string& ext) ifs >> set; - cout << "set.size() = " << set.size() << endl; - if (set.size() == 0) { cerr << filename << " data file is empty!"; @@ -122,22 +121,30 @@ void load_file(mlp::set& set, const string& ext) void ga() { eoGenContinue continuator(generations.value()); - - eoProportional select; + + // create population + eoInitChrom init; + eoPop pop(pop_size.value(), init); + + // evaluate population + eoEvalFuncPtr evaluator(eoChromEvaluator); + apply(evaluator, pop); + + // selector + // eoProportional select(pop); + eoStochTournament select; + + // genetic operators eoChromMutation mutation(generations); eoChromXover xover; - eoEvalFuncPtr evaluator(eoChromEvaluator); - + + // genetic algorithm eoSGA sga(select, xover, xover_rate.value(), mutation, mut_rate.value(), evaluator, continuator); - - eoInitChrom init; - eoPop pop(pop_size.value(), init); - apply(evaluator, pop); - + cout << pop << endl; sga(pop); diff --git a/eo/app/gprop/gprop.h b/eo/app/gprop/gprop.h index 8b5f3c82..41aa50db 100644 --- a/eo/app/gprop/gprop.h +++ b/eo/app/gprop/gprop.h @@ -8,6 +8,7 @@ //----------------------------------------------------------------------------- #include // istream ostream +#include // setprecision #include // string #include // EO #include // eoMonOp eoQuadraticOp @@ -29,15 +30,14 @@ struct phenotype static unsigned trn_max, val_max, tst_max; - phenotype(const double& _mse_error = 0): mse_error(_mse_error) {} - - operator double(void) const { return mse_error; } - + operator double(void) const { return val_ok; } + friend bool operator<(const phenotype& a, const phenotype& b) { - return a.mse_error < b.mse_error; + return a.val_ok < b.val_ok || + (!(a.val_ok < b.val_ok) && a.mse_error < b.mse_error); } - + friend ostream& operator<<(ostream& os, const phenotype& p) { return os << p.trn_ok << "/" << p.trn_max << " " @@ -69,17 +69,15 @@ extern unsigned in, out, hidden; class Chrom: public EO, public genotype { public: - Chrom(): genotype(in, out, vector(1, hidden)) - { - cout << "in = " << in << " out = " << out << " hidden = " << hidden << endl; - } + Chrom(): genotype(in, out, vector(1, hidden)) {} string className() const { return "Chrom"; } void printOn (ostream& os) const { - // os << static_cast(*this) << " " << fitness(); - os << fitness(); + os << setprecision(3) << static_cast(*this) << " \t" + << fitness(); + // os << fitness(); } void readFrom (istream& is) @@ -137,6 +135,12 @@ class eoChromXover: public eoQuadraticOp public: void operator()(Chrom& chrom1, Chrom& chrom2) { + chrom1.normalize(); + chrom2.desaturate(); + + mse::net tmp1(chrom1), tmp2(chrom2); + tmp1.train(trn_set, 100, 0, 0.001); + tmp2.train(trn_set, 100, 0, 0.001); } }; @@ -164,12 +168,8 @@ unsigned correct(const mlp::net& net, const qp::set& set) return sum; } - - phenotype eoChromEvaluator(const Chrom& chrom) { - // extern mlp::set trn_set, val_set, tst_set; - phenotype p; p.trn_ok = correct(chrom, trn_set); p.val_ok = correct(chrom, val_set); diff --git a/eo/src/eoProportional.h b/eo/src/eoProportional.h index f47cbba8..6840f697 100644 --- a/eo/src/eoProportional.h +++ b/eo/src/eoProportional.h @@ -35,7 +35,7 @@ //----------------------------------------------------------------------------- /** eoProportional: select an individual proportional to her stored fitness -value + value */ //----------------------------------------------------------------------------- @@ -43,30 +43,28 @@ value template class eoProportional: public eoSelectOne { public: - - /// Sanity check - eoProportional(void) : total(-1.0) - { - if (minimizing_fitness()) - { - throw logic_error("eoProportional: minimizing fitness"); - } - } + /// Sanity check + eoProportional(const eoPop& pop = eoPop()): + total((pop.size() == 0) ? -1.0 : sum_fitness(pop)) + { + if (minimizing_fitness()) + throw logic_error("eoProportional: minimizing fitness"); + } - void setup(const eoPop& _pop) - { - total = sum_fitness(_pop); - } + void setup(const eoPop& _pop) + { + total = sum_fitness(_pop); + } - /** do the selection, call roulette_wheel. - */ - const EOT& operator()(const eoPop& _pop) - { - return roulette_wheel(_pop, total) ; - } + /** do the selection, call roulette_wheel. + */ + const EOT& operator()(const eoPop& _pop) + { + return roulette_wheel(_pop, total) ; + } private : - typename EOT::Fitness total; + typename EOT::Fitness total; }; #endif