Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vantage point tree #708

Merged
merged 14 commits into from Aug 8, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -79,9 +79,7 @@ DualTreeTraverser<RuleType>::Traverse(
numBaseCases += referenceNode.Count();
}
}
else if (((!queryNode.IsLeaf()) && referenceNode.IsLeaf()) ||
(queryNode.NumDescendants() > 3 * referenceNode.NumDescendants() &&
!queryNode.IsLeaf() && !referenceNode.IsLeaf()))
else if ((!queryNode.IsLeaf()) && referenceNode.IsLeaf())
{
// We have to recurse down the query node. In this case the recursion order
// does not matter.
Expand Down
Expand Up @@ -680,15 +680,6 @@ void VantagePointTree<MetricType, StatisticType, MatType, BoundType, SplitType>:
if (count > 0)
bound |= dataset->cols(begin, begin + count - 1);

VantagePointTree* tree = this;

while (tree->Parent() != NULL)
{
tree->Parent()->Bound() |= tree->Bound();
tree->Parent()->furthestDescendantDistance = 0.5 *
tree->Parent()->Bound().Diameter();
tree = tree->Parent();
}
// Calculate the furthest descendant distance.
furthestDescendantDistance = 0.5 * bound.Diameter();

Expand Down Expand Up @@ -763,16 +754,6 @@ SplitNode(std::vector<size_t>& oldFromNew,
if (count > 0)
bound |= dataset->cols(begin, begin + count - 1);

VantagePointTree* tree = this;

while (tree->Parent() != NULL)
{
tree->Parent()->Bound() |= tree->Bound();
tree->Parent()->furthestDescendantDistance = 0.5 *
tree->Parent()->Bound().Diameter();
tree = tree->Parent();
}

// Calculate the furthest descendant distance.
furthestDescendantDistance = 0.5 * bound.Diameter();

Expand Down
Expand Up @@ -282,11 +282,15 @@ inline double NeighborSearchRules<SortPolicy, MetricType, TreeType>::Score(
traversalInfo.LastBaseCase() = baseCase;
}
else if (tree::TreeTraits<TreeType>::FirstSiblingFirstPointIsCentroid &&
queryNode.Parent() && referenceNode.Parent())
queryNode.Parent() && referenceNode.Parent() &&
!queryNode.IsLeaf() && !referenceNode.IsLeaf())
{
// The first point of the first sibling is the centroid, so we have to
// calculate the distance between the centroids if we have not calculated
// that yet.
// We can not use this property if the traverser does not recurse down
// the query or the reference node since two siblings may be traversed
// in two different branches of the recursion.
double baseCase;

TreeType* firstQuerySibling = &queryNode.Parent()->Child(0);
Expand Down
6 changes: 5 additions & 1 deletion src/mlpack/methods/range_search/range_search_rules_impl.hpp
Expand Up @@ -177,11 +177,15 @@ double RangeSearchRules<MetricType, TreeType>::Score(TreeType& queryNode,
traversalInfo.LastBaseCase() = baseCase;
}
else if (tree::TreeTraits<TreeType>::FirstSiblingFirstPointIsCentroid &&
queryNode.Parent() && referenceNode.Parent())
queryNode.Parent() && referenceNode.Parent() &&
!queryNode.IsLeaf() && !referenceNode.IsLeaf())
{
// The first point of the first sibling is the centroid, so we have to
// calculate the distance between the centroids if we have not calculated
// that yet.
// We can not use this property if the traverser does not recurse down
// the query or the reference node since two siblings may be traversed
// in two different branches of the recursion.
double baseCase;

TreeType* firstQuerySibling = &queryNode.Parent()->Child(0);
Expand Down
31 changes: 25 additions & 6 deletions src/mlpack/tests/vantage_point_tree_test.cpp
Expand Up @@ -34,7 +34,7 @@ BOOST_AUTO_TEST_CASE(VPTreeTraitsTest)
b = TreeTraits<TreeType>::RearrangesDataset;
BOOST_REQUIRE_EQUAL(b, true);
b = TreeTraits<TreeType>::BinaryTree;
BOOST_REQUIRE_EQUAL(b, true);
BOOST_REQUIRE_EQUAL(b, false);
}

BOOST_AUTO_TEST_CASE(HollowBallBoundTest)
Expand Down Expand Up @@ -130,11 +130,21 @@ BOOST_AUTO_TEST_CASE(HollowBallBoundTest)
template<typename TreeType>
void CheckBound(TreeType& tree)
{
typedef typename TreeType::ElemType ElemType;
if (tree.IsLeaf())
{
// Ensure that the bound contains all descendant points.
for (size_t i = 0; i < tree.NumPoints(); i++)
BOOST_REQUIRE_EQUAL(true,
tree.Bound().Contains(tree.Dataset().col(tree.Point(i))));
{
ElemType dist = tree.Bound().Metric().Evaluate(tree.Bound().Center(),
tree.Dataset().col(tree.Point(i)));

BOOST_REQUIRE_LE(tree.Bound().InnerRadius(), dist *
(1.0 + 10.0 * std::numeric_limits<ElemType>::epsilon()));

BOOST_REQUIRE_LE(dist, tree.Bound().OuterRadius() *
(1.0 + 10.0 * std::numeric_limits<ElemType>::epsilon()));
}
}
else
{
Expand All @@ -145,9 +155,18 @@ void CheckBound(TreeType& tree)
BOOST_REQUIRE_EQUAL(central->Bound().InnerRadius(), 0.0);
BOOST_REQUIRE_EQUAL(central->Bound().OuterRadius(), 0.0);

BOOST_REQUIRE_EQUAL(tree.Bound().Contains(tree.Central()->Bound()), true);
BOOST_REQUIRE_EQUAL(tree.Bound().Contains(tree.Inner()->Bound()), true);
BOOST_REQUIRE_EQUAL(tree.Bound().Contains(tree.Outer()->Bound()), true);
// Ensure that the bound contains all descendant points.
for (size_t i = 0; i < tree.NumDescendants(); i++)
{
ElemType dist = tree.Bound().Metric().Evaluate(tree.Bound().Center(),
tree.Dataset().col(tree.Descendant(i)));

BOOST_REQUIRE_LE(tree.Bound().InnerRadius(), dist *
(1.0 + 10.0 * std::numeric_limits<ElemType>::epsilon()));

BOOST_REQUIRE_LE(dist, tree.Bound().OuterRadius() *
(1.0 + 10.0 * std::numeric_limits<ElemType>::epsilon()));
}

CheckBound(*tree.Inner());
CheckBound(*tree.Outer());
Expand Down