[mlpack-svn] r16566 - mlpack/trunk/src/mlpack/core/tree/rectangle_tree

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue May 27 16:49:21 EDT 2014


Author: andrewmw94
Date: Tue May 27 16:49:20 2014
New Revision: 16566

Log:
more stuff for R Trees

Modified:
   mlpack/trunk/src/mlpack/core/tree/rectangle_tree/r_tree_split.hpp
   mlpack/trunk/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp
   mlpack/trunk/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp

Modified: mlpack/trunk/src/mlpack/core/tree/rectangle_tree/r_tree_split.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/tree/rectangle_tree/r_tree_split.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/tree/rectangle_tree/r_tree_split.hpp	Tue May 27 16:49:20 2014
@@ -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
+
+

Modified: mlpack/trunk/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp	Tue May 27 16:49:20 2014
@@ -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

Modified: mlpack/trunk/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp	Tue May 27 16:49:20 2014
@@ -18,9 +18,9 @@
 
 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 @@
 }
 
 /**
+ * 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 @@
   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 @@
 
 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 @@
 
 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 @@
  */
 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 @@
  */
 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 @@
  */
 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 @@
  */
 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 @@
  */
 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 @@
  */
 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 @@
  */
 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 @@
  */
 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 @@
  */
 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 @@
   //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 @@
  */
   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;



More information about the mlpack-svn mailing list