| 
									
										
										
										
											2010-10-14 12:54:38 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							| 
									
										
										
										
											2010-10-14 12:54:38 +08:00
										 |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-27 21:34:36 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |  * @file    timeGaussianFactorGraph.cpp | 
					
						
							| 
									
										
										
										
											2009-10-27 21:34:36 +08:00
										 |  |  |  * @brief   Time elimination with simple Kalman Smoothing example | 
					
						
							|  |  |  |  * @author  Frank Dellaert | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <time.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-26 04:10:33 +08:00
										 |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							| 
									
										
										
										
											2012-06-10 04:15:44 +08:00
										 |  |  | #include <tests/smallExample.h>
 | 
					
						
							| 
									
										
										
										
											2009-10-27 21:34:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							| 
									
										
										
										
											2010-01-19 13:33:44 +08:00
										 |  |  | using namespace example; | 
					
						
							| 
									
										
										
										
											2009-10-27 21:34:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // Create a Kalman smoother for t=1:T and optimize
 | 
					
						
							|  |  |  | double timeKalmanSmoother(int T) { | 
					
						
							| 
									
										
										
										
											2014-06-08 10:02:11 +08:00
										 |  |  |   GaussianFactorGraph smoother = createSmoother(T); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   clock_t start = clock(); | 
					
						
							| 
									
										
										
										
											2014-06-08 10:02:11 +08:00
										 |  |  |   // Keys will come out sorted since keys() returns a set
 | 
					
						
							|  |  |  |   smoother.optimize(Ordering(smoother.keys())); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   clock_t end = clock (); | 
					
						
							|  |  |  |   double dif = (double)(end - start) / CLOCKS_PER_SEC; | 
					
						
							|  |  |  |   return dif; | 
					
						
							| 
									
										
										
										
											2009-10-27 21:34:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-16 12:46:35 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // Create a planar factor graph and optimize
 | 
					
						
							| 
									
										
										
										
											2010-02-01 01:49:33 +08:00
										 |  |  | double timePlanarSmoother(int N, bool old = true) { | 
					
						
							| 
									
										
										
										
											2019-10-13 13:56:34 +08:00
										 |  |  |   GaussianFactorGraph fg = planarGraph(N).first; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   clock_t start = clock(); | 
					
						
							| 
									
										
										
										
											2014-06-08 10:02:11 +08:00
										 |  |  |   fg.optimize(); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   clock_t end = clock (); | 
					
						
							|  |  |  |   double dif = (double)(end - start) / CLOCKS_PER_SEC; | 
					
						
							|  |  |  |   return dif; | 
					
						
							| 
									
										
										
										
											2010-01-16 12:46:35 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-01 01:49:33 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | // Create a planar factor graph and eliminate
 | 
					
						
							|  |  |  | double timePlanarSmootherEliminate(int N, bool old = true) { | 
					
						
							| 
									
										
										
										
											2019-10-13 13:56:34 +08:00
										 |  |  |   GaussianFactorGraph fg = planarGraph(N).first; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   clock_t start = clock(); | 
					
						
							| 
									
										
										
										
											2014-06-08 10:02:11 +08:00
										 |  |  |   fg.eliminateMultifrontal(); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   clock_t end = clock (); | 
					
						
							|  |  |  |   double dif = (double)(end - start) / CLOCKS_PER_SEC; | 
					
						
							|  |  |  |   return dif; | 
					
						
							| 
									
										
										
										
											2010-02-01 01:49:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | ///* ************************************************************************* */
 | 
					
						
							|  |  |  | //// Create a planar factor graph and join factors until matrix formation
 | 
					
						
							|  |  |  | //// This variation uses the original join factors approach
 | 
					
						
							|  |  |  | //double timePlanarSmootherJoinAug(int N, size_t reps) {
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | //  GaussianFactorGraph fgBase;
 | 
					
						
							|  |  |  | //  VectorValues config;
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //  boost::tie(fgBase,config) = planarGraph(N);
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | //  Ordering ordering = fgBase.getOrdering();
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //  Symbol key = ordering.front();
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //  clock_t start = clock();
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //  for (size_t i = 0; i<reps; ++i) {
 | 
					
						
							|  |  |  | //    // setup
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | //    GaussianFactorGraph fg(fgBase);
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //    // combine some factors
 | 
					
						
							|  |  |  | //    GaussianFactor::shared_ptr joint_factor = removeAndCombineFactors(fg,key);
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //    // create an internal ordering to render Ab
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | //    Ordering render;
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //    render += key;
 | 
					
						
							| 
									
										
										
										
											2016-05-21 09:22:30 +08:00
										 |  |  | //    for(const Symbol& k: joint_factor->keys())
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //    if (k != key) render += k;
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //    Matrix Ab = joint_factor->matrix_augmented(render,false);
 | 
					
						
							|  |  |  | //  }
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //  clock_t end = clock ();
 | 
					
						
							|  |  |  | //  double dif = (double)(end - start) / CLOCKS_PER_SEC;
 | 
					
						
							|  |  |  | //  return dif;
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ///* ************************************************************************* */
 | 
					
						
							|  |  |  | //// Create a planar factor graph and join factors until matrix formation
 | 
					
						
							|  |  |  | //// This variation uses the single-allocate version to create the matrix
 | 
					
						
							|  |  |  | //double timePlanarSmootherCombined(int N, size_t reps) {
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | //  GaussianFactorGraph fgBase;
 | 
					
						
							|  |  |  | //  VectorValues config;
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //  boost::tie(fgBase,config) = planarGraph(N);
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | //  Ordering ordering = fgBase.getOrdering();
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //  Symbol key = ordering.front();
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //  clock_t start = clock();
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //  for (size_t i = 0; i<reps; ++i) {
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  | //    GaussianFactorGraph fg(fgBase);
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //    fg.eliminateOneMatrixJoin(key);
 | 
					
						
							|  |  |  | //  }
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //  clock_t end = clock ();
 | 
					
						
							|  |  |  | //  double dif = (double)(end - start) / CLOCKS_PER_SEC;
 | 
					
						
							|  |  |  | //  return dif;
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //}
 | 
					
						
							| 
									
										
										
										
											2010-02-01 01:49:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-27 21:34:36 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | TEST(timeGaussianFactorGraph, linearTime) | 
					
						
							| 
									
										
										
										
											2009-10-27 21:34:36 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   // Original T = 1000;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Alex's Results
 | 
					
						
							|  |  |  |   // T = 100000
 | 
					
						
							|  |  |  |   // 1907 (init)    : T - 1.65, 2T = 3.28
 | 
					
						
							|  |  |  |   //    int->size_t : T - 1.63, 2T = 3.27
 | 
					
						
							|  |  |  |   // 2100           : T - 1.52, 2T = 2.96
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   int T = 100000; | 
					
						
							|  |  |  |   double time1 = timeKalmanSmoother(  T);  cout << "timeKalmanSmoother( T): " << time1; | 
					
						
							|  |  |  |   double time2 = timeKalmanSmoother(2*T);  cout << "  (2*T): " << time2 << endl; | 
					
						
							|  |  |  |   DOUBLES_EQUAL(2*time1,time2,0.2); | 
					
						
							| 
									
										
										
										
											2010-01-16 12:46:35 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-01 01:49:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Timing with N = 30
 | 
					
						
							|  |  |  | // 1741: 8.12, 8.12, 8.12, 8.14, 8.16
 | 
					
						
							|  |  |  | // 1742: 5.97, 5.97, 5.97, 5.99, 6.02
 | 
					
						
							|  |  |  | // 1746: 5.96, 5.96, 5.97, 6.00, 6.04
 | 
					
						
							|  |  |  | // 1748: 5.91, 5.92, 5.93, 5.95, 5.96
 | 
					
						
							|  |  |  | // 1839: 0.206956 0.206939 0.206213 0.206092 0.206780 // colamd !!!!
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Alex's Machine
 | 
					
						
							|  |  |  | // Initial:
 | 
					
						
							|  |  |  | // 1907 (N = 30)               :  0.14
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //      (N = 100)         : 16.36
 | 
					
						
							| 
									
										
										
										
											2010-02-01 01:49:33 +08:00
										 |  |  | // Improved (int->size_t)
 | 
					
						
							|  |  |  | //      (N = 100)              : 15.39
 | 
					
						
							|  |  |  | // Using GSL/BLAS for updateAb : 12.87
 | 
					
						
							|  |  |  | // Using NoiseQR               : 16.33
 | 
					
						
							|  |  |  | // Using correct system        : 10.00
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Switch to 100*100 grid = 10K poses
 | 
					
						
							|  |  |  | // 1879: 15.6498 15.3851 15.5279
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 08:19:00 +08:00
										 |  |  | int grid_size = 100; | 
					
						
							| 
									
										
										
										
											2010-02-01 01:49:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-16 12:46:35 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-02-01 01:49:33 +08:00
										 |  |  | TEST(timeGaussianFactorGraph, planar_old) | 
					
						
							| 
									
										
										
										
											2010-01-16 12:46:35 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   cout << "Timing planar - original version" << endl; | 
					
						
							| 
									
										
										
										
											2019-07-16 08:19:00 +08:00
										 |  |  |   double time = timePlanarSmoother(grid_size); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   cout << "timeGaussianFactorGraph : " << time << endl; | 
					
						
							|  |  |  |   //DOUBLES_EQUAL(5.97,time,0.1);
 | 
					
						
							| 
									
										
										
										
											2009-10-27 21:34:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-01 01:49:33 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST(timeGaussianFactorGraph, planar_new) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   cout << "Timing planar - new version" << endl; | 
					
						
							| 
									
										
										
										
											2019-07-16 08:19:00 +08:00
										 |  |  |   double time = timePlanarSmoother(grid_size, false); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   cout << "timeGaussianFactorGraph : " << time << endl; | 
					
						
							|  |  |  |   //DOUBLES_EQUAL(5.97,time,0.1);
 | 
					
						
							| 
									
										
										
										
											2010-02-01 01:49:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST(timeGaussianFactorGraph, planar_eliminate_old) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   cout << "Timing planar Eliminate - original version" << endl; | 
					
						
							| 
									
										
										
										
											2019-07-16 08:19:00 +08:00
										 |  |  |   double time = timePlanarSmootherEliminate(grid_size); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   cout << "timeGaussianFactorGraph : " << time << endl; | 
					
						
							|  |  |  |   //DOUBLES_EQUAL(5.97,time,0.1);
 | 
					
						
							| 
									
										
										
										
											2010-02-01 01:49:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | TEST(timeGaussianFactorGraph, planar_eliminate_new) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   cout << "Timing planar Eliminate - new version" << endl; | 
					
						
							| 
									
										
										
										
											2019-07-16 08:19:00 +08:00
										 |  |  |   double time = timePlanarSmootherEliminate(grid_size, false); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   cout << "timeGaussianFactorGraph : " << time << endl; | 
					
						
							|  |  |  |   //DOUBLES_EQUAL(5.97,time,0.1);
 | 
					
						
							| 
									
										
										
										
											2010-02-01 01:49:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //size_t reps = 1000;
 | 
					
						
							|  |  |  | ///* ************************************************************************* */
 | 
					
						
							|  |  |  | //TEST(timeGaussianFactorGraph, planar_join_old)
 | 
					
						
							|  |  |  | //{
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //  cout << "Timing planar join - old" << endl;
 | 
					
						
							|  |  |  | //  double time = timePlanarSmootherJoinAug(size, reps);
 | 
					
						
							|  |  |  | //  cout << "timePlanarSmootherJoinAug " << size << " : " << time << endl;
 | 
					
						
							|  |  |  | //  //DOUBLES_EQUAL(5.97,time,0.1);
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //}
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | ///* ************************************************************************* */
 | 
					
						
							|  |  |  | //TEST(timeGaussianFactorGraph, planar_join_new)
 | 
					
						
							|  |  |  | //{
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | //  cout << "Timing planar join - new" << endl;
 | 
					
						
							|  |  |  | //  double time = timePlanarSmootherCombined(size, reps);
 | 
					
						
							|  |  |  | //  cout << "timePlanarSmootherCombined " << size << " : " << time << endl;
 | 
					
						
							|  |  |  | //  //DOUBLES_EQUAL(5.97,time,0.1);
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //}
 | 
					
						
							| 
									
										
										
										
											2010-02-01 01:49:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-27 21:34:36 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-02-21 06:22:45 +08:00
										 |  |  | int main() { TestResult tr; return TestRegistry::runAllTests(tr); } | 
					
						
							| 
									
										
										
										
											2009-10-27 21:34:36 +08:00
										 |  |  | /* ************************************************************************* */ |