From c829969431aa07b0cb111a96499c997fd002a09b Mon Sep 17 00:00:00 2001 From: gustavoromero Date: Fri, 1 Dec 2000 19:13:12 +0000 Subject: [PATCH] initial version of mastermind --- eo/app/mastermind/Makefile.am | 25 +++++ eo/app/mastermind/mastermind.cc | 146 +++++++++++++++++++++++++++++ eo/app/mastermind/mastermind.h | 158 ++++++++++++++++++++++++++++++++ 3 files changed, 329 insertions(+) create mode 100644 eo/app/mastermind/Makefile.am create mode 100644 eo/app/mastermind/mastermind.cc create mode 100644 eo/app/mastermind/mastermind.h diff --git a/eo/app/mastermind/Makefile.am b/eo/app/mastermind/Makefile.am new file mode 100644 index 00000000..621733f0 --- /dev/null +++ b/eo/app/mastermind/Makefile.am @@ -0,0 +1,25 @@ +############################################################################### +# +# Makefile.am for app/mastermind +# +############################################################################### + +DEPS = $(top_builddir)/src/libeo.a $(top_builddir)/src/utils/libeoutils.a + +############################################################################### + +INCLUDES = -I$(top_builddir)/src +LDADDS = $(top_builddir)/src/libeo.a $(top_builddir)/src/utils/libeoutils.a + +############################################################################### + +bin_PROGRAMS = mastermind + +############################################################################### + +mastermind_SOURCES = mastermind.cc +mastermind_DEPENDENCIES = $(DEPS) +mastermind_LDFLAGS = -lm +mastermind_LDADD = $(LDADDS) + +############################################################################### diff --git a/eo/app/mastermind/mastermind.cc b/eo/app/mastermind/mastermind.cc new file mode 100644 index 00000000..0a05ad86 --- /dev/null +++ b/eo/app/mastermind/mastermind.cc @@ -0,0 +1,146 @@ +//----------------------------------------------------------------------------- +// mastermind +//----------------------------------------------------------------------------- + +#include // EXIT_SUCCESS EXIT_FAILURE +#include // exception +#include // cerr cout +#include // ifstream +#include // string +#include // eoParser +#include // eoPop +#include // eoEvalFunc +#include // eoProportional +#include // eoGenContinue +#include // eoFitContinue +#include // eoCombinedContinue +#include // eoCheckPoint +#include // eoBestFitnessStat +#include // eoStdoutMonitor +#include // eoSGA +#include "mastermind.h" // Chrom eoChromInit eoChromMutation eoChromXover eoChromEvaluator + +//----------------------------------------------------------------------------- +// global variables +//----------------------------------------------------------------------------- + +unsigned in, out, hidden; + +//----------------------------------------------------------------------------- +// parameters +//----------------------------------------------------------------------------- + +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 sol_p(default_solution, "solution", "problem solution", 's'); + +//----------------------------------------------------------------------------- +// auxiliar functions +//----------------------------------------------------------------------------- + +void arg(int argc, char** argv); +void ga(); + +//----------------------------------------------------------------------------- +// main +//----------------------------------------------------------------------------- + +int main(int argc, char** argv) +{ + try + { + arg(argc, argv); + ga(); + } + catch (exception& e) + { + cerr << argv[0] << ": " << e.what() << endl; + exit(EXIT_FAILURE); + } + + return 0; +} + +//----------------------------------------------------------------------------- +// implementation +//----------------------------------------------------------------------------- + +void arg(int argc, char** argv) +{ + eoParser parser(argc, argv); + + parser.processParam(pop_size, "genetic operators"); + parser.processParam(generations, "genetic operators"); + parser.processParam(mut_rate, "genetic operators"); + parser.processParam(xover_rate, "genetic operators"); + parser.processParam(col_p, "problem"); + parser.processParam(len_p, "problem"); + parser.processParam(sol_p, "problem"); + + if (parser.userNeedsHelp()) + { + parser.printHelp(cout); + exit(EXIT_SUCCESS); + } + + init_eoChromEvaluator(col_p.value(), len_p.value(), sol_p.value()); + solution.fitness(eoChromEvaluator(solution)); +} + +//----------------------------------------------------------------------------- + +void ga() +{ + // create population + eoInitChrom init; + eoPop pop(pop_size.value(), init); + + // evaluate population + eoEvalFuncPtr evaluator(eoChromEvaluator); + apply(evaluator, pop); + + // selector + eoProportional select(pop); + + // genetic operators + eoChromMutation mutation; + eoChromXover xover; + + // stop condition + eoGenContinue continuator1(generations.value()); + eoFitContinue continuator2(solution.fitness()); + eoCombinedContinue continuator(continuator1, continuator2); + + // checkpoint + eoCheckPoint checkpoint(continuator); + + // monitor + eoStdoutMonitor monitor; + checkpoint.add(monitor); + + // statistics + eoBestFitnessStat stats; + checkpoint.add(stats); + monitor.add(stats); + + // genetic algorithm + eoSGA sga(select, + xover, xover_rate.value(), + mutation, mut_rate.value(), + evaluator, + checkpoint); + sga(pop); + + cout << "solution = " << solution << endl + << "best = " << *max_element(pop.begin(), pop.end()) << endl; +} + +//----------------------------------------------------------------------------- + +// Local Variables: +// mode:C++ +// End: diff --git a/eo/app/mastermind/mastermind.h b/eo/app/mastermind/mastermind.h new file mode 100644 index 00000000..2735d2c1 --- /dev/null +++ b/eo/app/mastermind/mastermind.h @@ -0,0 +1,158 @@ +//----------------------------------------------------------------------------- +// mastermind.h +//----------------------------------------------------------------------------- + +#ifndef mastermind_h +#define mastermind_h + +//----------------------------------------------------------------------------- + +#include // exit EXIT_FAILURE +#include // eoFixedLength +#include // eoMonOp eoQuadraticOp +#include // eoInit +#include // uniform_generator + +//----------------------------------------------------------------------------- +// phenotype +//----------------------------------------------------------------------------- + +typedef float phenotype; + +//----------------------------------------------------------------------------- +// genotype +//----------------------------------------------------------------------------- + +typedef vector genotype; + +//----------------------------------------------------------------------------- +// Chrom +//----------------------------------------------------------------------------- + +typedef eoFixedLength Chrom; + +//----------------------------------------------------------------------------- +// eoChromEvaluator +//----------------------------------------------------------------------------- + +const unsigned default_size = 8; +const string default_solution = "01234567"; + +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) +{ + Chrom tmp = solution; + unsigned black = 0, white = 0; + + // look for blacks + for (unsigned i = 0; i < chrom.size(); ++i) + if (chrom[i] == tmp[i]) + { + ++black; + tmp[i] = -1; + } + + // look for whites + for (unsigned i = 0; i < chrom.size(); ++i) + for (unsigned j = 0; j < tmp.size(); ++j) + if (chrom[i] == tmp[j]) + { + ++white; + tmp[j] = -1; + break; + } + + // return black * points_per_black + white * points_per_white; + return black * chrom.size() + white; +}; + +//----------------------------------------------------------------------------- +// eoChromInit +//----------------------------------------------------------------------------- + +class eoInitChrom: public eoInit +{ +public: + void operator()(Chrom& chrom) + { + uniform_generator color(0, num_colors); + chrom.resize(solution.size()); + generate(chrom.begin(), chrom.end(), color); + chrom.invalidate(); + } +}; + +//----------------------------------------------------------------------------- +// eoChromMutation +//----------------------------------------------------------------------------- + +class eoChromMutation: public eoMonOp +{ + // two changes in one mutation :( + void operator()(Chrom& chrom) + { + 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()]); + + chrom.invalidate(); + } +}; + +//----------------------------------------------------------------------------- +// eoChromXover +//----------------------------------------------------------------------------- + +class eoChromXover: public eoQuadraticOp +{ +public: + void operator()(Chrom& chrom1, Chrom& chrom2) + { + uniform_generator position(0, chrom1.size()); + swap_ranges(chrom1.begin(), chrom1.begin() + position(), chrom2.begin()); + chrom1.invalidate(); + chrom2.invalidate(); + } +}; + +//----------------------------------------------------------------------------- + +#endif // mastermind_h + +// Local Variables: +// mode:C++ +// End: