CholeskyRobustAuto.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * @file CholeskyRobustAuto.cpp
  3. * @brief robust cholesky decomposition by iteratively adding some noise
  4. * @author Erik Rodner
  5. * @date 01/06/2010
  6. */
  7. #include <iostream>
  8. #include "core/vector/Algorithms.h"
  9. #include "CholeskyRobustAuto.h"
  10. // include the optional cholesky sub-library, when available
  11. #ifdef NICE_USELIB_CUDACHOLESKY
  12. #include "cholesky-gpu/niceinterface/CudaCholeskyNICE.h"
  13. #endif
  14. using namespace std;
  15. using namespace NICE;
  16. /** number of maximum iterations used for regularization */
  17. const int maxiterations = 30;
  18. /** should we use exponential scaling for the regularization parameter */
  19. const bool exponential_noise = true;
  20. /** multiply in each iteration with this value (only if exponential noise is on) */
  21. const double exp_multiplication_factor = 10.0;
  22. CholeskyRobustAuto::CholeskyRobustAuto (const CholeskyRobustAuto & src):
  23. CholeskyRobust (src)
  24. {
  25. m_minMatrixLogDeterminant = src.m_minMatrixLogDeterminant;
  26. }
  27. double
  28. CholeskyRobustAuto::robustChol (const Matrix & A, Matrix & cholA)
  29. {
  30. // this additional memory requirement could be skipped
  31. // with a modified cholesky routine
  32. Matrix ARegularized (A);
  33. if (m_verbose)
  34. cerr << "CholeskyRobustAuto::robustChol: A " << A.rows () << " x " << A.cols () << endl;
  35. int i = 0;
  36. double noise = 0.0;
  37. double noiseStepExp = m_noise;
  38. bool robust = true;
  39. // iteration loop
  40. do
  41. {
  42. // first of all, we assume everything is fine
  43. robust = true;
  44. if (m_verbose)
  45. cerr << "CholeskyRobustAuto::robustChol: Cholesky decomposition" << endl;
  46. try
  47. {
  48. #ifdef NICE_USELIB_CUDACHOLESKY
  49. if (m_useCuda)
  50. CudaCholeskyNICE::choleskyDecomp (ARegularized, cholA);
  51. else
  52. #endif
  53. choleskyDecompLargeScale (ARegularized, cholA);
  54. #ifdef NICE_USELIB_CUDACHOLESKY
  55. } // catch a nasty Cuda error
  56. catch (CudaError & e)
  57. {
  58. fthrow (CudaError, e.what ());
  59. #endif
  60. }
  61. catch (Exception)
  62. {
  63. if (m_verbose)
  64. cerr << "CholeskyRobustAuto::robustChol: failed!" << endl;
  65. // Cholesky decomposition failed, therefore, we have to run again
  66. robust = false;
  67. }
  68. // check whether the Cholesky factor contains nasty NaN values
  69. if (robust && cholA.containsNaN ())
  70. {
  71. if (m_verbose)
  72. cerr <<
  73. "CholeskyRobustAuto::robustChol: cholesky matrix contains NaN values"
  74. << endl;
  75. // if it contains NaNs it is surely not robust
  76. robust = false;
  77. }
  78. // set the log determinant to the minimum value
  79. // just in case we do not achieve a robust estimate
  80. m_logDetMatrix = m_minMatrixLogDeterminant;
  81. if (robust)
  82. {
  83. // compute the log determinant
  84. m_logDetMatrix = 2 * triangleMatrixLogDet (cholA);
  85. if (m_verbose)
  86. cerr << "CholeskyRobustAuto::robustChol: Cholesky condition: " <<
  87. m_logDetMatrix << endl;
  88. }
  89. if (isnan (m_logDetMatrix) || (m_logDetMatrix < m_minMatrixLogDeterminant))
  90. {
  91. robust = false;
  92. }
  93. if (!robust)
  94. {
  95. robust = false;
  96. if (m_verbose)
  97. cerr << "CholeskyRobustAuto::robustChol: Adding noise " << noiseStepExp
  98. << endl;
  99. // in case the last estimate was not robust
  100. // (1) add something to the diagonal
  101. ARegularized.addIdentity (noiseStepExp);
  102. noise += noiseStepExp;
  103. // increase the noise
  104. if (exponential_noise)
  105. noiseStepExp *= exp_multiplication_factor;
  106. }
  107. i++;
  108. }
  109. // while we did not achieve a robust value and the maximum number of iterations
  110. // is not reached
  111. while (!robust && (i < maxiterations));
  112. if (!robust)
  113. fthrow (Exception,
  114. "Inverse matrix is instable (please adjust the noise step term)");
  115. return noise;
  116. }
  117. CholeskyRobustAuto::~CholeskyRobustAuto ()
  118. {
  119. }
  120. CholeskyRobustAuto::CholeskyRobustAuto (bool verbose, double noiseStep, double minMatrixLogDeterminant, bool useCuda):
  121. CholeskyRobust (verbose, noiseStep, useCuda)
  122. {
  123. m_minMatrixLogDeterminant = minMatrixLogDeterminant;
  124. }
  125. CholeskyRobustAuto *CholeskyRobustAuto::clone (void) const
  126. {
  127. return new CholeskyRobustAuto (*this);
  128. }