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/random.h>
11 #include <mrpt/math/kmeans.h>
13 #include <mrpt/system/CTicTac.h>
16 #include <iostream>
17 
18 using namespace mrpt::math;
19 using namespace mrpt::gui;
20 using namespace mrpt::random;
21 using namespace mrpt::system;
22 using namespace std;
23 
24 // ------------------------------------------------------
25 // TestKMeans
26 // ------------------------------------------------------
27 void TestKMeans()
28 {
29  typedef CArrayDouble<2> CPointType;
30  // typedef CArrayFloat<2> CPointType;
31 
33  CTicTac tictac;
34 
35  CDisplayWindowPlots win("k-means results");
36 
37  cout << "Close the window to end.\n";
38 
39  while (win.isOpen())
40  {
41  // Generate N clusters of random points:
43  const size_t nClusters =
45 
46  for (size_t cl = 0; cl < nClusters; cl++)
47  {
48  const size_t nPts = getRandomGenerator().drawUniform(5, 50);
49 
50  TPoint2D clCenter;
51  clCenter.x = getRandomGenerator().drawUniform(0, 10);
52  clCenter.y = getRandomGenerator().drawUniform(0, 10);
53 
54  for (size_t p = 0; p < nPts; p++)
55  {
56  CPointType v;
57  v[0] = clCenter.x + getRandomGenerator().drawGaussian1D(0, 1);
58  v[1] = clCenter.y + getRandomGenerator().drawGaussian1D(0, 1);
59  points.push_back(v);
60  }
61  }
62 
63  // do k-means
65  vector<int> assignments;
66  tictac.Tic();
67 
68  const double cost =
69  mrpt::math::kmeanspp(nClusters, points, assignments, &centers);
70 
71  cout << "Took: " << tictac.Tac() * 1e3 << " ms.\n";
72  cout << "cost: " << cost << endl;
73 
74  // Show:
75  win.clf();
76  win.hold_on();
77  static const char colors[6] = {'b', 'r', 'k', 'g', 'm', 'c'};
78 
79  for (size_t c = 0; c < nClusters; c++)
80  {
81  CVectorDouble xs, ys;
82 
83  for (size_t i = 0; i < points.size(); i++)
84  {
85  if (size_t(assignments[i]) == c)
86  {
87  xs.push_back(points[i][0]);
88  ys.push_back(points[i][1]);
89  }
90  }
91  win.plot(xs, ys, mrpt::format(".4%c", colors[c % 6]));
92  }
93  win.axis_fit();
94  win.axis_equal();
95 
96  cout << "Press any key to generate another random dataset...\n";
97  win.waitForKey();
98  };
99 }
100 
101 int main(int argc, char** argv)
102 {
103  try
104  {
105  TestKMeans();
106  return 0;
107  }
108  catch (std::exception& e)
109  {
110  std::cout << "MRPT exception caught: " << e.what() << std::endl;
111  return -1;
112  }
113  catch (...)
114  {
115  printf("Another exception!!");
116  return -1;
117  }
118 }
A namespace of pseudo-random numbers generators of diferent distributions.
uint32_t drawUniform32bit()
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, in the whole range of 32-bit integers.
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
double drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, scaled to the selected range.
double x
X,Y coordinates.
Create a GUI window and display plots with MATLAB-like interfaces and commands.
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
A high-performance stopwatch, with typical resolution of nanoseconds.
CArrayNumeric is an array for numeric types supporting several mathematical operations (actually...
Definition: CArrayNumeric.h:25
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:44
STL namespace.
void TestKMeans()
std::vector< T, mrpt::aligned_allocator_cpp11< T > > aligned_std_vector
double drawGaussian1D(const double mean, const double std)
Generate a normally distributed pseudo-random number.
GLsizei const GLfloat * points
Definition: glext.h:5339
This base provides a set of functions for maths stuff.
const GLubyte * c
Definition: glext.h:6313
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...
mrpt::gui::CDisplayWindow3D::Ptr win
const GLdouble * v
Definition: glext.h:3678
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
Lightweight 2D point.
GLfloat GLfloat p
Definition: glext.h:6305



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020