[mlpack] 33/324: more stuff for R Trees
Barak A. Pearlmutter
barak+git at cs.nuim.ie
Sun Aug 17 08:21:53 UTC 2014
This is an automated email from the git hooks/post-receive script.
bap pushed a commit to branch svn-trunk
in repository mlpack.
commit fe3b0196e05910e3da8348ab8d3d6b6e099e1d75
Author: andrewmw94 <andrewmw94 at 9d5b8971-822b-0410-80eb-d18c1038ef23>
Date: Tue May 27 20:49:20 2014 +0000
more stuff for R Trees
git-svn-id: http://svn.cc.gatech.edu/fastlab/mlpack/trunk@16566 9d5b8971-822b-0410-80eb-d18c1038ef23
---
.../core/tree/rectangle_tree/r_tree_split.hpp | 50 +++++++-
.../core/tree/rectangle_tree/r_tree_split_impl.hpp | 30 ++++-
.../tree/rectangle_tree/rectangle_tree_impl.hpp | 142 +++++++++++++--------
3 files changed, 166 insertions(+), 56 deletions(-)
diff --git a/src/mlpack/core/tree/rectangle_tree/r_tree_split.hpp b/src/mlpack/core/tree/rectangle_tree/r_tree_split.hpp
index 8d1c8b6..e2786fb 100644
--- a/src/mlpack/core/tree/rectangle_tree/r_tree_split.hpp
+++ b/src/mlpack/core/tree/rectangle_tree/r_tree_split.hpp
@@ -1 +1,49 @@
-
+/**
+ * @file r_tree_split.hpp
+ * @author Andrew Wells
+ *
+ * Defintion of the RTreeSplit class, a class that splits the nodes of an R tree, starting
+ * at a leaf node and moving upwards if necessary.
+ */
+#ifndef __MLPACK_CORE_TREE_RECTANGLE_TREE_R_TREE_SPLIT_HPP
+#define __MLPACK_CORE_TREE_RECTANGLE_TREE_R_TREE_SPLIT_HPP
+
+#include <mlpack/core.hpp>
+
+namespace mlpack {
+namespace tree /** Trees and tree-building procedures. */ {
+
+/**
+ * A Rectangle Tree has new points inserted at the bottom. When these
+ * nodes overflow, we split them, moving up the tree and splitting nodes
+ * as necessary.
+ */
+template<typename MatType = arma::mat>
+class RTreeSplit
+{
+public:
+
+/**
+ * Split a leaf node using the "default" algorithm. The methods for splitting non-leaf
+ * nodes are private since they should only be called if a leaf node overflows.
+ */
+static bool SplitLeafNode(const RectangleTree& tree);
+
+private:
+
+/**
+ * Split a non-leaf node using the "default" algorithm.
+ */
+static bool SplitNonLeafNode(const RectangleTree& tree);
+
+};
+
+}; // namespace tree
+}; // namespace mlpack
+
+// Include implementation
+#include "r_tree_split_impl.hpp"
+
+#endif
+
+
diff --git a/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp b/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp
index 8d1c8b6..389553a 100644
--- a/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp
+++ b/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp
@@ -1 +1,29 @@
-
+/**
+ * @file r_tree_split_impl.hpp
+ * @author Andrew Wells
+ *
+ * Implementation of class (RTreeSplit) to split a RectangleTree.
+ */
+#ifndef __MLPACK_CORE_TREE_RECTANGLE_TREE_R_TREE_SPLIT_IMPL_HPP
+#define __MLPACK_CORE_TREE_RECTANGLE_TREE_R_TREE_SPLIT_IMPL_HPP
+
+#include "r_tree_split.hpp"
+
+namespace mlpack {
+namespace tree {
+
+template<typename MatType>
+bool RTreeSplit<MatType>::SplitLeafNode(const RectangleTree& tree)
+{
+
+}
+
+bool RTreeSplit<MatType>::SplitNonLeafNode(const RectangleTree& tree)
+{
+
+}
+
+}; // namespace tree
+}; // namespace mlpack
+
+#endif
diff --git a/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp b/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp
index 0968edc..1643f85 100644
--- a/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp
+++ b/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp
@@ -18,9 +18,9 @@ namespace tree {
template<typename StatisticType,
typename MatType,
- typename SplitType
+ typename SplitType,
typename DescentType>
-RectangleTree<StatisticType, MatType, SplitType>::RectangleTree(
+RectangleTree<StatisticType, MatType, SplitType, DescentType>::RectangleTree(
MatType& data,
const size_t leafSize):
@@ -30,24 +30,50 @@ RectangleTree<StatisticType, MatType, SplitType>::RectangleTree(
}
/**
+ * Deletes this node, deallocating the memory for the children and calling
+ * their destructors in turn. This will invalidate any pointers or references
+ * to any nodes which are children of this one.
+ */
+template<typename StatisticType,
+ typename MatType,
+ typename SplitType,
+ typename DescentType>
+RectangleTree<BoundType, StatisticType, MatType, SplitType, DescentType>::
+ ~RectangleTree()
+{
+ for(int i = 0; i < numOfChildren; i++) {
+ delete children[i];
+ }
+}
+
+
+/**
* Recurse through the tree and insert the point at the leaf node chosen
* by the heuristic.
*/
template<typename StatisticType,
typename MatType,
- typename SplitType
- typename HueristicType>
-RectangleTree<StatisticType, MatType, SplitType, HueristicType>::
+ typename SplitType,
+ typename DescentType>
+RectangleTree<StatisticType, MatType, SplitType, DescentType>::
InsertPoint(const arma::vec& point)
{
+ // Expand the bound regardless of whether it is a leaf node.
+ bound |= point;
+
+ // If this is a leaf node, we stop here and add the point.
if(numChildren == 0) {
data.col(points++) = point;
+ splitNode();
return;
}
- double minScore = HueristicType.EvalNode(children[0].bound, point);
+
+ // If it is not a leaf node, we use the DescentHeuristic to choose a child
+ // to which we recurse.
+ double minScore = DescentType.EvalNode(children[0].bound, point);
int bestIndex = 0;
for(int i = 1; i < numChildren; i++) {
- double score = HueristicType.EvalNode(children[i].bound, point);
+ double score = DescentType.EvalNode(children[i].bound, point);
if(score < minScore) {
minScore = score;
bestIndex = i
@@ -56,27 +82,11 @@ RectangleTree<StatisticType, MatType, SplitType, HueristicType>::
children[bestIndex].InsertPoint(point);
}
-
-/**
- * Deletes this node, deallocating the memory for the children and calling
- * their destructors in turn. This will invalidate any pointers or references
- * to any nodes which are children of this one.
- */
-template<typename StatisticType,
- typename MatType,
- typename SplitType>
-RectangleTree<BoundType, StatisticType, MatType, SplitType>::
- ~RectangleTree()
-{
- for(int i = 0; i < numOfChildren; i++) {
- delete children[i];
- }
-}
-
template<typename StatisticType,
typename MatType,
- typename SplitType>
-size_t RectangleTree<StatisticType, MatType, SplitType>::
+ typename SplitType,
+ typename DescentType>
+size_t RectangleTree<StatisticType, MatType, SplitType, DescentType>::
TreeSize() const
{
int n = 0;
@@ -90,8 +100,9 @@ size_t RectangleTree<StatisticType, MatType, SplitType>::
template<typename StatisticType,
typename MatType,
- typename SplitType>
-size_t RectangleTree<StatisticType, MatType, SplitType>::
+ typename SplitType,
+ typename DescentType>
+size_t RectangleTree<StatisticType, MatType, SplitType, DescentType>::
TreeDepth() const
{
// Recursively count the depth of each subtree. The plus one is
@@ -107,8 +118,9 @@ size_t RectangleTree<StatisticType, MatType, SplitType>::
template<typename StatisticType,
typename MatType,
- typename SplitType>
-inline bool BinarySpaceTree<StatisticType, MatType, SplitType>::
+ typename SplitType,
+ typename DescentType>
+inline bool BinarySpaceTree<StatisticType, MatType, SplitType, DescentType>::
IsLeaf() const
{
return numOfChildren == 0;
@@ -119,8 +131,9 @@ inline bool BinarySpaceTree<StatisticType, MatType, SplitType>::
*/
template<typename StatisticType,
typename MatType,
- typename SplitType>
-inline size_t RectangleTree<StatisticType, MatType, SplitType>::
+ typename SplitType,
+ typename DescentType>
+inline size_t RectangleTree<StatisticType, MatType, SplitType, DescentType>::
NumChildren() const
{
return NumChildren;
@@ -132,8 +145,9 @@ inline size_t RectangleTree<StatisticType, MatType, SplitType>::
*/
template<typename StatisticType,
typename MatType,
- typename SplitType>
-inline double RectangleTree<StatisticType, MatType, SplitType>::
+ typename SplitType,
+ typename DescentType>
+inline double RectangleTree<StatisticType, MatType, SplitType, DescentType>::
FurthestPointDistance() const
{
if(!IsLeaf())
@@ -152,8 +166,9 @@ FurthestPointDistance() const
*/
template<typename StatisiticType,
typename MatType,
- typename SplitType>
-inline double RectangleTree<StatisticType, MatType, SplitType>::
+ typename SplitType,
+ typename DescentType>
+inline double RectangleTree<StatisticType, MatType, SplitType, DescentType>::
FurthestDescendantDistance() const
{
return furthestDescendantDistance;
@@ -164,9 +179,10 @@ inline double RectangleTree<StatisticType, MatType, SplitType>::
*/
template<typename StatisticType,
typename MatType,
- typename SplitType>
-inline RectangleTree<StatisticType, MatType, SplitType>&
- RectangleTree<StatisticType, MatType, SplitType>::
+ typename SplitType,
+ typename DescentType>
+inline RectangleTree<StatisticType, MatType, SplitType, DescentType>&
+ RectangleTree<StatisticType, MatType, SplitType, DescentType>::
Child(const size_t child) const
{
return children[child];
@@ -177,8 +193,9 @@ inline RectangleTree<StatisticType, MatType, SplitType>&
*/
template<typename StatisticType,
typename MatType,
- typename SplitType>
-inline size_t RectangleTree<StatisticType, MatType, SplitType>::
+ typename SplitType,
+ typename DescentType>
+inline size_t RectangleTree<StatisticType, MatType, SplitType, DescentType>::
NumPoints() const
{
if(numChildren == 0)
@@ -192,8 +209,9 @@ inline size_t RectangleTree<StatisticType, MatType, SplitType>::
*/
template<typename StatisticType,
typename MatType,
- typename SplitType>
-inline size_t RectangleTree<StatisticType, MatType, SplitType>::
+ typename SplitType,
+ typename DescentType>
+inline size_t RectangleTree<StatisticType, MatType, SplitType, DescentType>::
NumDescendants() const
{
return count;
@@ -204,8 +222,9 @@ inline size_t RectangleTree<StatisticType, MatType, SplitType>::
*/
template<typename StatisticType,
typename MatType,
- typename SplitType>
-inline size_t RectangleTree<StatisticType, MatType, SplitType>::
+ typename SplitType,
+ typename DescentType>
+inline size_t RectangleTree<StatisticType, MatType, SplitType, DescentType>::
Descendant(const size_t index> const
{
return (begin + index);
@@ -216,8 +235,9 @@ inline size_t RectangleTree<StatisticType, MatType, SplitType>::
*/
template<typename StatisticType,
typename MatType,
- typename SplitType>
-inline size_t RectangleTree<StatisticType, MatType, SplitType>::
+ typename SplitType,
+ typename DescentType>
+inline size_t RectangleTree<StatisticType, MatType, SplitType, DescentType>::
Point(const size_t index> const
{
return (begin + index);
@@ -229,8 +249,9 @@ inline size_t RectangleTree<StatisticType, MatType, SplitType>::
*/
template<typename StatisticType,
typename MatType,
- typename SplitType>
-inline size_t RectangleTree<StatisticType, MatType, SplitType>::End() const
+ typename SplitType,
+ typename DescentType>
+inline size_t RectangleTree<StatisticType, MatType, SplitType, DescentType>::End() const
{
if(numOfChildren)
return begin + count;
@@ -240,15 +261,27 @@ inline size_t RectangleTree<StatisticType, MatType, SplitType>::End() const
//have functions for returning the list of modified indices if we end up doing it that way.
/**
- * Split the tree. This moves up the tree recursively.
+ * Split the tree. This calls the SplitType code to split a node. This method should only
+ * be called on a leaf node.
*/
template<typename StatisticType,
typename MatType,
- typename SplitType>
-void RectangleTree<StatisticType, MatType, SplitType>::SplitNode(
+ typename SplitType,
+ typename DescentType>
+void RectangleTree<StatisticType, MatType, SplitType, DescentType>::SplitNode(
RetangleTree& tree)
{
+ // This should always be a leaf node. When we need to split other nodes,
+ // the split will be called from here but will take place in the SplitType code.
+ boost::assert(numChildren == 0);
+
+ // See if we are full.
+ if(points < leafSize)
+ return;
+ // If we are full, then we need to move up the tree. The SplitType takes
+ // care of this.
+ SplitType.SplitLeafNode(this);
}
@@ -257,8 +290,9 @@ void RectangleTree<StatisticType, MatType, SplitType>::SplitNode(
*/
template<typename StatisticType,
typename MatType,
- typename SplitType>
-std::string BinarySpaceTree<StatisticType, MatType, SplitType>::ToString() const
+ typename SplitType,
+ typename DescentType>
+std::string BinarySpaceTree<StatisticType, MatType, SplitType, DescentType>::ToString() const
{
std::ostringstream convert;
convert << "RectangleTree [" << this << "]" << std::endl;
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mlpack.git
More information about the debian-science-commits
mailing list