EvolvingObjects
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 
00042 template <class EOT>
00043 class eoFDCStat : public eoStat<EOT, double>
00044 {
00045 public:
00046 
00047     using eoStat<EOT, double>::value;
00048 
00050   eoFDCStat(eoDistance<EOT> & _dist, std::string _description = "FDC") :
00051       eoStat<EOT,double>(0, _description), dist(_dist), boolOpt(false) {}
00052 
00055   eoFDCStat(eoDistance<EOT> & _dist, EOT & _theBest,
00056             std::string _description = "FDC") :
00057     eoStat<EOT,double>(0, _description), dist(_dist),
00058     theBest(_theBest), boolOpt(true) {}
00059 
00063     virtual void operator()(const eoPop<EOT>& _pop)
00064     {
00065       unsigned i;
00066       if (!boolOpt)                // take the local best
00067         theBest = _pop.best_element();
00068       unsigned int pSize = _pop.size();
00069       distToBest.value().resize(pSize);
00070       fitnesses.value().resize(pSize);
00071       double sumFit = 0.0, sumDist = 0.0;
00072       for (i=0; i<pSize; i++)
00073         {
00074           sumDist += (distToBest.value()[i] = dist(_pop[i], theBest));
00075           sumFit += (fitnesses.value()[i] = _pop[i].fitness());
00076         }
00077       // now the FDC coefficient
00078       double avgDist = sumDist/pSize;
00079       double avgFit = sumFit/pSize;
00080       sumDist = sumFit = 0.0;
00081       double num = 0.0;
00082       for (i=0; i<pSize; i++)
00083         {
00084           double devDist = distToBest.value()[i] - avgDist ;
00085           double devFit = fitnesses.value()[i] - avgFit ;
00086           sumDist += devDist*devDist;
00087           sumFit += devFit * devFit;
00088           num += devDist * devFit ;
00089         }
00090       value() = num/(sqrt(sumDist)*sqrt(sumFit));
00091     }
00092 
00095   const eoValueParam<std::vector<double> > & theDist()
00096   { return distToBest; }
00097   const eoValueParam<std::vector<double> > & theFit()
00098   { return fitnesses; }
00099 
00100 
00101 private:
00102   eoDistance<EOT> & dist;
00103   EOT theBest;
00104   bool boolOpt;                    // whether the best is known or not
00105   eoValueParam<std::vector<double> > distToBest;
00106   eoValueParam<std::vector<double> > fitnesses;
00107 };
00108 
00116 template <class EOT>
00117 class eoFDCFileSnapshot : public eoFileSnapshot // is an eoMonitor
00118 {
00119 public:
00125   eoFDCFileSnapshot(eoFDCStat<EOT> & _FDCstat,
00126                     std::string _dirname = "tmpFDC", unsigned _frequency = 1,
00127                     std::string _filename = "FDC", std::string _delim = " "):
00128     eoFileSnapshot(_dirname, _frequency, _filename, _delim),
00129     FDCstat(_FDCstat)
00130   {
00131     eoFileSnapshot::add(FDCstat.theDist());
00132     eoFileSnapshot::add(FDCstat.theFit());
00133   }
00134 
00137   virtual void add(const eoParam& _param)
00138     { throw std::runtime_error("eoFDCFileSnapshot::add(). Trying to add stats to an eoFDCFileSnapshot"); }
00139 
00140 private:
00141   eoFDCStat<EOT> & FDCstat;
00142 };
00143 
00144 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends