initial version of mastermind
This commit is contained in:
parent
2abab21960
commit
c829969431
3 changed files with 329 additions and 0 deletions
25
eo/app/mastermind/Makefile.am
Normal file
25
eo/app/mastermind/Makefile.am
Normal file
|
|
@ -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)
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
146
eo/app/mastermind/mastermind.cc
Normal file
146
eo/app/mastermind/mastermind.cc
Normal file
|
|
@ -0,0 +1,146 @@
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// mastermind
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include <stdlib.h> // EXIT_SUCCESS EXIT_FAILURE
|
||||||
|
#include <stdexcept> // exception
|
||||||
|
#include <iostream> // cerr cout
|
||||||
|
#include <fstream> // ifstream
|
||||||
|
#include <string> // string
|
||||||
|
#include <utils/eoParser.h> // eoParser
|
||||||
|
#include <eoPop.h> // eoPop
|
||||||
|
#include <eoEvalFuncPtr.h> // eoEvalFunc
|
||||||
|
#include <eoProportional.h> // eoProportional
|
||||||
|
#include <eoGenContinue.h> // eoGenContinue
|
||||||
|
#include <eoFitContinue.h> // eoFitContinue
|
||||||
|
#include <eoCombinedContinue.h> // eoCombinedContinue
|
||||||
|
#include <utils/eoCheckPoint.h> // eoCheckPoint
|
||||||
|
#include <utils/eoStat.h> // eoBestFitnessStat
|
||||||
|
#include <utils/eoStdoutMonitor.h> // eoStdoutMonitor
|
||||||
|
#include <eoSGA.h> // eoSGA
|
||||||
|
#include "mastermind.h" // Chrom eoChromInit eoChromMutation eoChromXover eoChromEvaluator
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// global variables
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
unsigned in, out, hidden;
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// parameters
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
eoValueParam<unsigned> pop_size(16, "pop_size", "population size", 'p');
|
||||||
|
eoValueParam<unsigned> generations(100, "generations", "number of generation", 'g');
|
||||||
|
eoValueParam<double> mut_rate(0.1, "mut_rate", "mutation rate", 'm');
|
||||||
|
eoValueParam<double> xover_rate(0.5, "xover_rate", "default crossover rate", 'x');
|
||||||
|
eoValueParam<unsigned> col_p(8, "number_of_colors", "number of colors", 'c');
|
||||||
|
eoValueParam<unsigned> len_p(8, "solution_legth", "solution legth", 'l');
|
||||||
|
eoValueParam<string> 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<Chrom> pop(pop_size.value(), init);
|
||||||
|
|
||||||
|
// evaluate population
|
||||||
|
eoEvalFuncPtr<Chrom> evaluator(eoChromEvaluator);
|
||||||
|
apply<Chrom>(evaluator, pop);
|
||||||
|
|
||||||
|
// selector
|
||||||
|
eoProportional<Chrom> select(pop);
|
||||||
|
|
||||||
|
// genetic operators
|
||||||
|
eoChromMutation mutation;
|
||||||
|
eoChromXover xover;
|
||||||
|
|
||||||
|
// stop condition
|
||||||
|
eoGenContinue<Chrom> continuator1(generations.value());
|
||||||
|
eoFitContinue<Chrom> continuator2(solution.fitness());
|
||||||
|
eoCombinedContinue<Chrom> continuator(continuator1, continuator2);
|
||||||
|
|
||||||
|
// checkpoint
|
||||||
|
eoCheckPoint<Chrom> checkpoint(continuator);
|
||||||
|
|
||||||
|
// monitor
|
||||||
|
eoStdoutMonitor monitor;
|
||||||
|
checkpoint.add(monitor);
|
||||||
|
|
||||||
|
// statistics
|
||||||
|
eoBestFitnessStat<Chrom> stats;
|
||||||
|
checkpoint.add(stats);
|
||||||
|
monitor.add(stats);
|
||||||
|
|
||||||
|
// genetic algorithm
|
||||||
|
eoSGA<Chrom> 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:
|
||||||
158
eo/app/mastermind/mastermind.h
Normal file
158
eo/app/mastermind/mastermind.h
Normal file
|
|
@ -0,0 +1,158 @@
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// mastermind.h
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifndef mastermind_h
|
||||||
|
#define mastermind_h
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include <stdlib.h> // exit EXIT_FAILURE
|
||||||
|
#include <eoFixedLength.h> // eoFixedLength
|
||||||
|
#include <eoOp.h> // eoMonOp eoQuadraticOp
|
||||||
|
#include <eoInit.h> // eoInit
|
||||||
|
#include <utils/rnd_generators.h> // uniform_generator
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// phenotype
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
typedef float phenotype;
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// genotype
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
typedef vector<int> genotype;
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Chrom
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
typedef eoFixedLength<phenotype, int> 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<char> color('0', static_cast<char>('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<Chrom>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void operator()(Chrom& chrom)
|
||||||
|
{
|
||||||
|
uniform_generator<int> color(0, num_colors);
|
||||||
|
chrom.resize(solution.size());
|
||||||
|
generate(chrom.begin(), chrom.end(), color);
|
||||||
|
chrom.invalidate();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// eoChromMutation
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class eoChromMutation: public eoMonOp<Chrom>
|
||||||
|
{
|
||||||
|
// two changes in one mutation :(
|
||||||
|
void operator()(Chrom& chrom)
|
||||||
|
{
|
||||||
|
uniform_generator<unsigned> position(0, chrom.size());
|
||||||
|
|
||||||
|
// random gene change
|
||||||
|
uniform_generator<int> color(0, num_colors);
|
||||||
|
chrom[position()] = color();
|
||||||
|
|
||||||
|
// random gene swap
|
||||||
|
swap(chrom[position()], chrom[position()]);
|
||||||
|
|
||||||
|
chrom.invalidate();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// eoChromXover
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class eoChromXover: public eoQuadraticOp<Chrom>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void operator()(Chrom& chrom1, Chrom& chrom2)
|
||||||
|
{
|
||||||
|
uniform_generator<unsigned> 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:
|
||||||
Reference in a new issue