eoAssembledFitnessStat.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoAssembledFitnessStat.h
00005 // Marc Wintermantel & Oliver Koenig
00006 // IMES-ST@ETHZ.CH
00007 // April 2003
00008 
00009 //-----------------------------------------------------------------------------
00010 // eoStat.h
00011 // (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000
00012 /*
00013     This library is free software; you can redistribute it and/or
00014     modify it under the terms of the GNU Lesser General Public
00015     License as published by the Free Software Foundation; either
00016     version 2 of the License, or (at your option) any later version.
00017 
00018     This library is distributed in the hope that it will be useful,
00019     but WITHOUT ANY WARRANTY; without even the implied warranty of
00020     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021     Lesser General Public License for more details.
00022 
00023     You should have received a copy of the GNU Lesser General Public
00024     License along with this library; if not, write to the Free Software
00025     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026 
00027     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
00028              Marc.Schoenauer@polytechnique.fr
00029              mkeijzer@dhi.dk
00030  */
00031 //-----------------------------------------------------------------------------
00032 
00033 #ifndef _eoAssembledFitnessStat_h
00034 #define _eoAssembledFitnessStat_h
00035 
00036 #include <utils/eoStat.h>
00037 #include <eoScalarFitnessAssembled.h>
00038 
00045 template <class EOT>
00046 class eoAssembledFitnessAverageStat : public eoStat<EOT, double>
00047 {
00048 public :
00049 
00050     using eoStat<EOT, double>::value;
00051 
00052     typedef typename EOT::Fitness Fitness;
00053 
00054 
00055     eoAssembledFitnessAverageStat(unsigned _whichTerm=0, std::string _description = "Average Fitness")
00056         : eoStat<EOT, double>(Fitness(), _description), whichFitnessTerm(_whichTerm)
00057         {}
00058 
00059 
00060     virtual void operator()(const eoPop<EOT>& _pop) {
00061         if( whichFitnessTerm >= _pop[0].fitness().size() )
00062             throw std::logic_error("Fitness term requested out of range");
00063 
00064     double result =0.0;
00065     unsigned count = 0;
00066     for (typename eoPop<EOT>::const_iterator it = _pop.begin(); it != _pop.end(); ++it){
00067       if ( it->fitness().failed == false ){
00068         result+= it->fitness()[whichFitnessTerm];
00069         ++count;
00070       }
00071     }
00072 
00073     value() = result / (double) count;
00074   }
00075 
00076 private:
00077   // Store an index of the fitness term to be evaluated in eoScalarFitnessAssembled
00078   unsigned whichFitnessTerm;
00079 };
00080 
00086 template <class EOT>
00087 class eoAssembledFitnessBestStat : public eoStat<EOT, double>
00088 {
00089 public:
00090 
00091     using eoStat<EOT, double>::value;
00092 
00093     typedef typename EOT::Fitness Fitness;
00094 
00095     eoAssembledFitnessBestStat(unsigned _whichTerm=0, std::string _description = "Best Fitness")
00096         : eoStat<EOT, double>(Fitness(), _description), whichFitnessTerm(_whichTerm)
00097         {}
00098 
00099     virtual void operator()(const eoPop<EOT>& _pop) {
00100         if( whichFitnessTerm >= _pop[0].fitness().size() )
00101             throw std::logic_error("Fitness term requested out of range");
00102 
00103         value() = _pop.best_element().fitness()[whichFitnessTerm];
00104     }
00105 
00106 private:
00107 
00108     // Store an index of the fitness term to be evaluated in eoScalarFitnessAssembled
00109     unsigned whichFitnessTerm;
00110 };
00111 
00112 #endif

Generated on Thu Apr 19 11:02:26 2007 for EO by  doxygen 1.4.7