DiagonalMatrixApprox.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * @file DiagonalMatrixApprox.cpp
  3. * @brief find a diagonal matrix to approximate bilinear products
  4. * @author Erik Rodner
  5. * @date 05/31/2012
  6. */
  7. #include <iostream>
  8. #include <core/optimization/gradientBased/FirstOrderRasmussen.h>
  9. #include <core/vector/Eigen.h>
  10. #include "DiagonalMatrixApprox.h"
  11. using namespace NICE;
  12. using namespace std;
  13. DiagonalMatrixApprox::DiagonalMatrixApprox( bool verbose, int maxIterations )
  14. {
  15. this->epsilonStart = 1.0;
  16. this->epsilonShrinkFactor = 0.8;
  17. this->minFDelta = 1e-8;
  18. this->minSolDelta = 1e-8;
  19. this->maxEpsilonIterations = 100;
  20. this->verbose = verbose;
  21. this->optimizer = new FirstOrderRasmussen( /*false*/ this->verbose );
  22. ( dynamic_cast< FirstOrderRasmussen * > (optimizer) )->setMaxIterations(maxIterations);
  23. }
  24. DiagonalMatrixApprox::~DiagonalMatrixApprox()
  25. {
  26. }
  27. void DiagonalMatrixApprox::approx ( const Matrix & A, Vector & D ) const
  28. {
  29. double f0 = std::numeric_limits<double>::max();
  30. // not so proper intialization with the diagonal matrix
  31. /*
  32. D.resize(A.rows());
  33. for ( uint i = 0; i < D.size(); i++ )
  34. D[i] = A(i,i);
  35. */
  36. Vector D0 ( D );
  37. double epsilon = epsilonStart;
  38. for ( uint i = 0; i < maxEpsilonIterations; i++ )
  39. {
  40. epsilon = epsilonShrinkFactor * epsilon;
  41. // perform minimization with some gradient based method (this is a convex optimization problem)
  42. DiagonalMatrixApproxOptimizationProblem opt ( &A, D0, epsilon, false /*verbose*/ );
  43. optimizer->optimizeFirst ( opt );
  44. D = opt.position();
  45. double f = opt.computeObjective ();
  46. if ( verbose )
  47. {
  48. cerr << "DiagonalMatrixApprox [" << i << " / " << maxEpsilonIterations << "]: " << f << " (epsilon=" << epsilon << ")" << endl;
  49. cerr << D << endl;
  50. }
  51. if ( !finite(f) )
  52. {
  53. f = f0;
  54. D = D0;
  55. if ( verbose )
  56. cerr << "DiagonalMatrixApprox ended in iteration " << i << " due to numerical problems when decreasing epsilon..." << endl;
  57. return;
  58. }
  59. double eig_small = opt.getSmallestEigenvalue();
  60. if ( eig_small < 0.0 )
  61. {
  62. if ( verbose )
  63. cerr << "DiagonalMatrixApprox: resetting current value due to negative eigenvalue: " << eig_small << endl;
  64. D = f0;
  65. D = D0;
  66. } else {
  67. // otherwise check for convergence
  68. double solDelta = ( D - D0 ).normInf();
  69. double fDelta = f0 - f; // this value should be positive, otherwise convergence is likely
  70. if ( fDelta < minFDelta || solDelta < minSolDelta )
  71. {
  72. if ( verbose )
  73. cerr << "DiagonalMatrixApprox: convergence detected delta_f=" << fDelta << " x_delta=" << solDelta << endl;
  74. return;
  75. }
  76. }
  77. f0 = f;
  78. D0 = D;
  79. }
  80. }
  81. DiagonalMatrixApproxOptimizationProblem::DiagonalMatrixApproxOptimizationProblem ( const Matrix *A, const Vector & D0, double epsilon, bool verbose )
  82. : OptimizationProblemFirst( D0.size() )
  83. {
  84. this->A = A;
  85. this->parameters() = D0;
  86. this->epsilon = epsilon;
  87. this->verbose = verbose;
  88. }
  89. double DiagonalMatrixApproxOptimizationProblem::computeObjective()
  90. {
  91. // Theoretically, we have to compute lambda_max(A - diag(D)). However, we want to solve
  92. // the regularized and relaxed optimization problem, which involves all eigenvalues
  93. const Vector & D = parameters();
  94. Matrix M = (*A);
  95. M.addDiagonal ( (-1.0) * D );
  96. if ( verbose ) {
  97. cerr << "M = " << M << endl;
  98. cerr << "D = " << D << endl;
  99. cerr << "A = " << *A << endl;
  100. }
  101. //if ( verbose )
  102. // cerr << "Computing the eigenvalue decomposition ..." << endl;
  103. try {
  104. eigenvectorvalues ( M, eigenvectors, eigenvalues );
  105. } catch ( ... ) {
  106. // the matrix seems to be singular, maybe this is a good sign.
  107. // Does not have to be: only the smallest eigenvalue can be zero
  108. return 0.0;
  109. }
  110. //if ( verbose )
  111. // cerr << "Computing the objective ..." << endl;
  112. double sumExp = 0.0;
  113. for ( uint i = 0 ; i < eigenvalues.size(); i++ )
  114. sumExp += exp( eigenvalues[i] / epsilon );
  115. double fval = epsilon * log( sumExp ) + 0.5 * D.scalarProduct(D);
  116. //if ( verbose ) {
  117. cerr << "DiagonalMatrixApprox: maximum eigenvalue is " << eigenvalues.Max() << endl;
  118. //}
  119. if ( !finite(fval) )
  120. {
  121. // some numerical problems occured
  122. fval = numeric_limits<double>::infinity();
  123. }
  124. //if ( verbose )
  125. // cerr << "Objective value of the sub-problem is: " << fval << endl;
  126. return fval;
  127. }
  128. void DiagonalMatrixApproxOptimizationProblem::computeGradient(Vector& newGradient)
  129. {
  130. // inefficient but straightforward implementation with matrices P (denoted as A in the MATLAB code)
  131. const Vector & D = parameters();
  132. uint n = D.size();
  133. Matrix P ( n, n, 0.0 );
  134. Matrix W ( n, n, 0.0 );
  135. if ( verbose ) {
  136. cerr << "Eigenvectors are: " << eigenvectors << endl;
  137. cerr << "Eigenvalues are: " << eigenvalues << endl;
  138. }
  139. for ( uint i = 0 ; i < n ; i++ )
  140. {
  141. P(i,i) = 1.0;
  142. Matrix Ptilde = eigenvectors.transpose() * P * eigenvectors;
  143. for ( uint j = 0 ; j < n ; j++ )
  144. W(j,i) = Ptilde(j,j);
  145. P(i,i) = 0.0;
  146. }
  147. double eigmax = eigenvalues.Max();
  148. Vector mu (n);
  149. for ( uint i = 0 ; i < n ; i++ )
  150. mu[i] = exp( (eigenvalues[i] - eigmax)/epsilon );
  151. mu.normalizeL1();
  152. if ( verbose ) {
  153. cerr << "W = " << W << endl;
  154. cerr << "mu = " << mu << endl;
  155. }
  156. newGradient = - 1.0 * W.transpose() * mu + D;
  157. if ( verbose ) {
  158. cerr << "gradient = " << newGradient << endl;
  159. }
  160. }