-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
190 lines (156 loc) · 7.57 KB
/
main.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
#include <iostream>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <thread> // std::this_thread::sleep_for
#include <chrono>
#include "partitions_and_POIs.hpp"
#include "partitioned_grids.hpp"
#include "data_structures.hpp"
// for brute_force
#define TOP_LEFT_LAT 40.99
#define TOP_LEFT_LONG -74.276
#define LATITUDE_RANGE 0.442
#define LONGITUDE_RANGE 0.595
using namespace std;
// To get the whole original grid as a single partition for applying brute force.
Map get_brute_force_partition(vector<Data> dataRow)
{
Map unPartitioned;
Point topLeft, topRight, bottomLeft, bottomRight;
topLeft.latitude = TOP_LEFT_LAT;
topLeft.longitude = TOP_LEFT_LONG;
topRight.latitude = TOP_LEFT_LAT;
topRight.longitude = TOP_LEFT_LONG + LONGITUDE_RANGE;
bottomLeft.latitude = TOP_LEFT_LAT - LATITUDE_RANGE;
bottomLeft.longitude = TOP_LEFT_LONG;
bottomRight.latitude = TOP_LEFT_LAT - LATITUDE_RANGE;
bottomRight.longitude = TOP_LEFT_LONG + LONGITUDE_RANGE;
unPartitioned.p_id.top_left = topLeft;
unPartitioned.p_id.top_right = topRight;
unPartitioned.p_id.bottom_left = bottomLeft;
unPartitioned.p_id.bottom_right = bottomRight;
unPartitioned.noOfPOIs = dataRow.size();
unPartitioned.utility = new Data[unPartitioned.noOfPOIs];
copy(dataRow.begin(), dataRow.end(), &unPartitioned.utility[0]);
sort(unPartitioned.utility, unPartitioned.utility + unPartitioned.noOfPOIs, comparePOI);
/*for(int i = 0; i < unPartitioned.noOfPOIs; i++)
cout << unPartitioned.utility[i].utilities << endl;*/
//unPartitioned.utility = &dataRow[0];
return unPartitioned;
}
//Function to return randomly generated user points
double getRandom(double min, double max)
{
double before = rand() % (int)max + (int)min;
double after = (double)rand() / RAND_MAX;
double result = before + after;
if (result < min || result > max) {
result = getRandom(min, max);
}
return result;
}
int main()
{
srand (time(NULL));
int noOfSmallerPartitions, totalPartitionInRow, datarowSize, k;
clock_t t1, t2, t3;
double time_taken1, time_taken2, time_taken3;
float factorLat, factorLong;
string POICategory;
Point userLocation;
vector<Data> dataRow;
Map unpartitioned;
//Taking the number of boxes in initial partition by the user
cout << "Enter the number of grids (Should be a perfect square) :" << endl;
cin >> noOfSmallerPartitions;
//A function to take number of rows in the dataset and memory allocation to the object array.
cout << endl << "Calculating total no. of POIs" << endl;
datarowSize = getNoOfPOIs();
cout << "Total POIs = " << datarowSize << endl << endl;
// Store the POIs in our data structures
cout << "Loading the POIs in the data structures..." << endl;
dataRow = loadPOI_inDataStructures();
cout << "Successfully loaded." << endl << endl;
// Make partitions in the original grid
cout << "Making partitions in the original grid..." << endl;
vector<Map> initialPartition_vec = originalGrid(noOfSmallerPartitions, datarowSize);
cout << "Success." << endl;
// Load the POIs in the partitions created in the original grid
cout << "Loading POIs for the original grid inside the created partitions" << endl;
loadPoIs(dataRow, initialPartition_vec);
cout << "Successfully loaded." << endl << endl;
// Get factorLat and factorLong
totalPartitionInRow = (int) sqrt(noOfSmallerPartitions);
factorLat = getfactorLat(totalPartitionInRow);
factorLong = getfactorLong(totalPartitionInRow);
// Create top-left shifted grid
cout << "Shifting partitions in the top left direction" << endl;
vector<Map>topLeftShifted_vec = topLeftShift(noOfSmallerPartitions, datarowSize, factorLat, factorLong);
cout << "Done." << endl;
// Load the POIs in the partitions created in the top-left shifted grid
cout << "Loading POIs for the top-left shifted grid inside the created partitions" << endl;
loadPoIs(dataRow, topLeftShifted_vec);
cout << "Successfully loaded." << endl << endl;
// Create top-right shifted grid
cout << "Shifting partitions in the top right direction" << endl;
vector<Map>topRightShifted_vec = topRightShift(noOfSmallerPartitions, datarowSize, factorLat, factorLong);
cout << "Done." << endl;
// Load the POIs in the partitions created in the top-right shifted grid
cout << "Loading POIs for the top-left shifted grid inside the created partitions" << endl;
loadPoIs(dataRow, topRightShifted_vec);
cout << "Successfully loaded." << endl << endl;
// Create bottom-left shifted grid
cout << "Shifting partitions in the bottom left direction" << endl;
vector<Map>bottomLeftShifted_vec = bottomLeftShift(noOfSmallerPartitions, datarowSize, factorLat, factorLong);
cout << "Done." << endl;
// Load the POIs in the partitions created in the bottom-left shifted grid
cout << "Loading POIs for the bottom-left shifted grid inside the created partitions" << endl;
loadPoIs(dataRow, bottomLeftShifted_vec);
cout << "Successfully loaded." << endl << endl;
// Create bottom-right shifted grid
cout << "Shifting partitions in the bottom right direction" << endl;
vector<Map>bottomRightShifted_vec = bottomRightShift(noOfSmallerPartitions, datarowSize, factorLat, factorLong);
cout << "Done." << endl;
// Load the POIs in the partitions created in the bottom-right shifted grid
cout << "Loading POIs for the bottom-right shifted grid inside the created partitions" << endl;
loadPoIs(dataRow, bottomRightShifted_vec);
cout << "Successfully loaded." << endl << endl;
// Preprocessing for the brute_force
unpartitioned = get_brute_force_partition(dataRow);
//cout << unpartitioned.utility[0].utilities << endl;
// Enter the query
cout << "Preprocessing complete. Enter the user latitude and longitude. Should be within (40.9883317193, -74.2747664452) and (40.5508524674, -73.6838251982)" << endl;
cin >> userLocation.latitude;
cin >> userLocation.longitude;
//userLocation.latitude = getRandom(TOP_LEFT_LAT - LATITUDE_RANGE, TOP_LEFT_LAT);
//userLocation.longitude = getRandom(TOP_LEFT_LONG, TOP_LEFT_LONG + LONGITUDE_RANGE);
cout << "User coordinates: ";
cout << setprecision(10) << userLocation.latitude << ", " << setprecision(10) << userLocation.longitude << endl;;
cout << "Enter the POI category. (Airport, Bank, Park etc. Check dataset file for details)" << endl;
cin.ignore();
getline(cin, POICategory);
cout << "Enter the no. of POIs wanted." << endl;
cin >> k;
t1 = clock();
brute_force(userLocation, POICategory, unpartitioned, k);
t1 = clock() - t1;
time_taken1 = ((double)t1) / CLOCKS_PER_SEC;
cout << "Time taken by brute force: " << time_taken1<< endl;
this_thread::sleep_for (chrono::seconds(2));
t2 = clock();
find_K_NearestPOIs(userLocation,initialPartition_vec, topLeftShifted_vec, topRightShifted_vec, bottomLeftShifted_vec, bottomRightShifted_vec, k, POICategory, totalPartitionInRow);
t2 = clock() - t2;
time_taken2 = ((double)t2) / CLOCKS_PER_SEC;
cout << "Time taken by the entire recommendation algorithm: " << time_taken2 << endl;
this_thread::sleep_for (chrono::seconds(2));
t3 = clock();
originalGrid_recommendation(userLocation, POICategory, initialPartition_vec, k);
t3 = clock() - t3;
time_taken3 = ((double)t3) / CLOCKS_PER_SEC;
cout << "Time taken by the initial partition: " << time_taken3 << endl;
this_thread::sleep_for (chrono::seconds(2));
return 0;
}