[英]What is the best nearest neighbor algorithm for my case?
CGAL 有一个二维点库,用于基于 Delaunay 三角剖分数据结构的最近邻和范围搜索。
以下是针对您的用例的库的基准测试:
// file: cgal_benchmark_2dnn.cpp
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Point_set_2.h>
#include <chrono>
#include <list>
#include <random>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Point_set_2<K>::Vertex_handle Vertex_handle;
typedef K::Point_2 Point_2;
/**
* @brief Time a lambda function.
*
* @param lambda - the function to execute and time
*
* @return the number of microseconds elapsed while executing lambda
*/
template <typename Lambda>
std::chrono::microseconds time_lambda(Lambda lambda) {
auto start_time = std::chrono::high_resolution_clock::now();
lambda();
auto end_time = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(end_time -
start_time);
}
int main() {
const int num_index_points = 15000;
const int num_trials = 1000000;
std::random_device
rd; // Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
std::uniform_real_distribution<> dis(-1, 1.);
std::list<Point_2> index_point_list;
{
auto elapsed_microseconds = time_lambda([&] {
for (int i = 0; i < num_index_points; ++i) {
index_point_list.emplace_back(dis(gen), dis(gen));
}
});
std::cout << " Generating " << num_index_points << " random points took "
<< elapsed_microseconds.count() << " microseconds.\n";
}
CGAL::Point_set_2<K> point_set;
{
auto elapsed_microseconds = time_lambda([&] {
point_set.insert(index_point_list.begin(), index_point_list.end());
});
std::cout << " Building point set took " << elapsed_microseconds.count()
<< " microseconds.\n";
}
{
auto elapsed_microseconds = time_lambda([&] {
for (int j = 0; j < num_trials; ++j) {
Point_2 query_point(dis(gen), dis(gen));
Vertex_handle v = point_set.nearest_neighbor(query_point);
}
});
auto rate = elapsed_microseconds.count() / static_cast<double>(num_trials);
std::cout << " Querying " << num_trials << " random points took "
<< elapsed_microseconds.count()
<< " microseconds.\n >> Microseconds / query :" << rate << "\n";
}
}
在我的系统(Ubuntu 18.04)上,这可以用
g++ cgal_benchmark_2dnn.cpp -lCGAL -lgmp -O3
当运行产生性能时:
Generating 15000 random points took 1131 microseconds.
Building point set took 11469 microseconds.
Querying 1000000 random points took 2971201 microseconds.
>> Microseconds / query :2.9712
这是相当快的。 请注意,使用 N 个处理器,您可以将速度提高大约 N 倍。
如果以下两项或多项为真:
然后缓存一切! 您可以在索引点的边界框上预先计算所需精度的网格。 将每个网格单元映射到一个唯一的地址,该地址可以在知道查询点的 2D 坐标的情况下进行索引。
然后只需使用任何最近邻算法(例如我提供的算法)将每个网格单元映射到最近的索引点。 请注意,此步骤只需执行一次即可初始化网格内的网格单元。
要运行查询,这将需要一个 2D 坐标到网格单元坐标计算,然后是一次内存访问,这意味着您不能真正希望更快的方法(每个查询可能需要 2-3 个 CPU 周期。)
我怀疑(有一些洞察力)这就是像谷歌或 Facebook 这样的大公司会如何解决这个问题(因为 #3 对他们来说甚至对整个世界都不是问题。)即使是较小的非营利组织也使用这样的计划(比如NASA。)尽管如此,NASA 使用的方案要复杂得多,具有多种分辨率/精度。
从下面的评论中,很明显最后一部分没有被很好地理解,所以我将包含更多细节。
假设您的一组点由两个向量x
和y
给出,它们包含数据的 x & y 坐标(或 lat & long 或您使用的任何东西。)
然后用尺寸width = max(x)-min(x)
& height=max(y)-min(y)
定义数据的边界框。 现在使用一组测试点 (x_t,y_t) 的映射创建一个精细网格,以使用 NxM 个点来表示整个边界框
u(x_t) = round((x_t - min(x)) / double(width) * N)
v(y_t) = round((y_t - min(y)) / double(height) * M)
然后简单地使用indices = grid[u(x_t),v(y_t)]
,其中indices
是最近索引指向[x_t,y_t]
索引, grid
是一个预先计算的查找表,它将网格中的每个项目映射到最近的索引点[x,y]
。
例如,假设您的索引点是[0,0]
和[2,2]
( [2,2]
顺序)。您可以将网格创建为
grid[0,0] = 0
grid[0,1] = 0
grid[0,2] = 0 // this is a tie
grid[1,0] = 0
grid[1,1] = 0 // this is a tie
grid[1,2] = 1
grid[2,0] = 1 // this is a tie
grid[2,1] = 1
grid[2,2] = 1
上面的右手边是索引0
(映射到点[0,0]
)或1
(映射到点[2,2]
)。 注意:由于这种方法的离散性质,您将有一个点的距离与到另一个索引点的距离完全相等的关系,您将不得不想出一些方法来确定如何打破这些关系。 请注意, grid
的条目数决定了您要达到的精确度。 显然,在我上面给出的例子中,精度很糟糕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.