# eoFDCStat.h

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // eoFDCStat.h
    00005 // (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000, 2001
    00006 /*
    00007     This library is free software; you can redistribute it and/or
    00008     modify it under the terms of the GNU Lesser General Public
    00009     License as published by the Free Software Foundation; either
    00010     version 2 of the License, or (at your option) any later version.
    00011
    00012     This library is distributed in the hope that it will be useful,
    00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00015     Lesser General Public License for more details.
    00016
    00017     You should have received a copy of the GNU Lesser General Public
    00018     License along with this library; if not, write to the Free Software
    00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00020
    00021     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
    00022              Marc.Schoenauer@polytechnique.fr
    00023              mkeijzer@dhi.dk
    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)                // take the local best
    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       // now the FDC coefficient
    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;                    // whether the best is known or not
    00101   eoValueParam<std::vector<double> > distToBest;
    00102   eoValueParam<std::vector<double> > fitnesses;
    00103 };
    00104
    00110 template <class EOT>
    00111 class eoFDCFileSnapshot : public eoFileSnapshot // is an eoMonitor
    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