Main MRPT website > C++ reference for MRPT 1.9.9
test.cpp
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 
10 #include <mrpt/system/filesystem.h>
12 #include <mrpt/random.h>
13 #include <mrpt/math/utils.h>
14 #include <mrpt/math/ops_vectors.h>
15 #include <mrpt/math/data_utils.h>
17 #include <map>
18 #include <iostream>
19 
20 using namespace mrpt::bayes;
21 using namespace mrpt::math;
22 using namespace mrpt::random;
23 using namespace mrpt::system;
24 using namespace std;
25 
26 double MIN_LOG_WEIG = -1.0;
27 
28 unsigned int N_TESTS = 500;
29 int N_PARTICLES = 100;
30 
31 // For batch experiment:
33 map<string, CVectorDouble> results;
34 
35 // vectorToTextFile( out_indxs, #ALGOR, true, true); /* By rows, append */
36 
37 #define TEST_RESAMPLING(ALGOR) \
38  mrpt::system::deleteFile(#ALGOR); \
39  /*printf(#ALGOR);*/ \
40  /*printf("\n");*/ \
41  ERR_MEANs.clear(); \
42  ERR_STDs.clear(); \
43  for (size_t i = 0; i < N_TESTS; i++) \
44  { \
45  mrpt::random::getRandomGenerator().drawUniformVector( \
46  log_ws, MIN_LOG_WEIG, 0.0); \
47  CParticleFilterCapable::log2linearWeights(log_ws, lin_ws); \
48  CParticleFilterCapable::computeResampling( \
49  CParticleFilter::ALGOR, log_ws, out_indxs); \
50  hist_parts = mrpt::math::histogram(out_indxs, 0, M - 1, M, true); \
51  vector<double> errs_hist = lin_ws - hist_parts; \
52  ERR_MEANs.push_back(mrpt::math::mean(errs_hist)); \
53  ERR_STDs.push_back(mrpt::math::stddev(errs_hist)); \
54  } \
55  printf("%s: ERR_MEAN %e\n", #ALGOR, mrpt::math::mean(ERR_MEANs)); \
56  printf("%s: ERR_STD %f\n", #ALGOR, mrpt::math::mean(ERR_STDs)); \
57  results[#ALGOR].push_back(mrpt::math::mean(ERR_STDs));
58 
59 // ------------------------------------------------------
60 // TestResampling
61 // ------------------------------------------------------
62 void TestResampling()
63 {
64  vector<double> log_ws;
65  std::vector<size_t> out_indxs;
66 
67  const size_t M = N_PARTICLES;
68 
69  log_ws.resize(M);
70  // vectorToTextFile( log_ws, "log_ws.txt");
71 
72  // Compute normalized linear weights:
73  vector<double> lin_ws;
74  vector<double> hist_parts;
75  vector<double> ERR_MEANs;
76  vector<double> ERR_STDs;
77 
78  // prMultinomial
79  TEST_RESAMPLING(prMultinomial)
80  // prResidual
81  TEST_RESAMPLING(prResidual)
82  // prStratified
83  TEST_RESAMPLING(prStratified)
84  // prSystematic
85  TEST_RESAMPLING(prSystematic)
86 }
87 
88 void TestBatch()
89 {
90  for (double LL = -2; LL <= 2.01; LL += 0.08)
91  {
92  double L = pow(10.0, LL);
93 
94  min_log_ws.push_back(L);
95  printf("MIN_LOG_W=%f\n", L);
96 
97  MIN_LOG_WEIG = L;
99  }
100 
101  // Save results to files:
103 
104  vectorToTextFile(min_log_ws, "min_log_ws.txt");
105 
106  R = results["prMultinomial"];
107  vectorToTextFile(R, "prMultinomial.txt");
108  R = results["prResidual"];
109  vectorToTextFile(R, "prResidual.txt");
110  R = results["prStratified"];
111  vectorToTextFile(R, "prStratified.txt");
112  R = results["prSystematic"];
113  vectorToTextFile(R, "prSystematic.txt");
114 }
115 
116 // ------------------------------------------------------
117 // MAIN
118 // ------------------------------------------------------
119 int main(int argc, char** argv)
120 {
121  try
122  {
124 
125  if (argc > 1) N_PARTICLES = atoi(argv[1]);
126 
127  // TestResampling();
128  TestBatch();
129 
130  return 0;
131  }
132  catch (exception& e)
133  {
134  cerr << "EXCEPCTION: " << e.what() << endl;
135  return -1;
136  }
137  catch (...)
138  {
139  cerr << "Untyped excepcion!!";
140  return -1;
141  }
142 }
filesystem.h
vector_loadsave.h
mrpt::math::dynamic_vector
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction.
Definition: eigen_frwds.h:44
min_log_ws
CVectorDouble min_log_ws
Definition: vision_stereo_rectify/test.cpp:32
N_TESTS
unsigned int N_TESTS
Definition: vision_stereo_rectify/test.cpp:28
data_utils.h
MIN_LOG_WEIG
double MIN_LOG_WEIG
Definition: vision_stereo_rectify/test.cpp:26
R
const float R
Definition: CKinematicChain.cpp:138
results
map< string, CVectorDouble > results
Definition: vision_stereo_rectify/test.cpp:33
N_PARTICLES
int N_PARTICLES
Definition: vision_stereo_rectify/test.cpp:29
random.h
mrpt::random::CRandomGenerator::randomize
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
Definition: RandomGenerator.cpp:32
TestResampling
void TestResampling()
Definition: vision_stereo_rectify/test.cpp:62
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
ops_vectors.h
TestBatch
void TestBatch()
Definition: vision_stereo_rectify/test.cpp:88
CParticleFilterCapable.h
utils.h
mrpt::random::getRandomGenerator
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
Definition: RandomGenerator.cpp:19
mrpt::bayes
The namespace for Bayesian filtering algorithm: different particle filters and Kalman filter algorith...
Definition: CKalmanFilterCapable.h:32
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
mrpt::random
A namespace of pseudo-random numbers generators of diferent distributions.
Definition: random_shuffle.h:17
mrpt::system::vectorToTextFile
bool vectorToTextFile(const std::vector< float > &vec, const std::string &fileName, bool append=false, bool byRows=false)
A useful function for debugging, which saves a std::vector into a text file (compat.
Definition: system/src/vector_loadsave.cpp:19
TEST_RESAMPLING
#define TEST_RESAMPLING(ALGOR)
Definition: vision_stereo_rectify/test.cpp:37
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25



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