-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathz-hnsw.cpp
303 lines (251 loc) · 9.55 KB
/
z-hnsw.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
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <cmath>
#include <random>
#include <limits>
#include <algorithm>
#include <cassert>
#define POINT_COORDINATES 512
// Define the Point structure with better constructors
struct Point {
std::vector<float> coordinates;
std::string label;
// Use explicit constructors to avoid unexpected conversions
explicit Point(const std::vector<float>& coords, const std::string& lbl)
: coordinates(coords), label(lbl) {}
bool operator<(const Point& other) const {
return coordinates < other.coordinates;
}
bool operator==(const Point& other) const {
return coordinates == other.coordinates && label == other.label;
}
};
void printPoint(const Point& p) {
std::cout << p.label << " (";
size_t numCoordinates = p.coordinates.size();
for (size_t i = 0; i < std::min(numCoordinates, size_t(10)); ++i) {
std::cout << p.coordinates[i];
if (i < std::min(numCoordinates, size_t(10)) - 1) {
std::cout << ", ";
}
}
if (numCoordinates > 10) {
std::cout << ", ...";
}
std::cout << ")";
}
// Calculate the Euclidean distance between two points
float euclideanDistance(const Point& p1, const Point& p2) {
float distance = 0.0f;
for (size_t i = 0; i < std::min(p1.coordinates.size(), p2.coordinates.size()); ++i) {
distance += pow(p1.coordinates[i] - p2.coordinates[i], 2);
}
return sqrt(distance);
}
// Define the Node structure
struct Node {
Point point;
std::vector<std::vector<Node*>> neighbors;
explicit Node(const Point& pt, int maxLevel)
: point(pt), neighbors(maxLevel + 1) {}
};
// Define the Graph structure
class Graph {
public:
std::vector<Node*> nodes;
Node* entrance;
Graph() : entrance(nullptr) {}
~Graph() {
for (Node* node : nodes) {
delete node;
}
nodes.clear();
}
};
// Define the HNSW structure
class HNSW {
private:
Graph graph;
int maxLevel;
int maxNeighbors;
int efConstruction;
float levelMult;
std::default_random_engine generator;
int getRandomLevel() {
std::uniform_real_distribution<float> distribution(0.0, 1.0);
float r = distribution(generator);
return static_cast<int>(-std::log(r) * levelMult);
}
void insertNode(Node* newNode) {
if (graph.nodes.empty()) {
graph.nodes.push_back(newNode);
graph.entrance = newNode;
return;
}
Node* enterPoint = graph.entrance;
int level = maxLevel;
// Ensure layer access is within bounds
int effectiveLevel = std::min(static_cast<int>(newNode->neighbors.size()) - 1, level);
for (int lc = level; lc > effectiveLevel; --lc) {
enterPoint = searchLayer(enterPoint, newNode->point, lc, 1).top().second;
}
for (int lc = effectiveLevel; lc >= 0; --lc) {
auto topCandidates = searchLayer(enterPoint, newNode->point, lc, efConstruction);
selectNeighbors(newNode, topCandidates, lc);
}
maxLevel = std::max(maxLevel, effectiveLevel);
graph.nodes.push_back(newNode);
}
std::priority_queue<std::pair<float, Node*>> searchLayer(Node* enterPoint, const Point& point, int level, int ef) {
std::priority_queue<std::pair<float, Node*>> topCandidates;
std::priority_queue<std::pair<float, Node*>, std::vector<std::pair<float, Node*>>, std::greater<>> candidates;
std::unordered_map<Node*, bool> visited;
float lowerBound = euclideanDistance(point, enterPoint->point);
candidates.emplace(lowerBound, enterPoint);
topCandidates.emplace(lowerBound, enterPoint);
visited[enterPoint] = true;
while (!candidates.empty()) {
auto currPair = candidates.top();
float dist = currPair.first;
Node* currNode = currPair.second;
candidates.pop();
if (dist > lowerBound) break;
for (Node* neighbor : currNode->neighbors[level]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
float d = euclideanDistance(point, neighbor->point);
if (topCandidates.size() < static_cast<size_t>(ef) || d < lowerBound) {
candidates.emplace(d, neighbor);
topCandidates.emplace(d, neighbor);
if (topCandidates.size() > static_cast<size_t>(ef)) {
topCandidates.pop();
lowerBound = topCandidates.top().first;
}
}
}
}
}
return topCandidates;
}
void selectNeighbors(Node* newNode, std::priority_queue<std::pair<float, Node*>>& topCandidates, int level) {
std::vector<Node*> neighbors;
std::unordered_set<Node*> selected;
while (!topCandidates.empty() && neighbors.size() < static_cast<size_t>(maxNeighbors)) {
Node* neighbor = topCandidates.top().second;
topCandidates.pop();
if (selected.insert(neighbor).second) {
neighbors.push_back(neighbor);
}
}
for (Node* neighbor : neighbors) {
newNode->neighbors[level].push_back(neighbor);
neighbor->neighbors[level].push_back(newNode);
}
}
public:
HNSW(int maxNeighbors, int efConstr, float mult)
: maxLevel(0), maxNeighbors(maxNeighbors), efConstruction(efConstr), levelMult(mult) {}
void insert(const Point& point) {
Node* newNode = new Node(point, getRandomLevel());
insertNode(newNode);
}
std::vector<Point> search(const Point& query, int k) {
if (graph.nodes.empty()) return {};
Node* enterPoint = graph.entrance;
int level = maxLevel;
while (level > 0) {
auto searchResults = searchLayer(enterPoint, query, level, 1);
if (!searchResults.empty()) {
enterPoint = searchResults.top().second;
}
--level;
}
auto topCandidates = searchLayer(enterPoint, query, 0, k);
std::vector<Point> results;
while (!topCandidates.empty()) {
results.push_back(topCandidates.top().second->point);
topCandidates.pop();
}
return results;
}
};
bool verifyNearestNeighbors(const Point& queryPoint, std::vector<Point>& results, const std::vector<Point>& allPoints, size_t k) {
// Create a priority queue to find the k nearest neighbors in the original data
std::priority_queue<std::pair<float, Point>> pq;
for (const Point& point : allPoints) {
float distance = euclideanDistance(queryPoint, point);
pq.push(std::make_pair(distance, point));
if (pq.size() > k) {
pq.pop();
}
}
// Extract the k nearest neighbors from the priority queue
std::vector<std::pair<float, Point>> expectedResults;
while (!pq.empty()) {
expectedResults.push_back(pq.top());
pq.pop();
}
// Sort the expected results and the actual results by distance to compare them
auto distanceComparator = [](const std::pair<float, Point>& a, const std::pair<float, Point>& b) {
return a.first < b.first;
};
std::sort(expectedResults.begin(), expectedResults.end(), distanceComparator);
std::vector<std::pair<float, Point>> actualResults;
for (const Point& point : results) {
actualResults.push_back(std::make_pair(euclideanDistance(queryPoint, point), point));
}
std::sort(actualResults.begin(), actualResults.end(), distanceComparator);
// Verify that the actual results match the expected results
bool isCorrect = (expectedResults == actualResults);
std::cout << "Optimal neighbors:\n";
for (const auto& pair : expectedResults) {
const Point& point = pair.second;
printPoint(point);
std::cout << " Distance: " << pair.first << "\n";
}
std::cout << "HNSW neighbors:\n";
for (const auto& pair : actualResults) {
const Point& point = pair.second;
printPoint(point);
std::cout << " Distance: " << pair.first << "\n";
}
return isCorrect;
}
int main() {
HNSW hnsw(4, 200, 1.0f);
std::mt19937 gen(42);
std::uniform_real_distribution<float> dis(0.0, 1.0);
std::vector<Point> points;
for (int cluster = 0; cluster < 10; ++cluster) {
std::vector<float> center(POINT_COORDINATES);
for (float &val : center) {
val = dis(gen) * 100;
}
for (int i = 0; i < 10; ++i) {
std::vector<float> coordinates(POINT_COORDINATES);
for (int j = 0; j < POINT_COORDINATES; ++j) {
coordinates[j] = center[j] + dis(gen) * 10;
}
points.emplace_back(coordinates, "Point_" + std::to_string(cluster * 10 + i));
}
}
for (const Point& point : points) {
hnsw.insert(point);
}
const int pointID = 66; // Selecting a specific point for consistent results
Point queryPoint = points[pointID];
int k = 3; // Number of nearest neighbors to find
std::vector<Point> results = hnsw.search(queryPoint, k);
std::cout << "The " << k << " nearest neighbors to (" << queryPoint.label << "):" << std::endl;
std::cout << "\n";
for (const Point& result : results) {
printPoint(result);
std::cout << "\n";
}
// Verify the nearest neighbors
verifyNearestNeighbors(queryPoint, results, points, k);
return 0;
}