changing genetic operators
This commit is contained in:
parent
2b03bd6dae
commit
6a7102f5b6
3 changed files with 58 additions and 53 deletions
|
|
@ -11,6 +11,7 @@
|
||||||
#include <eoPop.h> // eoPop
|
#include <eoPop.h> // eoPop
|
||||||
#include <eoGenContinue.h> // eoGenContinue
|
#include <eoGenContinue.h> // eoGenContinue
|
||||||
#include <eoProportional.h> // eoProportional
|
#include <eoProportional.h> // eoProportional
|
||||||
|
#include <eoStochTournament.h>
|
||||||
#include <eoSGA.h> // eoSGA
|
#include <eoSGA.h> // eoSGA
|
||||||
#include "gprop.h" // Chrom eoChromInit eoChromMutation eoChromXover eoChromEvaluator
|
#include "gprop.h" // Chrom eoChromInit eoChromMutation eoChromXover eoChromEvaluator
|
||||||
|
|
||||||
|
|
@ -24,12 +25,12 @@ unsigned in, out, hidden;
|
||||||
// parameters
|
// parameters
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
eoValueParam<unsigned> pop_size(10, "pop_size", "default population size", 'p');
|
eoValueParam<unsigned> pop_size(10, "pop_size", "population size", 'p');
|
||||||
eoValueParam<unsigned> generations(10, "generations", "default generation number", 'g');
|
eoValueParam<unsigned> generations(10, "generations", "number of generation", 'g');
|
||||||
eoValueParam<double> mut_rate(0.1, "mut_rate", "default mutation rate", 'm');
|
eoValueParam<double> mut_rate(0.1, "mut_rate", "mutation rate", 'm');
|
||||||
eoValueParam<double> xover_rate(0.1, "xover_rate", "default crossover rate", 'x');
|
eoValueParam<double> xover_rate(0.1, "xover_rate", "default crossover rate", 'x');
|
||||||
eoValueParam<string> file("", "file", "common part 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(8, "hidden", "default number of neurons in hidden layer", 'h');
|
eoValueParam<unsigned> hiddenp(0, "hidden", "number of neurons in hidden layer", 'd');
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// auxiliar functions
|
// auxiliar functions
|
||||||
|
|
@ -108,8 +109,6 @@ void load_file(mlp::set& set, const string& ext)
|
||||||
|
|
||||||
ifs >> set;
|
ifs >> set;
|
||||||
|
|
||||||
cout << "set.size() = " << set.size() << endl;
|
|
||||||
|
|
||||||
if (set.size() == 0)
|
if (set.size() == 0)
|
||||||
{
|
{
|
||||||
cerr << filename << " data file is empty!";
|
cerr << filename << " data file is empty!";
|
||||||
|
|
@ -123,21 +122,29 @@ void ga()
|
||||||
{
|
{
|
||||||
eoGenContinue<Chrom> continuator(generations.value());
|
eoGenContinue<Chrom> continuator(generations.value());
|
||||||
|
|
||||||
eoProportional<Chrom> select;
|
// 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);
|
||||||
|
eoStochTournament<Chrom> select;
|
||||||
|
|
||||||
|
// genetic operators
|
||||||
eoChromMutation mutation(generations);
|
eoChromMutation mutation(generations);
|
||||||
eoChromXover xover;
|
eoChromXover xover;
|
||||||
eoEvalFuncPtr<Chrom> evaluator(eoChromEvaluator);
|
|
||||||
|
|
||||||
|
// genetic algorithm
|
||||||
eoSGA<Chrom> sga(select,
|
eoSGA<Chrom> sga(select,
|
||||||
xover, xover_rate.value(),
|
xover, xover_rate.value(),
|
||||||
mutation, mut_rate.value(),
|
mutation, mut_rate.value(),
|
||||||
evaluator,
|
evaluator,
|
||||||
continuator);
|
continuator);
|
||||||
|
|
||||||
eoInitChrom init;
|
|
||||||
eoPop<Chrom> pop(pop_size.value(), init);
|
|
||||||
apply<Chrom>(evaluator, pop);
|
|
||||||
|
|
||||||
cout << pop << endl;
|
cout << pop << endl;
|
||||||
|
|
||||||
sga(pop);
|
sga(pop);
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#include <iostream> // istream ostream
|
#include <iostream> // istream ostream
|
||||||
|
#include <iomanip> // setprecision
|
||||||
#include <string> // string
|
#include <string> // string
|
||||||
#include <EO.h> // EO
|
#include <EO.h> // EO
|
||||||
#include <eoOp.h> // eoMonOp eoQuadraticOp
|
#include <eoOp.h> // eoMonOp eoQuadraticOp
|
||||||
|
|
@ -29,13 +30,12 @@ struct phenotype
|
||||||
|
|
||||||
static unsigned trn_max, val_max, tst_max;
|
static unsigned trn_max, val_max, tst_max;
|
||||||
|
|
||||||
phenotype(const double& _mse_error = 0): mse_error(_mse_error) {}
|
operator double(void) const { return val_ok; }
|
||||||
|
|
||||||
operator double(void) const { return mse_error; }
|
|
||||||
|
|
||||||
friend bool operator<(const phenotype& a, const phenotype& b)
|
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)
|
friend ostream& operator<<(ostream& os, const phenotype& p)
|
||||||
|
|
@ -69,17 +69,15 @@ extern unsigned in, out, hidden;
|
||||||
class Chrom: public EO<phenotype>, public genotype
|
class Chrom: public EO<phenotype>, public genotype
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Chrom(): genotype(in, out, vector<unsigned>(1, hidden))
|
Chrom(): genotype(in, out, vector<unsigned>(1, hidden)) {}
|
||||||
{
|
|
||||||
cout << "in = " << in << " out = " << out << " hidden = " << hidden << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
string className() const { return "Chrom"; }
|
string className() const { return "Chrom"; }
|
||||||
|
|
||||||
void printOn (ostream& os) const
|
void printOn (ostream& os) const
|
||||||
{
|
{
|
||||||
// os << static_cast<genotype>(*this) << " " << fitness();
|
os << setprecision(3) << static_cast<genotype>(*this) << " \t"
|
||||||
os << fitness();
|
<< fitness();
|
||||||
|
// os << fitness();
|
||||||
}
|
}
|
||||||
|
|
||||||
void readFrom (istream& is)
|
void readFrom (istream& is)
|
||||||
|
|
@ -137,6 +135,12 @@ class eoChromXover: public eoQuadraticOp<Chrom>
|
||||||
public:
|
public:
|
||||||
void operator()(Chrom& chrom1, Chrom& chrom2)
|
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;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
phenotype eoChromEvaluator(const Chrom& chrom)
|
phenotype eoChromEvaluator(const Chrom& chrom)
|
||||||
{
|
{
|
||||||
// extern mlp::set trn_set, val_set, tst_set;
|
|
||||||
|
|
||||||
phenotype p;
|
phenotype p;
|
||||||
p.trn_ok = correct(chrom, trn_set);
|
p.trn_ok = correct(chrom, trn_set);
|
||||||
p.val_ok = correct(chrom, val_set);
|
p.val_ok = correct(chrom, val_set);
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
/** eoProportional: select an individual proportional to her stored fitness
|
/** eoProportional: select an individual proportional to her stored fitness
|
||||||
value
|
value
|
||||||
|
|
||||||
*/
|
*/
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
@ -43,30 +43,28 @@ value
|
||||||
template <class EOT> class eoProportional: public eoSelectOne<EOT>
|
template <class EOT> class eoProportional: public eoSelectOne<EOT>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
/// Sanity check
|
||||||
|
eoProportional(const eoPop<EOT>& pop = eoPop<EOT>()):
|
||||||
|
total((pop.size() == 0) ? -1.0 : sum_fitness(pop))
|
||||||
|
{
|
||||||
|
if (minimizing_fitness<EOT>())
|
||||||
|
throw logic_error("eoProportional: minimizing fitness");
|
||||||
|
}
|
||||||
|
|
||||||
/// Sanity check
|
void setup(const eoPop<EOT>& _pop)
|
||||||
eoProportional(void) : total(-1.0)
|
{
|
||||||
{
|
total = sum_fitness(_pop);
|
||||||
if (minimizing_fitness<EOT>())
|
}
|
||||||
{
|
|
||||||
throw logic_error("eoProportional: minimizing fitness");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup(const eoPop<EOT>& _pop)
|
/** do the selection, call roulette_wheel.
|
||||||
{
|
*/
|
||||||
total = sum_fitness(_pop);
|
const EOT& operator()(const eoPop<EOT>& _pop)
|
||||||
}
|
{
|
||||||
|
return roulette_wheel(_pop, total) ;
|
||||||
/** do the selection, call roulette_wheel.
|
}
|
||||||
*/
|
|
||||||
const EOT& operator()(const eoPop<EOT>& _pop)
|
|
||||||
{
|
|
||||||
return roulette_wheel(_pop, total) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
private :
|
private :
|
||||||
typename EOT::Fitness total;
|
typename EOT::Fitness total;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Reference in a new issue