Main MRPT website > C++ reference
MRPT logo

CMonteCarlo.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef _MRPT_MONTE_CARLO_H_
00029 #define _MRPT_MONTE_CARLO_H_
00030 
00031 #include <map>
00032 #include <cmath>
00033 #include <vector>
00034 #include <numeric>
00035 #include <algorithm>
00036 #include <stdexcept>
00037 #include <functional>
00038 //#include <mrpt/gui/CDisplayWindowPlots.h>
00039 #include <mrpt/random.h>
00040 #include <mrpt/utils/CTicTac.h>
00041 
00042 namespace mrpt  {       namespace math  {
00043 
00044         /** Montecarlo simulation for experiments in 1D.
00045              Template arguments are:
00046                         - T: base type, i.e., if an experiment needs to generate random points, then T may be a TPoint3D, and so on.
00047                         - NUM: the numeric type used to represent the error. Usually, double.
00048                         - OTHER: an intermediate type, used especially when testing inverse functions. Leave as int or double if you don't use it.
00049 
00050                 HOW TO USE THIS CLASS:
00051                         - Create an instance of the class.
00052                         - Refill the "valueGenerator" member with an appropriate function.
00053                         - If your experiment calculates the error directly from the base value, then refill the "errorFun1" member.
00054                         - Otherwise, if your experiment involves the calculation of some value whom with the experimental function is compared, refill "intermediateFun" and "errorFun2".
00055                         - Refill only on of the alternatives.
00056         */
00057         template<typename T,typename NUM,typename OTHER> class CMonteCarlo      {
00058         private:
00059                 mrpt::random::CRandomGenerator gen;
00060                 class CStatisticalAnalyzer      {
00061                 private:
00062                         Eigen::Matrix<NUM,Eigen::Dynamic,1> data;
00063                 public:
00064                         template<typename VEC> inline CStatisticalAnalyzer(const VEC &v1):data(v1.begin(),v1.end())     {}
00065                         template<typename VEC> inline void setData(const VEC &v1)       {
00066                                 data.assign(v1.begin(),v1.end());
00067                         }
00068                         template<typename VEC> inline void getData(VEC &v1) const       {
00069                                 v1.assign(data.begin(),data.end());
00070                         }
00071                         template<typename VEC1,typename VEC2> inline void getDistribution(VEC1 &vx,VEC2 &vy,const NUM width=1.0) const  {
00072                                 mrpt::vector_double vvx,vvy;
00073                                 getDistribution(vvx,vvy,width);
00074                                 vx.assign(vvx.begin(),vvx.end());
00075                                 vy.assign(vvy.begin(),vvy.end());
00076                         }
00077                         // Function overload, not specialization (GCC complains otherwise):
00078                         inline void getDistribution(mrpt::vector_double &vx,mrpt::vector_double &vy,const NUM width=1.0) const  {
00079                                 CHistogram hist(CHistogram::createWithFixedWidth(0,*max_element(data.begin(),data.end()),width));
00080                                 hist.add(data);
00081                                 hist.getHistogram(vx,vy);
00082                         }
00083 
00084                 };
00085         public:
00086                 //TODO: use templates for function types.
00087                 //Random generator.
00088                 T (*valueGenerator)(mrpt::random::CRandomGenerator &);
00089                 //Computes automatically the error (without an intermediate type)
00090                 NUM (*errorFun1)(const T &);
00091 
00092                 OTHER (*intermediateFun)(const T &);
00093                 NUM (*errorFun2)(const T &,const OTHER &);
00094                 inline CMonteCarlo():gen(),valueGenerator(NULL),errorFun1(NULL),intermediateFun(NULL),errorFun2(NULL)   {}
00095                 NUM doExperiment(size_t N,double &time,bool showInWindow=false) {
00096                         if (!valueGenerator) throw std::logic_error("Value generator function is not set.");
00097                         std::vector<T> baseData(N);
00098                         std::vector<NUM> errorData(N);
00099                         mrpt::utils::CTicTac meter;
00100                         for (size_t i=0;i<N;++i) baseData[i]=valueGenerator(gen);
00101                         if (errorFun1)  {
00102                                 meter.Tic();
00103                                 std::transform(baseData.begin(),baseData.end(),errorData.begin(),errorFun1);
00104                                 time=meter.Tac();
00105                         }       else    {
00106                                 if (!intermediateFun||!errorFun2) throw std::logic_error("Experiment-related functions are not set.");
00107                                 std::vector<OTHER> intermediate(N);
00108                                 transform(baseData.begin(),baseData.end(),intermediate.begin(),intermediateFun);
00109                                 meter.Tic();
00110                                 for (size_t i=0;i<N;++i) errorData[i]=errorFun2(baseData[i],intermediate[i]);
00111                                 time=meter.Tac();
00112                         }
00113                         NUM res=accumulate(errorData.begin(),errorData.end(),NUM(0))/errorData.size();
00114                         //if (showInWindow)     {
00115                         //      CStatisticalAnalyzer st(errorData);
00116                         //      mrpt::gui::CDisplayWindowPlots wnd("Error results from Monte Carlo simulation");
00117                         //      std::vector<NUM> errorX,errorY;
00118                         //      st.getDistribution(errorX,errorY,0.1);
00119                         //      wnd.plot(errorX,errorY,"b-","Plot1");
00120                         //      NUM maxVal=*std::max_element(errorY.begin(),errorY.end());
00121                         //      std::vector<NUM> dx(2,res),dy(mrpt::utils::make_vector<2,NUM>(0,maxVal));
00122                         //      wnd.plot(dx,dy,"r-","Plot2");
00123                         //      while (wnd.isOpen());
00124                         //}
00125                         return res;
00126                 }
00127         };
00128 
00129 }}      //End of namespaces
00130 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011