[mlpack-git] master: Add Tests for Hyperplane class. (17d2c2f)

gitdub at mlpack.org gitdub at mlpack.org
Thu Aug 11 09:01:37 EDT 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/0f4b25acd6aaa14294c044874ba6cc0751712baa...0a19d07bd39e6223991976474bc79671ba8aa0f0

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

commit 17d2c2f90d27b031b061644097dfaa398daa5472
Author: MarcosPividori <marcos.pividori at gmail.com>
Date:   Thu Aug 11 10:01:37 2016 -0300

    Add Tests for Hyperplane class.


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

17d2c2f90d27b031b061644097dfaa398daa5472
 src/mlpack/tests/CMakeLists.txt      |   1 +
 src/mlpack/tests/hyperplane_test.cpp | 128 +++++++++++++++++++++++++++++++++++
 2 files changed, 129 insertions(+)

diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt
index 1de4903..8fa96b3 100644
--- a/src/mlpack/tests/CMakeLists.txt
+++ b/src/mlpack/tests/CMakeLists.txt
@@ -22,6 +22,7 @@ add_executable(mlpack_test
   gmm_test.cpp
   hmm_test.cpp
   hoeffding_tree_test.cpp
+  hyperplane_test.cpp
   imputation_test.cpp
   ind2sub_test.cpp
   init_rules_test.cpp
diff --git a/src/mlpack/tests/hyperplane_test.cpp b/src/mlpack/tests/hyperplane_test.cpp
new file mode 100644
index 0000000..98465c6
--- /dev/null
+++ b/src/mlpack/tests/hyperplane_test.cpp
@@ -0,0 +1,128 @@
+/**
+ * @file hyperplane_test.cpp
+ *
+ * Tests for Hyperplane and ProjVector implementations.
+ */
+#include <mlpack/core.hpp>
+#include <mlpack/core/tree/space_split/hyperplane.hpp>
+
+#include <boost/test/unit_test.hpp>
+#include "test_tools.hpp"
+
+using namespace mlpack;
+using namespace mlpack::math;
+using namespace mlpack::tree;
+using namespace mlpack::metric;
+using namespace mlpack::bound;
+
+BOOST_AUTO_TEST_SUITE(HyperplaneTest);
+
+/**
+ * Ensure that a hyperplane, by default, consider all points to the left.
+ */
+BOOST_AUTO_TEST_CASE(HyperplaneEmptyConstructor)
+{
+  Hyperplane<EuclideanDistance> h1;
+  AxisOrthogonalHyperplane<EuclideanDistance> h2;
+
+  arma::mat dataset;
+  dataset.randu(3, 20); // 20 points in 3 dimensions.
+
+  for (size_t i = 0; i < dataset.n_cols; i++)
+  {
+    BOOST_REQUIRE(h1.Left(dataset.col(i)));
+    BOOST_REQUIRE(h2.Left(dataset.col(i)));
+    BOOST_REQUIRE(!h1.Right(dataset.col(i)));
+    BOOST_REQUIRE(!h2.Right(dataset.col(i)));
+  }
+}
+
+/**
+ * Ensure that we get the correct hyperplane given the projection vector.
+ */
+BOOST_AUTO_TEST_CASE(HyperplaneProjectionTest)
+{
+  // General hyperplane.
+  ProjVector projVect1(arma::vec("1 1"));
+  Hyperplane<EuclideanDistance> h1(projVect1, 0);
+
+  BOOST_REQUIRE_EQUAL(h1.Project(arma::vec("1 -1")), 0);
+  BOOST_REQUIRE(h1.Left(arma::vec("1 -1")));
+  BOOST_REQUIRE(!h1.Right(arma::vec("1 -1")));
+
+  BOOST_REQUIRE_EQUAL(h1.Project(arma::vec("-1 1")), 0);
+  BOOST_REQUIRE(h1.Left(arma::vec("-1 1")));
+  BOOST_REQUIRE(!h1.Right(arma::vec("-1 1")));
+
+  BOOST_REQUIRE_EQUAL(h1.Project(arma::vec("1 0")),
+      h1.Project(arma::vec("0 1")));
+  BOOST_REQUIRE(h1.Right(arma::vec("1 0")));
+  BOOST_REQUIRE(!h1.Left(arma::vec("1 0")));
+
+  BOOST_REQUIRE_EQUAL(h1.Project(arma::vec("-1 -1")),
+      h1.Project(arma::vec("-2 0")));
+  BOOST_REQUIRE(h1.Left(arma::vec("-1 -1")));
+  BOOST_REQUIRE(!h1.Right(arma::vec("-1 -1")));
+
+  // A simple 2-dimensional bound.
+  BallBound<EuclideanDistance> b1(2);
+
+  b1.Center() = arma::vec("-1 -1");
+  b1.Radius() = 1.41;
+  BOOST_REQUIRE(h1.Left(b1));
+  BOOST_REQUIRE(!h1.Right(b1));
+
+  b1.Center() = arma::vec("1 1");
+  b1.Radius() = 1.41;
+  BOOST_REQUIRE(h1.Right(b1));
+  BOOST_REQUIRE(!h1.Left(b1));
+
+  b1.Center() = arma::vec("0 0");
+  b1.Radius() = 1.41;
+  BOOST_REQUIRE(!h1.Right(b1));
+  BOOST_REQUIRE(!h1.Left(b1));
+
+  // AxisParallel hyperplane.
+  AxisParallelProjVector projVect2(1);
+  AxisOrthogonalHyperplane<EuclideanDistance> h2(projVect2, 1);
+
+  BOOST_REQUIRE_EQUAL(h2.Project(arma::vec("0 0")), -1);
+  BOOST_REQUIRE(h2.Left(arma::vec("0 0")));
+  BOOST_REQUIRE(!h2.Right(arma::vec("0 0")));
+
+  BOOST_REQUIRE_EQUAL(h2.Project(arma::vec("0 1")), 0);
+  BOOST_REQUIRE(h2.Left(arma::vec("0 1")));
+  BOOST_REQUIRE(!h2.Right(arma::vec("0 1")));
+
+  BOOST_REQUIRE_EQUAL(h2.Project(arma::vec("0 2")), 1);
+  BOOST_REQUIRE(h2.Right(arma::vec("0 2")));
+  BOOST_REQUIRE(!h2.Left(arma::vec("0 2")));
+
+  BOOST_REQUIRE_EQUAL(h2.Project(arma::vec("1 2")), 1);
+  BOOST_REQUIRE(h2.Right(arma::vec("1 2")));
+  BOOST_REQUIRE(!h2.Left(arma::vec("1 2")));
+
+  BOOST_REQUIRE_EQUAL(h2.Project(arma::vec("1 0")), -1);
+  BOOST_REQUIRE(h2.Left(arma::vec("1 0")));
+  BOOST_REQUIRE(!h2.Right(arma::vec("1 0")));
+
+  // A simple 2-dimensional bound.
+  HRectBound<EuclideanDistance> b2(2);
+
+  b2[0] = Range(-1.0, 1.0);
+  b2[1] = Range(-1.0, 1.0);
+  BOOST_REQUIRE(h2.Left(b2));
+  BOOST_REQUIRE(!h2.Right(b2));
+
+  b2[0] = Range(-1.0, 1.0);
+  b2[1] = Range(1.001, 2.0);
+  BOOST_REQUIRE(h2.Right(b2));
+  BOOST_REQUIRE(!h2.Left(b2));
+
+  b2[0] = Range(-1.0, 1.0);
+  b2[1] = Range(0, 2.0);
+  BOOST_REQUIRE(!h2.Right(b2));
+  BOOST_REQUIRE(!h2.Left(b2));
+}
+
+BOOST_AUTO_TEST_SUITE_END();




More information about the mlpack-git mailing list