[mlpack-svn] r16729 - in mlpack/trunk/src/mlpack: core/tree/rectangle_tree tests
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Jun 30 09:31:16 EDT 2014
Author: andrewmw94
Date: Mon Jun 30 09:31:15 2014
New Revision: 16729
Log:
memory leak fix
Modified:
mlpack/trunk/src/mlpack/core/tree/rectangle_tree/r_tree_descent_heuristic_impl.hpp
mlpack/trunk/src/mlpack/core/tree/rectangle_tree/r_tree_split_impl.hpp
mlpack/trunk/src/mlpack/core/tree/rectangle_tree/rectangle_tree.hpp
mlpack/trunk/src/mlpack/core/tree/rectangle_tree/rectangle_tree_impl.hpp
mlpack/trunk/src/mlpack/tests/rectangle_tree_test.cpp
Modified: mlpack/trunk/src/mlpack/core/tree/rectangle_tree/r_tree_descent_heuristic_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/tree/rectangle_tree/r_tree_descent_heuristic_impl.hpp (original)
+++ mlpack/trunk/src/mlpack/core/tree/rectangle_tree/r_tree_descent_heuristic_impl.hpp Mon Jun 30 09:31:15 2014
@@ -13,9 +13,18 @@
namespace mlpack {
namespace tree {
+// Return the increase in volume required when inserting point into bound.
inline double RTreeDescentHeuristic::EvalNode(const HRectBound<>& bound, const arma::vec& point)
{
- return bound.Contains(point) ? 0 : bound.MinDistance(point);
+ double v1 = 1.0;
+ double v2 = 1.0;
+ for(size_t i = 0; i < bound.Dim(); i++) {
+ v1 *= bound[i].Width();
+ v2 *= bound[i].Contains(point[i]) ? bound[i].Width() : (bound[i].Hi() < point[i] ? (point[i] - bound[i].Lo()) :
+ (bound[i].Hi() - point[i]));
+ }
+ assert(v2 - v1 >= 0);
+ return v2 - v1;
}
}; // namespace tree
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 Mon Jun 30 09:31:15 2014
@@ -12,9 +12,6 @@
namespace mlpack {
namespace tree {
-
- //-r ../test_data_3_1000.csv -n neighbors_out.csv -d distances_out.csv -k 3 -v --r_tree
-
/**
* We call GetPointSeeds to get the two points which will be the initial points in the new nodes
@@ -35,6 +32,7 @@
new RectangleTree<RTreeSplit<DescentType, StatisticType, MatType>, DescentType, StatisticType, MatType>(*tree); // We actually want to copy this way. Pointers and everything.
copy->Parent() = tree;
tree->Count() = 0;
+ tree->NullifyData();
tree->Children()[(tree->NumChildren())++] = copy; // Because this was a leaf node, numChildren must be 0.
assert(tree->NumChildren() == 1);
RTreeSplit<DescentType, StatisticType, MatType>::SplitLeafNode(copy);
@@ -69,9 +67,6 @@
par->Children()[index] = treeOne;
par->Children()[par->NumChildren()++] = treeTwo;
- // We need to delete this carefully since references to points are used.
- tree->softDelete();
-
// we only add one at a time, so we should only need to test for equality
// just in case, we use an assert.
assert(par->NumChildren() <= par->MaxNumChildren());
@@ -83,6 +78,10 @@
assert(treeOne->Parent()->NumChildren() >= treeOne->MinNumChildren());
assert(treeTwo->Parent()->NumChildren() < treeTwo->MaxNumChildren());
assert(treeTwo->Parent()->NumChildren() >= treeTwo->MinNumChildren());
+
+ // We need to delete this carefully since references to points are used.
+ tree->softDelete();
+
return;
}
@@ -106,6 +105,7 @@
new RectangleTree<RTreeSplit<DescentType, StatisticType, MatType>, DescentType, StatisticType, MatType>(*tree); // We actually want to copy this way. Pointers and everything.
copy->Parent() = tree;
tree->NumChildren() = 0;
+ tree->NullifyData();
tree->Children()[(tree->NumChildren())++] = copy;
RTreeSplit<DescentType, StatisticType, MatType>::SplitNonLeafNode(copy);
return true;
@@ -143,10 +143,6 @@
assert(par->Children()[i] != tree);
}
}
-
- // Because we now have pointers to the information stored under this tree,
- // we need to delete this node carefully.
- tree->softDelete(); //currently does nothing but leak memory.
// we only add one at a time, so should only need to test for equality
// just in case, we use an assert.
@@ -169,6 +165,10 @@
assert(treeTwo->NumChildren() < treeTwo->MaxNumChildren());
assert(treeOne->Parent()->NumChildren() < treeOne->MaxNumChildren());
+ // Because we now have pointers to the information stored under this tree,
+ // we need to delete this node carefully.
+ tree->softDelete(); //currently does nothing but leak memory.
+
return false;
}
Modified: mlpack/trunk/src/mlpack/core/tree/rectangle_tree/rectangle_tree.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/tree/rectangle_tree/rectangle_tree.hpp (original)
+++ mlpack/trunk/src/mlpack/core/tree/rectangle_tree/rectangle_tree.hpp Mon Jun 30 09:31:15 2014
@@ -126,6 +126,11 @@
* other trees.
*/
void softDelete();
+
+ /**
+ * Set dataset to null. Used for memory management. Be cafeful.
+ */
+ void NullifyData();
/**
* Inserts a point into the tree. The point will be copied to the data matrix
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 Mon Jun 30 09:31:15 2014
@@ -89,23 +89,46 @@
for(int i = 0; i < numChildren; i++) {
delete children[i];
}
- delete dataset;
+ //if(numChildren == 0)
+ delete dataset;
}
+
/**
* Deletes this node but leaves the children untouched. Needed for when we
* split nodes and remove nodes (inserting and deleting points).
*/
template<typename SplitType,
- typename DescentType,
- typename StatisticType,
- typename MatType>
+ typename DescentType,
+ typename StatisticType,
+ typename MatType>
void RectangleTree<SplitType, DescentType, StatisticType, MatType>::
softDelete()
{
- /* do nothing. I'm not sure how to handle this yet, so for now, we will leak memory */
+ //if(numChildren != 0)
+ //dataset = NULL;
+ parent = NULL;
+ for(int i = 0; i < children.size(); i++) {
+ children[i] = NULL;
+ }
+ numChildren = 0;
+ delete this;
+}
+
+/**
+ * Set the dataset to null.
+ */
+template<typename SplitType,
+ typename DescentType,
+ typename StatisticType,
+ typename MatType>
+void RectangleTree<SplitType, DescentType, StatisticType, MatType>::
+ NullifyData()
+{
+ dataset = NULL;
}
+
/**
* Recurse through the tree and insert the point at the leaf node chosen
* by the heuristic.
Modified: mlpack/trunk/src/mlpack/tests/rectangle_tree_test.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/tests/rectangle_tree_test.cpp (original)
+++ mlpack/trunk/src/mlpack/tests/rectangle_tree_test.cpp Mon Jun 30 09:31:15 2014
@@ -47,13 +47,14 @@
BOOST_AUTO_TEST_CASE(RectangleTreeConstructionCountTest)
{
arma::mat dataset;
- dataset.randu(3, 1000); // 1000 points in 3 dimensions.
+ dataset.randu(8, 1000); // 1000 points in 3 dimensions.
RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
tree::RTreeDescentHeuristic,
NeighborSearchStat<NearestNeighborSort>,
arma::mat> tree(dataset, 20, 6, 5, 2, 0);
- BOOST_REQUIRE_EQUAL(tree.NumDescendants(), 1000);
+ BOOST_REQUIRE_EQUAL(tree.NumDescendants(), 1000);
+ std::cout << tree.ToString() << std::endl;
}
std::vector<arma::vec*> getAllPointsInTree(const RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
@@ -79,7 +80,7 @@
BOOST_AUTO_TEST_CASE(RectangleTreeConstructionRepeatTest)
{
arma::mat dataset;
- dataset.randu(8, 1000); // 1000 points in 8 dimensions.
+ dataset.randu(8, 15); // 1000 points in 8 dimensions.
RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
tree::RTreeDescentHeuristic,
@@ -100,7 +101,7 @@
}
for(size_t i = 0; i < allPoints.size(); i++) {
delete allPoints[i];
- }
+ }
}
bool checkContainment(const RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
@@ -129,7 +130,7 @@
BOOST_AUTO_TEST_CASE(RectangleTreeContainmentTest)
{
arma::mat dataset;
- dataset.randu(8, 1000); // 1000 points in 8 dimensions.
+ dataset.randu(8, 15); // 1000 points in 8 dimensions.
RectangleTree<tree::RTreeSplit<tree::RTreeDescentHeuristic, NeighborSearchStat<NearestNeighborSort>, arma::mat>,
tree::RTreeDescentHeuristic,
More information about the mlpack-svn
mailing list