-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVPTree.cpp
294 lines (272 loc) · 6.46 KB
/
VPTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <time.h>
#include <iostream>
#include <exception>
#include <limits>
#include <fstream>
#include <deque>
#include <list>
#include <algorithm>
#include <random>
#include <boost/optional.hpp>
#include <boost/range/combine.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <typeinfo>
#include <memory>
#include <xtensor/xarray.hpp>
#include <xtensor/xsort.hpp>
#include <xtensor/xadapt.hpp>
#include <xtensor/xmath.hpp>
#include "VPTree.h"
using std::deque;
using namespace boost::accumulators;
bool operator!= (Point &lhs,Point &rhs)
{
if (lhs != rhs) return true;
else return false;
}
void VPTree::initializeVPTreePoints(deque<Point> points)
{
time_t start,end;
left = nullptr;
right = nullptr;
double inf = numeric_limits<double>::max();
left_min = inf;
left_max = 0;
right_min = inf;
right_max = 0;
time(&start);
//vp = points.front();
vp = _selectVantagePoint(points);
//points.pop_front();
if (points.size() == 0)
{
return;
}
deque<Point>::iterator it;
deque<double> distances;
try
{
for (it = points.begin();it != points.end();++it)
{
double d;
Point point = *it;
if (vp != point)
{
d = distance->calculateDistance(vp,point);
distances.push_back(d);
}
}
}
catch (const std::out_of_range& oor)
{
std::cerr <<"Out of Range error: " << oor.what() << endl;
exit(0);
}
double median = xt::median(xt::adapt(distances));
//cout <<"The value of median is " << median << endl;
time(&end);
deque<Point> left_points,right_points;
try
{
for (auto tup: boost::combine(points,distances))
{
Point point;
double dist;
boost::tie(point,dist) = tup;
if (dist >= median)
{
right_min = std::min(dist,right_min);
if (dist > right_max)
{
right_max = dist;
right_points.push_front(point);
}
else
{
right_points.push_back(point);
}
}
else
{
left_min = std::min(dist,left_min);
if (dist > left_max)
{
left_max = dist;
left_points.push_front(point);
}
else
{
left_points.push_back(point);
}
}
}
}
catch (const std::out_of_range& oor)
{
std::cerr <<"Out of Range error: " << oor.what() << endl;
}
if (left_points.size() > 0)
{
this->left = new VPTree();
this->left->initializeDistance(this->distance);
this->left->initializeVPTreePoints(left_points);
}
if (right_points.size() > 0)
{
this->right = new VPTree();
this->right->initializeDistance(this->distance);
this->right->initializeVPTreePoints(right_points);
}
}
Point VPTree::_selectVantagePoint(deque<Point> points)
{
vector<Point> randomPointsP;
vector<Point> randomPointsD;
vector<Point>::iterator it;
Point bestPoint;
double bestSpread = 0;
double spread = 0;
size_t nelems = points.size()/10;
vector<double> distances;
// Gets one random selection of Points
std::sample(points.begin(),points.end(),std::back_inserter(randomPointsP),
nelems,std::mt19937{std::random_device{}()});
for (auto p:randomPointsP)
{
// Gets another random selection of Points
std::sample(points.begin(),points.end(),std::back_inserter(randomPointsD),
nelems,std::mt19937{std::random_device{}()});
try
{
for (it = randomPointsD.begin();it != randomPointsD.end();++it)
{
double d;
Point point = *it;
d = distance->calculateDistance(p,point);
distances.push_back(d);
}
}
catch (const std::out_of_range& oor)
{
std::cerr <<"Out of Range error: " << oor.what() << endl;
exit(0);
}
spread = xt::variance(xt::adapt(distances))();
if (spread > bestSpread)
{
bestSpread = spread;
bestPoint = p;
break;
}
}
return bestPoint;
}
bool VPTree::_isLeaf()
{
if (!(left) and !(right))
{
return true;
}
else
{
return false;
}
}
double VPTree::_findMedian(deque<double>distances)
{
size_t size = distances.size();
if (size == 0)
{
return 0;
}
else
{
// First we sort the array
sort(distances.begin(),distances.end());
if (size % 2 == 0)
{
return (distances[size/2 -1] + distances[size/2])/2;
}
else
{
return distances[size/2];
}
}
}
std::vector<std::vector<std::pair<double,Point>>> VPTree::getAllInRange(std::vector<Point> queryPoints,double maxDistance)
{
std::vector<Point>::iterator it;
std::vector<std::vector<std::pair<double,Point>>> neighborCollection;
try
{
for (it = queryPoints.begin();it != queryPoints.end();++it)
{
Point query = *it;
vector<pair<double,Point>> neighbors;
neighbors = getAllInRange(query,maxDistance);
neighborCollection.push_back(neighbors);
}
}
catch(const std::out_of_range& oor)
{
exit(0);
}
return neighborCollection;
}
//NPY_BEGIN_ALLOW_THREADS
//NPY_END_ALLOW_THREADS
//Similar to Py_BEGIN_ALLOW_THREADS
// And Py_END_ALLOW_THREADS
vector<pair<double,Point>> VPTree::getAllInRange(Point query, double maxDistance)
{
vector<pair<double,Point>> neighbors;
deque<pair<VPTree*,double>> nodes_to_visit;
VPTree *node;
double d0;
nodes_to_visit.push_front(make_pair(this,0));
while (nodes_to_visit.size() > 0 )
{
deque<pair<VPTree*,double>>::iterator it = nodes_to_visit.begin();
node = it->first;
d0 = it->second;
nodes_to_visit.erase(it);
if (node == nullptr or d0 > maxDistance)
continue;
Point point = node->vp;
double dist = distance->calculateDistance(query,point);
if (dist < maxDistance)
{
neighbors.push_back(make_pair(dist,point));
}
if (node->_isLeaf())
continue;
if (node->left_min <= dist && dist <= node->left_max)
{
nodes_to_visit.push_front(make_pair(node->left,0));
}
else if (node->left_min-maxDistance <= dist && dist <= (node->left_max + maxDistance) )
{
double dd;
if (dist < node->left_min)
dd = node->left_min - dist;
else
dd = dist - node->left_max;
nodes_to_visit.push_back(make_pair(node->left,dd));
}
if (node->right_min <= dist && dist <= node->right_max)
{
nodes_to_visit.push_front(make_pair(node->right,0));
}
else if (node->right_min-maxDistance <= dist && dist <= node->right_max + maxDistance )
{
double dd;
if (dist < node->right_min)
dd = node->right_min - dist;
else
dd = dist - node->right_max;
nodes_to_visit.push_back(make_pair(node->right,dd));
}
}
return neighbors;
}