paradiseo/eo/app/gprop/gprop.h
maartenkeijzer 3a9b5a0e7e Well, what do you know, major commit.
Changed the signature of eoMon, eoBin and eoQuadOp to return a bool,
without invalidating fitness. Added a set of invalidators to take over
that job (see for instance eoSGA and eoSGATransform how this can transparantly used)

Derived eoState from eoFunctorStore (for convenience, from a design perspective this may sound wrong)

Added a wrap_op function that does the wrapping for you (see eoOpContainer how this made this functor
exceedingly less hairy). Checked all the tests removed the eoGeneric*Op family (not needed anymore)
and of course changed all the operators to reflect the change (and found a few that didn't
invalidate the fitness, thus really pointing out the advantage of the current approach)
2001-02-14 10:35:26 +00:00

181 lines
4.8 KiB
C++

//-----------------------------------------------------------------------------
// gprop.h
//-----------------------------------------------------------------------------
#ifndef gprop_h
#define gprop_h
//-----------------------------------------------------------------------------
#include <iostream> // istream ostream
#include <iomanip> // setprecision
#include <string> // string
#include <EO.h> // EO
#include <eoOp.h> // eoMonOp eoQuadraticOp
#include <eoInit.h> // eoInit
#include <utils/rnd_generators.h> // normal_generator
#include "mlp.h" // mlp::net mlp::set
#include "qp.h" // qp::set
#include "mse.h" // mse::error
//-----------------------------------------------------------------------------
// phenotype
//-----------------------------------------------------------------------------
struct phenotype
{
unsigned trn_ok, val_ok, tst_ok;
double mse_error;
static unsigned trn_max, val_max, tst_max;
friend bool operator<(const phenotype& a, const phenotype& b)
{
return a.val_ok < b.val_ok || !(b.val_ok < a.val_ok) && b.mse_error < a.mse_error;
}
friend ostream& operator<<(ostream& os, const phenotype& p)
{
return os << p.trn_ok << "/" << p.trn_max << " "
<< p.val_ok << "/" << p.val_max << " "
<< p.tst_ok << "/" << p.tst_max << " "
<< p.mse_error;
}
friend istream& operator>>(istream& is, phenotype& p)
{
return is; // complete me
}
};
unsigned phenotype::trn_max = 0, phenotype::val_max = 0, phenotype::tst_max = 0;
//-----------------------------------------------------------------------------
// genotype
//-----------------------------------------------------------------------------
typedef mlp::net genotype;
//-----------------------------------------------------------------------------
// Chrom
//-----------------------------------------------------------------------------
extern unsigned in, out, hidden;
class Chrom: public EO<phenotype>, public genotype
{
public:
Chrom(): genotype(in, out, vector<unsigned>(hidden < 1 ? 0 : 1, hidden)) {}
string className() const { return "Chrom"; }
void printOn (ostream& os) const
{
os << setprecision(3) << static_cast<genotype>(*this) << " \t"
<< fitness();
// os << fitness();
}
void readFrom (istream& is)
{
invalidate(); // complete me
}
};
//-----------------------------------------------------------------------------
// eoChromInit
//-----------------------------------------------------------------------------
class eoInitChrom: public eoInit<Chrom>
{
public:
void operator()(Chrom& chrom)
{
chrom.reset();
chrom.invalidate();
}
};
//-----------------------------------------------------------------------------
// global variables
//-----------------------------------------------------------------------------
mlp::set trn_set, val_set, tst_set;
//-----------------------------------------------------------------------------
// eoChromMutation
//-----------------------------------------------------------------------------
class eoChromMutation: public eoMonOp<Chrom>
{
public:
bool operator()(Chrom& chrom)
{
mse::net tmp(chrom);
tmp.train(trn_set, 10, 0, 0.001);
return true;
}
};
//-----------------------------------------------------------------------------
// eoChromXover
//-----------------------------------------------------------------------------
class eoChromXover: public eoQuadOp<Chrom>
{
public:
bool 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);
return true;
}
};
//-----------------------------------------------------------------------------
// eoChromEvaluator
//-----------------------------------------------------------------------------
unsigned correct(const mlp::net& net, const qp::set& set)
{
unsigned sum = 0;
for (qp::set::const_iterator s = set.begin(); s != set.end(); ++s)
{
unsigned partial = 0;
for (unsigned i = 0; i < s->output.size(); ++i)
if (s->output[i] < 0.5 && net(s->input)[i] < 0.5 ||
s->output[i] > 0.5 && net(s->input)[i] > 0.5)
++partial;
if (partial == s->output.size())
++sum;
}
return sum;
}
phenotype eoChromEvaluator(const Chrom& chrom)
{
phenotype p;
p.trn_ok = correct(chrom, trn_set);
p.val_ok = correct(chrom, val_set);
p.tst_ok = correct(chrom, tst_set);
p.mse_error = mse::error(chrom, val_set);
return p;
};
//-----------------------------------------------------------------------------
#endif // gprop_h
// Local Variables:
// mode:C++
// End: