small improvements
This commit is contained in:
parent
e5ff1bf59c
commit
8ee0ae0e3a
3 changed files with 89 additions and 47 deletions
|
|
@ -10,7 +10,7 @@
|
||||||
#include <utils/eoParser.h> // eoParser
|
#include <utils/eoParser.h> // eoParser
|
||||||
#include <eoPop.h> // eoPop
|
#include <eoPop.h> // eoPop
|
||||||
#include <eoEvalFuncPtr.h> // eoEvalFunc
|
#include <eoEvalFuncPtr.h> // eoEvalFunc
|
||||||
#include <eoStochTournament.h> // eoStochTournament
|
#include <eoStochTournament.h> // eoStochTournament
|
||||||
#include <eoGenContinue.h> // eoGenContinue
|
#include <eoGenContinue.h> // eoGenContinue
|
||||||
#include <eoFitContinue.h> // eoFitContinue
|
#include <eoFitContinue.h> // eoFitContinue
|
||||||
#include <eoCombinedContinue.h> // eoCombinedContinue
|
#include <eoCombinedContinue.h> // eoCombinedContinue
|
||||||
|
|
@ -32,8 +32,8 @@ unsigned in, out, hidden;
|
||||||
|
|
||||||
eoValueParam<unsigned> pop_size(10, "pop_size", "population size", 'p');
|
eoValueParam<unsigned> pop_size(10, "pop_size", "population size", 'p');
|
||||||
eoValueParam<unsigned> generations(10, "generations", "number of generation", 'g');
|
eoValueParam<unsigned> generations(10, "generations", "number of generation", 'g');
|
||||||
eoValueParam<double> mut_rate(0.1, "mut_rate", "mutation rate", 'm');
|
eoValueParam<double> mut_rate(0.25, "mut_rate", "mutation rate", 'm');
|
||||||
eoValueParam<double> xover_rate(0.1, "xover_rate", "default crossover rate", 'x');
|
eoValueParam<double> xover_rate(0.25, "xover_rate", "default crossover rate", 'x');
|
||||||
eoValueParam<string> file("", "file", "common start of patterns filenames *.trn *.val and *.tst", 'f');
|
eoValueParam<string> file("", "file", "common start of patterns filenames *.trn *.val and *.tst", 'f');
|
||||||
eoValueParam<unsigned> hiddenp(0, "hidden", "number of neurons in hidden layer", 'd');
|
eoValueParam<unsigned> hiddenp(0, "hidden", "number of neurons in hidden layer", 'd');
|
||||||
|
|
||||||
|
|
@ -165,11 +165,9 @@ void ga()
|
||||||
evaluator,
|
evaluator,
|
||||||
checkpoint);
|
checkpoint);
|
||||||
|
|
||||||
cout << pop << endl;
|
|
||||||
|
|
||||||
sga(pop);
|
sga(pop);
|
||||||
|
|
||||||
cout << pop << endl;
|
cout << "best: " << *max_element(pop.begin(), pop.end()) << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
|
@ -34,8 +34,8 @@ eoValueParam<unsigned> pop_size(16, "pop_size", "population size", 'p');
|
||||||
eoValueParam<unsigned> generations(100, "generations", "number of generation", 'g');
|
eoValueParam<unsigned> generations(100, "generations", "number of generation", 'g');
|
||||||
eoValueParam<double> mut_rate(0.1, "mut_rate", "mutation rate", 'm');
|
eoValueParam<double> mut_rate(0.1, "mut_rate", "mutation rate", 'm');
|
||||||
eoValueParam<double> xover_rate(0.5, "xover_rate", "default crossover rate", 'x');
|
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> col_p(default_colors, "colors", "number of colors", 'c');
|
||||||
eoValueParam<unsigned> len_p(8, "solution_legth", "solution legth", 'l');
|
eoValueParam<unsigned> len_p(default_length, "legth", "solution legth", 'l');
|
||||||
eoValueParam<string> sol_p(default_solution, "solution", "problem solution", 's');
|
eoValueParam<string> 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());
|
init_eoChromEvaluator(col_p.value(), len_p.value(), sol_p.value());
|
||||||
solution.fitness(eoChromEvaluator(solution));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
|
@ -35,38 +35,8 @@ typedef eoFixedLength<phenotype, int> Chrom;
|
||||||
// eoChromEvaluator
|
// eoChromEvaluator
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
const unsigned default_size = 8;
|
// const unsigned points_per_black = 3, points_per_white = 1;
|
||||||
const string default_solution = "01234567";
|
|
||||||
|
|
||||||
Chrom solution;
|
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)
|
phenotype eoChromEvaluator(const Chrom& chrom)
|
||||||
{
|
{
|
||||||
|
|
@ -95,6 +65,61 @@ phenotype eoChromEvaluator(const Chrom& chrom)
|
||||||
return black * chrom.size() + white;
|
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<char> color('0', static_cast<char>('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<int> color(0, num_colors);
|
||||||
|
generate(solution.begin(), solution.end(), color);
|
||||||
|
}
|
||||||
|
|
||||||
|
solution.fitness(eoChromEvaluator(solution));
|
||||||
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// eoChromInit
|
// eoChromInit
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
@ -117,17 +142,37 @@ public:
|
||||||
|
|
||||||
class eoChromMutation: public eoMonOp<Chrom>
|
class eoChromMutation: public eoMonOp<Chrom>
|
||||||
{
|
{
|
||||||
// two changes in one mutation :(
|
// many operators in one :(
|
||||||
void operator()(Chrom& chrom)
|
void operator()(Chrom& chrom)
|
||||||
{
|
{
|
||||||
|
uniform_generator<unsigned> what(0, 2);
|
||||||
uniform_generator<unsigned> position(0, chrom.size());
|
uniform_generator<unsigned> position(0, chrom.size());
|
||||||
|
|
||||||
// random gene change
|
switch(what())
|
||||||
uniform_generator<int> color(0, num_colors);
|
{
|
||||||
chrom[position()] = color();
|
case 0:
|
||||||
|
{
|
||||||
// random gene swap
|
cout << "mutation" << endl;
|
||||||
swap(chrom[position()], chrom[position()]);
|
// gene change
|
||||||
|
uniform_generator<int> 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();
|
chrom.invalidate();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Reference in a new issue