diff --git a/src/mlpack/methods/neighbor_search/kfn_main.cpp b/src/mlpack/methods/neighbor_search/kfn_main.cpp index d6807400e7..163e8ed741 100644 --- a/src/mlpack/methods/neighbor_search/kfn_main.cpp +++ b/src/mlpack/methods/neighbor_search/kfn_main.cpp @@ -72,6 +72,12 @@ PARAM_INT("seed", "Random seed (if 0, std::time(NULL) is used).", "s", 0); PARAM_FLAG("naive", "If true, O(n^2) naive mode is used for computation.", "N"); PARAM_FLAG("single_mode", "If true, single-tree search is used (as opposed to " "dual-tree search).", "s"); +PARAM_DOUBLE("epsilon", "If specified, will do approximate furthest neighbor " + "search with given relative error. Must be in the range [0,1).", "e", 0); +PARAM_DOUBLE("percentage", "If specified, will do approximate furthest neighbor" + " search. Must be in the range (0,1] (decimal form). Resultant neighbors " + "will be at least (p*100) % of the distance as the true furthest neighbor.", + "p", 1); // Convenience typedef. typedef NSModel KFNModel; @@ -138,6 +144,24 @@ int main(int argc, char *argv[]) Log::Fatal << "Invalid leaf size: " << lsInt << ". Must be greater than 0." << endl; + // Sanity check on epsilon. + double epsilon = CLI::GetParam("epsilon"); + if (epsilon < 0 || epsilon >= 1) + Log::Fatal << "Invalid epsilon: " << epsilon << ". Must be in the range " + << "[0,1)." << endl; + + // Sanity check on percentage. + const double percentage = CLI::GetParam("percentage"); + if (percentage <= 0 || percentage > 1) + Log::Fatal << "Invalid percentage: " << percentage << ". Must be in the " + << "range (0,1] (decimal form)." << endl; + + if (CLI::HasParam("percentage") && CLI::HasParam("epsilon")) + Log::Fatal << "Cannot provide both epsilon and percentage." << endl; + + if (CLI::HasParam("percentage")) + epsilon = 1 - percentage; + // We either have to load the reference data, or we have to load the model. NSModel kfn; const bool naive = CLI::HasParam("naive"); @@ -175,7 +199,8 @@ int main(int argc, char *argv[]) Log::Info << "Loaded reference data from '" << referenceFile << "' (" << referenceSet.n_rows << "x" << referenceSet.n_cols << ")." << endl; - kfn.BuildModel(std::move(referenceSet), size_t(lsInt), naive, singleMode); + kfn.BuildModel(std::move(referenceSet), size_t(lsInt), naive, singleMode, + epsilon); } else { @@ -191,6 +216,7 @@ int main(int argc, char *argv[]) kfn.SingleMode() = CLI::HasParam("single_mode"); kfn.Naive() = CLI::HasParam("naive"); kfn.LeafSize() = size_t(lsInt); + kfn.Epsilon() = epsilon; } // Perform search, if desired. diff --git a/src/mlpack/methods/neighbor_search/knn_main.cpp b/src/mlpack/methods/neighbor_search/knn_main.cpp index 4957e88ebe..880f5db90f 100644 --- a/src/mlpack/methods/neighbor_search/knn_main.cpp +++ b/src/mlpack/methods/neighbor_search/knn_main.cpp @@ -74,6 +74,8 @@ PARAM_INT("seed", "Random seed (if 0, std::time(NULL) is used).", "s", 0); PARAM_FLAG("naive", "If true, O(n^2) naive mode is used for computation.", "N"); PARAM_FLAG("single_mode", "If true, single-tree search is used (as opposed to " "dual-tree search).", "S"); +PARAM_DOUBLE("epsilon", "If specified, will do approximate nearest neighbor " + "search with given relative error.", "e", 0); // Convenience typedef. typedef NSModel KNNModel; @@ -137,10 +139,14 @@ int main(int argc, char *argv[]) // Sanity check on leaf size. const int lsInt = CLI::GetParam("leaf_size"); if (lsInt < 1) - { Log::Fatal << "Invalid leaf size: " << lsInt << ". Must be greater " "than 0." << endl; - } + + // Sanity check on epsilon. + const double epsilon = CLI::GetParam("epsilon"); + if (epsilon < 0) + Log::Fatal << "Invalid epsilon: " << epsilon << ". Must be non-negative. " + << endl; // We either have to load the reference data, or we have to load the model. NSModel knn; @@ -180,7 +186,8 @@ int main(int argc, char *argv[]) << referenceSet.n_rows << " x " << referenceSet.n_cols << ")." << endl; - knn.BuildModel(std::move(referenceSet), size_t(lsInt), naive, singleMode); + knn.BuildModel(std::move(referenceSet), size_t(lsInt), naive, singleMode, + epsilon); } else { @@ -196,6 +203,7 @@ int main(int argc, char *argv[]) knn.SingleMode() = CLI::HasParam("single_mode"); knn.Naive() = CLI::HasParam("naive"); knn.LeafSize() = size_t(lsInt); + knn.Epsilon() = epsilon; } // Perform search, if desired. diff --git a/src/mlpack/methods/neighbor_search/neighbor_search.hpp b/src/mlpack/methods/neighbor_search/neighbor_search.hpp index 999f261c8f..f1acea458d 100644 --- a/src/mlpack/methods/neighbor_search/neighbor_search.hpp +++ b/src/mlpack/methods/neighbor_search/neighbor_search.hpp @@ -84,11 +84,13 @@ class NeighborSearch * dual-tree search). This overrides singleMode (if it is set to true). * @param singleMode If true, single-tree search will be used (as opposed to * dual-tree search). + * @param epsilon Relative approximate error (non-negative). * @param metric An optional instance of the MetricType class. */ NeighborSearch(const MatType& referenceSet, const bool naive = false, const bool singleMode = false, + const double epsilon = 0, const MetricType metric = MetricType()); /** @@ -108,11 +110,13 @@ class NeighborSearch * dual-tree search). This overrides singleMode (if it is set to true). * @param singleMode If true, single-tree search will be used (as opposed to * dual-tree search). + * @param epsilon Relative approximate error (non-negative). * @param metric An optional instance of the MetricType class. */ NeighborSearch(MatType&& referenceSet, const bool naive = false, const bool singleMode = false, + const double epsilon = 0, const MetricType metric = MetricType()); /** @@ -138,10 +142,12 @@ class NeighborSearch * @param referenceSet Set of reference points corresponding to referenceTree. * @param singleMode Whether single-tree computation should be used (as * opposed to dual-tree computation). + * @param epsilon Relative approximate error (non-negative). * @param metric Instantiated distance metric. */ NeighborSearch(Tree* referenceTree, const bool singleMode = false, + const double epsilon = 0, const MetricType metric = MetricType()); /** @@ -152,10 +158,12 @@ class NeighborSearch * @param naive Whether to use naive search. * @param singleMode Whether single-tree computation should be used (as * opposed to dual-tree computation). + * @param epsilon Relative approximate error (non-negative). * @param metric Instantiated metric. */ NeighborSearch(const bool naive = false, const bool singleMode = false, + const double epsilon = 0, const MetricType metric = MetricType()); @@ -270,6 +278,11 @@ class NeighborSearch //! Modify whether or not search is done in single-tree mode. bool& SingleMode() { return singleMode; } + //! Access the relative error to be considered in approximate search. + double Epsilon() const { return epsilon; } + //! Modify the relative error to be considered in approximate search. + double& Epsilon() { return epsilon; } + //! Access the reference dataset. const MatType& ReferenceSet() const { return *referenceSet; } @@ -294,6 +307,8 @@ class NeighborSearch bool naive; //! Indicates if single-tree search is being used (as opposed to dual-tree). bool singleMode; + //! Indicates the relative error to be considered in approximate search. + double epsilon; //! Instantiation of metric. MetricType metric; diff --git a/src/mlpack/methods/neighbor_search/neighbor_search_impl.hpp b/src/mlpack/methods/neighbor_search/neighbor_search_impl.hpp index d86f5146e1..2d7468bbf7 100644 --- a/src/mlpack/methods/neighbor_search/neighbor_search_impl.hpp +++ b/src/mlpack/methods/neighbor_search/neighbor_search_impl.hpp @@ -75,6 +75,7 @@ NeighborSearch:: NeighborSearch(const MatType& referenceSetIn, const bool naive, const bool singleMode, + const double epsilon, const MetricType metric) : referenceTree(naive ? NULL : BuildTree(referenceSetIn, oldFromNewReferences)), @@ -83,12 +84,14 @@ NeighborSearch(const MatType& referenceSetIn, setOwner(false), naive(naive), singleMode(!naive && singleMode), // No single mode if naive. + epsilon(epsilon), metric(metric), baseCases(0), scores(0), treeNeedsReset(false) { - // Nothing to do. + if (epsilon < 0) + throw std::invalid_argument("epsilon must be non-negative"); } // Construct the object. @@ -103,6 +106,7 @@ NeighborSearch:: NeighborSearch(MatType&& referenceSetIn, const bool naive, const bool singleMode, + const double epsilon, const MetricType metric) : referenceTree(naive ? NULL : BuildTree(std::move(referenceSetIn), @@ -113,12 +117,14 @@ NeighborSearch(MatType&& referenceSetIn, setOwner(naive), naive(naive), singleMode(!naive && singleMode), + epsilon(epsilon), metric(metric), baseCases(0), scores(0), treeNeedsReset(false) { - // Nothing to do. + if (epsilon < 0) + throw std::invalid_argument("epsilon must be non-negative"); } // Construct the object. @@ -132,6 +138,7 @@ template:: NeighborSearch(Tree* referenceTree, const bool singleMode, + const double epsilon, const MetricType metric) : referenceTree(referenceTree), referenceSet(&referenceTree->Dataset()), @@ -139,12 +146,14 @@ NeighborSearch(Tree* referenceTree, setOwner(false), naive(false), singleMode(singleMode), + epsilon(epsilon), metric(metric), baseCases(0), scores(0), treeNeedsReset(false) { - // Nothing else to initialize. + if (epsilon < 0) + throw std::invalid_argument("epsilon must be non-negative"); } // Construct the object without a reference dataset. @@ -158,6 +167,7 @@ template:: NeighborSearch(const bool naive, const bool singleMode, + const double epsilon, const MetricType metric) : referenceTree(NULL), referenceSet(new MatType()), // Empty matrix. @@ -165,11 +175,14 @@ NeighborSearch:: setOwner(true), naive(naive), singleMode(singleMode), + epsilon(epsilon), metric(metric), baseCases(0), scores(0), treeNeedsReset(false) { + if (epsilon < 0) + throw std::invalid_argument("epsilon must be non-negative"); // Build the tree on the empty dataset, if necessary. if (!naive) { @@ -364,7 +377,8 @@ Search(const MatType& querySet, if (naive) { // Create the helper object for the tree traversal. - RuleType rules(*referenceSet, querySet, *neighborPtr, *distancePtr, metric); + RuleType rules(*referenceSet, querySet, *neighborPtr, *distancePtr, metric, + epsilon); // The naive brute-force traversal. for (size_t i = 0; i < querySet.n_cols; ++i) @@ -376,7 +390,8 @@ Search(const MatType& querySet, else if (singleMode) { // Create the helper object for the tree traversal. - RuleType rules(*referenceSet, querySet, *neighborPtr, *distancePtr, metric); + RuleType rules(*referenceSet, querySet, *neighborPtr, *distancePtr, metric, + epsilon); // Create the traverser. typename Tree::template SingleTreeTraverser traverser(rules); @@ -402,7 +417,7 @@ Search(const MatType& querySet, // Create the helper object for the tree traversal. RuleType rules(*referenceSet, queryTree->Dataset(), *neighborPtr, - *distancePtr, metric); + *distancePtr, metric, epsilon); // Create the traverser. TraversalType traverser(rules); @@ -527,7 +542,8 @@ Search(Tree* queryTree, // Create the helper object for the traversal. typedef NeighborSearchRules RuleType; - RuleType rules(*referenceSet, querySet, *neighborPtr, distances, metric); + RuleType rules(*referenceSet, querySet, *neighborPtr, distances, metric, + epsilon); // Create the traverser. TraversalType traverser(rules); @@ -598,7 +614,7 @@ Search(const size_t k, // Create the helper object for the traversal. typedef NeighborSearchRules RuleType; RuleType rules(*referenceSet, *referenceSet, *neighborPtr, *distancePtr, - metric, true /* don't return the same point as nearest neighbor */); + metric, epsilon, true /* don't return the same point as nearest neighbor */); if (naive) { diff --git a/src/mlpack/methods/neighbor_search/neighbor_search_rules.hpp b/src/mlpack/methods/neighbor_search/neighbor_search_rules.hpp index 474d22b005..47a7933dd0 100644 --- a/src/mlpack/methods/neighbor_search/neighbor_search_rules.hpp +++ b/src/mlpack/methods/neighbor_search/neighbor_search_rules.hpp @@ -22,6 +22,7 @@ class NeighborSearchRules arma::Mat& neighbors, arma::mat& distances, MetricType& metric, + const double epsilon = 0, const bool sameSet = false); /** * Get the distance from the query point to the reference point. @@ -120,6 +121,9 @@ class NeighborSearchRules //! Denotes whether or not the reference and query sets are the same. bool sameSet; + //! Relative error to be considered in approximate search. + const double epsilon; + //! The last query point BaseCase() was called with. size_t lastQueryIndex; //! The last reference point BaseCase() was called with. diff --git a/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp b/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp index cc2b957491..24f94856f5 100644 --- a/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp +++ b/src/mlpack/methods/neighbor_search/neighbor_search_rules_impl.hpp @@ -1,8 +1,8 @@ /** - * @file nearest_neighbor_rules_impl.hpp + * @file neighbor_search_rules_impl.hpp * @author Ryan Curtin * - * Implementation of NearestNeighborRules. + * Implementation of NeighborSearchRules. */ #ifndef MLPACK_METHODS_NEIGHBOR_SEARCH_NEAREST_NEIGHBOR_RULES_IMPL_HPP #define MLPACK_METHODS_NEIGHBOR_SEARCH_NEAREST_NEIGHBOR_RULES_IMPL_HPP @@ -20,6 +20,7 @@ NeighborSearchRules::NeighborSearchRules( arma::Mat& neighbors, arma::mat& distances, MetricType& metric, + const double epsilon, const bool sameSet) : referenceSet(referenceSet), querySet(querySet), @@ -27,6 +28,7 @@ NeighborSearchRules::NeighborSearchRules( distances(distances), metric(metric), sameSet(sameSet), + epsilon(epsilon), lastQueryIndex(querySet.n_cols), lastReferenceIndex(referenceSet.n_cols), baseCases(0), @@ -112,7 +114,8 @@ inline double NeighborSearchRules::Score( } // Compare against the best k'th distance for this query point so far. - const double bestDistance = distances(distances.n_rows - 1, queryIndex); + double bestDistance = distances(distances.n_rows - 1, queryIndex); + bestDistance = SortPolicy::Relax(bestDistance, epsilon); return (SortPolicy::IsBetter(distance, bestDistance)) ? distance : DBL_MAX; } @@ -128,7 +131,8 @@ inline double NeighborSearchRules::Rescore( return oldScore; // Just check the score again against the distances. - const double bestDistance = distances(distances.n_rows - 1, queryIndex); + double bestDistance = distances(distances.n_rows - 1, queryIndex); + bestDistance = SortPolicy::Relax(bestDistance, epsilon); return (SortPolicy::IsBetter(oldScore, bestDistance)) ? oldScore : DBL_MAX; } @@ -419,6 +423,8 @@ inline double NeighborSearchRules:: queryNode.Stat().SecondBound() = bestDistance; queryNode.Stat().AuxBound() = auxDistance; + worstDistance = SortPolicy::Relax(worstDistance, epsilon); + if (SortPolicy::IsBetter(worstDistance, bestDistance)) return worstDistance; else diff --git a/src/mlpack/methods/neighbor_search/ns_model.hpp b/src/mlpack/methods/neighbor_search/ns_model.hpp index d87549e920..711d640703 100644 --- a/src/mlpack/methods/neighbor_search/ns_model.hpp +++ b/src/mlpack/methods/neighbor_search/ns_model.hpp @@ -69,7 +69,11 @@ class MonoSearchVisitor : public boost::static_visitor MonoSearchVisitor(const size_t k, arma::Mat& neighbors, - arma::mat& distances); + arma::mat& distances) : + k(k), + neighbors(neighbors), + distances(distances) + {}; }; /** @@ -177,6 +181,16 @@ class NaiveVisitor : public boost::static_visitor bool& operator()(NSType *ns) const; }; +/** + * EpsilonVisitor exposes the Epsilon method of the given NSType. + */ +class EpsilonVisitor : public boost::static_visitor +{ + public: + template + double& operator()(NSType *ns) const; +}; + /** * ReferenceSetVisitor exposes the referenceSet of the given NSType. */ @@ -266,6 +280,10 @@ class NSModel bool Naive() const; bool& Naive(); + //! Expose Epsilon. + double Epsilon() const; + double& Epsilon(); + //! Expose leafSize. size_t LeafSize() const { return leafSize; } size_t& LeafSize() { return leafSize; } @@ -282,7 +300,8 @@ class NSModel void BuildModel(arma::mat&& referenceSet, const size_t leafSize, const bool naive, - const bool singleMode); + const bool singleMode, + const double epsilon = 0); //! Perform neighbor search. The query set will be reordered. void Search(arma::mat&& querySet, diff --git a/src/mlpack/methods/neighbor_search/ns_model_impl.hpp b/src/mlpack/methods/neighbor_search/ns_model_impl.hpp index 5ed97721cd..da8ab07f98 100644 --- a/src/mlpack/methods/neighbor_search/ns_model_impl.hpp +++ b/src/mlpack/methods/neighbor_search/ns_model_impl.hpp @@ -18,15 +18,6 @@ namespace mlpack { namespace neighbor { -//! Save parameters for monochromatic neighbor search. -MonoSearchVisitor::MonoSearchVisitor(const size_t k, - arma::Mat& neighbors, - arma::mat& distances) : - k(k), - neighbors(neighbors), - distances(distances) -{} - //! Monochromatic neighbor search on the given NSType instance. template void MonoSearchVisitor::operator()(NSType *ns) const @@ -185,6 +176,15 @@ bool& NaiveVisitor::operator()(NSType* ns) const throw std::runtime_error("no neighbor search model initialized"); } +//! Expose the Epsilon method of the given NSType. +template +double& EpsilonVisitor::operator()(NSType* ns) const +{ + if (ns) + return ns->Epsilon(); + throw std::runtime_error("no neighbor search model initialized"); +} + //! Expose the referenceSet of the given NSType. template const arma::mat& ReferenceSetVisitor::operator()(NSType* ns) const @@ -293,12 +293,25 @@ bool& NSModel::Naive() return boost::apply_visitor(NaiveVisitor(), nSearch); } +template +double NSModel::Epsilon() const +{ + return boost::apply_visitor(EpsilonVisitor(), nSearch); +} + +template +double& NSModel::Epsilon() +{ + return boost::apply_visitor(EpsilonVisitor(), nSearch); +} + //! Build the reference tree. template void NSModel::BuildModel(arma::mat&& referenceSet, const size_t leafSize, const bool naive, - const bool singleMode) + const bool singleMode, + const double epsilon) { // Initialize random basis if necessary. if (randomBasis) @@ -348,23 +361,26 @@ void NSModel::BuildModel(arma::mat&& referenceSet, switch (treeType) { case KD_TREE: - nSearch = new NSType(naive, singleMode); + nSearch = new NSType(naive, singleMode, + epsilon); break; case COVER_TREE: nSearch = new NSType(naive, - singleMode); + singleMode, epsilon); break; case R_TREE: - nSearch = new NSType(naive, singleMode); + nSearch = new NSType(naive, singleMode, epsilon); break; case R_STAR_TREE: - nSearch = new NSType(naive, singleMode); + nSearch = new NSType(naive, singleMode, + epsilon); break; case BALL_TREE: - nSearch = new NSType(naive, singleMode); + nSearch = new NSType(naive, singleMode, + epsilon); break; case X_TREE: - nSearch = new NSType(naive, singleMode); + nSearch = new NSType(naive, singleMode, epsilon); break; } @@ -389,13 +405,16 @@ void NSModel::Search(arma::mat&& querySet, if (randomBasis) querySet = q * querySet; - Log::Info << "Searching for " << k << " nearest neighbors with "; + Log::Info << "Searching for " << k << " neighbors with "; if (!Naive() && !SingleMode()) Log::Info << "dual-tree " << TreeName() << " search..." << std::endl; else if (!Naive()) Log::Info << "single-tree " << TreeName() << " search..." << std::endl; else Log::Info << "brute-force (naive) search..." << std::endl; + if (Epsilon() != 0 && !Naive()) + Log::Info << "Maximum of " << Epsilon() * 100 << "% relative error." + << std::endl; BiSearchVisitor search(querySet, k, neighbors, distances, leafSize); @@ -408,13 +427,16 @@ void NSModel::Search(const size_t k, arma::Mat& neighbors, arma::mat& distances) { - Log::Info << "Searching for " << k << " nearest neighbors with "; + Log::Info << "Searching for " << k << " neighbors with "; if (!Naive() && !SingleMode()) Log::Info << "dual-tree " << TreeName() << " search..." << std::endl; else if (!Naive()) Log::Info << "single-tree " << TreeName() << " search..." << std::endl; else Log::Info << "brute-force (naive) search..." << std::endl; + if (Epsilon() != 0 && !Naive()) + Log::Info << "Maximum of " << Epsilon() * 100 << "% relative error." + << std::endl; MonoSearchVisitor search(k, neighbors, distances); boost::apply_visitor(search, nSearch); diff --git a/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort.cpp b/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort.cpp index aee4877398..f58e4d2c22 100644 --- a/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort.cpp +++ b/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort.cpp @@ -1,5 +1,5 @@ /*** - * @file nearest_neighbor_sort.cpp + * @file furthest_neighbor_sort.cpp * @author Ryan Curtin * * Implementation of the simple FurthestNeighborSort policy class. @@ -12,7 +12,7 @@ size_t FurthestNeighborSort::SortDistance(const arma::vec& list, const arma::Col& indices, double newDistance) { - // The first element in the list is the nearest neighbor. We only want to + // The first element in the list is the furthest neighbor. We only want to // insert if the new distance is greater than the last element in the list. if (newDistance < list[list.n_elem - 1]) return (size_t() - 1); // Do not insert. diff --git a/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort.hpp b/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort.hpp index 87a72622e8..a69c167921 100644 --- a/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort.hpp +++ b/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort.hpp @@ -145,6 +145,23 @@ class FurthestNeighborSort */ static inline double CombineWorst(const double a, const double b) { return std::max(a - b, 0.0); } + + /** + * Return the given value relaxed. + * + * @param value Value to relax. + * @param epsilon Relative error (non-negative). + * + * @return double Value relaxed. + */ + static inline double Relax(const double value, const double epsilon) + { + if (value == 0) + return 0; + if (value == DBL_MAX || epsilon >= 1) + return DBL_MAX; + return (1 / (1 - epsilon)) * value; + } }; } // namespace neighbor diff --git a/src/mlpack/methods/neighbor_search/sort_policies/nearest_neighbor_sort.hpp b/src/mlpack/methods/neighbor_search/sort_policies/nearest_neighbor_sort.hpp index f57635a2a5..42a08b0641 100644 --- a/src/mlpack/methods/neighbor_search/sort_policies/nearest_neighbor_sort.hpp +++ b/src/mlpack/methods/neighbor_search/sort_policies/nearest_neighbor_sort.hpp @@ -150,6 +150,21 @@ class NearestNeighborSort return DBL_MAX; return a + b; } + + /** + * Return the given value relaxed. + * + * @param value Value to relax. + * @param epsilon Relative error (non-negative). + * + * @return double Value relaxed. + */ + static inline double Relax(const double value, const double epsilon) + { + if (value == DBL_MAX) + return DBL_MAX; + return (1 / (1 + epsilon)) * value; + } }; } // namespace neighbor diff --git a/src/mlpack/tests/CMakeLists.txt b/src/mlpack/tests/CMakeLists.txt index bd204f7e0d..967edeee4e 100644 --- a/src/mlpack/tests/CMakeLists.txt +++ b/src/mlpack/tests/CMakeLists.txt @@ -28,9 +28,11 @@ add_executable(mlpack_test kernel_pca_test.cpp kernel_traits_test.cpp kfn_test.cpp + akfn_test.cpp kmeans_test.cpp knn_test.cpp krann_search_test.cpp + aknn_test.cpp lars_test.cpp lbfgs_test.cpp lin_alg_test.cpp diff --git a/src/mlpack/tests/activation_functions_test.cpp b/src/mlpack/tests/activation_functions_test.cpp index 34ec009ddb..9f32f8d5f9 100644 --- a/src/mlpack/tests/activation_functions_test.cpp +++ b/src/mlpack/tests/activation_functions_test.cpp @@ -25,7 +25,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::ann; diff --git a/src/mlpack/tests/ada_delta_test.cpp b/src/mlpack/tests/ada_delta_test.cpp index 3471821505..483ce62b2f 100644 --- a/src/mlpack/tests/ada_delta_test.cpp +++ b/src/mlpack/tests/ada_delta_test.cpp @@ -12,7 +12,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace arma; using namespace mlpack::optimization; diff --git a/src/mlpack/tests/adaboost_test.cpp b/src/mlpack/tests/adaboost_test.cpp index fd5680d822..9edc57a606 100644 --- a/src/mlpack/tests/adaboost_test.cpp +++ b/src/mlpack/tests/adaboost_test.cpp @@ -8,7 +8,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" #include "serialization.hpp" using namespace arma; diff --git a/src/mlpack/tests/adam_test.cpp b/src/mlpack/tests/adam_test.cpp index df36980509..6daa5cb19c 100644 --- a/src/mlpack/tests/adam_test.cpp +++ b/src/mlpack/tests/adam_test.cpp @@ -11,7 +11,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace arma; using namespace mlpack::optimization; diff --git a/src/mlpack/tests/akfn_test.cpp b/src/mlpack/tests/akfn_test.cpp new file mode 100644 index 0000000000..61ec6f57a3 --- /dev/null +++ b/src/mlpack/tests/akfn_test.cpp @@ -0,0 +1,240 @@ +/** + * @file akfn_test.cpp + * + * Tests for KFN (k-furthest-neighbors) with different values of epsilon. + */ +#include +#include +#include +#include +#include "test_tools.hpp" + +using namespace mlpack; +using namespace mlpack::neighbor; +using namespace mlpack::tree; +using namespace mlpack::metric; +using namespace mlpack::bound; + +BOOST_AUTO_TEST_SUITE(AKFNTest); + +/** + * Test the dual-tree furthest-neighbors method with different values for + * epsilon. This uses both a query and reference dataset. + * + * Errors are produced if the results are not according to relative error. + */ +BOOST_AUTO_TEST_CASE(AproxVsExact1) +{ + arma::mat dataset; + + if (!data::Load("test_data_3_1000.csv", dataset)) + BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!"); + + KFN exact(dataset); + arma::Mat neighborsExact; + arma::mat distancesExact; + exact.Search(dataset, 15, neighborsExact, distancesExact); + + for (size_t c = 0; c < 4; c++) + { + KFN* akfn; + double epsilon; + + switch (c) + { + case 0: // Use the dual-tree method with e=0.02. + epsilon = 0.02; + break; + case 1: // Use the dual-tree method with e=0.05. + epsilon = 0.05; + break; + case 2: // Use the dual-tree method with e=0.10. + epsilon = 0.10; + break; + case 3: // Use the dual-tree method with e=0.20. + epsilon = 0.20; + break; + } + + // Now perform the actual calculation. + akfn = new KFN(dataset, false, false, epsilon); + arma::Mat neighborsAprox; + arma::mat distancesAprox; + akfn->Search(dataset, 15, neighborsAprox, distancesAprox); + + for (size_t i = 0; i < neighborsAprox.n_elem; i++) + REQUIRE_RELATIVE_ERR(distancesAprox(i), distancesExact(i), epsilon); + + // Clean the memory. + delete akfn; + } +} + +/** + * Test the dual-tree furthest-neighbors method with the exact method. This + * uses only a reference dataset. + * + * Errors are produced if the results are not according to relative error. + */ +BOOST_AUTO_TEST_CASE(AproxVsExact2) +{ + arma::mat dataset; + + if (!data::Load("test_data_3_1000.csv", dataset)) + BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!"); + + KFN exact(dataset); + arma::Mat neighborsExact; + arma::mat distancesExact; + exact.Search(15, neighborsExact, distancesExact); + + KFN akfn(dataset, false, false, 0.05); + arma::Mat neighborsAprox; + arma::mat distancesAprox; + akfn.Search(15, neighborsAprox, distancesAprox); + + for (size_t i = 0; i < neighborsAprox.n_elem; i++) + REQUIRE_RELATIVE_ERR(distancesAprox[i], distancesExact[i], 0.05); +} + +/** + * Test the single-tree furthest-neighbors method with the exact method. This + * uses only a reference dataset. + * + * Errors are produced if the results are not according to relative error. + */ +BOOST_AUTO_TEST_CASE(SingleTreeVsExact) +{ + arma::mat dataset; + + if (!data::Load("test_data_3_1000.csv", dataset)) + BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!"); + + KFN exact(dataset); + arma::Mat neighborsExact; + arma::mat distancesExact; + exact.Search(15, neighborsExact, distancesExact); + + KFN akfn(dataset, false, true, 0.05); + arma::Mat neighborsAprox; + arma::mat distancesAprox; + akfn.Search(15, neighborsAprox, distancesAprox); + + for (size_t i = 0; i < neighborsAprox.n_elem; i++) + REQUIRE_RELATIVE_ERR(distancesAprox[i], distancesExact[i], 0.05); +} + +/** + * Test the cover tree single-tree furthest-neighbors method against the exact + * method. This uses only a random reference dataset. + * + * Errors are produced if the results are not according to relative error. + */ +BOOST_AUTO_TEST_CASE(SingleCoverTreeTest) +{ + arma::mat dataset; + dataset.randu(75, 1000); // 75 dimensional, 1000 points. + + KFN exact(dataset); + arma::Mat neighborsExact; + arma::mat distancesExact; + exact.Search(dataset, 15, neighborsExact, distancesExact); + + StandardCoverTree, + arma::mat> tree(dataset); + + NeighborSearch, arma::mat, StandardCoverTree> + coverTreeSearch(&tree, true, 0.05); + + arma::Mat neighborsCoverTree; + arma::mat distancesCoverTree; + coverTreeSearch.Search(dataset, 15, neighborsCoverTree, distancesCoverTree); + + for (size_t i = 0; i < neighborsCoverTree.n_elem; ++i) + REQUIRE_RELATIVE_ERR(distancesCoverTree[i], distancesExact[i], 0.05); +} + +/** + * Test the cover tree dual-tree furthest neighbors method against the exact + * method. + * + * Errors are produced if the results are not according to relative error. + */ +BOOST_AUTO_TEST_CASE(DualCoverTreeTest) +{ + arma::mat dataset; + data::Load("test_data_3_1000.csv", dataset); + + KFN exact(dataset); + arma::Mat neighborsExact; + arma::mat distancesExact; + exact.Search(dataset, 15, neighborsExact, distancesExact); + + StandardCoverTree, + arma::mat> referenceTree(dataset); + + NeighborSearch, arma::mat, StandardCoverTree> + coverTreeSearch(&referenceTree, false, 0.05); + + arma::Mat neighborsCoverTree; + arma::mat distancesCoverTree; + coverTreeSearch.Search(dataset, 15, neighborsCoverTree, distancesCoverTree); + + for (size_t i = 0; i < neighborsCoverTree.n_elem; ++i) + REQUIRE_RELATIVE_ERR(distancesCoverTree[i], distancesExact[i], 0.05); +} + +/** + * Test the ball tree single-tree furthest-neighbors method against the exact + * method. This uses only a random reference dataset. + * + * Errors are produced if the results are not according to relative error. + */ +BOOST_AUTO_TEST_CASE(SingleBallTreeTest) +{ + arma::mat dataset; + dataset.randu(75, 1000); // 75 dimensional, 1000 points. + + KFN exact(dataset); + arma::Mat neighborsExact; + arma::mat distancesExact; + exact.Search(dataset, 15, neighborsExact, distancesExact); + + NeighborSearch + ballTreeSearch(dataset, false, true, 0.05); + + arma::Mat neighborsBallTree; + arma::mat distancesBallTree; + ballTreeSearch.Search(dataset, 15, neighborsBallTree, distancesBallTree); + + for (size_t i = 0; i < neighborsBallTree.n_elem; ++i) + REQUIRE_RELATIVE_ERR(distancesBallTree(i), distancesExact(i), 0.05); +} + +/** + * Test the ball tree dual-tree furthest neighbors method against the exact + * method. + * + * Errors are produced if the results are not according to relative error. + */ +BOOST_AUTO_TEST_CASE(DualBallTreeTest) +{ + arma::mat dataset; + data::Load("test_data_3_1000.csv", dataset); + + KFN exact(dataset); + arma::Mat neighborsExact; + arma::mat distancesExact; + exact.Search(15, neighborsExact, distancesExact); + + NeighborSearch + ballTreeSearch(dataset, false, false, 0.05); + arma::Mat neighborsBallTree; + arma::mat distancesBallTree; + ballTreeSearch.Search(15, neighborsBallTree, distancesBallTree); + + for (size_t i = 0; i < neighborsBallTree.n_elem; ++i) + REQUIRE_RELATIVE_ERR(distancesBallTree(i), distancesExact(i), 0.05); +} + +BOOST_AUTO_TEST_SUITE_END(); diff --git a/src/mlpack/tests/aknn_test.cpp b/src/mlpack/tests/aknn_test.cpp new file mode 100644 index 0000000000..23c7c9fda5 --- /dev/null +++ b/src/mlpack/tests/aknn_test.cpp @@ -0,0 +1,400 @@ +/** + * @file aknn_test.cpp + * + * Test file for KNN class with different values of epsilon. + */ +#include +#include +#include +#include +#include +#include +#include +#include "test_tools.hpp" + +using namespace mlpack; +using namespace mlpack::neighbor; +using namespace mlpack::tree; +using namespace mlpack::metric; +using namespace mlpack::bound; + +BOOST_AUTO_TEST_SUITE(AKNNTest); + +/** + * Test the dual-tree nearest-neighbors method with different values for + * epsilon. This uses both a query and reference dataset. + * + * Errors are produced if the results are not according to relative error. + */ +BOOST_AUTO_TEST_CASE(AproxVsExact1) +{ + arma::mat dataset; + + if (!data::Load("test_data_3_1000.csv", dataset)) + BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!"); + + KNN exact(dataset); + arma::Mat neighborsExact; + arma::mat distancesExact; + exact.Search(dataset, 15, neighborsExact, distancesExact); + + for (size_t c = 0; c < 4; c++) + { + KNN* aknn; + double epsilon; + + switch (c) + { + case 0: // Use the dual-tree method with e=0.02. + epsilon = 0.02; + break; + case 1: // Use the dual-tree method with e=0.05. + epsilon = 0.05; + break; + case 2: // Use the dual-tree method with e=0.10. + epsilon = 0.10; + break; + case 3: // Use the dual-tree method with e=0.20. + epsilon = 0.20; + break; + } + + // Now perform the actual calculation. + aknn = new KNN(dataset, false, false, epsilon); + arma::Mat neighborsAprox; + arma::mat distancesAprox; + aknn->Search(dataset, 15, neighborsAprox, distancesAprox); + + for (size_t i = 0; i < neighborsAprox.n_elem; i++) + REQUIRE_RELATIVE_ERR(distancesAprox(i), distancesExact(i), epsilon); + + // Clean the memory. + delete aknn; + } +} + +/** + * Test the dual-tree nearest-neighbors method with the exact method. This uses + * only a reference dataset. + * + * Errors are produced if the results are not according to relative error. + */ +BOOST_AUTO_TEST_CASE(AproxVsExact2) +{ + arma::mat dataset; + + if (!data::Load("test_data_3_1000.csv", dataset)) + BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!"); + + KNN exact(dataset); + arma::Mat neighborsExact; + arma::mat distancesExact; + exact.Search(15, neighborsExact, distancesExact); + + KNN aknn(dataset, false, false, 0.05); + arma::Mat neighborsAprox; + arma::mat distancesAprox; + aknn.Search(15, neighborsAprox, distancesAprox); + + for (size_t i = 0; i < neighborsAprox.n_elem; i++) + REQUIRE_RELATIVE_ERR(distancesAprox(i), distancesExact(i), 0.05); +} + +/** + * Test the single-tree nearest-neighbors method with the exact method. This + * uses only a reference dataset. + * + * Errors are produced if the results are not according to relative error. + */ +BOOST_AUTO_TEST_CASE(SingleTreeAproxVsExact) +{ + arma::mat dataset; + + if (!data::Load("test_data_3_1000.csv", dataset)) + BOOST_FAIL("Cannot load test dataset test_data_3_1000.csv!"); + + KNN exact(dataset); + arma::Mat neighborsExact; + arma::mat distancesExact; + exact.Search(15, neighborsExact, distancesExact); + + KNN aknn(dataset, false, true, 0.05); + arma::Mat neighborsAprox; + arma::mat distancesAprox; + aknn.Search(15, neighborsAprox, distancesAprox); + + for (size_t i = 0; i < neighborsAprox.n_elem; i++) + REQUIRE_RELATIVE_ERR(distancesAprox[i], distancesExact[i], 0.05); +} + +/** + * Test the cover tree single-tree nearest-neighbors method against the exact + * method. This uses only a random reference dataset. + * + * Errors are produced if the results are not according to relative error. + */ +BOOST_AUTO_TEST_CASE(SingleCoverTreeTest) +{ + arma::mat dataset; + dataset.randu(75, 1000); // 75 dimensional, 1000 points. + + KNN exact(dataset); + arma::Mat neighborsExact; + arma::mat distancesExact; + exact.Search(dataset, 15, neighborsExact, distancesExact); + + StandardCoverTree, + arma::mat> tree(dataset); + + NeighborSearch, arma::mat, StandardCoverTree> + coverTreeSearch(&tree, true, 0.05); + + arma::Mat neighborsCoverTree; + arma::mat distancesCoverTree; + coverTreeSearch.Search(dataset, 15, neighborsCoverTree, distancesCoverTree); + + for (size_t i = 0; i < neighborsCoverTree.n_elem; ++i) + REQUIRE_RELATIVE_ERR(distancesCoverTree[i], distancesExact[i], 0.05); +} + +/** + * Test the cover tree dual-tree nearest neighbors method against the exact + * method. + * + * Errors are produced if the results are not according to relative error. + */ +BOOST_AUTO_TEST_CASE(DualCoverTreeTest) +{ + arma::mat dataset; + data::Load("test_data_3_1000.csv", dataset); + + KNN exact(dataset); + arma::Mat neighborsExact; + arma::mat distancesExact; + exact.Search(dataset, 15, neighborsExact, distancesExact); + + StandardCoverTree, + arma::mat> referenceTree(dataset); + + NeighborSearch coverTreeSearch(&referenceTree, false, 0.05); + + arma::Mat neighborsCoverTree; + arma::mat distancesCoverTree; + coverTreeSearch.Search(&referenceTree, 15, neighborsCoverTree, + distancesCoverTree); + + for (size_t i = 0; i < neighborsCoverTree.n_elem; ++i) + REQUIRE_RELATIVE_ERR(distancesCoverTree[i], distancesExact[i], 0.05); +} + +/** + * Test the ball tree single-tree nearest-neighbors method against the exact + * method. This uses only a random reference dataset. + * + * Errors are produced if the results are not according to relative error. + */ +BOOST_AUTO_TEST_CASE(SingleBallTreeTest) +{ + arma::mat dataset; + dataset.randu(50, 300); // 50 dimensional, 300 points. + + KNN exact(dataset); + arma::Mat neighborsExact; + arma::mat distancesExact; + exact.Search(dataset, 15, neighborsExact, distancesExact); + + NeighborSearch + ballTreeSearch(dataset, false, true, 0.05); + + arma::Mat neighborsBallTree; + arma::mat distancesBallTree; + ballTreeSearch.Search(dataset, 15, neighborsBallTree, distancesBallTree); + + for (size_t i = 0; i < neighborsBallTree.n_elem; ++i) + REQUIRE_RELATIVE_ERR(distancesBallTree(i), distancesExact(i), 0.05); +} + +/** + * Test the ball tree dual-tree nearest neighbors method against the exact + * method. + * + * Errors are produced if the results are not according to relative error. + */ +BOOST_AUTO_TEST_CASE(DualBallTreeTest) +{ + arma::mat dataset; + data::Load("test_data_3_1000.csv", dataset); + + KNN exact(dataset); + arma::Mat neighborsExact; + arma::mat distancesExact; + exact.Search(15, neighborsExact, distancesExact); + + NeighborSearch + ballTreeSearch(dataset, false, false, 0.05); + arma::Mat neighborsBallTree; + arma::mat distancesBallTree; + ballTreeSearch.Search(15, neighborsBallTree, distancesBallTree); + + for (size_t i = 0; i < neighborsBallTree.n_elem; ++i) + REQUIRE_RELATIVE_ERR(distancesBallTree(i), distancesExact(i), 0.05); +} + +/** + * Make sure sparse nearest neighbors works with kd trees. + */ +BOOST_AUTO_TEST_CASE(SparseKNNKDTreeTest) +{ + // The dimensionality of these datasets must be high so that the probability + // of a completely empty point is very low. In this case, with dimensionality + // 70, the probability of all 70 dimensions being zero is 0.8^70 = 1.65e-7 in + // the reference set and 0.9^70 = 6.27e-4 in the query set. + arma::sp_mat queryDataset; + queryDataset.sprandu(70, 200, 0.2); + arma::sp_mat referenceDataset; + referenceDataset.sprandu(70, 500, 0.1); + arma::mat denseQuery(queryDataset); + arma::mat denseReference(referenceDataset); + + typedef NeighborSearch SparseKNN; + + SparseKNN aknn(referenceDataset, false, false, 0.05); + arma::mat distancesSparse; + arma::Mat neighborsSparse; + aknn.Search(queryDataset, 10, neighborsSparse, distancesSparse); + + KNN exact(denseReference); + arma::mat distancesExact; + arma::Mat neighborsExact; + exact.Search(denseQuery, 10, neighborsExact, distancesExact); + + for (size_t i = 0; i < neighborsExact.n_cols; ++i) + for (size_t j = 0; j < neighborsExact.n_rows; ++j) + REQUIRE_RELATIVE_ERR(distancesSparse(j, i), distancesExact(j, i), 0.05); +} + +/** + * Ensure that we can build an NSModel and get correct + * results. + */ +BOOST_AUTO_TEST_CASE(KNNModelTest) +{ + typedef NSModel KNNModel; + + arma::mat queryData = arma::randu(10, 50); + arma::mat referenceData = arma::randu(10, 200); + + // Build all the possible models. + KNNModel models[12]; + models[0] = KNNModel(KNNModel::TreeTypes::KD_TREE, true); + models[1] = KNNModel(KNNModel::TreeTypes::KD_TREE, false); + models[2] = KNNModel(KNNModel::TreeTypes::COVER_TREE, true); + models[3] = KNNModel(KNNModel::TreeTypes::COVER_TREE, false); + models[4] = KNNModel(KNNModel::TreeTypes::R_TREE, true); + models[5] = KNNModel(KNNModel::TreeTypes::R_TREE, false); + models[6] = KNNModel(KNNModel::TreeTypes::R_STAR_TREE, true); + models[7] = KNNModel(KNNModel::TreeTypes::R_STAR_TREE, false); + models[8] = KNNModel(KNNModel::TreeTypes::X_TREE, true); + models[9] = KNNModel(KNNModel::TreeTypes::X_TREE, false); + models[10] = KNNModel(KNNModel::TreeTypes::BALL_TREE, true); + models[11] = KNNModel(KNNModel::TreeTypes::BALL_TREE, false); + + for (size_t j = 0; j < 3; ++j) + { + // Get a baseline. + KNN aknn(referenceData); + arma::Mat neighborsExact; + arma::mat distancesExact; + aknn.Search(queryData, 3, neighborsExact, distancesExact); + + for (size_t i = 0; i < 12; ++i) + { + // We only have std::move() constructors so make a copy of our data. + arma::mat referenceCopy(referenceData); + arma::mat queryCopy(queryData); + if (j == 0) + models[i].BuildModel(std::move(referenceCopy), 20, false, false, 0.05); + if (j == 1) + models[i].BuildModel(std::move(referenceCopy), 20, false, true, 0.05); + if (j == 2) + models[i].BuildModel(std::move(referenceCopy), 20, true, false); + + arma::Mat neighborsAprox; + arma::mat distancesAprox; + + models[i].Search(std::move(queryCopy), 3, neighborsAprox, distancesAprox); + + BOOST_REQUIRE_EQUAL(neighborsAprox.n_rows, neighborsExact.n_rows); + BOOST_REQUIRE_EQUAL(neighborsAprox.n_cols, neighborsExact.n_cols); + BOOST_REQUIRE_EQUAL(neighborsAprox.n_elem, neighborsExact.n_elem); + BOOST_REQUIRE_EQUAL(distancesAprox.n_rows, distancesExact.n_rows); + BOOST_REQUIRE_EQUAL(distancesAprox.n_cols, distancesExact.n_cols); + BOOST_REQUIRE_EQUAL(distancesAprox.n_elem, distancesExact.n_elem); + for (size_t k = 0; k < distancesAprox.n_elem; ++k) + REQUIRE_RELATIVE_ERR(distancesAprox[k], distancesExact[k], 0.05); + } + } +} + +/** + * Ensure that we can build an NSModel and get correct + * results, in the case where the reference set is the same as the query set. + */ +BOOST_AUTO_TEST_CASE(KNNModelMonochromaticTest) +{ + typedef NSModel KNNModel; + + arma::mat referenceData = arma::randu(10, 200); + + // Build all the possible models. + KNNModel models[12]; + models[0] = KNNModel(KNNModel::TreeTypes::KD_TREE, true); + models[1] = KNNModel(KNNModel::TreeTypes::KD_TREE, false); + models[2] = KNNModel(KNNModel::TreeTypes::COVER_TREE, true); + models[3] = KNNModel(KNNModel::TreeTypes::COVER_TREE, false); + models[4] = KNNModel(KNNModel::TreeTypes::R_TREE, true); + models[5] = KNNModel(KNNModel::TreeTypes::R_TREE, false); + models[6] = KNNModel(KNNModel::TreeTypes::R_STAR_TREE, true); + models[7] = KNNModel(KNNModel::TreeTypes::R_STAR_TREE, false); + models[8] = KNNModel(KNNModel::TreeTypes::X_TREE, true); + models[9] = KNNModel(KNNModel::TreeTypes::X_TREE, false); + models[10] = KNNModel(KNNModel::TreeTypes::BALL_TREE, true); + models[11] = KNNModel(KNNModel::TreeTypes::BALL_TREE, false); + + for (size_t j = 0; j < 2; ++j) + { + // Get a baseline. + KNN exact(referenceData); + arma::Mat neighborsExact; + arma::mat distancesExact; + exact.Search(3, neighborsExact, distancesExact); + + for (size_t i = 0; i < 12; ++i) + { + // We only have a std::move() constructor... so copy the data. + arma::mat referenceCopy(referenceData); + if (j == 0) + models[i].BuildModel(std::move(referenceCopy), 20, false, false, 0.05); + if (j == 1) + models[i].BuildModel(std::move(referenceCopy), 20, false, true, 0.05); + + arma::Mat neighborsAprox; + arma::mat distancesAprox; + + models[i].Search(3, neighborsAprox, distancesAprox); + + BOOST_REQUIRE_EQUAL(neighborsAprox.n_rows, neighborsExact.n_rows); + BOOST_REQUIRE_EQUAL(neighborsAprox.n_cols, neighborsExact.n_cols); + BOOST_REQUIRE_EQUAL(neighborsAprox.n_elem, neighborsExact.n_elem); + BOOST_REQUIRE_EQUAL(distancesAprox.n_rows, distancesExact.n_rows); + BOOST_REQUIRE_EQUAL(distancesAprox.n_cols, distancesExact.n_cols); + BOOST_REQUIRE_EQUAL(distancesAprox.n_elem, distancesExact.n_elem); + for (size_t k = 0; k < distancesAprox.n_elem; ++k) + REQUIRE_RELATIVE_ERR(distancesAprox[k], distancesExact[k], 0.05); + } + } +} + +BOOST_AUTO_TEST_SUITE_END(); diff --git a/src/mlpack/tests/arma_extend_test.cpp b/src/mlpack/tests/arma_extend_test.cpp index 076a02f3e3..79fd0e90d4 100644 --- a/src/mlpack/tests/arma_extend_test.cpp +++ b/src/mlpack/tests/arma_extend_test.cpp @@ -7,7 +7,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace arma; diff --git a/src/mlpack/tests/armadillo_svd_test.cpp b/src/mlpack/tests/armadillo_svd_test.cpp index cb945b3219..5cfc156ee4 100644 --- a/src/mlpack/tests/armadillo_svd_test.cpp +++ b/src/mlpack/tests/armadillo_svd_test.cpp @@ -2,7 +2,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" BOOST_AUTO_TEST_SUITE(ArmadilloSVDTest); diff --git a/src/mlpack/tests/aug_lagrangian_test.cpp b/src/mlpack/tests/aug_lagrangian_test.cpp index 9507899dbb..fd6ce6d8d5 100644 --- a/src/mlpack/tests/aug_lagrangian_test.cpp +++ b/src/mlpack/tests/aug_lagrangian_test.cpp @@ -10,7 +10,7 @@ #include #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::optimization; diff --git a/src/mlpack/tests/binarize_test.cpp b/src/mlpack/tests/binarize_test.cpp index d0488a2303..ea2638baa0 100644 --- a/src/mlpack/tests/binarize_test.cpp +++ b/src/mlpack/tests/binarize_test.cpp @@ -9,7 +9,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace arma; diff --git a/src/mlpack/tests/cf_test.cpp b/src/mlpack/tests/cf_test.cpp index 834e326f76..6a1f1a6620 100644 --- a/src/mlpack/tests/cf_test.cpp +++ b/src/mlpack/tests/cf_test.cpp @@ -10,7 +10,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" #include "serialization.hpp" BOOST_AUTO_TEST_SUITE(CFTest); diff --git a/src/mlpack/tests/cli_test.cpp b/src/mlpack/tests/cli_test.cpp index d0ebbd3a6e..e6cbc64bfe 100644 --- a/src/mlpack/tests/cli_test.cpp +++ b/src/mlpack/tests/cli_test.cpp @@ -22,7 +22,7 @@ #define DEFAULT_INT 42 #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" #define BASH_RED "\033[0;31m" #define BASH_GREEN "\033[0;32m" diff --git a/src/mlpack/tests/convolution_test.cpp b/src/mlpack/tests/convolution_test.cpp index 368f32d7a2..b273330ff1 100644 --- a/src/mlpack/tests/convolution_test.cpp +++ b/src/mlpack/tests/convolution_test.cpp @@ -13,7 +13,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::ann; diff --git a/src/mlpack/tests/convolutional_network_test.cpp b/src/mlpack/tests/convolutional_network_test.cpp index baa2dff36d..1ca68c2dee 100644 --- a/src/mlpack/tests/convolutional_network_test.cpp +++ b/src/mlpack/tests/convolutional_network_test.cpp @@ -23,7 +23,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::ann; diff --git a/src/mlpack/tests/cosine_tree_test.cpp b/src/mlpack/tests/cosine_tree_test.cpp index 925684f496..6a8c586e6d 100644 --- a/src/mlpack/tests/cosine_tree_test.cpp +++ b/src/mlpack/tests/cosine_tree_test.cpp @@ -9,7 +9,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" BOOST_AUTO_TEST_SUITE(CosineTreeTest); diff --git a/src/mlpack/tests/decision_stump_test.cpp b/src/mlpack/tests/decision_stump_test.cpp index 36c83f7b9e..dae9875dc7 100644 --- a/src/mlpack/tests/decision_stump_test.cpp +++ b/src/mlpack/tests/decision_stump_test.cpp @@ -8,7 +8,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::decision_stump; diff --git a/src/mlpack/tests/det_test.cpp b/src/mlpack/tests/det_test.cpp index 9e4e91edfe..7e32defa4d 100644 --- a/src/mlpack/tests/det_test.cpp +++ b/src/mlpack/tests/det_test.cpp @@ -7,7 +7,7 @@ */ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" // This trick does not work on Windows. We will have to comment out the tests // that depend on it. diff --git a/src/mlpack/tests/distribution_test.cpp b/src/mlpack/tests/distribution_test.cpp index 0ca0dfda1e..d0261dd545 100644 --- a/src/mlpack/tests/distribution_test.cpp +++ b/src/mlpack/tests/distribution_test.cpp @@ -7,7 +7,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::distribution; diff --git a/src/mlpack/tests/emst_test.cpp b/src/mlpack/tests/emst_test.cpp index b4776584b1..9e8831aa2e 100644 --- a/src/mlpack/tests/emst_test.cpp +++ b/src/mlpack/tests/emst_test.cpp @@ -6,7 +6,7 @@ #include #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" #include diff --git a/src/mlpack/tests/fastmks_test.cpp b/src/mlpack/tests/fastmks_test.cpp index 973da37965..a7b20e9598 100644 --- a/src/mlpack/tests/fastmks_test.cpp +++ b/src/mlpack/tests/fastmks_test.cpp @@ -9,7 +9,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" #include "serialization.hpp" using namespace mlpack; diff --git a/src/mlpack/tests/feedforward_network_test.cpp b/src/mlpack/tests/feedforward_network_test.cpp index 6ae92d4ab2..e1412b462b 100644 --- a/src/mlpack/tests/feedforward_network_test.cpp +++ b/src/mlpack/tests/feedforward_network_test.cpp @@ -24,7 +24,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::ann; diff --git a/src/mlpack/tests/gmm_test.cpp b/src/mlpack/tests/gmm_test.cpp index 83b9fd3f5b..561c261afe 100644 --- a/src/mlpack/tests/gmm_test.cpp +++ b/src/mlpack/tests/gmm_test.cpp @@ -15,7 +15,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::gmm; diff --git a/src/mlpack/tests/hmm_test.cpp b/src/mlpack/tests/hmm_test.cpp index 762090fdf1..7ea6403b8a 100644 --- a/src/mlpack/tests/hmm_test.cpp +++ b/src/mlpack/tests/hmm_test.cpp @@ -8,7 +8,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::hmm; diff --git a/src/mlpack/tests/hoeffding_tree_test.cpp b/src/mlpack/tests/hoeffding_tree_test.cpp index c060267cd1..1c6a678cea 100644 --- a/src/mlpack/tests/hoeffding_tree_test.cpp +++ b/src/mlpack/tests/hoeffding_tree_test.cpp @@ -12,7 +12,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" #include "serialization.hpp" #include diff --git a/src/mlpack/tests/ind2sub_test.cpp b/src/mlpack/tests/ind2sub_test.cpp index ef1014be0c..7f3518f2e9 100644 --- a/src/mlpack/tests/ind2sub_test.cpp +++ b/src/mlpack/tests/ind2sub_test.cpp @@ -6,7 +6,7 @@ */ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" BOOST_AUTO_TEST_SUITE(ind2subTest); diff --git a/src/mlpack/tests/init_rules_test.cpp b/src/mlpack/tests/init_rules_test.cpp index 3d09c268a2..5ef4b9929b 100644 --- a/src/mlpack/tests/init_rules_test.cpp +++ b/src/mlpack/tests/init_rules_test.cpp @@ -14,7 +14,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::ann; diff --git a/src/mlpack/tests/kernel_pca_test.cpp b/src/mlpack/tests/kernel_pca_test.cpp index dae716ff0b..e154630fbf 100644 --- a/src/mlpack/tests/kernel_pca_test.cpp +++ b/src/mlpack/tests/kernel_pca_test.cpp @@ -10,7 +10,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" BOOST_AUTO_TEST_SUITE(KernelPCATest); diff --git a/src/mlpack/tests/kernel_test.cpp b/src/mlpack/tests/kernel_test.cpp index f21835c570..1c320190bd 100644 --- a/src/mlpack/tests/kernel_test.cpp +++ b/src/mlpack/tests/kernel_test.cpp @@ -19,7 +19,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::kernel; diff --git a/src/mlpack/tests/kernel_traits_test.cpp b/src/mlpack/tests/kernel_traits_test.cpp index f9eb88d93b..408df4bcaf 100644 --- a/src/mlpack/tests/kernel_traits_test.cpp +++ b/src/mlpack/tests/kernel_traits_test.cpp @@ -9,7 +9,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::kernel; diff --git a/src/mlpack/tests/kfn_test.cpp b/src/mlpack/tests/kfn_test.cpp index 2701a6a71f..1fc536a63b 100644 --- a/src/mlpack/tests/kfn_test.cpp +++ b/src/mlpack/tests/kfn_test.cpp @@ -7,7 +7,7 @@ #include #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::neighbor; diff --git a/src/mlpack/tests/kmeans_test.cpp b/src/mlpack/tests/kmeans_test.cpp index 3089a3e76f..9353557259 100644 --- a/src/mlpack/tests/kmeans_test.cpp +++ b/src/mlpack/tests/kmeans_test.cpp @@ -18,7 +18,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::kmeans; diff --git a/src/mlpack/tests/knn_test.cpp b/src/mlpack/tests/knn_test.cpp index 51a854aafb..85c6b7a79a 100644 --- a/src/mlpack/tests/knn_test.cpp +++ b/src/mlpack/tests/knn_test.cpp @@ -10,7 +10,7 @@ #include #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::neighbor; @@ -888,7 +888,9 @@ BOOST_AUTO_TEST_CASE(DualBallTreeTest) } } -// Make sure sparse nearest neighbors works with kd trees. +/** + * Make sure sparse nearest neighbors works with kd trees. + */ BOOST_AUTO_TEST_CASE(SparseKNNKDTreeTest) { // The dimensionality of these datasets must be high so that the probability diff --git a/src/mlpack/tests/krann_search_test.cpp b/src/mlpack/tests/krann_search_test.cpp index 37e9b35bcc..805adb39e9 100644 --- a/src/mlpack/tests/krann_search_test.cpp +++ b/src/mlpack/tests/krann_search_test.cpp @@ -10,7 +10,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" #include #include diff --git a/src/mlpack/tests/lars_test.cpp b/src/mlpack/tests/lars_test.cpp index 83bd0a8357..5b410fec3a 100644 --- a/src/mlpack/tests/lars_test.cpp +++ b/src/mlpack/tests/lars_test.cpp @@ -10,7 +10,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::regression; diff --git a/src/mlpack/tests/layer_traits_test.cpp b/src/mlpack/tests/layer_traits_test.cpp index 0a0e3cc50b..373d8781c5 100644 --- a/src/mlpack/tests/layer_traits_test.cpp +++ b/src/mlpack/tests/layer_traits_test.cpp @@ -13,7 +13,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::ann; diff --git a/src/mlpack/tests/lbfgs_test.cpp b/src/mlpack/tests/lbfgs_test.cpp index ea111f92ac..f16803c924 100644 --- a/src/mlpack/tests/lbfgs_test.cpp +++ b/src/mlpack/tests/lbfgs_test.cpp @@ -10,7 +10,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack::optimization; using namespace mlpack::optimization::test; diff --git a/src/mlpack/tests/lin_alg_test.cpp b/src/mlpack/tests/lin_alg_test.cpp index afb8876c30..6d11b36297 100644 --- a/src/mlpack/tests/lin_alg_test.cpp +++ b/src/mlpack/tests/lin_alg_test.cpp @@ -10,7 +10,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace arma; using namespace mlpack; diff --git a/src/mlpack/tests/linear_regression_test.cpp b/src/mlpack/tests/linear_regression_test.cpp index 6a0e8686d7..7f175c9544 100644 --- a/src/mlpack/tests/linear_regression_test.cpp +++ b/src/mlpack/tests/linear_regression_test.cpp @@ -7,7 +7,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::regression; diff --git a/src/mlpack/tests/load_save_test.cpp b/src/mlpack/tests/load_save_test.cpp index 3917aead1c..4eb8f12598 100644 --- a/src/mlpack/tests/load_save_test.cpp +++ b/src/mlpack/tests/load_save_test.cpp @@ -9,7 +9,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::data; diff --git a/src/mlpack/tests/local_coordinate_coding_test.cpp b/src/mlpack/tests/local_coordinate_coding_test.cpp index 2e51d90861..cc293b3767 100644 --- a/src/mlpack/tests/local_coordinate_coding_test.cpp +++ b/src/mlpack/tests/local_coordinate_coding_test.cpp @@ -10,7 +10,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" #include "serialization.hpp" using namespace arma; diff --git a/src/mlpack/tests/log_test.cpp b/src/mlpack/tests/log_test.cpp index 09a56c26f8..3a5a43d0d3 100644 --- a/src/mlpack/tests/log_test.cpp +++ b/src/mlpack/tests/log_test.cpp @@ -8,7 +8,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; diff --git a/src/mlpack/tests/logistic_regression_test.cpp b/src/mlpack/tests/logistic_regression_test.cpp index 7881bb248d..60acb1c0b5 100644 --- a/src/mlpack/tests/logistic_regression_test.cpp +++ b/src/mlpack/tests/logistic_regression_test.cpp @@ -9,7 +9,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::regression; diff --git a/src/mlpack/tests/lrsdp_test.cpp b/src/mlpack/tests/lrsdp_test.cpp index 1fe0f77f04..5a7b1cee3b 100644 --- a/src/mlpack/tests/lrsdp_test.cpp +++ b/src/mlpack/tests/lrsdp_test.cpp @@ -8,7 +8,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::optimization; diff --git a/src/mlpack/tests/lsh_test.cpp b/src/mlpack/tests/lsh_test.cpp index 65d1d78f66..8844972527 100644 --- a/src/mlpack/tests/lsh_test.cpp +++ b/src/mlpack/tests/lsh_test.cpp @@ -6,7 +6,7 @@ #include #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" #include #include diff --git a/src/mlpack/tests/lstm_peephole_test.cpp b/src/mlpack/tests/lstm_peephole_test.cpp index 6192e64123..fef1196235 100644 --- a/src/mlpack/tests/lstm_peephole_test.cpp +++ b/src/mlpack/tests/lstm_peephole_test.cpp @@ -9,7 +9,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::ann; diff --git a/src/mlpack/tests/math_test.cpp b/src/mlpack/tests/math_test.cpp index d5f80e8a3f..c68441d8c5 100644 --- a/src/mlpack/tests/math_test.cpp +++ b/src/mlpack/tests/math_test.cpp @@ -8,7 +8,7 @@ #include #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace math; diff --git a/src/mlpack/tests/matrix_completion_test.cpp b/src/mlpack/tests/matrix_completion_test.cpp index df7020f8ea..697e8098bc 100644 --- a/src/mlpack/tests/matrix_completion_test.cpp +++ b/src/mlpack/tests/matrix_completion_test.cpp @@ -8,7 +8,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::matrix_completion; diff --git a/src/mlpack/tests/maximal_inputs_test.cpp b/src/mlpack/tests/maximal_inputs_test.cpp index 62062e1a43..c6e8c109ce 100644 --- a/src/mlpack/tests/maximal_inputs_test.cpp +++ b/src/mlpack/tests/maximal_inputs_test.cpp @@ -9,7 +9,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; diff --git a/src/mlpack/tests/mean_shift_test.cpp b/src/mlpack/tests/mean_shift_test.cpp index e777e154aa..e7577d744f 100644 --- a/src/mlpack/tests/mean_shift_test.cpp +++ b/src/mlpack/tests/mean_shift_test.cpp @@ -8,7 +8,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::meanshift; diff --git a/src/mlpack/tests/metric_test.cpp b/src/mlpack/tests/metric_test.cpp index 7eff0bbe90..10952fea8e 100644 --- a/src/mlpack/tests/metric_test.cpp +++ b/src/mlpack/tests/metric_test.cpp @@ -6,7 +6,7 @@ #include #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace std; using namespace mlpack::metric; diff --git a/src/mlpack/tests/minibatch_sgd_test.cpp b/src/mlpack/tests/minibatch_sgd_test.cpp index 90e5b58577..410d4e3c45 100644 --- a/src/mlpack/tests/minibatch_sgd_test.cpp +++ b/src/mlpack/tests/minibatch_sgd_test.cpp @@ -13,7 +13,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace std; using namespace arma; diff --git a/src/mlpack/tests/mlpack_test.cpp b/src/mlpack/tests/mlpack_test.cpp index 5b9f7a8341..b10486ee0e 100644 --- a/src/mlpack/tests/mlpack_test.cpp +++ b/src/mlpack/tests/mlpack_test.cpp @@ -17,7 +17,7 @@ #endif #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" /** * Provide a global fixture for each test. diff --git a/src/mlpack/tests/nbc_test.cpp b/src/mlpack/tests/nbc_test.cpp index a3e43cdd64..dc96f8d954 100644 --- a/src/mlpack/tests/nbc_test.cpp +++ b/src/mlpack/tests/nbc_test.cpp @@ -7,7 +7,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace naive_bayes; diff --git a/src/mlpack/tests/nca_test.cpp b/src/mlpack/tests/nca_test.cpp index d1c00c5a8e..34b1a39714 100644 --- a/src/mlpack/tests/nca_test.cpp +++ b/src/mlpack/tests/nca_test.cpp @@ -11,7 +11,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::metric; diff --git a/src/mlpack/tests/network_util_test.cpp b/src/mlpack/tests/network_util_test.cpp index 766ed852c5..30d633129d 100644 --- a/src/mlpack/tests/network_util_test.cpp +++ b/src/mlpack/tests/network_util_test.cpp @@ -12,7 +12,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::ann; diff --git a/src/mlpack/tests/nmf_test.cpp b/src/mlpack/tests/nmf_test.cpp index 605ce39acc..f50b10a03b 100644 --- a/src/mlpack/tests/nmf_test.cpp +++ b/src/mlpack/tests/nmf_test.cpp @@ -12,7 +12,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" BOOST_AUTO_TEST_SUITE(NMFTest); diff --git a/src/mlpack/tests/nystroem_method_test.cpp b/src/mlpack/tests/nystroem_method_test.cpp index f705439b8e..241d9716b5 100644 --- a/src/mlpack/tests/nystroem_method_test.cpp +++ b/src/mlpack/tests/nystroem_method_test.cpp @@ -8,7 +8,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" #include #include diff --git a/src/mlpack/tests/pca_test.cpp b/src/mlpack/tests/pca_test.cpp index d7a78c8830..e4f3b6a311 100644 --- a/src/mlpack/tests/pca_test.cpp +++ b/src/mlpack/tests/pca_test.cpp @@ -8,7 +8,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" BOOST_AUTO_TEST_SUITE(PCATest); diff --git a/src/mlpack/tests/perceptron_test.cpp b/src/mlpack/tests/perceptron_test.cpp index 8f2de1e9b6..4fc1c8b57c 100644 --- a/src/mlpack/tests/perceptron_test.cpp +++ b/src/mlpack/tests/perceptron_test.cpp @@ -8,7 +8,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace arma; diff --git a/src/mlpack/tests/performance_functions_test.cpp b/src/mlpack/tests/performance_functions_test.cpp index 84839a13a7..9ad9342f45 100644 --- a/src/mlpack/tests/performance_functions_test.cpp +++ b/src/mlpack/tests/performance_functions_test.cpp @@ -11,7 +11,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::ann; diff --git a/src/mlpack/tests/pooling_rules_test.cpp b/src/mlpack/tests/pooling_rules_test.cpp index b85f3e85c9..9909316aeb 100644 --- a/src/mlpack/tests/pooling_rules_test.cpp +++ b/src/mlpack/tests/pooling_rules_test.cpp @@ -10,7 +10,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::ann; diff --git a/src/mlpack/tests/quic_svd_test.cpp b/src/mlpack/tests/quic_svd_test.cpp index f859e2116a..218bfff4c3 100644 --- a/src/mlpack/tests/quic_svd_test.cpp +++ b/src/mlpack/tests/quic_svd_test.cpp @@ -9,7 +9,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" BOOST_AUTO_TEST_SUITE(QUICSVDTest); diff --git a/src/mlpack/tests/radical_test.cpp b/src/mlpack/tests/radical_test.cpp index c5dc323696..c14f449ae7 100644 --- a/src/mlpack/tests/radical_test.cpp +++ b/src/mlpack/tests/radical_test.cpp @@ -7,7 +7,7 @@ #include #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" BOOST_AUTO_TEST_SUITE(RadicalTest); diff --git a/src/mlpack/tests/range_search_test.cpp b/src/mlpack/tests/range_search_test.cpp index 84cfed8dfc..88429719bd 100644 --- a/src/mlpack/tests/range_search_test.cpp +++ b/src/mlpack/tests/range_search_test.cpp @@ -9,7 +9,7 @@ #include #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::range; diff --git a/src/mlpack/tests/rectangle_tree_test.cpp b/src/mlpack/tests/rectangle_tree_test.cpp index f9278c554a..92595c6841 100644 --- a/src/mlpack/tests/rectangle_tree_test.cpp +++ b/src/mlpack/tests/rectangle_tree_test.cpp @@ -12,7 +12,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::neighbor; diff --git a/src/mlpack/tests/recurrent_network_test.cpp b/src/mlpack/tests/recurrent_network_test.cpp index 355aa81d4b..0ec1bcfe4f 100644 --- a/src/mlpack/tests/recurrent_network_test.cpp +++ b/src/mlpack/tests/recurrent_network_test.cpp @@ -20,7 +20,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::ann; diff --git a/src/mlpack/tests/regularized_svd_test.cpp b/src/mlpack/tests/regularized_svd_test.cpp index 1cbb741b5a..b29fe844f6 100644 --- a/src/mlpack/tests/regularized_svd_test.cpp +++ b/src/mlpack/tests/regularized_svd_test.cpp @@ -8,7 +8,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::svd; diff --git a/src/mlpack/tests/rmsprop_test.cpp b/src/mlpack/tests/rmsprop_test.cpp index 6fb3e745a3..b62d77f531 100644 --- a/src/mlpack/tests/rmsprop_test.cpp +++ b/src/mlpack/tests/rmsprop_test.cpp @@ -20,7 +20,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace arma; using namespace mlpack; diff --git a/src/mlpack/tests/sa_test.cpp b/src/mlpack/tests/sa_test.cpp index 05e7d9f181..3d87e5d954 100644 --- a/src/mlpack/tests/sa_test.cpp +++ b/src/mlpack/tests/sa_test.cpp @@ -14,7 +14,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace std; using namespace arma; diff --git a/src/mlpack/tests/sdp_primal_dual_test.cpp b/src/mlpack/tests/sdp_primal_dual_test.cpp index f3ed242497..0c42ea1d13 100644 --- a/src/mlpack/tests/sdp_primal_dual_test.cpp +++ b/src/mlpack/tests/sdp_primal_dual_test.cpp @@ -9,7 +9,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::optimization; diff --git a/src/mlpack/tests/serialization.hpp b/src/mlpack/tests/serialization.hpp index 39cf1d8ad9..a53a6e8b8f 100644 --- a/src/mlpack/tests/serialization.hpp +++ b/src/mlpack/tests/serialization.hpp @@ -17,7 +17,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" namespace mlpack { diff --git a/src/mlpack/tests/serialization_test.cpp b/src/mlpack/tests/serialization_test.cpp index 5dbb9aaf40..49a913f23f 100644 --- a/src/mlpack/tests/serialization_test.cpp +++ b/src/mlpack/tests/serialization_test.cpp @@ -7,7 +7,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" #include "serialization.hpp" #include diff --git a/src/mlpack/tests/sgd_test.cpp b/src/mlpack/tests/sgd_test.cpp index 1b273dcbdf..a8f389b1ac 100644 --- a/src/mlpack/tests/sgd_test.cpp +++ b/src/mlpack/tests/sgd_test.cpp @@ -10,7 +10,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace std; using namespace arma; diff --git a/src/mlpack/tests/softmax_regression_test.cpp b/src/mlpack/tests/softmax_regression_test.cpp index d7ed969dce..4fe8568eaf 100644 --- a/src/mlpack/tests/softmax_regression_test.cpp +++ b/src/mlpack/tests/softmax_regression_test.cpp @@ -8,7 +8,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::regression; diff --git a/src/mlpack/tests/sort_policy_test.cpp b/src/mlpack/tests/sort_policy_test.cpp index c6fcf5b87a..e336a76170 100644 --- a/src/mlpack/tests/sort_policy_test.cpp +++ b/src/mlpack/tests/sort_policy_test.cpp @@ -12,7 +12,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::neighbor; diff --git a/src/mlpack/tests/sparse_autoencoder_test.cpp b/src/mlpack/tests/sparse_autoencoder_test.cpp index 03d4e72d99..24d0cc6feb 100644 --- a/src/mlpack/tests/sparse_autoencoder_test.cpp +++ b/src/mlpack/tests/sparse_autoencoder_test.cpp @@ -8,7 +8,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::nn; diff --git a/src/mlpack/tests/sparse_coding_test.cpp b/src/mlpack/tests/sparse_coding_test.cpp index eab0b9c8aa..6815405e01 100644 --- a/src/mlpack/tests/sparse_coding_test.cpp +++ b/src/mlpack/tests/sparse_coding_test.cpp @@ -11,7 +11,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" #include "serialization.hpp" using namespace arma; diff --git a/src/mlpack/tests/split_data_test.cpp b/src/mlpack/tests/split_data_test.cpp index bbc529baae..1c52f632ba 100644 --- a/src/mlpack/tests/split_data_test.cpp +++ b/src/mlpack/tests/split_data_test.cpp @@ -8,7 +8,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace arma; diff --git a/src/mlpack/tests/svd_batch_test.cpp b/src/mlpack/tests/svd_batch_test.cpp index 36f354442f..45f9a12da3 100644 --- a/src/mlpack/tests/svd_batch_test.cpp +++ b/src/mlpack/tests/svd_batch_test.cpp @@ -7,7 +7,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" BOOST_AUTO_TEST_SUITE(SVDBatchTest); diff --git a/src/mlpack/tests/svd_incremental_test.cpp b/src/mlpack/tests/svd_incremental_test.cpp index 2def227416..4fdd4e4ca2 100644 --- a/src/mlpack/tests/svd_incremental_test.cpp +++ b/src/mlpack/tests/svd_incremental_test.cpp @@ -9,7 +9,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" BOOST_AUTO_TEST_SUITE(SVDIncrementalTest); diff --git a/src/mlpack/tests/termination_policy_test.cpp b/src/mlpack/tests/termination_policy_test.cpp index da51afbd88..945d82ce46 100644 --- a/src/mlpack/tests/termination_policy_test.cpp +++ b/src/mlpack/tests/termination_policy_test.cpp @@ -10,7 +10,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" BOOST_AUTO_TEST_SUITE(TerminationPolicyTest); diff --git a/src/mlpack/tests/old_boost_test_definitions.hpp b/src/mlpack/tests/test_tools.hpp similarity index 69% rename from src/mlpack/tests/old_boost_test_definitions.hpp rename to src/mlpack/tests/test_tools.hpp index 9d98c0b3ed..77fd1c189b 100644 --- a/src/mlpack/tests/old_boost_test_definitions.hpp +++ b/src/mlpack/tests/test_tools.hpp @@ -1,12 +1,11 @@ /** - * @file old_boost_test_definitions.hpp + * @file test_tools.hpp * @author Ryan Curtin * - * Ancient Boost.Test versions don't act how we expect. This file includes the - * things we need to fix that. + * This file includes some useful macros for tests. */ -#ifndef MLPACK_TESTS_OLD_BOOST_TEST_DEFINITIONS_HPP -#define MLPACK_TESTS_OLD_BOOST_TEST_DEFINITIONS_HPP +#ifndef MLPACK_TESTS_TEST_TOOLS_HPP +#define MLPACK_TESTS_TEST_TOOLS_HPP #include @@ -35,4 +34,9 @@ #endif +// Require the approximation L to be within a relative error of E respect to the +// actual value R. +#define REQUIRE_RELATIVE_ERR( L, R, E ) \ + BOOST_REQUIRE_LE( abs((R) - (L)), (E) * abs(R)) + #endif diff --git a/src/mlpack/tests/tree_test.cpp b/src/mlpack/tests/tree_test.cpp index 14b2f51dd0..81a94463b2 100644 --- a/src/mlpack/tests/tree_test.cpp +++ b/src/mlpack/tests/tree_test.cpp @@ -14,7 +14,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::math; diff --git a/src/mlpack/tests/tree_traits_test.cpp b/src/mlpack/tests/tree_traits_test.cpp index e7b4925924..cf0395d19f 100644 --- a/src/mlpack/tests/tree_traits_test.cpp +++ b/src/mlpack/tests/tree_traits_test.cpp @@ -15,7 +15,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::tree; diff --git a/src/mlpack/tests/union_find_test.cpp b/src/mlpack/tests/union_find_test.cpp index 8468a2a78e..89e78ea8b6 100644 --- a/src/mlpack/tests/union_find_test.cpp +++ b/src/mlpack/tests/union_find_test.cpp @@ -8,7 +8,7 @@ #include #include -#include "old_boost_test_definitions.hpp" +#include "test_tools.hpp" using namespace mlpack; using namespace mlpack::emst;