[mlpack-svn] r10093 - mlpack/trunk/src/contrib/march/n_point

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Oct 31 12:24:36 EDT 2011


Author: march
Date: 2011-10-31 12:24:35 -0400 (Mon, 31 Oct 2011)
New Revision: 10093

Added:
   mlpack/trunk/src/contrib/march/n_point/generate_random_problem.cc
   mlpack/trunk/src/contrib/march/n_point/generate_random_problem.h
   mlpack/trunk/src/contrib/march/n_point/two_point_timing.cc
Modified:
   mlpack/trunk/src/contrib/march/n_point/CMakeLists.txt
   mlpack/trunk/src/contrib/march/n_point/angle_matcher.h
   mlpack/trunk/src/contrib/march/n_point/n_point_single_main.cc
   mlpack/trunk/src/contrib/march/n_point/node_tuple.h
   mlpack/trunk/src/contrib/march/n_point/single_bandwidth.test.cc
   mlpack/trunk/src/contrib/march/n_point/test_single_bandwidth.cc
   mlpack/trunk/src/contrib/march/n_point/test_single_bandwidth.h
Log:
refactored random generation stuff, wrote a simple timing case

Modified: mlpack/trunk/src/contrib/march/n_point/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/contrib/march/n_point/CMakeLists.txt	2011-10-31 16:09:32 UTC (rev 10092)
+++ mlpack/trunk/src/contrib/march/n_point/CMakeLists.txt	2011-10-31 16:24:35 UTC (rev 10093)
@@ -14,6 +14,8 @@
   angle_results.cc  
   efficient_resampling.h
   efficient_resampling.cc
+  generate_random_problem.h
+  generate_random_problem.cc
   generic_npt_alg.h
   generic_npt_alg_impl.h
   naive_resampling.h
@@ -85,4 +87,14 @@
 	boost_random
 )
 
+add_executable(two_point_timing
+	EXCLUDE_FROM_ALL
+	two_point_timing.cc
+)
+target_link_libraries(two_point_timing
+#	mlpack
+	mlpack_contrib
+	boost_random
+)
 
+

Modified: mlpack/trunk/src/contrib/march/n_point/angle_matcher.h
===================================================================
--- mlpack/trunk/src/contrib/march/n_point/angle_matcher.h	2011-10-31 16:09:32 UTC (rev 10092)
+++ mlpack/trunk/src/contrib/march/n_point/angle_matcher.h	2011-10-31 16:24:35 UTC (rev 10093)
@@ -15,8 +15,8 @@
 
 #include "boost/multi_array.hpp"
 #include "node_tuple.h"
