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 
11 #include <mrpt/system/CTicTac.h>
12 #include <iostream>
13 
14 using namespace mrpt;
15 using namespace mrpt::math;
16 using namespace mrpt::system;
17 using namespace std;
18 
19 // The error function F(x):
20 void myFunction(
21  const CVectorDouble& x, const CVectorDouble& y, CVectorDouble& out_f)
22 {
23  out_f.resize(1);
24 
25  // 1-cos(x+1) *cos(x*y+1)
26  out_f[0] = 1 - cos(x[0] + 1) * cos(x[0] * x[1] + 1);
27 }
28 
29 // ------------------------------------------------------
30 // TestLM
31 // ------------------------------------------------------
32 void TestLM()
33 {
34  CVectorDouble optimal_x;
35  CVectorDouble initial_x;
36  CVectorDouble increments_x;
38 
40  CTicTac tictac;
41 
42  initial_x.resize(2);
43  initial_x[0] = 1.4; // x
44  initial_x[1] = 2.5; // y
45 
46  increments_x.resize(2);
47  increments_x.setConstant(0.0001);
48 
49  double T;
50  size_t N = 1;
51 
53  tictac.Tic();
54  for (size_t k = 0; k < N; k++)
55  lm.execute(optimal_x, initial_x, myFunction, increments_x, y, info);
56 
57  T = tictac.Tac() / N;
58 
59  cout << "Iterations: " << info.iterations_executed << endl;
60  cout << "Final sqr error: " << info.final_sqr_err << endl;
61 
62  cout << endl << "Final optimized position: " << optimal_x << endl;
63 
64  cout << "Time: " << T * 1e6 << " us" << endl;
65 
66  info.path.saveToTextFile("lm-path.txt");
67  cout << "Path saved to 'lm-path.txt'" << endl;
68 }
69 
70 // ------------------------------------------------------
71 // MAIN
72 // ------------------------------------------------------
73 int main()
74 {
75  try
76  {
77  TestLM();
78 
79  return 0;
80  }
81  catch (std::exception& e)
82  {
83  std::cout << "MRPT exception caught: " << e.what() << std::endl;
84  return -1;
85  }
86  catch (...)
87  {
88  printf("Untyped exception!!");
89  return -1;
90  }
91 }
mrpt::math::CLevenbergMarquardtTempl::execute
void execute(VECTORTYPE &out_optimal_x, const VECTORTYPE &x0, TFunctorEval functor, const VECTORTYPE &increments, const USERPARAM &userParam, TResultInfo &out_info, mrpt::system::VerbosityLevel verbosity=mrpt::system::LVL_INFO, const size_t maxIter=200, const NUMTYPE tau=1e-3, const NUMTYPE e1=1e-8, const NUMTYPE e2=1e-8, bool returnPath=true, TFunctorIncrement x_increment_adder=nullptr)
Executes the LM-method, with derivatives estimated from functor is a user-provided function which tak...
Definition: CLevenbergMarquardt.h:102
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
mrpt::math::dynamic_vector
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction.
Definition: eigen_frwds.h:44
TestLM
void TestLM()
Definition: vision_stereo_rectify/test.cpp:32
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
CLevenbergMarquardt.h
mrpt::math::CLevenbergMarquardtTempl
An implementation of the Levenberg-Marquardt algorithm for least-square minimization.
Definition: CLevenbergMarquardt.h:37
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
mrpt::math::CLevenbergMarquardtTempl::TResultInfo::iterations_executed
size_t iterations_executed
Definition: CLevenbergMarquardt.h:72
myFunction
void myFunction(const CVectorDouble &x, const CVectorDouble &y, CVectorDouble &out_f)
Definition: vision_stereo_rectify/test.cpp:20
mrpt::math::CLevenbergMarquardtTempl::TResultInfo
Definition: CLevenbergMarquardt.h:69
CTicTac.h
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
mrpt::math::CLevenbergMarquardtTempl::TResultInfo::path
matrix_t path
Each row is the optimized value at each iteration.
Definition: CLevenbergMarquardt.h:76
mrpt::math::CLevenbergMarquardtTempl::TResultInfo::final_sqr_err
NUMTYPE final_sqr_err
Definition: CLevenbergMarquardt.h:71
y
GLenum GLint GLint y
Definition: glext.h:3538
x
GLenum GLint x
Definition: glext.h:3538
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