27 #ifndef BATCHING_POMP_CSPACEBELIEF_KNNMODEL_HPP_ 28 #define BATCHING_POMP_CSPACEBELIEF_KNNMODEL_HPP_ 34 #include <ompl/datastructures/NearestNeighbors.h> 35 #include <ompl/datastructures/NearestNeighborsLinear.h> 36 #include <ompl/datastructures/NearestNeighborsGNAT.h> 37 #include <Eigen/Dense> 39 #include "BeliefPoint.hpp" 42 namespace cspacebelief {
57 double _prior,
double _priorWeight,
62 , mPriorWeight{_priorWeight}
63 , mDistanceFunction{_distanceFunction}
65 mBeliefPointNN.reset(
new ompl::NearestNeighborsGNAT<BeliefPoint>());
66 mBeliefPointNN->setDistanceFunction(mDistanceFunction);
71 void setPrior(
double _prior)
76 void setPriorWeight(
double _priorWeight)
78 mPriorWeight = _priorWeight;
81 void setKNN(
size_t _knn)
91 mBeliefPointNN->add(data);
97 mBeliefPointNN->remove(data);
104 if(mNumPoints == 0) {
108 size_t knn = std::min(mKNN , mNumPoints);
110 std::vector<BeliefPoint> neighboursVect;
111 mBeliefPointNN->nearestK(query,knn,neighboursVect);
113 if(knn != neighboursVect.size()) {
114 throw std::runtime_error(
"Model could not return the expected number of neighbours");
117 Eigen::VectorXd weights(knn);
118 Eigen::VectorXd values(knn);
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();
125 double result{mPrior};
127 result = weights.dot(values) / weights.sum();
130 result = (result + mPriorWeight*mPrior) / (1 + mPriorWeight);
151 std::unique_ptr<ompl::NearestNeighbors<BeliefPoint>> mBeliefPointNN;
154 std::function<double(const BeliefPoint&, const BeliefPoint&)> mDistanceFunction;
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