[mlpack-git] master: Added the Hilbert R tree to RAModel (d8c6ce4)

gitdub at mlpack.org gitdub at mlpack.org
Tue Jun 28 10:39:29 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/9dd66c7312ffcce6bc7b51aff00d38b75263f4b0...6077f120935e10ffa01d80a8398e28a90a8d011a

>---------------------------------------------------------------

commit d8c6ce46f8fe2ba85010c0abaf66fde036ae028a
Author: Mikhail Lozhnikov <lozhnikovma at gmail.com>
Date:   Tue Jun 28 17:39:29 2016 +0300

    Added the Hilbert R tree to RAModel


>---------------------------------------------------------------

d8c6ce46f8fe2ba85010c0abaf66fde036ae028a
 src/mlpack/methods/rann/krann_main.cpp    |  6 ++--
 src/mlpack/methods/rann/ra_model.hpp      |  5 ++-
 src/mlpack/methods/rann/ra_model_impl.hpp | 56 ++++++++++++++++++++++++++++++-
 3 files changed, 63 insertions(+), 4 deletions(-)

diff --git a/src/mlpack/methods/rann/krann_main.cpp b/src/mlpack/methods/rann/krann_main.cpp
index 5fede84..bcd57e1 100644
--- a/src/mlpack/methods/rann/krann_main.cpp
+++ b/src/mlpack/methods/rann/krann_main.cpp
@@ -64,7 +64,7 @@ PARAM_INT("k", "Number of nearest neighbors to find.", "k", 0);
 // The user may specify the type of tree to use, and a few parameters for tree
 // building.
 PARAM_STRING("tree_type", "Type of tree to use: 'kd', 'cover', 'r', or "
-    "'x', 'r-star'.", "t", "kd");
+    "'x', 'r-star', 'hilbert-r'.", "t", "kd");
 PARAM_INT("leaf_size", "Leaf size for tree building (used for kd-trees, R "
     "trees, and R* trees).", "l", 20);
 PARAM_FLAG("random_basis", "Before tree-building, project the data onto a "
@@ -172,9 +172,11 @@ int main(int argc, char *argv[])
       tree = RANNModel::R_STAR_TREE;
     else if (treeType == "x")
       tree = RANNModel::X_TREE;
+    else if (treeType == "hilbert-r")
+      tree = RANNModel::HILBERT_R_TREE;
     else
       Log::Fatal << "Unknown tree type '" << treeType << "'; valid choices are "
-          << "'kd', 'cover', 'r', 'r-star' and 'x'." << endl;
+          << "'kd', 'cover', 'r', 'r-star', 'x' and 'hilbert-r'." << endl;
 
     rann.TreeType() = tree;
     rann.RandomBasis() = randomBasis;
diff --git a/src/mlpack/methods/rann/ra_model.hpp b/src/mlpack/methods/rann/ra_model.hpp
index a04107f..2c92979 100644
--- a/src/mlpack/methods/rann/ra_model.hpp
+++ b/src/mlpack/methods/rann/ra_model.hpp
@@ -40,7 +40,8 @@ class RAModel
     COVER_TREE,
     R_TREE,
     R_STAR_TREE,
-    X_TREE
+    X_TREE,
+    HILBERT_R_TREE
   };
 
  private:
@@ -73,6 +74,8 @@ class RAModel
   RAType<tree::RStarTree>* rStarTreeRA;
   //! Non-NULL if the X tree is used.
   RAType<tree::XTree>* xTreeRA;
