MRPT  2.0.4
CMonteCarlo.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/random.h>
12 #include <mrpt/system/CTicTac.h>
13 #include <algorithm>
14 #include <functional>
15 #include <map>
16 #include <numeric>
17 #include <stdexcept>
18 #include <vector>
19 
20 namespace mrpt::math
21 {
22 /** Montecarlo simulation for experiments in 1D.
23  Template arguments are:
24  - T: base type, i.e., if an experiment needs to generate random points,
25  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
28  functions. Leave as int or double if you don't use it.
29 
30  HOW TO USE THIS CLASS:
31  - Create an instance of the class.
32  - Refill the "valueGenerator" member with an appropriate function.
33  - If your experiment calculates the error directly from the base value,
34  then refill the "errorFun1" member.
35  - Otherwise, if your experiment involves the calculation of some value
36  whom with the experimental function is compared, refill "intermediateFun" and
37  "errorFun2".
38  - Refill only on of the alternatives.
39  * \ingroup mrpt_math_grp
40 */
41 template <typename T, typename NUM, typename OTHER>
43 {
44  private:
47  {
48  private:
50 
51  public:
52  template <typename VEC>
53  inline CStatisticalAnalyzer(const VEC& v1) : data(v1.begin(), v1.end())
54  {
55  }
56  template <typename VEC>
57  inline void setData(const VEC& v1)
58  {
59  data.assign(v1.begin(), v1.end());
60  }
61  template <typename VEC>
62  inline void getData(VEC& v1) const
63  {
64  v1.assign(data.begin(), data.end());
65  }
66  template <typename VEC1, typename VEC2>
67  inline void getDistribution(
68  VEC1& vx, VEC2& vy, const NUM width = 1.0) const
69  {
70  std::vector<double> vvx, vvy;
71  getDistribution(vvx, vvy, width);
72  vx.assign(vvx.begin(), vvx.end());
73  vy.assign(vvy.begin(), vvy.end());
74  }
75  // Function overload, not specialization (GCC complains otherwise):
76  inline void getDistribution(
77  std::vector<double>& vx, std::vector<double>& vy,
78  const NUM width = 1.0) const
79  {
81  0, *max_element(data.begin(), data.end()), width));
82  hist.add(data);
83  hist.getHistogram(vx, vy);
84  }
85  };
86 
87  public:
88  // TODO: use templates for function types.
89  // Random generator.
91  // Computes automatically the error (without an intermediate type)
92  NUM (*errorFun1)(const T&);
93 
94  OTHER (*intermediateFun)(const T&);
95  NUM (*errorFun2)(const T&, const OTHER&);
96  inline CMonteCarlo()
97  : gen(),
98  valueGenerator(nullptr),
99  errorFun1(nullptr),
100  intermediateFun(nullptr),
101  errorFun2(nullptr)
102  {
103  }
104  NUM doExperiment(size_t N, double& time, bool showInWindow = false)
105  {
106  if (!valueGenerator)
107  throw std::logic_error("Value generator function is not set.");
108  std::vector<T> baseData(N);
109  std::vector<NUM> errorData(N);
110  mrpt::system::CTicTac meter;
111  for (size_t i = 0; i < N; ++i) baseData[i] = valueGenerator(gen);
112  if (errorFun1)
113  {
114  meter.Tic();
115  std::transform(
116  baseData.begin(), baseData.end(), errorData.begin(), errorFun1);
117  time = meter.Tac();
118  }
119  else
120  {
121  if (!intermediateFun || !errorFun2)
122  throw std::logic_error(
123  "Experiment-related functions are not set.");
124  std::vector<OTHER> intermediate(N);
125  transform(
126  baseData.begin(), baseData.end(), intermediate.begin(),
128  meter.Tic();
129  for (size_t i = 0; i < N; ++i)
130  errorData[i] = errorFun2(baseData[i], intermediate[i]);
131  time = meter.Tac();
132  }
133  NUM res = accumulate(errorData.begin(), errorData.end(), NUM(0)) /
134  errorData.size();
135 #if 0
136  if (showInWindow)
137  {
138  CStatisticalAnalyzer st(errorData);
139  mrpt::gui::CDisplayWindowPlots wnd("Error results from Monte Carlo simulation");
140  std::vector<NUM> errorX,errorY;
141  st.getDistribution(errorX,errorY,0.1);
142  wnd.plot(errorX,errorY,"b-","Plot1");
143  NUM maxVal=*std::max_element(errorY.begin(),errorY.end());
144  const std::vector<NUM> dx{res, res}, dy{.0, maxVal};
145  wnd.plot(dx,dy,"r-","Plot2");
146  while (wnd.isOpen()) {};
147  }
148 #endif
149  return res;
150  }
151 };
152 } // namespace mrpt::math
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:87
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:67
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:104
T(* valueGenerator)(mrpt::random::CRandomGenerator &)
Definition: CMonteCarlo.h:90
A high-performance stopwatch, with typical resolution of nanoseconds.
A thred-safe pseudo random number generator, based on an internal MT19937 randomness generator...
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:49
bool isOpen()
Returns false if the user has already closed the window.
OTHER(* intermediateFun)(const T &)
Definition: CMonteCarlo.h:94
NUM(* errorFun1)(const T &)
Definition: CMonteCarlo.h:92
void getDistribution(std::vector< double > &vx, std::vector< double > &vy, const NUM width=1.0) const
Definition: CMonteCarlo.h:76
mrpt::random::CRandomGenerator gen
Definition: CMonteCarlo.h:45
const_iterator end() const
Definition: ts_hash_map.h:246
const_iterator begin() const
Definition: ts_hash_map.h:240
Montecarlo simulation for experiments in 1D.
Definition: CMonteCarlo.h:42
void plot(const VEC1 &x, const VEC2 &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...
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:76
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:95



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 7b5ddf9de Fri May 29 14:02:56 2020 +0200 at vie may 29 14:15:09 CEST 2020