batching_pomp  1.0
This is an implementation of an algorithmic framework for anytime motion planning on large dense roadmaps.
KNNModel.hpp
1 /***********************************************************************
2 Copyright (c) 2017, Shushman Choudhury
3 All rights reserved.
4 Authors: Shushman Choudhury <shushmanchoudhury@gmail.com>
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are
8 met:
9  Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in the
13  documentation and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *************************************************************************/
26 
27 #ifndef BATCHING_POMP_CSPACEBELIEF_KNNMODEL_HPP_
28 #define BATCHING_POMP_CSPACEBELIEF_KNNMODEL_HPP_
29 
30 #include <functional>
31 #include <exception>
32 #include <memory>
33 #include <cmath>
34 #include <ompl/datastructures/NearestNeighbors.h>
35 #include <ompl/datastructures/NearestNeighborsLinear.h>
36 #include <ompl/datastructures/NearestNeighborsGNAT.h>
37 #include <Eigen/Dense>
38 #include "Model.hpp"
39 #include "BeliefPoint.hpp"
40 
41 namespace batching_pomp {
42 namespace cspacebelief {
43 
45 
50 class KNNModel : public virtual Model<BeliefPoint>
51 {
52 public:
53 
56  KNNModel(size_t _KNN,
57  double _prior, double _priorWeight,
58  const std::function<double(const BeliefPoint&, const BeliefPoint&)>& _distanceFunction)
59  : mNumPoints{0}
60  , mKNN{_KNN}
61  , mPrior{_prior}
62  , mPriorWeight{_priorWeight}
63  , mDistanceFunction{_distanceFunction}
64  {
65  mBeliefPointNN.reset(new ompl::NearestNeighborsGNAT<BeliefPoint>());
66  mBeliefPointNN->setDistanceFunction(mDistanceFunction);
67  }
68 
70  // Setters
71  void setPrior(double _prior)
72  {
73  mPrior = _prior;
74  }
75 
76  void setPriorWeight(double _priorWeight)
77  {
78  mPriorWeight = _priorWeight;
79  }
80 
81  void setKNN(size_t _knn)
82  {
83  mKNN = _knn;
84  }
85 
86 
88  // Overriden methods
89  void addPoint(const BeliefPoint& data) override
90  {
91  mBeliefPointNN->add(data);
92  mNumPoints++;
93  }
94 
95  void removePoint(const BeliefPoint& data) override
96  {
97  mBeliefPointNN->remove(data);
98  mNumPoints--;
99  }
100 
101  double estimate(const BeliefPoint& query) const override
102  {
103  // This is important for cases where edges need to be initialized before any checks.
104  if(mNumPoints == 0) {
105  return mPrior;
106  }
107 
108  size_t knn = std::min(mKNN , mNumPoints);
109 
110  std::vector<BeliefPoint> neighboursVect;
111  mBeliefPointNN->nearestK(query,knn,neighboursVect);
112 
113  if(knn != neighboursVect.size()) {
114  throw std::runtime_error("Model could not return the expected number of neighbours");
115  }
116 
117  Eigen::VectorXd weights(knn);
118  Eigen::VectorXd values(knn);
119 
120  for(size_t i = 0; i < knn; i++) {
121  double distance = std::max(0.0001,mDistanceFunction(query, neighboursVect[i]));
122  weights[i] = 1.0/distance;
123  values[i] = neighboursVect[i].getValue(); // Second element of pair is value
124  }
125  double result{mPrior};
126 
127  result = weights.dot(values) / weights.sum();
128 
129  // Do (result + pw*p) / (1 + pw) for smoothing by prior
130  result = (result + mPriorWeight*mPrior) / (1 + mPriorWeight);
131 
132 
133  return result;
134  }
135 
136 private:
137 
138  // The current number of belief points in the model
139  size_t mNumPoints;
140 
141  // The value of k for the kNN lookup
142  size_t mKNN;
143 
144  // The prior probability in (0,1) to use for smoothing
145  double mPrior;
146 
147  // The weight in (0,1) to assign to the prior for smoothing
148  double mPriorWeight;
149 
150  // The kind of nearest neighbour structure for the belief model, GNAT in this case.
151  std::unique_ptr<ompl::NearestNeighbors<BeliefPoint>> mBeliefPointNN;
152 
153  // The distance metric for the nearest neighbour structure
154  std::function<double(const BeliefPoint&, const BeliefPoint&)> mDistanceFunction;
155 };
156 
157 } //namespace cspacebelief
158 } //namespace batching_pomp
159 
160 #endif //BATCHING_POMP_CSPACEBELIEF_KNNMODEL_HPP_
void addPoint(const BeliefPoint &data) override
Definition: KNNModel.hpp:89
void removePoint(const BeliefPoint &data) override
Definition: KNNModel.hpp:95
Abstract class that represents the configuration space belief manager.
Definition: Model.hpp:40
Definition: BatchingManager.hpp:36
An example datatype of the configuration space belief model.
Definition: BeliefPoint.hpp:43
double estimate(const BeliefPoint &query) const override
Definition: KNNModel.hpp:101
Derived class of Model that uses kNN to represent the C-space belief.
Definition: KNNModel.hpp:50
KNNModel(size_t _KNN, double _prior, double _priorWeight, const std::function< double(const BeliefPoint &, const BeliefPoint &)> &_distanceFunction)
Definition: KNNModel.hpp:56