edoEstimatorNormalMono.h
00001 /*
00002 The Evolving Distribution Objects framework (EDO) is a template-based,
00003 ANSI-C++ evolutionary computation library which helps you to write your
00004 own estimation of distribution algorithms.
00005 
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 This library is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public
00017 License along with this library; if not, write to the Free Software
00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020 Copyright (C) 2010 Thales group
00021 */
00022 /*
00023 Authors:
00024     Johann Dréo <johann.dreo@thalesgroup.com>
00025     Caner Candan <caner.candan@thalesgroup.com>
00026 */
00027 
00028 #ifndef _edoEstimatorNormalMono_h
00029 #define _edoEstimatorNormalMono_h
00030 
00031 #include "edoEstimator.h"
00032 #include "edoNormalMono.h"
00033 
00039 template < typename EOT >
00040 class edoEstimatorNormalMono : public edoEstimator< edoNormalMono< EOT > >
00041 {
00042 public:
00043     typedef typename EOT::AtomType AtomType;
00044 
00045     class Variance
00046     {
00047     public:
00048         Variance() : _sumvar(0){}
00049 
00050         void update(AtomType v)
00051         {
00052             _n++;
00053 
00054             AtomType d = v - _mean;
00055 
00056             _mean += 1 / _n * d;
00057             _sumvar += (_n - 1) / _n * d * d;
00058         }
00059 
00060         AtomType get_mean() const {return _mean;}
00061         AtomType get_var() const {return _sumvar / (_n - 1);}
00062         AtomType get_std() const {return sqrt( get_var() );}
00063 
00064     private:
00065         AtomType _n;
00066         AtomType _mean;
00067         AtomType _sumvar;
00068     };
00069 
00070 public:
00071     edoNormalMono< EOT > operator()(eoPop<EOT>& pop)
00072     {
00073         unsigned int popsize = pop.size();
00074         assert(popsize > 0);
00075 
00076         unsigned int dimsize = pop[0].size();
00077         assert(dimsize > 0);
00078 
00079         std::vector< Variance > var( dimsize );
00080 
00081         for (unsigned int i = 0; i < popsize; ++i)
00082             {
00083             for (unsigned int d = 0; d < dimsize; ++d)
00084                 {
00085                 var[d].update( pop[i][d] );
00086                 }
00087             }
00088 
00089         EOT mean( dimsize );
00090         EOT variance( dimsize );
00091 
00092         for (unsigned int d = 0; d < dimsize; ++d)
00093             {
00094             mean[d] = var[d].get_mean();
00095             variance[d] = var[d].get_var();
00096             }
00097 
00098         return edoNormalMono< EOT >( mean, variance );
00099     }
00100 };
00101 
00102 #endif // !_edoEstimatorNormalMono_h
 All Classes Functions Variables Typedefs