C++ 使用Boost查询点
在使用boost查询积分时,我似乎得到了不正确的结果。使用单独的算法(使用BSTs),我每个查询平均得到2000分的答案,但使用boost我得到10分的答案。这是我第一次使用Boost,所以有人能帮我解决我的问题吗 关于代码:我放置了1M个随机点(x,y在0和1之间)。然后,我对100个小的随机区域进行查询,并计算匹配数C++ 使用Boost查询点,c++,boost,boost-geometry,C++,Boost,Boost Geometry,在使用boost查询积分时,我似乎得到了不正确的结果。使用单独的算法(使用BSTs),我每个查询平均得到2000分的答案,但使用boost我得到10分的答案。这是我第一次使用Boost,所以有人能帮我解决我的问题吗 关于代码:我放置了1M个随机点(x,y在0和1之间)。然后,我对100个小的随机区域进行查询,并计算匹配数 #include <iostream> #include <vector> #include <time.h> #include <m
#include <iostream>
#include <vector>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/index/rtree.hpp>
using namespace std;
namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
typedef bg::model::point<double, 2, bg::cs::cartesian> point;
typedef bg::model::box<point> box;
struct particle
{
double x;
double y;
};
int main(int argc, char *argv[])
{
int N=1000000;
clock_t start,stop;
vector<particle> myvec(N);
vector<particle>::iterator cii;
//Set vector values
for (cii = myvec.begin(); cii != myvec.end(); ++cii)
{
cii->x =1.0*rand()/RAND_MAX;
cii->y =1.0*rand()/RAND_MAX;
}
//Build R-tree
start=clock();
bgi::rtree<point, bgi::quadratic<16> > rtree;
for (cii=myvec.begin();cii!=myvec.end(); ++cii)
{
double x = cii->x;
double y = cii->y;
point p(x,y);
rtree.insert(p);
}
stop=clock();
cout<<"Time for building R tree "<<(stop-start)/(double) CLOCKS_PER_SEC<<endl;
//Build Query List
vector<particle> querylist(100);
for (cii = querylist.begin(); cii != querylist.end(); ++cii)
{
cii->x =1.0*rand()/RAND_MAX;
cii->y =1.0*rand()/RAND_MAX;
}
//Query R-tree
start=clock();
for (cii = querylist.begin(); cii != querylist.end(); ++cii)
{
double x = cii->x;
double y = cii->y;
double lx = x - .001;
double ux = x + .001;
double ly = y - .001;
double uy = y + .001;
point p1(lx,ly), p2(ux,uy);
box query_box(p1, p2);
vector<point> queryresult;
rtree.query(bgi::intersects(query_box), std::back_inserter(queryresult));
std::cout << "The intersection has " << (queryresult.size()) << " elements:\n";
}
stop=clock();
cout<<"Time for query "<<(stop-start)/(double) CLOCKS_PER_SEC<<endl;
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
名称空间bg=boost::geometry;
名称空间bgi=boost::geometry::index;
typedef bg::model::point;
typedef bg::model::box;
结构粒子
{
双x;
双y;
};
int main(int argc,char*argv[])
{
int N=1000000;
时钟没有启动,停止;
载体myvec(N);
向量::迭代器cii;
//设置向量值
for(cii=myvec.begin();cii!=myvec.end();++cii)
{
cii->x=1.0*rand()/rand_MAX;
cii->y=1.0*rand()/rand_MAX;
}
//构建R-树
开始=时钟();
bgi::rtree-rtree;
for(cii=myvec.begin();cii!=myvec.end();++cii)
{
双x=cii->x;
双y=cii->y;
点p(x,y);
rtree.插入(p);
}
停止=时钟();
cout您的样本在统计上不可靠
让我们将查询框大大放大:
double const delta = .05; // so boxes are .1x.1, i.e. 1/100th of total area
// and later
for (auto& p : querylist) {
box query_box(point {p.x - delta, p.y - delta}, point {p.x + delta, p.y + delta});
结果正好达到预期的点击数:
Time for building R tree 2182054657 nanoseconds
Time for query 52749607 nanoseconds
Average intersections: 10000.3
Standard deviation: 98.9173
Expected mean: 10000
代码
注释
- 将
rand()
替换为Boost Random(uniform\u real
distribution)
- 修复了框与区域边界不重叠的问题(因此我们不会得到扭曲的结果)
- 不必每次填充向量,只需测量查询结果的数量:
auto n = std::distance(
bgi::qbegin(rtree, bgi::intersects(query_box)),
bgi::qend(rtree));
- 使用增压累加器进行适当的统计,而不是“轶事式”报告一些“交叉点”计数:
std::cout << "Average intersections: " << ba::mean(stats) << "\n";
std::cout << "Standard deviation: " << sqrt(ba::variance(stats)) << "\n";
std::cout << "Expected mean: " << (N * (2*delta) * (2*delta)) << "\n";
甚至
double const delta = .00005; // so boxes are .0001x.0001, i.e. 1/100000000th of total area
Time for building R tree 726361394 nanoseconds
Time for query 216690 nanoseconds
Average intersections: 0.02
Standard deviation: 0.14
Expected mean: 0.01
都德,请把你的代码改成C++
,因为它现在基本上是C加类的。请不要使用std::rand()
(谷歌it)。使用std::chrono
进行时间测量。这段代码会伤害我的身体。除了格式化之外,这段代码对我来说大致没什么问题。我明天会看一看,以防没有其他人使用收集统计数据的所有工具来量化性能基准。此外,修复了随机和计时。无法要求更多。
double const delta = .005; // so boxes are .01x.01, i.e. 1/10000th of total area
Time for building R tree 2101529662 nanoseconds
Time for query 2982070 nanoseconds
Average intersections: 99.69
Standard deviation: 8.96069
Expected mean: 100
double const delta = .00005; // so boxes are .0001x.0001, i.e. 1/100000000th of total area
Time for building R tree 726361394 nanoseconds
Time for query 216690 nanoseconds
Average intersections: 0.02
Standard deviation: 0.14
Expected mean: 0.01