+  //! Non-NULL if the Hilbert R tree is used.
+  RAType<tree::HilbertRTree>* hilbertRTreeRA;
 
  public:
   /**
diff --git a/src/mlpack/methods/rann/ra_model_impl.hpp b/src/mlpack/methods/rann/ra_model_impl.hpp
index 48b4b4a..edaf038 100644
--- a/src/mlpack/methods/rann/ra_model_impl.hpp
+++ b/src/mlpack/methods/rann/ra_model_impl.hpp
@@ -22,7 +22,8 @@ RAModel<SortPolicy>::RAModel(const TreeTypes treeType, const bool randomBasis) :
     coverTreeRA(NULL),
     rTreeRA(NULL),
     rStarTreeRA(NULL),
-    xTreeRA(NULL)
+    xTreeRA(NULL),
+    hilbertRTreeRA(NULL)
 {
   // Nothing to do.
 }
@@ -40,6 +41,8 @@ RAModel<SortPolicy>::~RAModel()
     delete rStarTreeRA;
   if (xTreeRA)
     delete xTreeRA;
+  if (hilbertRTreeRA)
+    delete hilbertRTreeRA;
 }
 
 template<typename SortPolicy>
@@ -64,6 +67,8 @@ void RAModel<SortPolicy>::Serialize(Archive& ar,
       delete rStarTreeRA;
     if (xTreeRA)
       delete xTreeRA;
+    if (hilbertRTreeRA)
+      delete hilbertRTreeRA;
 
     // Set all the pointers to NULL.
     kdTreeRA = NULL;
@@ -71,6 +76,7 @@ void RAModel<SortPolicy>::Serialize(Archive& ar,
     rTreeRA = NULL;
     rStarTreeRA = NULL;
     xTreeRA = NULL;
+    hilbertRTreeRA = NULL;
   }
 
   // We only need to serialize one of the kRANN objects.
@@ -91,6 +97,9 @@ void RAModel<SortPolicy>::Serialize(Archive& ar,
     case X_TREE:
       ar & data::CreateNVP(xTreeRA, "ra_model");
       break;
+    case HILBERT_R_TREE:
+      ar & data::CreateNVP(hilbertRTreeRA, "ra_model");
+      break;
   }
 }
 
@@ -107,6 +116,8 @@ const arma::mat& RAModel<SortPolicy>::Dataset() const
     return rStarTreeRA->ReferenceSet();
   else if (xTreeRA)
     return xTreeRA->ReferenceSet();
+  else if (hilbertRTreeRA)
+    return hilbertRTreeRA->ReferenceSet();
 
   throw std::runtime_error("no rank-approximate nearest neighbor search model "
       "initialized");
@@ -125,6 +136,8 @@ bool RAModel<SortPolicy>::Naive() const
     return rStarTreeRA->Naive();
   else if (xTreeRA)
     return xTreeRA->Naive();
+  else if (hilbertRTreeRA)
+    return hilbertRTreeRA->Naive();
 
   throw std::runtime_error("no rank-approximate nearest neighbor search model "
       "initialized");
@@ -143,6 +156,8 @@ bool& RAModel<SortPolicy>::Naive()
     return rStarTreeRA->Naive();
   else if (xTreeRA)
     return xTreeRA->Naive();
+  else if (hilbertRTreeRA)
+    return hilbertRTreeRA->Naive();
 
   throw std::runtime_error("no rank-approximate nearest neighbor search model "
       "initialized");
@@ -161,6 +176,8 @@ bool RAModel<SortPolicy>::SingleMode() const
     return rStarTreeRA->SingleMode();
   else if (xTreeRA)
     return xTreeRA->SingleMode();
+  else if (hilbertRTreeRA)
+    return hilbertRTreeRA->SingleMode();
 
   throw std::runtime_error("no rank-approximate nearest neighbor search model "
       "initialized");
@@ -179,6 +196,8 @@ bool& RAModel<SortPolicy>::SingleMode()
     return rStarTreeRA->SingleMode();
   else if (xTreeRA)
     return xTreeRA->SingleMode();
+  else if (hilbertRTreeRA)
+    return hilbertRTreeRA->SingleMode();
 
   throw std::runtime_error("no rank-approximate nearest neighbor search model "
       "initialized");
@@ -197,6 +216,8 @@ double RAModel<SortPolicy>::Tau() const
     return rStarTreeRA->Tau();
   else if (xTreeRA)
     return xTreeRA->Tau();
+  else if (hilbertRTreeRA)
+    return hilbertRTreeRA->Tau();
 
   throw std::runtime_error("no rank-approximate nearest neighbor search model "
       "initialized");
@@ -215,6 +236,8 @@ double& RAModel<SortPolicy>::Tau()
     return rStarTreeRA->Tau();
   else if (xTreeRA)
     return xTreeRA->Tau();
+  else if (hilbertRTreeRA)
+    return hilbertRTreeRA->Tau();
 
   throw std::runtime_error("no rank-approximate nearest neighbor search model "
       "initialized");
@@ -233,6 +256,8 @@ double RAModel<SortPolicy>::Alpha() const
     return rStarTreeRA->Alpha();
   else if (xTreeRA)
     return xTreeRA->Alpha();
+  else if (hilbertRTreeRA)
+    return hilbertRTreeRA->Alpha();
 
   throw std::runtime_error("no rank-approximate nearest neighbor search model "
       "initialized");
@@ -251,6 +276,8 @@ double& RAModel<SortPolicy>::Alpha()
     return rStarTreeRA->Alpha();
   else if (xTreeRA)
     return xTreeRA->Alpha();
+  else if (hilbertRTreeRA)
+    return hilbertRTreeRA->Alpha();
 
   throw std::runtime_error("no rank-approximate nearest neighbor search model "
       "initialized");
@@ -269,6 +296,8 @@ bool RAModel<SortPolicy>::SampleAtLeaves() const
     return rStarTreeRA->SampleAtLeaves();
   else if (xTreeRA)
     return xTreeRA->SampleAtLeaves();
+  else if (hilbertRTreeRA)
+    return hilbertRTreeRA->SampleAtLeaves();
 
   throw std::runtime_error("no rank-approximate nearest neighbor search model "
       "initialized");
@@ -287,6 +316,8 @@ bool& RAModel<SortPolicy>::SampleAtLeaves()
     return rStarTreeRA->SampleAtLeaves();
   else if (xTreeRA)
     return xTreeRA->SampleAtLeaves();
+  else if (hilbertRTreeRA)
+    return hilbertRTreeRA->SampleAtLeaves();
 
   throw std::runtime_error("no rank-approximate nearest neighbor search model "
       "initialized");
@@ -305,6 +336,8 @@ bool RAModel<SortPolicy>::FirstLeafExact() const
     return rStarTreeRA->FirstLeafExact();
   else if (xTreeRA)
     return xTreeRA->FirstLeafExact();
+  else if (hilbertRTreeRA)
+    return hilbertRTreeRA->FirstLeafExact();
 
   throw std::runtime_error("no rank-approximate nearest neighbor search model "
       "initialized");
@@ -323,6 +356,8 @@ bool& RAModel<SortPolicy>::FirstLeafExact()
     return rStarTreeRA->FirstLeafExact();
   else if (xTreeRA)
     return xTreeRA->FirstLeafExact();
+  else if (hilbertRTreeRA)
+    return hilbertRTreeRA->FirstLeafExact();
 
   throw std::runtime_error("no rank-approximate nearest neighbor search model "
       "initialized");
@@ -341,6 +376,8 @@ size_t RAModel<SortPolicy>::SingleSampleLimit() const
     return rStarTreeRA->SingleSampleLimit();
   else if (xTreeRA)
     return xTreeRA->SingleSampleLimit();
+  else if (hilbertRTreeRA)
+    return hilbertRTreeRA->SingleSampleLimit();
 
   throw std::runtime_error("no rank-approximate nearest neighbor search model "
       "initialized");
@@ -359,6 +396,8 @@ size_t& RAModel<SortPolicy>::SingleSampleLimit()
     return rStarTreeRA->SingleSampleLimit();
   else if (xTreeRA)
     return xTreeRA->SingleSampleLimit();
+  else if (hilbertRTreeRA)
+    return hilbertRTreeRA->SingleSampleLimit();
 
   throw std::runtime_error("no rank-approximate nearest neighbor search model "
       "initialized");
@@ -424,6 +463,8 @@ void RAModel<SortPolicy>::BuildModel(arma::mat&& referenceSet,
     delete rStarTreeRA;
   if (xTreeRA)
     delete xTreeRA;
+  if (hilbertRTreeRA)
+    delete hilbertRTreeRA;
 
   if (randomBasis)
     referenceSet = q * referenceSet;
@@ -472,6 +513,10 @@ void RAModel<SortPolicy>::BuildModel(arma::mat&& referenceSet,
       xTreeRA = new RAType<tree::XTree>(std::move(referenceSet), naive,
           singleMode);
       break;
+    case HILBERT_R_TREE:
+      hilbertRTreeRA = new RAType<tree::HilbertRTree>(std::move(referenceSet),
+          naive, singleMode);
+      break;
   }
 
   if (!naive)
@@ -549,6 +594,10 @@ void RAModel<SortPolicy>::Search(arma::mat&& querySet,
       // No mapping necessary.
       xTreeRA->Search(querySet, k, neighbors, distances);
       break;
+    case HILBERT_R_TREE:
+      // No mapping necessary.
+      hilbertRTreeRA->Search(querySet, k, neighbors, distances);
+      break;
   }
 }
 
@@ -583,6 +632,9 @@ void RAModel<SortPolicy>::Search(const size_t k,
     case X_TREE:
       xTreeRA->Search(k, neighbors, distances);
       break;
+    case HILBERT_R_TREE:
+      hilbertRTreeRA->Search(k, neighbors, distances);
+      break;
   }
 }
 
@@ -601,6 +653,8 @@ std::string RAModel<SortPolicy>::TreeName() const
       return "R* tree";
     case X_TREE:
       return "X tree";
+    case HILBERT_R_TREE:
+      return "Hilbert R tree";
     default:
       return "unknown tree";
   }




More information about the mlpack-git mailing list