EvolvingObjects
|
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 00049 template <class EOT> 00050 class eoAssembledFitnessAverageStat : public eoStat<EOT, double> 00051 { 00052 public : 00053 00054 using eoStat<EOT, double>::value; 00055 00056 typedef typename EOT::Fitness Fitness; 00057 00058 00059 eoAssembledFitnessAverageStat(unsigned _whichTerm=0, std::string _description = "Average Fitness") 00060 : eoStat<EOT, double>(Fitness(), _description), whichFitnessTerm(_whichTerm) 00061 {} 00062 00063 00064 virtual void operator()(const eoPop<EOT>& _pop) { 00065 if( whichFitnessTerm >= _pop[0].fitness().size() ) 00066 throw std::logic_error("Fitness term requested out of range"); 00067 00068 double result =0.0; 00069 unsigned count = 0; 00070 for (typename eoPop<EOT>::const_iterator it = _pop.begin(); it != _pop.end(); ++it){ 00071 if ( it->fitness().failed == false ){ 00072 result+= it->fitness()[whichFitnessTerm]; 00073 ++count; 00074 } 00075 } 00076 00077 value() = result / (double) count; 00078 } 00079 00080 private: 00081 // Store an index of the fitness term to be evaluated in eoScalarFitnessAssembled 00082 unsigned whichFitnessTerm; 00083 }; 00084 00090 template <class EOT> 00091 class eoAssembledFitnessBestStat : public eoStat<EOT, double> 00092 { 00093 public: 00094 00095 using eoStat<EOT, double>::value; 00096 00097 typedef typename EOT::Fitness Fitness; 00098 00099 eoAssembledFitnessBestStat(unsigned _whichTerm=0, std::string _description = "Best Fitness") 00100 : eoStat<EOT, double>(Fitness(), _description), whichFitnessTerm(_whichTerm) 00101 {} 00102 00103 virtual void operator()(const eoPop<EOT>& _pop) { 00104 if( whichFitnessTerm >= _pop[0].fitness().size() ) 00105 throw std::logic_error("Fitness term requested out of range"); 00106 00107 value() = _pop.best_element().fitness()[whichFitnessTerm]; 00108 } 00109 00110 private: 00111 00112 // Store an index of the fitness term to be evaluated in eoScalarFitnessAssembled 00113 unsigned whichFitnessTerm; 00114 }; 00115 00117 #endif