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 #pragma once
40 
41 #include <functional>
42 #include <random>
43 #include <boost/numeric/ublas/matrix.hpp>
44 
46 {
50 template <typename _T>
51 class GreedyKCenters
52 {
53 public:
55  using DistanceFunction = std::function<double(const _T&, const _T&)>;
57  using Matrix = boost::numeric::ublas::matrix<double>;
58 
59  GreedyKCenters() = default;
60 
61  virtual ~GreedyKCenters() = default;
62 
64  void setDistanceFunction(const DistanceFunction& distFun)
65  {
66  distFun_ = distFun;
67  }
68 
71  {
72  return distFun_;
73  }
74 
83  void kcenters(const std::vector<_T>& data, unsigned int k, std::vector<unsigned int>& centers, Matrix& dists)
84  {
85  // array containing the minimum distance between each data point
86  // and the centers computed so far
87  std::vector<double> min_dist(data.size(), std::numeric_limits<double>::infinity());
88 
89  centers.clear();
90  centers.reserve(k);
91  if (dists.size1() < data.size() || dists.size2() < k)
92  dists.resize(std::max(2 * dists.size1() + 1, data.size()), k, false);
93  // first center is picked randomly
94  centers.push_back(std::uniform_int_distribution<size_t>{ 0, data.size() - 1 }(generator_));
95  for (unsigned i = 1; i < k; ++i)
96  {
97  unsigned ind = 0;
98  const _T& center = data[centers[i - 1]];
99  double max_dist = -std::numeric_limits<double>::infinity();
100  for (unsigned j = 0; j < data.size(); ++j)
101  {
102  if ((dists(j, i - 1) = distFun_(data[j], center)) < min_dist[j])
103  min_dist[j] = dists(j, i - 1);
104  // the j-th center is the one furthest away from center 0,..,j-1
105  if (min_dist[j] > max_dist)
106  {
107  ind = j;
108  max_dist = min_dist[j];
109  }
110  }
111  // no more centers available
112  if (max_dist < std::numeric_limits<double>::epsilon())
113  break;
114  centers.push_back(ind);
115  }
116 
117  const _T& center = data[centers.back()];
118  unsigned i = centers.size() - 1;
119  for (unsigned j = 0; j < data.size(); ++j)
120  dists(j, i) = distFun_(data[j], center);
121  }
122 
123 protected:
126 
128  std::mt19937 generator_{ std::random_device{}() };
129 };
130 } // namespace cached_ik_kinematics_plugin
cached_ik_kinematics_plugin::GreedyKCenters::kcenters
void kcenters(const std::vector< _T > &data, unsigned int k, std::vector< unsigned int > &centers, Matrix &dists)
Greedy algorithm for selecting k centers.
Definition: GreedyKCenters.h:147
cached_ik_kinematics_plugin
Definition: cached_ik_kinematics_plugin-inl.h:40
cached_ik_kinematics_plugin::GreedyKCenters::generator_
std::mt19937 generator_
Definition: GreedyKCenters.h:192
cached_ik_kinematics_plugin::GreedyKCenters::getDistanceFunction
const DistanceFunction & getDistanceFunction() const
Get the distance function used.
Definition: GreedyKCenters.h:134
cached_ik_kinematics_plugin::GreedyKCenters::~GreedyKCenters
virtual ~GreedyKCenters()=default
cached_ik_kinematics_plugin::GreedyKCenters::DistanceFunction
std::function< double(const _T &, const _T &)> DistanceFunction
The definition of a distance function.
Definition: GreedyKCenters.h:119
cached_ik_kinematics_plugin::GreedyKCenters::Matrix
boost::numeric::ublas::matrix< double > Matrix
A matrix type for storing distances between points and centers.
Definition: GreedyKCenters.h:121
cached_ik_kinematics_plugin::GreedyKCenters::GreedyKCenters
GreedyKCenters()=default
cached_ik_kinematics_plugin::GreedyKCenters::setDistanceFunction
void setDistanceFunction(const DistanceFunction &distFun)
Set the distance function to use.
Definition: GreedyKCenters.h:128
cached_ik_kinematics_plugin::GreedyKCenters::distFun_
DistanceFunction distFun_
The used distance function.
Definition: GreedyKCenters.h:189


moveit_kinematics
Author(s): Dave Coleman , Ioan Sucan , Sachin Chitta
autogenerated on Fri May 3 2024 02:29:33