-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainLSH.cpp
151 lines (124 loc) · 4.24 KB
/
mainLSH.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
#include <iostream>
#include <fstream>
#include <ctime>
#include <utility>
#include <sstream>
#include "dataStructs.hpp"
#include "fileReading.hpp"
#include "bruteForce.hpp"
#include "LSH.hpp"
using namespace std;
// $./lsh –d <input file> –q <query file> –k <int> -L <int> -ο <output file>
int main(int argc, char *argv[])
{
if (argc > 11)
{
cout << "Invalid Input!\n";
return -1;
}
string inputFile, queryFile, outputFile;
int k = 3, L = 5;
string optionBuffer, parameterBuffer;
for (int i = 1; i <= (argc - 1); i += 2)
{
optionBuffer = string(argv[i]);
parameterBuffer = string(argv[i + 1]);
if (optionBuffer == "-d")
inputFile = parameterBuffer;
else if (optionBuffer == "-q")
queryFile = parameterBuffer;
else if (optionBuffer == "-k")
k = stoi(parameterBuffer, nullptr, 10);
else if (optionBuffer == "-L")
L = stoi(parameterBuffer, nullptr, 10);
else if (optionBuffer == "-o")
outputFile = parameterBuffer;
}
int w = 4500;
class Reading reader;
int tableSize;
vector<class Point *> *inputTable, *queryTable;
pair<vector<class Point *> *, double> input = reader.readPoints(inputFile, 'i');
inputTable = input.first;
pair<vector<class Point *> *, double> queries = reader.readPoints(queryFile, 'q');
queryTable = queries.first;
double radius = queries.second;
class LSH<class Point *> lshImplementation(k, L, w, inputTable);
class Point *q, *b = NULL;
double distance, tempAF, maxAF = 0.0, avgAF = 0.0;
clock_t timeLSH, timeBrute;
pair<class Point *, double> *bruteNN;
ofstream outfile;
outfile.open(outputFile);
ifstream readBrute("brutePoints.txt");
string bruteNeighborID;
string line;
double bruteDist;
for (int i = 0; i < queryTable->size(); i++)
{
//read the output of the bruteforce file
if (getline(readBrute, line))
{
istringstream buffer(line);
buffer >> bruteNeighborID >> bruteDist >> timeBrute;
}
else
{
cout << "ERROR reading bruteforce file!" << endl;
}
////////////////////////////////
q = queryTable->at(i);
timeLSH = clock();
b = lshImplementation.findNN(q, &distance);
timeLSH = clock() - timeLSH;
tempAF = distance / bruteDist;
avgAF += tempAF;
if (tempAF > maxAF)
maxAF = tempAF;
outfile << "Query Point: " << q->getID() << endl;
if (b != NULL)
outfile << "Nearest Neighbor LSH: " << b->getID() << endl
<< "Distance LSH: " << distance << endl;
else
outfile << "Nearest Neighbor LSH: None Found!" << endl
<< "Distance LSH: -" << endl;
outfile << "True Neighbor: " << bruteNeighborID << endl
<< "DistanceTrue: " << bruteDist << endl;
outfile << "tLSH: " << (float)timeLSH / CLOCKS_PER_SEC << endl
<< "tTrue: " << (float)timeBrute / CLOCKS_PER_SEC << endl;
if (radius > 0.0)
{
vector<pair<class Point *, double> > *radiusNeighbors = lshImplementation.findRadiusNN(q, radius);
outfile << "R-nearest neighbors:" << endl;
if (radiusNeighbors != NULL)
{
for (int i = 0; i < radiusNeighbors->size(); i++)
{
outfile << "ID: " << radiusNeighbors->at(i).first->getID() << " Distance: " << radiusNeighbors->at(i).second << endl;
}
outfile << endl;
radiusNeighbors->clear();
delete (radiusNeighbors);
}
else
outfile << "None Found!" << endl
<< endl;
}
}
cout << "MaxAF: " << maxAF << endl;
cout << "AvgAF: " << avgAF / queryTable->size() << endl;
outfile.close();
//delete the tables
while (!inputTable->empty())
{
delete inputTable->back();
inputTable->pop_back();
}
delete (inputTable);
while (!queryTable->empty())
{
delete queryTable->back();
queryTable->pop_back();
}
delete (queryTable);
}