-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBruteForce.hpp
More file actions
138 lines (116 loc) · 4.26 KB
/
BruteForce.hpp
File metadata and controls
138 lines (116 loc) · 4.26 KB
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
#include <utility>
#include <limits>
#include <ctime>
#include <cmath>
#include "../core/item.hpp"
#include "../metrics/metrics.hpp"
#include "../utils/search.hpp"
#include "../LSH/LSHFun.hpp"
/*
The following class implements a simple brute force implementation of kNN
*/
template <typename T>
class BruteForce {
private:
// Number of items in training set
int imageCount;
// Data dimension
int dimension;
// Array of item pointers
Item<T>** items;
public:
BruteForce (interface::Data<T>& ds) {
// Initialize data
imageCount = ds.n;
dimension = ds.dimension;
items = ds.items;
}
~BruteForce () {
}
/*
Each neighbor is represented as a pair of <distanceToQuery, neighborItem*>
The following function returns a vector of these pairs
*/
std::vector<std::pair<int, Item<T>*>> kNN (T* query, int N) {
// At first initalize the vector itself
std::vector<std::pair<int, Item<T>*>> d;
// Then initialize each pair with distance -> (max integer) and a null item
for (int i = 0; i < N; i++)
d.push_back(std::make_pair(std::numeric_limits<int>::max(), new Item<T>()));
// For each item...
for (int i = 0; i < imageCount; i++) {
// Calculate its distance from the query item
int distance = metrics::ManhattanDistance<T>(query, items[i]->data, dimension);
/*
If the distance is less than the last pair's in the vector,
replace the pair with the new distance and the current item.
Then, sort the vector by ascending order based on distance.
This is done so that whenever we find a good neighbor candidate,
we replace the least similar neighbor in the vector
*/
if (distance < d[N-1].first) {
d[N-1].first = distance;
if (d[N-1].second->null)
delete d[N-1].second;
d[N-1].second = items[i];
std::sort(d.begin(), d.end(), utils::comparePairs<T>);
}
}
return d;
}
/*
The following function creates a SearchOutput object.
This object contains all information required to create the output file.
This is used only on ANN (not clustering) and also requires that its LSH or Hypercube
counterpart will be executed (so as to compare times and distances).
*/
void buildOutput (interface::output::SearchOutput& output, interface::Dataset<T>& query, bool original) {
// A vector containing the ids of all query items
std::vector<int> queryIdVec;
// A vector containing the nearest neighbor ids of all query items
std::vector<int> nnIdVec;
// The true distances of the nearest neighbor of each query item
std::vector<double> timeVec;
double total_time = 0.0;
// For each query item...
for (int i = 0; i < query.number_of_images; i++) {
// Execute kNN and calculate time elapsed
clock_t begin = clock();
std::vector<std::pair<int, Item<T>*>> kNNRes = kNN(query.images[i], 1);
clock_t end = clock();
double elapsed = double(end - begin) / CLOCKS_PER_SEC;
total_time += elapsed;
// Push the relevant data to the vectors
queryIdVec.push_back(i);
nnIdVec.push_back( (int) kNNRes[0].second->id);
timeVec.push_back(elapsed);
if ((i+1)%1000 == 0)
std::cout << "BF: " << i+1 << " query items..." << '\n';
}
// Set the following SearchOutput's attributes to the vectors created above
if (original){
output.query_id = queryIdVec;
output.true_neighbors_id = nnIdVec;
output.true_time = timeVec;
output.true_total_time = total_time;
}else{
output.reduced_neighbors_id = nnIdVec;
output.reduced_time = timeVec;
output.reduced_total_time = total_time;
}
}
/*
Function which executes kNN for all query items provided and returns the combined results
as a vector. Used in the manhattan metric evaluation program only.
*/
std::vector<std::vector<std::pair<int, Item<T>*>>> getNeighbors (interface::Dataset<T>& query, int N, int metric){
std::vector<std::vector<std::pair<int, Item<T>*>>> neighbors;
for (int i = 0; i < query.number_of_images; i++){
std::vector<std::pair<int, Item<T>*>> kNNRes = kNN(query.images[i], N);
neighbors.push_back(kNNRes);
if ((i+1)%1000 == 0)
std::cout << "BF: " << i+1 << " query items..." << '\n';
}
return neighbors;
}
};