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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST