GreedyKCenters.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Rice University
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Rice University nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Mark Moll */
36 
37 // This file is a slightly modified version of <ompl/datastructures/GreedyKCenters.h>
38 
39 #ifndef MOVEIT_ROS_PLANNING_CACHED_IK_KINEMATICS_GREEDY_K_CENTERS_
40 #define MOVEIT_ROS_PLANNING_CACHED_IK_KINEMATICS_GREEDY_K_CENTERS_
41 
42 #include <functional>
43 #include <random>
44 #include <boost/numeric/ublas/matrix.hpp>
45 
47 {
51 template <typename _T>
53 {
54 public:
56  using DistanceFunction = std::function<double(const _T&, const _T&)>;
58  using Matrix = boost::numeric::ublas::matrix<double>;
59 
60  GreedyKCenters() = default;
61 
62  virtual ~GreedyKCenters() = default;
63 
66  {
67  distFun_ = distFun;
68  }
69 
72  {
73  return distFun_;
74  }
75 
84  void kcenters(const std::vector<_T>& data, unsigned int k, std::vector<unsigned int>& centers, Matrix& dists)
85  {
86  // array containing the minimum distance between each data point
87  // and the centers computed so far
88  std::vector<double> minDist(data.size(), std::numeric_limits<double>::infinity());
89 
90  centers.clear();
91  centers.reserve(k);
92  if (dists.size1() < data.size() || dists.size2() < k)
93  dists.resize(std::max(2 * dists.size1() + 1, data.size()), k, false);
94  // first center is picked randomly
95  centers.push_back(std::uniform_int_distribution<size_t>{ 0, data.size() - 1 }(generator_));
96  for (unsigned i = 1; i < k; ++i)
97  {
98  unsigned ind;
99  const _T& center = data[centers[i - 1]];
100  double maxDist = -std::numeric_limits<double>::infinity();
101  for (unsigned j = 0; j < data.size(); ++j)
102  {
103  if ((dists(j, i - 1) = distFun_(data[j], center)) < minDist[j])
104  minDist[j] = dists(j, i - 1);
105  // the j-th center is the one furthest away from center 0,..,j-1
106  if (minDist[j] > maxDist)
107  {
108  ind = j;
109  maxDist = minDist[j];
110  }
111  }
112  // no more centers available
113  if (maxDist < std::numeric_limits<double>::epsilon())
114  break;
115  centers.push_back(ind);
116  }
117 
118  const _T& center = data[centers.back()];
119  unsigned i = centers.size() - 1;
120  for (unsigned j = 0; j < data.size(); ++j)
121  dists(j, i) = distFun_(data[j], center);
122  }
123 
124 protected:
127 
129  std::mt19937 generator_{ std::random_device{}() };
130 };
131 }
132 
133 #endif
DistanceFunction distFun_
The used distance function.
const DistanceFunction & getDistanceFunction() const
Get the distance function used.
void setDistanceFunction(const DistanceFunction &distFun)
Set the distance function to use.
An instance of this class can be used to greedily select a given number of representatives from a set...
std::function< double(const IKEntry *&, const IKEntry *&)> DistanceFunction
The definition of a distance function.
void kcenters(const std::vector< _T > &data, unsigned int k, std::vector< unsigned int > &centers, Matrix &dists)
Greedy algorithm for selecting k centers.
boost::numeric::ublas::matrix< double > Matrix
A matrix type for storing distances between points and centers.


moveit_kinematics
Author(s): Dave Coleman , Ioan Sucan , Sachin Chitta
autogenerated on Sun Sep 15 2019 03:58:37