-#include <mlpack/core/tree/bounds.h>
-#include <mlpack/core/tree/spacetree.h>
+//#include <mlpack/core/tree/bounds.h>
+//#include <mlpack/core/tree/spacetree.h>
 
 /*
  *  Takes in the parameters for an angle-based set of matchers (r1, r2_mult,
@@ -40,7 +40,7 @@
 // only satisfy one
 
 // IMPORTANT: I think I'm assuming that r2 is enough larger than r1 that there
-// isn't any overlap
+// isn't any overlap - NOT true any more
 
 namespace npt {
 

Added: mlpack/trunk/src/contrib/march/n_point/generate_random_problem.cc
===================================================================
--- mlpack/trunk/src/contrib/march/n_point/generate_random_problem.cc	                        (rev 0)
+++ mlpack/trunk/src/contrib/march/n_point/generate_random_problem.cc	2011-10-31 16:24:35 UTC (rev 10093)
@@ -0,0 +1,73 @@
+/*
+ *  generate_random_problem.cc
+ *  
+ *
+ *  Created by William March on 10/31/11.
+ *  Copyright 2011 __MyCompanyName__. All rights reserved.
+ *
+ */
+
+#include "generate_random_problem.h"
+
+void npt::GenerateRandomProblem::GenerateRandomSet(arma::mat& data) {
+  
+  for (unsigned int row_ind = 0; row_ind < data.n_rows; row_ind++) {
+    
+    for (unsigned int col_ind = 0; col_ind < data.n_cols; col_ind++) {
+      
+      data(row_ind,col_ind) = data_gen();
+      
+    }
+    
+  }
+  
+} // GenerateRandomSet
+
+// fills the matcher with distances and returns the thickness
+double npt::GenerateRandomProblem::GenerateRandomMatcher(arma::mat& matcher) {
+  
+  for (unsigned int i = 0; i < matcher.n_rows; i++) {
+    
+    matcher(i,i) = 0.0;
+    
+    for (unsigned int j = i+1; j < matcher.n_cols; j++) {
+      
+      matcher(i,j) = matcher_dist_gen();
+      matcher(j,i) = matcher(i,j);
+      
+    }
+    
+  }
+  
+  return matcher_thick_gen();
+  
+} 
+
+void npt::GenerateRandomProblem::GenerateRandomMatcher(arma::mat& lower_bounds, 
+                                                       arma::mat& upper_bounds) {
+  
+  assert(lower_bounds.n_cols == lower_bounds.n_rows);
+  assert(lower_bounds.n_cols == upper_bounds.n_rows);
+  assert(upper_bounds.n_rows == upper_bounds.n_cols);
+  
+  for (unsigned int i = 0; i < lower_bounds.n_rows; i++) {
+    
+    lower_bounds(i,i) = 0.0;
+    upper_bounds(i,i) = 0.0;
+    
+    for (unsigned int j = i+1; j < lower_bounds.n_cols; j++) {
+      
+      double val1 = matcher_dist_gen();
+      double val2 = matcher_dist_gen();
+      
+      upper_bounds(i,j) = std::max(val1, val2);
+      upper_bounds(j,i) = upper_bounds(i,j);
+      
+      lower_bounds(i,j) = std::min(val1, val2);
+      lower_bounds(j,i) = lower_bounds(i,j);
+      
+    }
+    
+  }
+  
+} // GenerateRandomMatcher (upper and lower)

Added: mlpack/trunk/src/contrib/march/n_point/generate_random_problem.h
===================================================================
--- mlpack/trunk/src/contrib/march/n_point/generate_random_problem.h	                        (rev 0)
+++ mlpack/trunk/src/contrib/march/n_point/generate_random_problem.h	2011-10-31 16:24:35 UTC (rev 10093)
@@ -0,0 +1,73 @@
+/*
+ *  generate_random_problem.h
+ *  
+ *
+ *  Created by William March on 10/31/11.
+ *  Copyright 2011 __MyCompanyName__. All rights reserved.
+ *
+ */
+
+#ifndef GENERATE_RANDOM_PROBLEM_H
+#define GENERATE_RANDOM_PROBLEM_H
+
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/random/uniform_int_distribution.hpp>
+#include <boost/random/uniform_real_distribution.hpp>
+#include <boost/random/uniform_01.hpp>
+#include <boost/random/variate_generator.hpp>
+
+#include <mlpack/core.h>
+
+
+
+namespace npt {
+
+  class GenerateRandomProblem {
+    
+  private:
+    
+    boost::random::mt19937 generator_;
+    boost::random::uniform_int_distribution<> num_data_dist; 
+    
+    boost::random::uniform_01<double> data_dist;
+    boost::random::uniform_real_distribution<double> matcher_dist;
+    boost::random::uniform_real_distribution<double> matcher_thick_dist;
+    
+    boost::random::variate_generator<boost::random::mt19937, 
+    boost::random::uniform_01<> > data_gen;
+    boost::random::variate_generator<boost::random::mt19937, 
+    boost::random::uniform_real_distribution<> > matcher_dist_gen;
+    boost::random::variate_generator<boost::random::mt19937, 
+    boost::random::uniform_real_distribution<> > matcher_thick_gen;
+    
+    
+  public:
+    
+    void GenerateRandomSet(arma::mat& data);
+    
+    // fills the matcher with distances and returns the thickness
+    double GenerateRandomMatcher(arma::mat& matcher);
+    
+    void GenerateRandomMatcher(arma::mat& lower_bounds, 
+                               arma::mat& upper_bounds);
+
+    
+    GenerateRandomProblem() : 
+    generator_(std::time(0)),
+    matcher_dist(0.05, 0.25), 
+    matcher_thick_dist(0.05,0.1),
+    data_gen(generator_, data_dist),
+    matcher_dist_gen(generator_, matcher_dist),
+    matcher_thick_gen(generator_, matcher_thick_dist)
+    {
+      //printf("constructed test class\n");
+    }
+    
+    
+    
+  }; // class
+  
+} 
+
+
+#endif
\ No newline at end of file

