MRPT  1.9.9
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-2018, 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/system/CTicTac.h>
20 
21 namespace mrpt::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,
26  then T may be a TPoint3D, and so on.
27  - NUM: the numeric type used to represent the error. Usually, double.
28  - OTHER: an intermediate type, used especially when testing inverse
29  functions. Leave as int or double if you don't use it.
30 
31  HOW TO USE THIS CLASS:
32  - Create an instance of the class.
33  - Refill the "valueGenerator" member with an appropriate function.
34  - If your experiment calculates the error directly from the base value,
35  then refill the "errorFun1" member.
36  - Otherwise, if your experiment involves the calculation of some value
37  whom with the experimental function is compared, refill "intermediateFun" and
38  "errorFun2".
39  - Refill only on of the alternatives.
40  * \ingroup mrpt_math_grp
41 */
42 template <typename T, typename NUM, typename OTHER>
44 {
45  private:
48  {
49  private:
50  Eigen::Matrix<NUM, Eigen::Dynamic, 1> data;
51 
52  public:
53  template <typename VEC>
54  inline CStatisticalAnalyzer(const VEC& v1) : data(v1.begin(), v1.end())
55  {
56  }
57  template <typename VEC>
58  inline void setData(const VEC& v1)
59  {
60  data.assign(v1.begin(), v1.end());
61  }
62  template <typename VEC>
63  inline void getData(VEC& v1) const
64  {
65  v1.assign(data.begin(), data.end());
66  }
67  template <typename VEC1, typename VEC2>
68  inline void getDistribution(
69  VEC1& vx, VEC2& vy, const NUM width = 1.0) const
70  {
71  std::vector<double> vvx, vvy;
72  getDistribution(vvx, vvy, width);
73  vx.assign(vvx.begin(), vvx.end());
74  vy.assign(vvy.begin(), vvy.end());
75  }
76  // Function overload, not specialization (GCC complains otherwise):
77  inline void getDistribution(
78  std::vector<double>& vx, std::vector<double>& vy,
79  const NUM width = 1.0) const
80  {
81  CHistogram hist(
83  0, *max_element(data.begin(), data.end()), width));
84  hist.add(data);
85  hist.getHistogram(vx, vy);
86  }
87  };
88 
89  public:
90  // TODO: use templates for function types.
91  // Random generator.
93  // Computes automatically the error (without an intermediate type)
94  NUM (*errorFun1)(const T&);
95 
96  OTHER (*intermediateFun)(const T&);
97  NUM (*errorFun2)(const T&, const OTHER&);
98  inline CMonteCarlo()
99  : gen(),
100  valueGenerator(nullptr),
101  errorFun1(nullptr),
102  intermediateFun(nullptr),
103  errorFun2(nullptr)
104  {
105  }
106  NUM doExperiment(size_t N, double& time, bool showInWindow = false)
107  {
108  if (!valueGenerator)
109  throw std::logic_error("Value generator function is not set.");
110  std::vector<T> baseData(N);
111  std::vector<NUM> errorData(N);
112  mrpt::system::CTicTac meter;
113  for (size_t i = 0; i < N; ++i) baseData[i] = valueGenerator(gen);
114  if (errorFun1)
115  {
116  meter.Tic();
118  baseData.begin(), baseData.end(), errorData.begin(), errorFun1);
119  time = meter.Tac();
120  }
121  else
122  {
123  if (!intermediateFun || !errorFun2)
124  throw std::logic_error(
125  "Experiment-related functions are not set.");
126  std::vector<OTHER> intermediate(N);
127  transform(
128  baseData.begin(), baseData.end(), intermediate.begin(),
130  meter.Tic();
131  for (size_t i = 0; i < N; ++i)
132  errorData[i] = errorFun2(baseData[i], intermediate[i]);
133  time = meter.Tac();
134  }
135  NUM res = accumulate(errorData.begin(), errorData.end(), NUM(0)) /
136  errorData.size();
137 #if 0
138  if (showInWindow)
139  {
140  CStatisticalAnalyzer st(errorData);
141  mrpt::gui::CDisplayWindowPlots wnd("Error results from Monte Carlo simulation");
142  std::vector<NUM> errorX,errorY;
143  st.getDistribution(errorX,errorY,0.1);
144  wnd.plot(errorX,errorY,"b-","Plot1");
145  NUM maxVal=*std::max_element(errorY.begin(),errorY.end());
146  const std::vector<NUM> dx{res, res}, dy{.0, maxVal};
147  wnd.plot(dx,dy,"r-","Plot2");
148  while (wnd.isOpen()) {};
149  }
150 #endif
151  return res;
152  }
153 };
154 }
155 #endif
156 
157 
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
This class provides an easy way of computing histograms for unidimensional real valued variables...
Definition: CHistogram.h:33
void getDistribution(VEC1 &vx, VEC2 &vy, const NUM width=1.0) const
Definition: CMonteCarlo.h:68
Create a GUI window and display plots with MATLAB-like interfaces and commands.
NUM doExperiment(size_t N, double &time, bool showInWindow=false)
Definition: CMonteCarlo.h:106
T(* valueGenerator)(mrpt::random::CRandomGenerator &)
Definition: CMonteCarlo.h:92
A high-performance stopwatch, with typical resolution of nanoseconds.
EIGEN_STRONG_INLINE iterator begin()
Definition: eigen_plugins.h:29
A thred-safe pseudo random number generator, based on an internal MT19937 randomness generator...
GLenum GLsizei width
Definition: glext.h:3531
This base provides a set of functions for maths stuff.
void add(const double x)
Add an element to the histogram.
Definition: CHistogram.cpp:42
Eigen::Matrix< NUM, Eigen::Dynamic, 1 > data
Definition: CMonteCarlo.h:50
GLuint GLuint end
Definition: glext.h:3528
bool isOpen()
Returns false if the user has already closed the window.
OTHER(* intermediateFun)(const T &)
Definition: CMonteCarlo.h:96
void plot(const std::vector< T1 > &x, const std::vector< T2 > &y, const std::string &lineFormat=std::string("b-"), const std::string &plotName=std::string("plotXY"))
Adds a new layer with a 2D plot based on two vectors of X and Y points, using a MATLAB-like syntax...
NUM(* errorFun1)(const T &)
Definition: CMonteCarlo.h:94
void getDistribution(std::vector< double > &vx, std::vector< double > &vy, const NUM width=1.0) const
Definition: CMonteCarlo.h:77
mrpt::random::CRandomGenerator gen
Definition: CMonteCarlo.h:46
GLfloat GLfloat v1
Definition: glext.h:4105
GLuint res
Definition: glext.h:7268
Montecarlo simulation for experiments in 1D.
Definition: CMonteCarlo.h:43
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
GLuint GLenum GLenum transform
Definition: glext.h:6975
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3546
CHistogram createWithFixedWidth(double min, double max, double binWidth)
Constructor with a fixed bin width.
Definition: CHistogram.cpp:97
NUM(* errorFun2)(const T &, const OTHER &)
Definition: CMonteCarlo.h:97



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020