[mlpack-git] master, mlpack-1.0.x: Test BinarySpaceTree<...>::ParentDistance(). (32695f4)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 21:42:09 EST 2015
Repository : https://github.com/mlpack/mlpack
On branches: master,mlpack-1.0.x
Link : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40
>---------------------------------------------------------------
commit 32695f4f5776f4a32214b66c3497e76cb9731f5c
Author: Ryan Curtin <ryan at ratml.org>
Date: Thu Feb 6 19:55:50 2014 +0000
Test BinarySpaceTree<...>::ParentDistance().
>---------------------------------------------------------------
32695f4f5776f4a32214b66c3497e76cb9731f5c
src/mlpack/tests/tree_test.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git a/src/mlpack/tests/tree_test.cpp b/src/mlpack/tests/tree_test.cpp
index 023cf5e..b26d088 100644
--- a/src/mlpack/tests/tree_test.cpp
+++ b/src/mlpack/tests/tree_test.cpp
@@ -12,6 +12,7 @@
#include <mlpack/core/tree/cosine_tree/cosine_tree_builder.hpp>
#include <queue>
+#include <stack>
#include <boost/test/unit_test.hpp>
#include "old_boost_test_definitions.hpp"
@@ -1630,6 +1631,48 @@ BOOST_AUTO_TEST_CASE(FurthestPointDistanceTest)
}
}
+BOOST_AUTO_TEST_CASE(ParentDistanceTest)
+{
+ arma::mat dataset;
+ dataset.randu(5, 500);
+
+ BinarySpaceTree<HRectBound<2> > tree(dataset);
+
+ // The root's parent distance should be 0 (although maybe it doesn't actually
+ // matter; I just want to be sure it's not an uninitialized value, which this
+ // test *sort* of checks).
+ BOOST_REQUIRE_EQUAL(tree.ParentDistance(), 0.0);
+
+ // Do a depth-first traversal and make sure the parent distance is the same as
+ // we calculate.
+ std::stack<BinarySpaceTree<HRectBound<2> >*> nodeStack;
+ nodeStack.push(&tree);
+
+ while (!nodeStack.empty())
+ {
+ BinarySpaceTree<HRectBound<2> >* node = nodeStack.top();
+ nodeStack.pop();
+
+ // If it's a leaf, nothing to check.
+ if (node->NumChildren() == 0)
+ continue;
+
+ arma::vec centroid, leftCentroid, rightCentroid;
+ node->Centroid(centroid);
+ node->Left()->Centroid(leftCentroid);
+ node->Right()->Centroid(rightCentroid);
+
+ const double leftDistance = LMetric<2>::Evaluate(centroid, leftCentroid);
+ const double rightDistance = LMetric<2>::Evaluate(centroid, rightCentroid);
+
+ BOOST_REQUIRE_CLOSE(leftDistance, node->Left()->ParentDistance(), 1e-5);
+ BOOST_REQUIRE_CLOSE(rightDistance, node->Right()->ParentDistance(), 1e-5);
+
+ nodeStack.push(node->Left());
+ nodeStack.push(node->Right());
+ }
+}
+
// Forward declaration of methods we need for the next test.
template<typename TreeType, typename MatType>
bool CheckPointBounds(TreeType* node, const MatType& data);
More information about the mlpack-git
mailing list