Modified: mlpack/trunk/src/contrib/march/n_point/n_point_single_main.cc
===================================================================
--- mlpack/trunk/src/contrib/march/n_point/n_point_single_main.cc	2011-10-31 16:09:32 UTC (rev 10092)
+++ mlpack/trunk/src/contrib/march/n_point/n_point_single_main.cc	2011-10-31 16:24:35 UTC (rev 10093)
@@ -9,7 +9,7 @@
 
 //#include "fastlib/fastlib.h"
 //#include <fastlib/fx/io.h>
-#include<log.h>
+//#include<mlpack/log.h>
 
 #include <mlpack/core.h>
 

Modified: mlpack/trunk/src/contrib/march/n_point/node_tuple.h
===================================================================
--- mlpack/trunk/src/contrib/march/n_point/node_tuple.h	2011-10-31 16:09:32 UTC (rev 10092)
+++ mlpack/trunk/src/contrib/march/n_point/node_tuple.h	2011-10-31 16:24:35 UTC (rev 10093)
@@ -11,8 +11,8 @@
 #define NODE_TUPLE_H
 
 #include <mlpack/core.h>
-#include <mlpack/core/tree/bounds.h>
-#include <mlpack/core/tree/spacetree.h>
+#include <mlpack/core/tree/bounds.hpp>
+#include <mlpack/core/tree/binary_space_tree.hpp>
 
 /*
  *  This class holds an ordered tuple of n tree nodes (for an n-point 
@@ -26,11 +26,11 @@
  *
  */
 
