timeiSAM2Chain.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* ----------------------------------------------------------------------------
  2. * GTSAM Copyright 2010, Georgia Tech Research Corporation,
  3. * Atlanta, Georgia 30332-0415
  4. * All Rights Reserved
  5. * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
  6. * See LICENSE for the license information
  7. * -------------------------------------------------------------------------- */
  8. /**
  9. * @file timeiSAM2Chain.cpp
  10. * @brief Times each iteration of a long chain in iSAM2, to measure asymptotic performance
  11. * @author Richard Roberts
  12. */
  13. #include <gtsam/base/timing.h>
  14. #include <gtsam/slam/dataset.h>
  15. #include <gtsam/geometry/Pose2.h>
  16. #include <gtsam/slam/BetweenFactor.h>
  17. #include <gtsam/inference/Symbol.h>
  18. #include <gtsam/nonlinear/ISAM2.h>
  19. #include <gtsam/nonlinear/Marginals.h>
  20. #include <fstream>
  21. #include <boost/archive/binary_oarchive.hpp>
  22. #include <boost/archive/binary_iarchive.hpp>
  23. #include <boost/serialization/export.hpp>
  24. #include <boost/random/mersenne_twister.hpp>
  25. #include <boost/random/uniform_real.hpp>
  26. #include <boost/random/variate_generator.hpp>
  27. using namespace std;
  28. using namespace gtsam;
  29. using namespace gtsam::symbol_shorthand;
  30. typedef Pose2 Pose;
  31. typedef NoiseModelFactor1<Pose> NM1;
  32. typedef NoiseModelFactor2<Pose,Pose> NM2;
  33. noiseModel::Unit::shared_ptr model = noiseModel::Unit::Create(3);
  34. int main(int argc, char *argv[]) {
  35. const size_t steps = 50000;
  36. cout << "Playing forward " << steps << " time steps..." << endl;
  37. ISAM2 isam2;
  38. // Times
  39. vector<double> times(steps);
  40. for(size_t step=0; step < steps; ++step) {
  41. Values newVariables;
  42. NonlinearFactorGraph newFactors;
  43. // Collect measurements and new variables for the current step
  44. gttic_(Create_measurements);
  45. if(step == 0) {
  46. // Add prior
  47. newFactors.addPrior(0, Pose(), noiseModel::Unit::Create(3));
  48. newVariables.insert(0, Pose());
  49. } else {
  50. Vector eta = Vector::Random(3) * 0.1;
  51. Pose2 between = Pose().retract(eta);
  52. // Add between
  53. newFactors.add(BetweenFactor<Pose>(step-1, step, between, model));
  54. newVariables.insert(step, isam2.calculateEstimate<Pose>(step-1) * between);
  55. }
  56. gttoc_(Create_measurements);
  57. // Update iSAM2
  58. gttic_(Update_ISAM2);
  59. isam2.update(newFactors, newVariables);
  60. gttoc_(Update_ISAM2);
  61. tictoc_finishedIteration_();
  62. tictoc_getNode(node, Update_ISAM2);
  63. times[step] = node->time();
  64. if(step % 1000 == 0) {
  65. cout << "Step " << step << endl;
  66. tictoc_print_();
  67. }
  68. }
  69. tictoc_print_();
  70. // Write times
  71. ofstream timesFile("times.txt");
  72. for(double t: times) {
  73. timesFile << t << "\n"; }
  74. return 0;
  75. }