Single tree methods are running
This commit is contained in:
@@ -210,6 +210,10 @@ void TREE__::NearestNeighbor(NodePtr_t ptr,
|
||||
NEIGHBORTYPE range,
|
||||
bool &found) {
|
||||
computations_.UpdateComparisons();
|
||||
Precision_t max_distance;
|
||||
if (Loki::TypeTraits<NEIGHBORTYPE>::isStdFloat==true) {
|
||||
max_distance=range;
|
||||
}
|
||||
if (!ptr->IsLeaf()){
|
||||
computations_.UpdateComparisons();
|
||||
pair<NodePtr_t, NodePtr_t> child_pair =
|
||||
@@ -217,9 +221,13 @@ void TREE__::NearestNeighbor(NodePtr_t ptr,
|
||||
|
||||
NearestNeighbor(child_pair.first, test_point, nearest_point,
|
||||
range, found);
|
||||
if (Loki::TypeTraits<NEIGHBORTYPE>::isStdFloat==false) {
|
||||
max_distance=nearest_point->back().first;
|
||||
}
|
||||
|
||||
if (child_pair.second->get_box().CrossesBoundaries(test_point,
|
||||
dimension_,
|
||||
nearest_point->end()->first,
|
||||
max_distance,
|
||||
computations_)) {
|
||||
NearestNeighbor(child_pair.second,
|
||||
test_point,
|
||||
@@ -230,8 +238,12 @@ void TREE__::NearestNeighbor(NodePtr_t ptr,
|
||||
if (found == true) {
|
||||
return;
|
||||
} else {
|
||||
if (Loki::TypeTraits<NEIGHBORTYPE>::isStdFloat==false) {
|
||||
max_distance=nearest_point->back().first;
|
||||
}
|
||||
found = ptr->get_box().IsWithin(test_point,
|
||||
dimension_, nearest_point->end()->first,
|
||||
dimension_,
|
||||
max_distance,
|
||||
computations_)==0;
|
||||
if (found == true) {
|
||||
return;
|
||||
@@ -242,8 +254,11 @@ void TREE__::NearestNeighbor(NodePtr_t ptr,
|
||||
range, dimension_,
|
||||
discriminator_,
|
||||
computations_);
|
||||
if (Loki::TypeTraits<NEIGHBORTYPE>::isStdFloat==false) {
|
||||
max_distance=nearest_point->back().first;
|
||||
}
|
||||
found = ptr->get_box().IsWithin(test_point, dimension_,
|
||||
nearest_point->end()->first,
|
||||
max_distance,
|
||||
computations_);
|
||||
}
|
||||
}
|
||||
@@ -373,7 +388,7 @@ void TREE__::InitAllKNearestNeighborOutput(string file,
|
||||
fwrite(buffer, sizeof(typename Node_t::NNResult),
|
||||
(num_of_points_%kChunk)*knns, fp );
|
||||
fclose(fp);
|
||||
delete buffer;
|
||||
delete []buffer;
|
||||
int fd=open(file.c_str(), O_RDWR);
|
||||
typename Node_t::NNResult *ptr =(typename Node_t::NNResult *)mmap(NULL,
|
||||
sizeof(typename Node_t::NNResult)*knns*num_of_points_,
|
||||
|
||||
@@ -21,6 +21,7 @@ void TREE__::Init(BinaryDataset<Precision_t> *data) {
|
||||
min_depth_ = numeric_limits<index_t>::max();
|
||||
max_points_on_leaf_ = 30;
|
||||
log_progress_=true;
|
||||
pivoter_.Init(data_);
|
||||
}
|
||||
|
||||
TEMPLATE__
|
||||
@@ -209,6 +210,10 @@ void TREE__::NearestNeighbor(NodePtr_t ptr,
|
||||
NEIGHBORTYPE range,
|
||||
bool &found) {
|
||||
computations_.UpdateComparisons();
|
||||
Precision_t max_distance;
|
||||
if (Loki::TypeTraits<NEIGHBORTYPE>::isStdFloat==true) {
|
||||
max_distance=range;
|
||||
}
|
||||
if (!ptr->IsLeaf()){
|
||||
computations_.UpdateComparisons();
|
||||
pair<NodePtr_t, NodePtr_t> child_pair =
|
||||
@@ -216,9 +221,13 @@ void TREE__::NearestNeighbor(NodePtr_t ptr,
|
||||
|
||||
NearestNeighbor(child_pair.first, test_point, nearest_point,
|
||||
range, found);
|
||||
if (Loki::TypeTraits<NEIGHBORTYPE>::isStdFloat==false) {
|
||||
max_distance=nearest_point->back().first;
|
||||
}
|
||||
|
||||
if (child_pair.second->get_box().CrossesBoundaries(test_point,
|
||||
dimension_,
|
||||
nearest_point->end()->first,
|
||||
max_distance,
|
||||
computations_)) {
|
||||
NearestNeighbor(child_pair.second,
|
||||
test_point,
|
||||
@@ -229,8 +238,12 @@ void TREE__::NearestNeighbor(NodePtr_t ptr,
|
||||
if (found == true) {
|
||||
return;
|
||||
} else {
|
||||
if (Loki::TypeTraits<NEIGHBORTYPE>::isStdFloat==false) {
|
||||
max_distance=nearest_point->back().first;
|
||||
}
|
||||
found = ptr->get_box().IsWithin(test_point,
|
||||
dimension_, nearest_point->end()->first,
|
||||
dimension_,
|
||||
max_distance,
|
||||
computations_)==0;
|
||||
if (found == true) {
|
||||
return;
|
||||
@@ -241,8 +254,11 @@ void TREE__::NearestNeighbor(NodePtr_t ptr,
|
||||
range, dimension_,
|
||||
discriminator_,
|
||||
computations_);
|
||||
if (Loki::TypeTraits<NEIGHBORTYPE>::isStdFloat==false) {
|
||||
max_distance=nearest_point->back().first;
|
||||
}
|
||||
found = ptr->get_box().IsWithin(test_point, dimension_,
|
||||
nearest_point->end()->first,
|
||||
max_distance,
|
||||
computations_);
|
||||
}
|
||||
}
|
||||
@@ -372,7 +388,7 @@ void TREE__::InitAllKNearestNeighborOutput(string file,
|
||||
fwrite(buffer, sizeof(typename Node_t::NNResult),
|
||||
(num_of_points_%kChunk)*knns, fp );
|
||||
fclose(fp);
|
||||
delete buffer;
|
||||
delete buffer[];
|
||||
int fd=open(file.c_str(), O_RDWR);
|
||||
typename Node_t::NNResult *ptr =(typename Node_t::NNResult *)mmap(NULL,
|
||||
sizeof(typename Node_t::NNResult)*knns*num_of_points_,
|
||||
|
||||
@@ -76,22 +76,24 @@ class BinaryTreeTest {
|
||||
void BuildDepthFirst(){
|
||||
printf("Testing BuildDepthFirst...\n");
|
||||
tree_.BuildDepthFirst();
|
||||
tree_.Print();
|
||||
//tree_.Print();
|
||||
printf("%s\n", tree_.Statistics().c_str());
|
||||
}
|
||||
void BuildBreadthFirst() {
|
||||
printf("Testing BuildBreadthFirst...\n");
|
||||
tree_.BuildBreadthFirst();
|
||||
tree_.Print();
|
||||
// tree_.Print();
|
||||
printf("%s\n", tree_.Statistics().c_str());
|
||||
}
|
||||
void kNearestNeighbor() {
|
||||
printf("Testing kNearestNeighbor...\n");
|
||||
tree_.BuildDepthFirst();
|
||||
tree_.Print();
|
||||
vector<pair<Precision_t, Point_t> > nearest_tree;
|
||||
pair<Precision_t, index_t> nearest_naive[num_of_points_];
|
||||
for(index_t i=0; i<num_of_points_; i++) {
|
||||
tree_.NearestNeighbor(data_.get_point(i),
|
||||
nearest_tree.clear();
|
||||
tree_.NearestNeighbor(data_.get_point(i),
|
||||
&nearest_tree,
|
||||
knns_);
|
||||
Naive(i, nearest_naive);
|
||||
@@ -110,14 +112,17 @@ class BinaryTreeTest {
|
||||
vector<pair<Precision_t, Point_t> > nearest_tree;
|
||||
pair<Precision_t, index_t> nearest_naive[num_of_points_];
|
||||
for(index_t i=0; i<num_of_points_; i++) {
|
||||
tree_.NearestNeighbor(data_.get_point(i),
|
||||
nearest_tree.clear();
|
||||
tree_.NearestNeighbor(data_.get_point(i),
|
||||
&nearest_tree,
|
||||
range_);
|
||||
std::sort(nearest_tree.begin(), nearest_tree.end(),
|
||||
typename Node_t::PairComparator());
|
||||
Naive(i, nearest_naive);
|
||||
for(index_t j=0; j<(index_t)nearest_tree.size(); j++) {
|
||||
TEST_DOUBLE_APPROX(nearest_naive[j+1].first,
|
||||
nearest_tree[j].first,
|
||||
numeric_limits<Precision_t>::epsilon());
|
||||
nearest_tree[j].first,
|
||||
numeric_limits<Precision_t>::epsilon());
|
||||
TEST_ASSERT(nearest_tree[j].second.get_id()==
|
||||
nearest_naive[j+1].second) ;
|
||||
}
|
||||
@@ -131,11 +136,11 @@ class BinaryTreeTest {
|
||||
tree_.AllNearestNeighbors(tree_.parent_, knns_);
|
||||
tree_.CloseAllKNearestNeighborOutput(knns_);
|
||||
struct stat info;
|
||||
if (stat(data_file_.c_str(), &info)!=0) {
|
||||
if (stat(result_file_.c_str(), &info)!=0) {
|
||||
FATAL("Error %s file %s\n",
|
||||
strerror(errno), data_file_.c_str());
|
||||
}
|
||||
uint64 map_size = info.st_size-sizeof(int32);
|
||||
uint64 map_size = info.st_size;
|
||||
|
||||
int fp=open(result_file_.c_str(), O_RDWR);
|
||||
typename Node_t::NNResult *res;
|
||||
@@ -232,18 +237,14 @@ class BinaryTreeTest {
|
||||
|
||||
void Naive(index_t query,
|
||||
pair<Precision_t, index_t> *result) {
|
||||
|
||||
for(index_t i=0; i<num_of_points_; i++) {
|
||||
if (unlikely(data_.get_id(i)==data_.get_id(query))) {
|
||||
continue;
|
||||
}
|
||||
Precision_t dist=Metric_t::Distance(data_.At(i),
|
||||
data_.At(query),
|
||||
dimension_);
|
||||
result[i].first=dist;
|
||||
result[i].second=i;
|
||||
result[i].second=data_.get_id(i);
|
||||
}
|
||||
sort(result, result+num_of_points_);
|
||||
std::sort(result, result+num_of_points_);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -76,21 +76,24 @@ class BinaryTreeTest {
|
||||
void BuildDepthFirst(){
|
||||
printf("Testing BuildDepthFirst...\n");
|
||||
tree_.BuildDepthFirst();
|
||||
tree_.Print();
|
||||
//tree_.Print();
|
||||
printf("%s\n", tree_.Statistics().c_str());
|
||||
}
|
||||
void BuildBreadthFirst() {
|
||||
printf("Testing BuildBreadthFirst...\n");
|
||||
tree_.BuildBreadthFirst();
|
||||
// tree_.Print();
|
||||
printf("%s\n", tree_.Statistics().c_str());
|
||||
}
|
||||
void kNearestNeighbor() {
|
||||
printf("Testing kNearestNeighbor...\n");
|
||||
tree_.BuildDepthFirst();
|
||||
tree_.Print();
|
||||
vector<pair<Precision_t, Point_t> > nearest_tree;
|
||||
pair<Precision_t, index_t> nearest_naive[num_of_points_];
|
||||
for(index_t i=0; i<num_of_points_; i++) {
|
||||
tree_.NearestNeighbor(data_.get_point(i),
|
||||
nearest_tree.clear();
|
||||
tree_.NearestNeighbor(data_.get_point(i),
|
||||
&nearest_tree,
|
||||
knns_);
|
||||
Naive(i, nearest_naive);
|
||||
@@ -109,14 +112,17 @@ class BinaryTreeTest {
|
||||
vector<pair<Precision_t, Point_t> > nearest_tree;
|
||||
pair<Precision_t, index_t> nearest_naive[num_of_points_];
|
||||
for(index_t i=0; i<num_of_points_; i++) {
|
||||
tree_.NearestNeighbor(data_.get_point(i),
|
||||
nearest_tree.clear();
|
||||
tree_.NearestNeighbor(data_.get_point(i),
|
||||
&nearest_tree,
|
||||
range_);
|
||||
std::sort(nearest_tree.begin(), nearest_tree.end(),
|
||||
typename Node_t::PairComparator());
|
||||
Naive(i, nearest_naive);
|
||||
for(index_t j=0; j<(index_t)nearest_tree.size(); j++) {
|
||||
TEST_DOUBLE_APPROX(nearest_naive[j+1].first,
|
||||
nearest_tree[j].first,
|
||||
numeric_limits<Precision_t>::epsilon());
|
||||
nearest_tree[j].first,
|
||||
numeric_limits<Precision_t>::epsilon());
|
||||
TEST_ASSERT(nearest_tree[j].second.get_id()==
|
||||
nearest_naive[j+1].second) ;
|
||||
}
|
||||
@@ -130,7 +136,7 @@ class BinaryTreeTest {
|
||||
tree_.AllNearestNeighbors(tree_.parent_, knns_);
|
||||
tree_.CloseAllKNearestNeighborOutput(knns_);
|
||||
struct stat info;
|
||||
if (stat(data_file_.c_str(), &info)!=0) {
|
||||
if (stat(result_file_.c_str(), &info)!=0) {
|
||||
FATAL("Error %s file %s\n",
|
||||
strerror(errno), data_file_.c_str());
|
||||
}
|
||||
@@ -231,18 +237,14 @@ class BinaryTreeTest {
|
||||
|
||||
void Naive(index_t query,
|
||||
pair<Precision_t, index_t> *result) {
|
||||
|
||||
for(index_t i=0; i<num_of_points_; i++) {
|
||||
if (unlikely(data_.get_id(i)==data_.get_id(query))) {
|
||||
continue;
|
||||
}
|
||||
Precision_t dist=Metric_t::Distance(data_.At(i),
|
||||
data_.At(query),
|
||||
dimension_);
|
||||
result[i].first=dist;
|
||||
result[i].second=i;
|
||||
result[i].second=data_.get_id(i);
|
||||
}
|
||||
sort(result, result+num_of_points_);
|
||||
std::sort(result, result+num_of_points_);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -45,6 +45,13 @@ class Node {
|
||||
Point_t nearest_;
|
||||
Precision_t distance_;
|
||||
};
|
||||
class PairComparator {
|
||||
public:
|
||||
bool operator()(const pair<Precision_t, Point_t> &a,
|
||||
const pair<Precision_t, Point_t> &b) {
|
||||
return a.first<b.first;
|
||||
}
|
||||
};
|
||||
Node();
|
||||
// Use this for node
|
||||
void Init(const BoundingBox_t &box,
|
||||
@@ -160,13 +167,7 @@ class Node {
|
||||
FILE *range_nn_fp_;
|
||||
};
|
||||
Precision_t min_dist_so_far_;
|
||||
class PairComparator {
|
||||
public:
|
||||
bool operator()(const pair<Precision_t, Point_t> &a,
|
||||
const pair<Precision_t, Point_t> &b) {
|
||||
return a.first<b.first;
|
||||
}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#include "node_impl.h"
|
||||
|
||||
@@ -144,9 +144,8 @@ class Node {
|
||||
void set_min_dist_so_far(Precision_t distance) {
|
||||
min_dist_so_far_=distance;
|
||||
}
|
||||
string Print(int32 dimension) {
|
||||
|
||||
}
|
||||
string Print(int32 dimension);
|
||||
|
||||
private:
|
||||
BoundingBox_t box_;
|
||||
index_t node_id_;
|
||||
@@ -163,7 +162,7 @@ class Node {
|
||||
Precision_t min_dist_so_far_;
|
||||
class PairComparator {
|
||||
public:
|
||||
bool operator()(const pair<Precision_t, Point_t> &a,
|
||||
bool operator()(const pair<Precision_t, Point_t> &a,
|
||||
const pair<Precision_t, Point_t> &b) {
|
||||
return a.first<b.first;
|
||||
}
|
||||
|
||||
@@ -140,13 +140,23 @@ inline void NODE__::FindNearest(POINTTYPE query_point,
|
||||
// for k-nearest neighbors
|
||||
if (Loki::TypeTraits<NEIGHBORTYPE>::isStdFloat==false) {
|
||||
typename std::vector<pair<Precision_t, Point_t> >::iterator it;
|
||||
it=nearest.begin()+(index_t)range;
|
||||
std::partial_sort(nearest.begin(),
|
||||
it,
|
||||
nearest.end(),
|
||||
PairComparator());
|
||||
if (nearest.size()>(uint32)range) {
|
||||
//it=nearest.begin()+(index_t)range;
|
||||
index_t j=0;
|
||||
for(it=nearest.begin(), j=0; j<(index_t)range; j++) {
|
||||
it++;
|
||||
}
|
||||
std::sort(nearest.begin(),
|
||||
nearest.end(),
|
||||
PairComparator());
|
||||
if (likely(nearest.size()>(uint32)range)) {
|
||||
nearest.erase(it, nearest.end());
|
||||
} else {
|
||||
pair<Precision_t, Point_t> dummy;
|
||||
dummy.first=numeric_limits<Precision_t>::max();
|
||||
index_t extra_size=(index_t)(range-nearest.size());
|
||||
for(index_t i=0; i<extra_size; i++) {
|
||||
nearest.push_back(dummy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,13 +140,23 @@ inline void NODE__::FindNearest(POINTTYPE query_point,
|
||||
// for k-nearest neighbors
|
||||
if (Loki::TypeTraits<NEIGHBORTYPE>::isStdFloat==false) {
|
||||
typename std::vector<pair<Precision_t, Point_t> >::iterator it;
|
||||
it=nearest.begin()+(index_t)range;
|
||||
std::partial_sort(nearest.begin(),
|
||||
it,
|
||||
nearest.end(),
|
||||
PairComparator());
|
||||
if (nearest.size()>(uint32)range) {
|
||||
//it=nearest.begin()+(index_t)range;
|
||||
index_t j=0;
|
||||
for(it=nearest.begin(), j=0; j<range; j++) {
|
||||
it++;
|
||||
}
|
||||
std::sort(nearest.begin(),
|
||||
nearest.end(),
|
||||
PairComparator());
|
||||
if (likely(nearest.size()>(uint32)range)) {
|
||||
nearest.erase(it, nearest.end());
|
||||
} else {
|
||||
pair<Precision_t, Point_t> dummy;
|
||||
dummy.first=numeric_limits<Precision_t>::max();
|
||||
index_t extra_size=(index_t)(range-nearest.size());
|
||||
for(index_t i=0; i<extra_size; i++) {
|
||||
nearest.push_back(dummy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -251,7 +261,7 @@ string NODE__::Print(int32 dimension) {
|
||||
sprintf(buf,"%lg ", points_[i*dimension+j]);
|
||||
str.append(buf);
|
||||
}
|
||||
sprintf(buf, "-"LI" \n", index_[i]);
|
||||
sprintf(buf, "-%llu \n",(unsigned long long) index_[i]);
|
||||
str.append(buf);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user