+using namespace mlpack;
 
 namespace npt {
 
-  typedef mlpack::tree::BinarySpaceTree<mlpack::bound::HRectBound<2>, 
-                                        arma::mat> NptNode;
+  typedef tree::BinarySpaceTree<bound::HRectBound<2> > NptNode;
   
   // this is now just responsible for checking symmetry
   class NodeTuple { 

Modified: mlpack/trunk/src/contrib/march/n_point/single_bandwidth.test.cc
===================================================================
--- mlpack/trunk/src/contrib/march/n_point/single_bandwidth.test.cc	2011-10-31 16:09:32 UTC (rev 10092)
+++ mlpack/trunk/src/contrib/march/n_point/single_bandwidth.test.cc	2011-10-31 16:24:35 UTC (rev 10093)
@@ -25,6 +25,7 @@
 
 BOOST_AUTO_TEST_CASE(TestCaseSingleBandwidth) {
 
+  
   printf("making test class\n");
   npt::TestSingleBandwidth single_test;
   printf("running tests\n");

Modified: mlpack/trunk/src/contrib/march/n_point/test_single_bandwidth.cc
===================================================================
--- mlpack/trunk/src/contrib/march/n_point/test_single_bandwidth.cc	2011-10-31 16:09:32 UTC (rev 10092)
+++ mlpack/trunk/src/contrib/march/n_point/test_single_bandwidth.cc	2011-10-31 16:24:35 UTC (rev 10093)
@@ -9,78 +9,45 @@
 
 #include "test_single_bandwidth.h"
 
-void npt::TestSingleBandwidth::GenerateRandomSet_(arma::mat& data) {
-  
-  for (unsigned int row_ind = 0; row_ind < data.n_rows; row_ind++) {
-    
-    for (unsigned int col_ind = 0; col_ind < data.n_cols; col_ind++) {
-      
-      data(row_ind,col_ind) = data_gen();
-      
-    }
-    
-  }
-  
-} // GenerateRandomSet();
 
-double npt::TestSingleBandwidth::GenerateRandomMatcher_(arma::mat& matcher) {
-  
-  for (unsigned int i = 0; i < matcher.n_rows; i++) {
-    
-    matcher(i,i) = 0.0;
-    
-    for (unsigned int j = i+1; j < matcher.n_cols; j++) {
-      
-      matcher(i,j) = matcher_dist_gen();
-      matcher(j,i) = matcher(i,j);
-      
-    }
-    
-  }
-  
-  return matcher_thick_gen();
-  
-}
-
-
 bool npt::TestSingleBandwidth::StressTest() {
   
   // pick a number of points and number of dimensions (small)
-  
-  printf("generating data\n");
+    
+  //printf("generating data\n");
   int num_data_points = num_data_gen();
-  printf("num_data: %d, generating random\n", num_data_points);
+  //printf("num_data: %d, generating random\n", num_data_points);
   int num_random_points = num_data_gen();
-  printf("num_random: %d, generating tuple_size\n", num_random_points);
+  //printf("num_random: %d, generating tuple_size\n", num_random_points);
   int num_dimensions = 3;
-  printf("generating tuple size\n");
+  //printf("generating tuple size\n");
   int tuple_size = tuple_size_gen();
   
-  mlpack::Log::Info << "====Running Test===\n";
-  mlpack::Log::Info << "Tuple size: " << tuple_size << "\n";
-  mlpack::Log::Info << "Num data points: " << num_data_points << "\n";
-  mlpack::Log::Info << "Num random points: " << num_random_points << "\n";
+  std::cout << "====Running Test===\n";
+  std::cout << "Tuple size: " << tuple_size << "\n";
+  std::cout << "Num data points: " << num_data_points << "\n";
+  std::cout << "Num random points: " << num_random_points << "\n";
   
   
   // Generate a random data set
   
-  printf("generating_data\n");
+  //printf("generating_data\n");
   arma::mat data_mat(num_dimensions, num_data_points);
-  GenerateRandomSet_(data_mat);
+  problem_gen.GenerateRandomSet(data_mat);
   arma::colvec data_weights(num_data_points);
   
   // Generate a random random set
-  printf("generating_random\n");
+  //printf("generating_random\n");
   arma::mat random_mat(num_dimensions, num_random_points);
-  GenerateRandomSet_(random_mat);
+  problem_gen.GenerateRandomSet(random_mat);
   arma::colvec random_weights(num_random_points);
-  printf("random generated\n");
+  //printf("random generated\n");
   
   // Generate a random matcher and matcher thickness multiplier
   
-  printf("generating matchers \n");
+  //printf("generating matchers \n");
   arma::mat matcher_dists(tuple_size, tuple_size);
-  double matcher_thick = GenerateRandomMatcher_(matcher_dists);
+  double matcher_thick = problem_gen.GenerateRandomMatcher(matcher_dists);
   
   // Run tree algorithm
 
@@ -98,9 +65,9 @@
   
   mlpack::CLI::GetParam<int>("tree/leaf_size") = leaf_size;
   
-  mlpack::Log::Info << "Leaf size: " << leaf_size << "\n";
+  std::cout << "Leaf size: " << leaf_size << "\n";
   
-  printf("building trees\n");
+  //printf("building trees\n");
   NptNode* data_tree = new NptNode(data_mat, old_from_new_data);
   NptNode* random_tree = new NptNode(random_mat, old_from_new_random);
   
@@ -114,7 +81,7 @@
   
   bool results_match = true;
   
-  printf("starting main loop\n");
+  //printf("starting main loop\n");
   // iterate over num_random
   for (int num_random = 0; num_random <= tuple_size && results_match; 
        num_random++) {
@@ -166,8 +133,8 @@
     // Get and store the result
     tree_results[num_random] = tree_matcher.results();
     
-    mlpack::Log::Info << "tree_results[" << num_random << "]: ";
-    mlpack::Log::Info << tree_results[num_random] << "\n";
+    std::cout << "tree_results[" << num_random << "]: ";
+    std::cout << tree_results[num_random] << "\n";
     
     
     printf("Running naive algorithm, iteration %d\n", num_random);
@@ -183,8 +150,8 @@
     
     naive_results[num_random] = naive_matcher.results();
     
-    mlpack::Log::Info << "naive_results[" << num_random << "]: ";
-    mlpack::Log::Info << naive_results[num_random] << "\n";
+    std::cout << "naive_results[" << num_random << "]: ";
+    std::cout << naive_results[num_random] << "\n";
 
     
     // Compare results
@@ -192,14 +159,14 @@
     
     if (!results_match) {
       
-      //mlpack::Log::Info << "Results fail to match for num_random: ";
-      //mlpack::Log::Info << num_random << "\n";
+      //std::cout << "Results fail to match for num_random: ";
+      //std::cout << num_random << "\n";
       printf("Results fail to match\n");
       
       
     }
     
-    mlpack::Log::Info << "\n\n";
+    std::cout << "\n\n";
     
   } // for num_random
     
@@ -212,13 +179,13 @@
 // the driver
 bool npt::TestSingleBandwidth::StressTestMain() {
  
-  printf("running stress test main\n");
+  //printf("running stress test main\n");
   
   bool results_match = true;
   
   for (int i = 0; i < 5 && results_match; i++) {
    
-    printf("Running test: %d\n", i);
+    //printf("Running test: %d\n", i);
     results_match = StressTest();
     
   }

Modified: mlpack/trunk/src/contrib/march/n_point/test_single_bandwidth.h
===================================================================
--- mlpack/trunk/src/contrib/march/n_point/test_single_bandwidth.h	2011-10-31 16:09:32 UTC (rev 10092)
+++ mlpack/trunk/src/contrib/march/n_point/test_single_bandwidth.h	2011-10-31 16:24:35 UTC (rev 10093)
@@ -15,6 +15,7 @@
 #include <time.h>
 #include "generic_npt_alg.h"
 #include "single_matcher.h"
+#include "generate_random_problem.h"
 
 // include boost random number generation stuff
 #include <boost/random/mersenne_twister.hpp>
@@ -36,10 +37,6 @@
     
     boost::random::uniform_int_distribution<> num_leaves_dist;
     
-    boost::random::uniform_01<double> data_dist;
-    boost::random::uniform_real_distribution<double> matcher_dist;
-    boost::random::uniform_real_distribution<double> matcher_thick_dist;
-    
     boost::random::variate_generator<boost::random::mt19937, 
       boost::random::uniform_int_distribution<> > num_data_gen;
     boost::random::variate_generator<boost::random::mt19937, 
@@ -47,35 +44,20 @@
     boost::random::variate_generator<boost::random::mt19937, 
       boost::random::uniform_int_distribution<> > num_leaves_gen;
     
-    boost::random::variate_generator<boost::random::mt19937, 
-      boost::random::uniform_01<> > data_gen;
-    boost::random::variate_generator<boost::random::mt19937, 
-      boost::random::uniform_real_distribution<> > matcher_dist_gen;
-    boost::random::variate_generator<boost::random::mt19937, 
-      boost::random::uniform_real_distribution<> > matcher_thick_gen;
+    GenerateRandomProblem problem_gen;
     
-    
-    
-    void GenerateRandomSet_(arma::mat& data);
-    
-    // fills the matcher with distances and returns the thickness
-    double GenerateRandomMatcher_(arma::mat& matcher);
-    
   public:
     
     TestSingleBandwidth() : 
     generator_(std::time(0)),
     num_data_dist(100,300), tuple_size_dist(2,3),
-    num_leaves_dist(1,25), matcher_dist(0.05, 0.25), 
-    matcher_thick_dist(0.05,0.1),
+    num_leaves_dist(1,25),
     num_data_gen(generator_, num_data_dist),
     tuple_size_gen(generator_, tuple_size_dist),
     num_leaves_gen(generator_, num_leaves_dist),
-    data_gen(generator_, data_dist),
-    matcher_dist_gen(generator_, matcher_dist),
-    matcher_thick_gen(generator_, matcher_thick_dist)
+    problem_gen()
     {
-      printf("constructed test class\n");
+      //printf("constructed test class\n");
     }
     
     bool StressTest();

Added: mlpack/trunk/src/contrib/march/n_point/two_point_timing.cc
===================================================================
--- mlpack/trunk/src/contrib/march/n_point/two_point_timing.cc	                        (rev 0)
+++ mlpack/trunk/src/contrib/march/n_point/two_point_timing.cc	2011-10-31 16:24:35 UTC (rev 10093)
@@ -0,0 +1,87 @@
+/*
+ *  two_point_timing.cc
+ *  
+ *
+ *  Created by William March on 10/31/11.
+ *  Copyright 2011 __MyCompanyName__. All rights reserved.
+ *
+ */
+
+
+#include <mlpack/core.h>
+
+#include "generate_random_problem.h"
+#include "generic_npt_alg.h"
+#include "single_matcher.h"
+
+
+PARAM_INT_REQ("num_points", "The size of the computation to run", NULL);
+PARAM_INT("tuple_size", "The order of the correlation to time", NULL, 2);
+
+using namespace mlpack;
+using namespace npt;
+
+int main(int argc, char* argv[]) {
+  
+  //fx_init(argc, argv, NULL);
+  CLI::ParseCommandLine(argc, argv);
+
+  GenerateRandomProblem problem_gen;
+  
+  int num_points = CLI::GetParam<int>("num_points");
+  
+  int tuple_size = CLI::GetParam<int>("tuple_size");
+  
+  arma::mat data_mat(3, num_points);
+  problem_gen.GenerateRandomSet(data_mat);
+  
+  arma::colvec weights(num_points);
+
+  arma::mat matcher_lower_bounds(tuple_size, tuple_size);
+  arma::mat matcher_upper_bounds(tuple_size, tuple_size);
+  
+  problem_gen.GenerateRandomMatcher(matcher_lower_bounds, matcher_upper_bounds);
+  
+  CLI::StartTimer("single_bandwidth_time");
+  
+  std::vector<size_t> old_from_new_data;
+  
+  NptNode* data_tree = new NptNode(data_mat, old_from_new_data);
+
+  
+  std::vector<arma::mat*> comp_mats(tuple_size);
+  std::vector<int> comp_multi(2);
+  comp_multi[0] = 0;
+  comp_multi[1] = 0;
+  std::vector<arma::colvec*> comp_weights(tuple_size);
+  std::vector<NptNode*> comp_trees(tuple_size);
+  std::vector<std::vector<size_t>*> old_from_new_list(tuple_size);
+  
+  for (int i = 0; i < tuple_size; i++) {
+    comp_mats[i] = &data_mat;
+    comp_weights[i] = &weights;
+    comp_multi[1]++;
+    comp_trees[i] = data_tree;
+    old_from_new_list[i] = &old_from_new_data;
+  }
+  
+  SingleMatcher matcher(comp_mats, comp_weights, old_from_new_list,
+                        matcher_lower_bounds,
+                        matcher_upper_bounds);
+  
+  GenericNptAlg<SingleMatcher> alg(comp_trees, 
+                                   //comp_multi, 
+                                   matcher);
+  
+  alg.Compute();
+  
+  
+  CLI::StopTimer("single_bandwidth_time");
+  
+  Log::Info << std::endl << "Single bandwidth num tuples: " <<  matcher.results();
+  
+  Log::Info << std::endl << std::endl;
+  
+  return 0;
+  
+} // main
\ No newline at end of file




More information about the mlpack-svn mailing list