MRPT  2.0.4
test.cpp
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 
11 #include <mrpt/math/CVectorFixed.h>
12 #include <mrpt/math/TPoint2D.h>
13 #include <mrpt/math/kmeans.h>
14 #include <mrpt/random.h>
15 #include <mrpt/system/CTicTac.h>
16 #include <iostream>
17 #include <vector>
18 
19 using namespace mrpt::math;
20 using namespace mrpt::gui;
21 using namespace mrpt::random;
22 using namespace mrpt::system;
23 using namespace std;
24 
25 // ------------------------------------------------------
26 // TestKMeans
27 // ------------------------------------------------------
28 void TestKMeans()
29 {
30  typedef CVectorFixedDouble<2> CPointType;
31  // typedef CVectorFixedFloat<2> CPointType;
32 
34  CTicTac tictac;
35 
36  CDisplayWindowPlots win("k-means results");
37 
38  cout << "Close the window to end.\n";
39 
40  while (win.isOpen())
41  {
42  // Generate N clusters of random points:
43  std::vector<CPointType> points;
44  const size_t nClusters =
46 
47  for (size_t cl = 0; cl < nClusters; cl++)
48  {
49  const size_t nPts = getRandomGenerator().drawUniform<size_t>(5, 50);
50 
51  TPoint2D clCenter;
52  clCenter.x = getRandomGenerator().drawUniform(0, 10);
53  clCenter.y = getRandomGenerator().drawUniform(0, 10);
54 
55  for (size_t p = 0; p < nPts; p++)
56  {
57  CPointType v;
58  v[0] = clCenter.x + getRandomGenerator().drawGaussian1D(0, 1);
59  v[1] = clCenter.y + getRandomGenerator().drawGaussian1D(0, 1);
60  points.push_back(v);
61  }
62  }
63 
64  // do k-means
65  std::vector<CPointType> centers;
66  vector<int> assignments;
67  tictac.Tic();
68 
69  const double cost =
70  mrpt::math::kmeanspp(nClusters, points, assignments, &centers);
71 
72  cout << "Took: " << tictac.Tac() * 1e3 << " ms.\n";
73  cout << "cost: " << cost << endl;
74 
75  // Show:
76  win.clf();
77  win.hold_on();
78  static const char colors[6] = {'b', 'r', 'k', 'g', 'm', 'c'};
79 
80  for (size_t c = 0; c < nClusters; c++)
81  {
82  CVectorDouble xs, ys;
83 
84  for (size_t i = 0; i < points.size(); i++)
85  {
86  if (size_t(assignments[i]) == c)
87  {
88  xs.push_back(points[i][0]);
89  ys.push_back(points[i][1]);
90  }
91  }
92  win.plot(xs, ys, mrpt::format(".4%c", colors[c % 6]));
93  }
94  win.axis_fit();
95  win.axis_equal();
96 
97  cout << "Press any key to generate another random dataset...\n";
98  win.waitForKey();
99  };
100 }
101 
102 int main(int argc, char** argv)
103 {
104  try
105  {
106  TestKMeans();
107  return 0;
108  }
109  catch (const std::exception& e)
110  {
111  std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
112  return -1;
113  }
114  catch (...)
115  {
116  printf("Another exception!!");
117  return -1;
118  }
119 }
mrpt::math::kmeanspp
double kmeanspp(const size_t k, const LIST_OF_VECTORS1 &points, std::vector< int > &assignments, LIST_OF_VECTORS2 *out_centers=nullptr, const size_t attempts=3)
k-means++ algorithm to cluster a list of N points of arbitrary dimensionality into exactly K clusters...
Definition: kmeans.h:157
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:18
TPoint2D.h
mrpt::math::TPoint2D_< double >
TestKMeans
void TestKMeans()
Definition: vision_stereo_rectify/test.cpp:28
random.h
mrpt::random::CRandomGenerator::randomize
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
Definition: RandomGenerator.cpp:92
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:87
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
CVectorFixed.h
mrpt::random::CRandomGenerator::drawUniform
return_t drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm,...
Definition: RandomGenerators.h:142
mrpt::math::CMatrixFixed
A compile-time fixed-size numeric matrix container.
Definition: CMatrixFixed.h:34
win
mrpt::gui::CDisplayWindow3D::Ptr win
Definition: vision_stereo_rectify/test.cpp:31
argv
const char * argv[]
Definition: RawlogGrabberApp_unittest.cpp:50
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:76
mrpt::gui
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:15
mrpt::math::CVectorDynamic< double >
mrpt::gui::CDisplayWindowPlots
Create a GUI window and display plots with MATLAB-like interfaces and commands.
Definition: CDisplayWindowPlots.h:34
mrpt::math::TPoint2D_data::x
T x
X,Y coordinates.
Definition: TPoint2D.h:25
mrpt::random::getRandomGenerator
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
Definition: RandomGenerator.cpp:89
CTicTac.h
argc
const int argc
Definition: RawlogGrabberApp_unittest.cpp:51
mrpt::random::CRandomGenerator::drawGaussian1D
return_t drawGaussian1D(const double mean, const double std)
Generate a normally distributed pseudo-random number.
Definition: RandomGenerators.h:194
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:12
mrpt::math::CVectorDynamic::push_back
void push_back(const T &val)
Definition: CVectorDynamic.h:197
mrpt::random
A namespace of pseudo-random numbers generators of diferent distributions.
Definition: random_shuffle.h:19
mrpt::exception_to_str
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59
CDisplayWindowPlots.h
mrpt::format
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
mrpt::random::CRandomGenerator::drawUniform32bit
uint32_t drawUniform32bit()
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm,...
Definition: RandomGenerator.cpp:91
mrpt::system
Definition: backtrace.h:15
kmeans.h



Page generated by Doxygen 1.8.18 for MRPT 2.0.4 at Thu Sep 24 07:14:18 UTC 2020