00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef _eoFDCStat_h
00028 #define _eoFDCStat_h
00029
00030 #include <utils/eoStat.h>
00031 #include <utils/eoDistance.h>
00032 #include <utils/eoFileSnapshot.h>
00033
00038 template <class EOT>
00039 class eoFDCStat : public eoStat<EOT, double>
00040 {
00041 public:
00042
00043 using eoStat<EOT, double>::value;
00044
00046 eoFDCStat(eoDistance<EOT> & _dist, std::string _description = "FDC") :
00047 eoStat<EOT,double>(0, _description), dist(_dist), boolOpt(false) {}
00048
00051 eoFDCStat(eoDistance<EOT> & _dist, EOT & _theBest,
00052 std::string _description = "FDC") :
00053 eoStat<EOT,double>(0, _description), dist(_dist),
00054 theBest(_theBest), boolOpt(true) {}
00055
00059 virtual void operator()(const eoPop<EOT>& _pop)
00060 {
00061 unsigned i;
00062 if (!boolOpt)
00063 theBest = _pop.best_element();
00064 unsigned int pSize = _pop.size();
00065 distToBest.value().resize(pSize);
00066 fitnesses.value().resize(pSize);
00067 double sumFit = 0.0, sumDist = 0.0;
00068 for (i=0; i<pSize; i++)
00069 {
00070 sumDist += (distToBest.value()[i] = dist(_pop[i], theBest));
00071 sumFit += (fitnesses.value()[i] = _pop[i].fitness());
00072 }
00073
00074 double avgDist = sumDist/pSize;
00075 double avgFit = sumFit/pSize;
00076 sumDist = sumFit = 0.0;
00077 double num = 0.0;
00078 for (i=0; i<pSize; i++)
00079 {
00080 double devDist = distToBest.value()[i] - avgDist ;
00081 double devFit = fitnesses.value()[i] - avgFit ;
00082 sumDist += devDist*devDist;
00083 sumFit += devFit * devFit;
00084 num += devDist * devFit ;
00085 }
00086 value() = num/(sqrt(sumDist)*sqrt(sumFit));
00087 }
00088
00091 const eoValueParam<std::vector<double> > & theDist()
00092 { return distToBest; }
00093 const eoValueParam<std::vector<double> > & theFit()
00094 { return fitnesses; }
00095
00096
00097 private:
00098 eoDistance<EOT> & dist;
00099 EOT theBest;
00100 bool boolOpt;
00101 eoValueParam<std::vector<double> > distToBest;
00102 eoValueParam<std::vector<double> > fitnesses;
00103 };
00104
00110 template <class EOT>
00111 class eoFDCFileSnapshot : public eoFileSnapshot
00112 {
00113 public:
00119 eoFDCFileSnapshot(eoFDCStat<EOT> & _FDCstat,
00120 std::string _dirname = "tmpFDC", unsigned _frequency = 1,
00121 std::string _filename = "FDC", std::string _delim = " "):
00122 eoFileSnapshot(_dirname, _frequency, _filename, _delim),
00123 FDCstat(_FDCstat)
00124 {
00125 eoFileSnapshot::add(FDCstat.theDist());
00126 eoFileSnapshot::add(FDCstat.theFit());
00127 }
00128
00131 virtual void add(const eoParam& _param)
00132 { throw std::runtime_error("eoFDCFileSnapshot::add(). Trying to add stats to an eoFDCFileSnapshot"); }
00133
00134 private:
00135 eoFDCStat<EOT> & FDCstat;
00136 };
00137
00138 #endif
00139