changing genetic operators

This commit is contained in:
gustavoromero 2000-11-24 12:33:44 +00:00
commit 6a7102f5b6
3 changed files with 58 additions and 53 deletions

View file

@ -11,6 +11,7 @@
#include <eoPop.h> // eoPop
#include <eoGenContinue.h> // eoGenContinue
#include <eoProportional.h> // eoProportional
#include <eoStochTournament.h>
#include <eoSGA.h> // eoSGA
#include "gprop.h" // Chrom eoChromInit eoChromMutation eoChromXover eoChromEvaluator
@ -24,12 +25,12 @@ unsigned in, out, hidden;
// parameters
//-----------------------------------------------------------------------------
eoValueParam<unsigned> pop_size(10, "pop_size", "default population size", 'p');
eoValueParam<unsigned> generations(10, "generations", "default generation number", 'g');
eoValueParam<double> mut_rate(0.1, "mut_rate", "default mutation rate", 'm');
eoValueParam<unsigned> pop_size(10, "pop_size", "population size", 'p');
eoValueParam<unsigned> generations(10, "generations", "number of generation", 'g');
eoValueParam<double> mut_rate(0.1, "mut_rate", "mutation rate", 'm');
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<unsigned> hiddenp(8, "hidden", "default number of neurons in hidden layer", 'h');
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');
//-----------------------------------------------------------------------------
// auxiliar functions
@ -108,8 +109,6 @@ void load_file(mlp::set& set, const string& ext)
ifs >> set;
cout << "set.size() = " << set.size() << endl;
if (set.size() == 0)
{
cerr << filename << " data file is empty!";
@ -122,22 +121,30 @@ void load_file(mlp::set& set, const string& ext)
void ga()
{
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);
eoChromXover xover;
eoEvalFuncPtr<Chrom> evaluator(eoChromEvaluator);
// genetic algorithm
eoSGA<Chrom> sga(select,
xover, xover_rate.value(),
mutation, mut_rate.value(),
evaluator,
continuator);
eoInitChrom init;
eoPop<Chrom> pop(pop_size.value(), init);
apply<Chrom>(evaluator, pop);
cout << pop << endl;
sga(pop);

View file

@ -8,6 +8,7 @@
//-----------------------------------------------------------------------------
#include <iostream> // istream ostream
#include <iomanip> // setprecision
#include <string> // string
#include <EO.h> // EO
#include <eoOp.h> // eoMonOp eoQuadraticOp
@ -29,15 +30,14 @@ struct phenotype
static unsigned trn_max, val_max, tst_max;
phenotype(const double& _mse_error = 0): mse_error(_mse_error) {}
operator double(void) const { return mse_error; }
operator double(void) const { return val_ok; }
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)
{
return os << p.trn_ok << "/" << p.trn_max << " "
@ -69,17 +69,15 @@ extern unsigned in, out, hidden;
class Chrom: public EO<phenotype>, public genotype
{
public:
Chrom(): genotype(in, out, vector<unsigned>(1, hidden))
{
cout << "in = " << in << " out = " << out << " hidden = " << hidden << endl;
}
Chrom(): genotype(in, out, vector<unsigned>(1, hidden)) {}
string className() const { return "Chrom"; }
void printOn (ostream& os) const
{
// os << static_cast<genotype>(*this) << " " << fitness();
os << fitness();
os << setprecision(3) << static_cast<genotype>(*this) << " \t"
<< fitness();
// os << fitness();
}
void readFrom (istream& is)
@ -137,6 +135,12 @@ class eoChromXover: public eoQuadraticOp<Chrom>
public:
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;
}
phenotype eoChromEvaluator(const Chrom& chrom)
{
// extern mlp::set trn_set, val_set, tst_set;
phenotype p;
p.trn_ok = correct(chrom, trn_set);
p.val_ok = correct(chrom, val_set);

View file

@ -35,7 +35,7 @@
//-----------------------------------------------------------------------------
/** eoProportional: select an individual proportional to her stored fitness
value
value
*/
//-----------------------------------------------------------------------------
@ -43,30 +43,28 @@ value
template <class EOT> class eoProportional: public eoSelectOne<EOT>
{
public:
/// Sanity check
eoProportional(void) : total(-1.0)
{
if (minimizing_fitness<EOT>())
{
throw logic_error("eoProportional: minimizing fitness");
}
}
/// 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");
}
void setup(const eoPop<EOT>& _pop)
{
total = sum_fitness(_pop);
}
void setup(const eoPop<EOT>& _pop)
{
total = sum_fitness(_pop);
}
/** do the selection, call roulette_wheel.
*/
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 :
typename EOT::Fitness total;
typename EOT::Fitness total;
};
#endif