From 8ee0ae0e3a9c9549489d673f104e25f06a71bce9 Mon Sep 17 00:00:00 2001 From: gustavoromero Date: Mon, 4 Dec 2000 19:10:20 +0000 Subject: [PATCH] small improvements --- eo/app/gprop/gprop.cc | 10 ++- eo/app/mastermind/mastermind.cc | 5 +- eo/app/mastermind/mastermind.h | 121 ++++++++++++++++++++++---------- 3 files changed, 89 insertions(+), 47 deletions(-) diff --git a/eo/app/gprop/gprop.cc b/eo/app/gprop/gprop.cc index 12a53014..3b7d93ec 100644 --- a/eo/app/gprop/gprop.cc +++ b/eo/app/gprop/gprop.cc @@ -10,7 +10,7 @@ #include // eoParser #include // eoPop #include // eoEvalFunc -#include // eoStochTournament +#include // eoStochTournament #include // eoGenContinue #include // eoFitContinue #include // eoCombinedContinue @@ -32,8 +32,8 @@ unsigned in, out, hidden; 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 mut_rate(0.25, "mut_rate", "mutation rate", 'm'); +eoValueParam xover_rate(0.25, "xover_rate", "default crossover rate", 'x'); eoValueParam file("", "file", "common start of patterns filenames *.trn *.val and *.tst", 'f'); eoValueParam hiddenp(0, "hidden", "number of neurons in hidden layer", 'd'); @@ -165,11 +165,9 @@ void ga() evaluator, checkpoint); - cout << pop << endl; - sga(pop); - cout << pop << endl; + cout << "best: " << *max_element(pop.begin(), pop.end()) << endl; } //----------------------------------------------------------------------------- diff --git a/eo/app/mastermind/mastermind.cc b/eo/app/mastermind/mastermind.cc index 0a05ad86..6be66e84 100644 --- a/eo/app/mastermind/mastermind.cc +++ b/eo/app/mastermind/mastermind.cc @@ -34,8 +34,8 @@ eoValueParam pop_size(16, "pop_size", "population size", 'p'); eoValueParam generations(100, "generations", "number of generation", 'g'); eoValueParam mut_rate(0.1, "mut_rate", "mutation rate", 'm'); eoValueParam xover_rate(0.5, "xover_rate", "default crossover rate", 'x'); -eoValueParam col_p(8, "number_of_colors", "number of colors", 'c'); -eoValueParam len_p(8, "solution_legth", "solution legth", 'l'); +eoValueParam col_p(default_colors, "colors", "number of colors", 'c'); +eoValueParam len_p(default_length, "legth", "solution legth", 'l'); eoValueParam sol_p(default_solution, "solution", "problem solution", 's'); //----------------------------------------------------------------------------- @@ -88,7 +88,6 @@ void arg(int argc, char** argv) } init_eoChromEvaluator(col_p.value(), len_p.value(), sol_p.value()); - solution.fitness(eoChromEvaluator(solution)); } //----------------------------------------------------------------------------- diff --git a/eo/app/mastermind/mastermind.h b/eo/app/mastermind/mastermind.h index 2735d2c1..fb60135f 100644 --- a/eo/app/mastermind/mastermind.h +++ b/eo/app/mastermind/mastermind.h @@ -35,38 +35,8 @@ typedef eoFixedLength Chrom; // eoChromEvaluator //----------------------------------------------------------------------------- -const unsigned default_size = 8; -const string default_solution = "01234567"; - +// const unsigned points_per_black = 3, points_per_white = 1; Chrom solution; -unsigned num_colors; - -void init_eoChromEvaluator(const unsigned& c, const unsigned& l, string s) -{ - num_colors = c; - - // generate a random solution - if (s == default_solution || s.size() != l) - { - uniform_generator color('0', static_cast('0' + num_colors)); - s.resize(l); - generate(s.begin(), s.end(), color); - } - - // check solution - for (unsigned i = 0; i < solution.size(); ++i) - if (solution[i] >= num_colors) - { - cerr << "too high color number found!" << endl; - exit(EXIT_FAILURE); - } - - solution.resize(s.size()); - for (unsigned i = 0; i < solution.size(); ++i) - solution[i] = s[i] - '0'; -} - -const unsigned points_per_black = 3, points_per_white = 1; phenotype eoChromEvaluator(const Chrom& chrom) { @@ -95,6 +65,61 @@ phenotype eoChromEvaluator(const Chrom& chrom) return black * chrom.size() + white; }; +const unsigned default_length = 8; +const unsigned default_colors = 8; +const string default_solution = "01234567"; + + +unsigned num_colors; + +void init_eoChromEvaluator(const unsigned& c, const unsigned& l, string s) +{ + num_colors = c; + + // check consistency between parameters + if (s != default_solution) + { + // check length + if (l != default_length && s.size() != l) + { + cerr << "solution length != length" << endl; + exit(EXIT_FAILURE); + } + + // check number of colors + if (c != default_colors && c < *max_element(s.begin(), s.end()) - '0') + { + cerr << "too high color number found!" << endl; + exit(EXIT_FAILURE); + } + } + else + if (l != default_length || c != default_colors ) + // generate a random solution + if(num_colors <= 10) + { + uniform_generator color('0', static_cast('0' + c)); + s.resize(l); + generate(s.begin(), s.end(), color); + } + + // put the solution parameter on the solution chromosome + if (num_colors <= 10) + { + solution.resize(s.size()); + for (unsigned i = 0; i < solution.size(); ++i) + solution[i] = s[i] - '0'; + } + else + { + solution.resize(l); + uniform_generator color(0, num_colors); + generate(solution.begin(), solution.end(), color); + } + + solution.fitness(eoChromEvaluator(solution)); +} + //----------------------------------------------------------------------------- // eoChromInit //----------------------------------------------------------------------------- @@ -117,17 +142,37 @@ public: class eoChromMutation: public eoMonOp { - // two changes in one mutation :( + // many operators in one :( void operator()(Chrom& chrom) { + uniform_generator what(0, 2); uniform_generator position(0, chrom.size()); - // random gene change - uniform_generator color(0, num_colors); - chrom[position()] = color(); - - // random gene swap - swap(chrom[position()], chrom[position()]); + switch(what()) + { + case 0: + { + cout << "mutation" << endl; + // gene change + uniform_generator color(0, num_colors); + chrom[position()] = color(); + break; + } + case 1: + { + cout << "transposition" << endl; + // transposition + swap(chrom[position()], chrom[position()]); + break; + + } + default: + { + cerr << "unknown operator!" << endl; + exit(EXIT_FAILURE); + break; + } + } chrom.invalidate(); }