-
Notifications
You must be signed in to change notification settings - Fork 0
/
getResults.cpp
428 lines (329 loc) · 10.9 KB
/
getResults.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include <time.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <vector>
#include <set>
#include <cmath>
#include <queue>
#include <list>
#include <algorithm>
#include <cstdlib>
using namespace std;
const double x_min = -90.0;
const double x_max = 90.0;
const double y_min = -176.3;
const double y_max = 177.5;
struct LocationQuery{
double latitude;
double longitude;
LocationQuery(double lat, double lon):latitude(lat), longitude(lon){}
};
vector<LocationQuery> createQuery(int count){
vector<LocationQuery> v = vector<LocationQuery>();
for(int i = 0; i < count; i++){
double latitude = ((double)rand() / RAND_MAX)*(x_max - x_min) + x_min;
double longitude = ((double)rand() / RAND_MAX)*(y_max - y_min) + y_min;
v.push_back(LocationQuery(latitude, longitude));
}
return v;
}
class LocationWithID{
public:
double latitude;
double longitude;
string locationID;
LocationWithID(double lat, double lon, string locID):latitude(lat), longitude(lon), locationID(locID){}
};
struct PointDistance{
double distance;
double latitude;
double longitude;
string locationID;
PointDistance(double lat, double lon, double dist, string locID):latitude(lat), longitude(lon), distance(dist), locationID(locID){}
};
struct BoxIndex{
int x_index;
int y_index;
BoxIndex(int x, int y):x_index(x), y_index(y){}
bool operator<(BoxIndex const & b1) const {
return b1.x_index < this->x_index || b1.x_index == this->x_index && b1.y_index < this->y_index;
}
};
set<BoxIndex> calculateBoxIndexes(int x_index, int y_index, int layer, int top_limit){
set<BoxIndex> boxIndex;
int x_min_limit = x_index - layer;
int x_max_limit = x_index + layer;
int y_min_limit = y_index - layer;
int y_max_limit = y_index + layer;
for(int i = x_min_limit; i <= x_max_limit; i++){
if(i < 0 || i > top_limit){
continue;
}
if(y_min_limit < 0 && y_max_limit > top_limit){
break;
}else if(y_min_limit < 0){
boxIndex.insert(BoxIndex(i, y_max_limit));
continue;
}else if(y_max_limit > top_limit){
boxIndex.insert(BoxIndex(i, y_min_limit));
continue;
}
boxIndex.insert(BoxIndex(i, y_max_limit));
boxIndex.insert(BoxIndex(i, y_min_limit));
}
for(int i = y_min_limit; i <= y_max_limit; i++){
if(i < 0 || i > top_limit){
continue;
}
if(x_min_limit < 0 && x_max_limit > top_limit){
break;
}else if(x_min_limit < 0){
boxIndex.insert(BoxIndex(x_max_limit, i));
continue;
}else if(x_max_limit > top_limit){
boxIndex.insert(BoxIndex(x_min_limit, i));
continue;
}
boxIndex.insert(BoxIndex(x_min_limit, i));
boxIndex.insert(BoxIndex(x_max_limit, i));
}
return boxIndex;
}
struct ComparePoint{
bool operator()(PointDistance const & p1, PointDistance const & p2){
return p1.distance < p2.distance;
}
};
double euclidean_distance(double x_1, double y_1, double x_2, double y_2){
return sqrt(pow((x_2 - x_1), 2) + pow((y_2 - y_1), 2));
}
bool box_check(int box_x, int box_y, int point_box_x, int point_box_y, double point_x, double point_y, double top, int n){
double distance_to_box = 0.0;
double horizontal_size = (x_max - x_min) / n; //actually latitude runs vertically, with -90 degrees to 90 degrees.
double vertical_size = (y_max - y_min) / n;
if(point_box_x == box_x){
if(point_box_y < box_y){
distance_to_box = point_y - (y_max - (box_y)*vertical_size);
}else{
distance_to_box = (y_max - (box_y + 1)*vertical_size) - point_y;
}
}else if(point_box_y == box_y){
if(point_box_x < box_x){
distance_to_box = (x_min + (box_x)*horizontal_size) - point_x;
}else{
distance_to_box = point_x - (x_min + (box_x + 1)*horizontal_size);
}
}else if(point_box_x > box_x && point_box_y > box_y){
//compared box on left top
distance_to_box = euclidean_distance(point_x, point_y, (x_min + (box_x + 1)*horizontal_size), (y_max - (box_y + 1)*vertical_size));
}else if(point_box_x < box_x && point_box_y > box_y){
//compared box on right top
distance_to_box = euclidean_distance(point_x, point_y, (x_min + (box_x)*horizontal_size), (y_max - (box_y + 1)*vertical_size));
}else if(point_box_x > box_x && point_box_y < box_y){
//compared box on left bottom
distance_to_box = euclidean_distance(point_x, point_y, (x_min + (box_x + 1)*horizontal_size), (y_max - (box_y)*vertical_size));
}else if(point_box_x < box_x && point_box_y < box_y){
//compared box on right bottom
distance_to_box = euclidean_distance(point_x, point_y, (x_min + (box_x)*horizontal_size), (y_max - (box_y)*vertical_size));
}
if(distance_to_box > top){
return false;
}else{
return true;
}
}
PointDistance get_top_from_vector(vector<PointDistance> v){
return v.back();
}
string knn_grid(double x, double y, char * index_path, int k, int n)
{
// to get the 5-NN result with the help of the grid index
// Please store the 5-NN results by a String of location ids, like "11, 789, 125, 2, 771"
ifstream file;
file.open(index_path);
cout.precision(14);
vector<LocationWithID> ***dataArray = new vector<LocationWithID>**[n];
for(int i = 0; i < n; i++){
dataArray[i] = new vector<LocationWithID>*[n];
for(int j = 0; j < n; j++){
dataArray[i][j] = new vector<LocationWithID>();
}
}
string cell;
string flush;
string index;
string line;
int counter = 0;
while(getline(file, line)){
istringstream iss(line);
iss >> flush >> index;
int commaPosition = index.find(',');
int colonPosition = index.find(':');
int y_index = stoi(index.substr(0, commaPosition));
int x_index = stoi(index.substr(commaPosition + 1, colonPosition - commaPosition - 1));
string point;
while(iss >> point){
int first_us = point.find('_');
int second_us = point.find('_', first_us + 1);
string locationID = point.substr(0, first_us);
string latitude = point.substr(first_us + 1, second_us - first_us - 1);
string longitude = point.substr(second_us + 1, point.length() - second_us - 1);
counter += 1;
dataArray[x_index][y_index]->push_back(LocationWithID(stod(latitude), stod(longitude), locationID));
}
}
//vector implementation
/*
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
for(vector<LocationWithID>::iterator itr = dataArray[i][j]->begin(); itr != dataArray[i][j]->end(); itr++){
//cout << itr->locationID << ' ' << itr->latitude << ' '<< itr-> longitude << endl;
}
}
}
*/
double query_x = x;
double query_y = y;
double horizontal_size = (x_max - x_min) / n; //actually latitude runs vertically, with -90 degrees to 90 degrees.
double vertical_size = (y_max - y_min) / n;
int horizontal_box_num = (query_x - x_min) / horizontal_size;
int vertical_box_num = (query_y - y_min) / vertical_size;
int index_x;
int index_y;
index_x = horizontal_box_num;
index_y = n - vertical_box_num - 1;
if(((query_x - x_min)/horizontal_size - double(index_x) == 0.0 && index_x != 0) || index_x == n){
index_x -= 1;
}
if(index_y == -1){
index_y += 1;
}
priority_queue<PointDistance, vector<PointDistance>, ComparePoint> pq;
//vector declaration for vector implementation of kNN container
//vector<PointDistance> result_vector = vector<PointDistance>();
int layer = 0;
int box_counter = 0;
int cumul_box_counter = 0;
while(true){
set<BoxIndex> boxIndexes = calculateBoxIndexes(index_x, index_y, layer, n - 1);
for(set<BoxIndex>::iterator box_itr = boxIndexes.begin(); box_itr != boxIndexes.end(); box_itr++){
if(!pq.empty() && !box_check(box_itr->x_index, box_itr->y_index, index_x, index_y, x, y, pq.top().distance, n)){
box_counter += 1;
}
if(dataArray[box_itr->x_index][box_itr->y_index]->size() != 0){
for(vector<LocationWithID>::iterator itr = dataArray[box_itr->x_index][box_itr->y_index]->begin();
itr != dataArray[box_itr->x_index][box_itr->y_index]->end(); itr++){
double euc_distance = euclidean_distance(x, y, itr->latitude, itr->longitude);
if(pq.size() >= k && pq.top().distance < euc_distance){
continue;
}
pq.push(PointDistance(itr->latitude, itr->longitude, euc_distance, itr->locationID));
}
int queue_size = pq.size();
for(int i = 0; i < queue_size - k; i++){
pq.pop();
}
}
}
if(box_counter == boxIndexes.size() && pq.size() == k){
break;
}else if(cumul_box_counter >= (n*n)){
break;
}else{
cumul_box_counter += box_counter;
box_counter = 0;
}
layer += 1;
}
while(!pq.empty()){
PointDistance p = pq.top();
pq.pop();
if(pq.empty()){
cout << p.locationID;
}else{
cout << p.locationID << ", ";
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
delete dataArray[i][j];
}
delete[] dataArray[i];
}
delete[] dataArray;
file.close();
return "";
}
string knn_linear_scan(double x, double y, char * data_path_new, int k)
{
// to get the 5-NN result by linear scan
// Please store the 5-NN results by a String of location ids, like "11, 789, 125, 2, 771"
clock_t a = clock();
ifstream file;
file.open(data_path_new);
cout.precision(14);
string latitude;
string longitude;
string locationID;
vector<LocationWithID> dataArray = vector<LocationWithID>();
priority_queue<PointDistance, vector<PointDistance>, ComparePoint> pq;
//vector implementation of kNN container
//vector<PointDistance> result_vector = vector<PointDistance>();
while(file >> latitude >> longitude >> locationID){
double latdouble = stod(latitude);
double londouble = stod(longitude);
dataArray.push_back(LocationWithID(latdouble, londouble, locationID));
}
double distance = 0;
for(vector<LocationWithID>::iterator itr = dataArray.begin(); itr != dataArray.end(); itr++){
distance = euclidean_distance(x, y, itr->latitude, itr->longitude);
if(pq.size() >= k){
if(distance > pq.top().distance){
continue;
}
}
pq.push(PointDistance(itr->latitude, itr->longitude, distance, itr->locationID));
int queue_size = pq.size();
for(int i = 0; i < queue_size - k; i++){
pq.pop();
}
}
while(!pq.empty()){
PointDistance p = pq.top();
pq.pop();
if(pq.empty()){
cout << p.locationID;
}else{
cout << p.locationID << ", ";
}
}
return "";
}
int main(int argc, char** argv)
{
if (argc != 7){
cerr << "Usage: " << argv[0] << " X Y DATA_PATH_NEW INDEX_PATH K N" << endl;
/*
X(double): the latitude of the query point q
Y(double): the longitude of the query point q
DATA_PATH_NEW(char *): the file path of dataset you generated without duplicates
INDEX_PATH(char *): the file path of the grid index
K(integer): the k value for k-NN search
N(integer): the grid index size
*/
return -1;
}
clock_t s, t;
s=clock();
cout << "Linear scan results: "<<knn_linear_scan(atof(argv[1]), atof(argv[2]), argv[3], atoi(argv[5]))<<endl;
t=clock();
cout<<"Linear scan time: "<<(double)(t-s)<<endl;
s=clock();
cout << "Grid index search results: "<<knn_grid(atof(argv[1]), atof(argv[2]), argv[4], atoi(argv[5]), atoi(argv[6]))<<endl;
t=clock();
cout<<"Grid index search time: "<<(double)(t-s)<<endl;
return 0;
}