Main MRPT website > C++ reference for MRPT 1.5.7
CMonteCarlo.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef _MRPT_MONTE_CARLO_H_
10 #define _MRPT_MONTE_CARLO_H_
11 
12 #include <map>
13 #include <vector>
14 #include <numeric>
15 #include <algorithm>
16 #include <stdexcept>
17 #include <functional>
18 #include <mrpt/random.h>
19 #include <mrpt/utils/CTicTac.h>
20 
21 namespace mrpt { namespace math {
22 
23  /** Montecarlo simulation for experiments in 1D.
24  Template arguments are:
25  - T: base type, i.e., if an experiment needs to generate random points, then T may be a TPoint3D, and so on.
26  - NUM: the numeric type used to represent the error. Usually, double.
27  - OTHER: an intermediate type, used especially when testing inverse functions. Leave as int or double if you don't use it.
28 
29  HOW TO USE THIS CLASS:
30  - Create an instance of the class.
31  - Refill the "valueGenerator" member with an appropriate function.
32  - If your experiment calculates the error directly from the base value, then refill the "errorFun1" member.
33  - Otherwise, if your experiment involves the calculation of some value whom with the experimental function is compared, refill "intermediateFun" and "errorFun2".
34  - Refill only on of the alternatives.
35  * \ingroup mrpt_base_grp
36  */
37  template<typename T,typename NUM,typename OTHER> class CMonteCarlo {
38  private:
41  private:
42  Eigen::Matrix<NUM,Eigen::Dynamic,1> data;
43  public:
44  template<typename VEC> inline CStatisticalAnalyzer(const VEC &v1):data(v1.begin(),v1.end()) {}
45  template<typename VEC> inline void setData(const VEC &v1) {
46  data.assign(v1.begin(),v1.end());
47  }
48  template<typename VEC> inline void getData(VEC &v1) const {
49  v1.assign(data.begin(),data.end());
50  }
51  template<typename VEC1,typename VEC2> inline void getDistribution(VEC1 &vx,VEC2 &vy,const NUM width=1.0) const {
52  std::vector<double> vvx,vvy;
53  getDistribution(vvx,vvy,width);
54  vx.assign(vvx.begin(),vvx.end());
55  vy.assign(vvy.begin(),vvy.end());
56  }
57  // Function overload, not specialization (GCC complains otherwise):
58  inline void getDistribution(std::vector<double> &vx,std::vector<double> &vy,const NUM width=1.0) const {
59  CHistogram hist(CHistogram::createWithFixedWidth(0,*max_element(data.begin(),data.end()),width));
60  hist.add(data);
61  hist.getHistogram(vx,vy);
62  }
63 
64  };
65  public:
66  //TODO: use templates for function types.
67  //Random generator.
69  //Computes automatically the error (without an intermediate type)
70  NUM (*errorFun1)(const T &);
71 
72  OTHER (*intermediateFun)(const T &);
73  NUM (*errorFun2)(const T &,const OTHER &);
74  inline CMonteCarlo():gen(),valueGenerator(NULL),errorFun1(NULL),intermediateFun(NULL),errorFun2(NULL) {}
75  NUM doExperiment(size_t N,double &time,bool showInWindow=false) {
76  if (!valueGenerator) throw std::logic_error("Value generator function is not set.");
77  std::vector<T> baseData(N);
78  std::vector<NUM> errorData(N);
80  for (size_t i=0;i<N;++i) baseData[i]=valueGenerator(gen);
81  if (errorFun1) {
82  meter.Tic();
83  std::transform(baseData.begin(),baseData.end(),errorData.begin(),errorFun1);
84  time=meter.Tac();
85  } else {
86  if (!intermediateFun||!errorFun2) throw std::logic_error("Experiment-related functions are not set.");
87  std::vector<OTHER> intermediate(N);
88  transform(baseData.begin(),baseData.end(),intermediate.begin(),intermediateFun);
89  meter.Tic();
90  for (size_t i=0;i<N;++i) errorData[i]=errorFun2(baseData[i],intermediate[i]);
91  time=meter.Tac();
92  }
93  NUM res=accumulate(errorData.begin(),errorData.end(),NUM(0))/errorData.size();
94  //if (showInWindow) {
95  // CStatisticalAnalyzer st(errorData);
96  // mrpt::gui::CDisplayWindowPlots wnd("Error results from Monte Carlo simulation");
97  // std::vector<NUM> errorX,errorY;
98  // st.getDistribution(errorX,errorY,0.1);
99  // wnd.plot(errorX,errorY,"b-","Plot1");
100  // NUM maxVal=*std::max_element(errorY.begin(),errorY.end());
101  // std::vector<NUM> dx(2,res),dy(mrpt::utils::make_vector<2,NUM>(0,maxVal));
102  // wnd.plot(dx,dy,"r-","Plot2");
103  // while (wnd.isOpen());
104  //}
105  return res;
106  }
107  };
108 
109 }} //End of namespaces
110 #endif
This class provides an easy way of computing histograms for unidimensional real valued variables...
Definition: CHistogram.h:35
void getDistribution(VEC1 &vx, VEC2 &vy, const NUM width=1.0) const
Definition: CMonteCarlo.h:51
NUM doExperiment(size_t N, double &time, bool showInWindow=false)
Definition: CMonteCarlo.h:75
T(* valueGenerator)(mrpt::random::CRandomGenerator &)
Definition: CMonteCarlo.h:68
EIGEN_STRONG_INLINE iterator begin()
Definition: eigen_plugins.h:26
A thred-safe pseudo random number generator, based on an internal MT19937 randomness generator...
void Tic()
Starts the stopwatch.
Definition: CTicTac.cpp:77
GLenum GLsizei width
Definition: glext.h:3513
void add(const double x)
Add an element to the histogram.
Definition: CHistogram.cpp:42
This class implements a high-performance stopwatch.
Definition: CTicTac.h:24
GLuint GLuint end
Definition: glext.h:3512
OTHER(* intermediateFun)(const T &)
Definition: CMonteCarlo.h:72
NUM(* errorFun1)(const T &)
Definition: CMonteCarlo.h:70
void getDistribution(std::vector< double > &vx, std::vector< double > &vy, const NUM width=1.0) const
Definition: CMonteCarlo.h:58
mrpt::random::CRandomGenerator gen
Definition: CMonteCarlo.h:39
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Eigen::Matrix< NUM, Eigen::Dynamic, 1 > data
Definition: CMonteCarlo.h:42
GLfloat GLfloat v1
Definition: glext.h:3922
double Tac()
Stops the stopwatch.
Definition: CTicTac.cpp:92
GLuint res
Definition: glext.h:6298
Montecarlo simulation for experiments in 1D.
Definition: CMonteCarlo.h:37
GLuint GLenum GLenum transform
Definition: glext.h:6092
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3520
static CHistogram createWithFixedWidth(double min, double max, double binWidth)
Constructor with a fixed bin width.
Definition: CHistogram.h:52
NUM(* errorFun2)(const T &, const OTHER &)
Definition: CMonteCarlo.h:73



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019