MatrixIterativeOptimizer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // MatrixIterativeOptimizer.cpp: Implementation of the MatrixIterativeOptimizer
  4. // Optimizer class.
  5. //
  6. // Written by Matthias Wacker
  7. //
  8. //////////////////////////////////////////////////////////////////////
  9. #include "optimization/MatrixIterativeOptimizer.h"
  10. #include <iostream>
  11. #include "optimization/AdditionalIceUtils.h" // for linsolve
  12. #include "optimization/DownhillSimplexOptimizer.h" //FIXME WACKER: INCLUDE DOWNHILL SIMPLEX FOR THE GET RANK FUNCTION -> EXPORT IT
  13. MatrixIterativeOptimizer::MatrixIterativeOptimizer(OptLogBase *loger) : SuperClass(loger)
  14. {
  15. m_IterationMatrix = matrix_type(m_numberOfParameters,m_numberOfParameters);
  16. m_hk = matrix_type(m_numberOfParameters,1);
  17. m_stepSize = matrix_type(m_numberOfParameters,1);
  18. m_oldGradient = matrix_type(m_numberOfParameters,1);
  19. m_oldParams = matrix_type(m_numberOfParameters,1);
  20. m_lambdak = 1.0;
  21. }
  22. MatrixIterativeOptimizer::MatrixIterativeOptimizer( const MatrixIterativeOptimizer &opt) : SuperClass(opt)
  23. {
  24. m_IterationMatrix = opt.m_IterationMatrix;
  25. m_hk = opt.m_hk;
  26. m_lin = opt.m_lin;
  27. m_stepSize = opt.m_stepSize;
  28. m_lambdak = opt.m_lambdak;
  29. m_oldParams = opt.m_oldParams;
  30. m_oldGradient = opt.m_oldGradient;
  31. }
  32. MatrixIterativeOptimizer::~MatrixIterativeOptimizer()
  33. {
  34. }
  35. void MatrixIterativeOptimizer::init()
  36. {
  37. if(m_loger)
  38. m_loger->logTrace("entering MatrixIterativeOptimizer:: init ... \n");
  39. SuperClass::init();
  40. m_lin= ArmijoLineSearcher(m_costFunction,m_loger);
  41. //if(m_verbose==true) //FIXME commented out as output is not human readable
  42. // m_lin.setVerbose(true);
  43. if(m_maximize == true)
  44. {
  45. m_lin.setMaximize(true);
  46. }
  47. m_IterationMatrix=matrix_type(m_numberOfParameters,m_numberOfParameters);
  48. for(int i =0;i < static_cast<int>(m_numberOfParameters); i++)
  49. {
  50. m_IterationMatrix[i][i] = 1.0;
  51. }
  52. if(m_loger)
  53. m_loger->logTrace("leaving MatrixIterativeOptimizer:: init ...\n");
  54. }
  55. MatrixIterativeOptimizer & MatrixIterativeOptimizer::operator=(const MatrixIterativeOptimizer &opt)
  56. {
  57. /*
  58. avoid self-copy
  59. */
  60. if(this != &opt)
  61. {
  62. /*
  63. =operator of SuperClass
  64. */
  65. SuperClass::operator=(opt);
  66. /*
  67. own values:
  68. */
  69. m_IterationMatrix = opt.m_IterationMatrix;
  70. m_hk = opt.m_hk;
  71. m_lin = opt.m_lin;
  72. m_stepSize = opt.m_stepSize;
  73. m_lambdak = opt.m_lambdak;
  74. m_oldParams = opt.m_oldParams;
  75. m_oldGradient = opt.m_oldGradient;
  76. }
  77. return *this;
  78. }
  79. void MatrixIterativeOptimizer::computeDescentDirection()
  80. {
  81. if(m_loger)
  82. m_loger->logTrace("entering MatrixIterativeOptimizer::computeDescentDirection ... \n");
  83. if(m_numberOfParameters == 1)
  84. {
  85. if(fabs(m_IterationMatrix[0][0]) >= 1.0e-20 )
  86. {
  87. m_hk[0][0] = -m_gradient[0][0] / m_IterationMatrix[0][0];
  88. }
  89. else
  90. {
  91. m_hk = -m_gradient;
  92. }
  93. }
  94. else
  95. {
  96. //if(getRank(m_IterationMatrix, 1.0e-5 ) == m_numberOfParameters)
  97. if(true)
  98. {
  99. /*
  100. solve system equation for descent direction
  101. */
  102. matrix_type tmp=(m_gradient * -1.0 );
  103. linSolve(m_IterationMatrix, m_hk, tmp );
  104. /*
  105. direction orthogonal to gradient? -> use gradient
  106. */
  107. if(fabs((!m_hk * m_gradient)[0][0]) <= 1.0e-3)
  108. {
  109. m_hk = -m_gradient;
  110. }
  111. }
  112. else
  113. {
  114. m_hk = -m_gradient;
  115. }
  116. }
  117. if(m_loger)
  118. m_loger->logTrace("leaving MatrixIterativeOptimizer::computeDescentDirection ... \n");
  119. }
  120. void MatrixIterativeOptimizer::resetMatrix()
  121. {
  122. m_IterationMatrix=matrix_type(m_numberOfParameters,m_numberOfParameters);
  123. for(int i =0;i < static_cast<int>(m_numberOfParameters); i++)
  124. {
  125. m_IterationMatrix[i][i] = 1.0;
  126. }
  127. }
  128. void MatrixIterativeOptimizer::setStepSize(const matrix_type & stepSize)
  129. {
  130. m_stepSize = stepSize;
  131. }
  132. int MatrixIterativeOptimizer::optimize()
  133. {
  134. if(m_loger)
  135. m_loger->logTrace("entering MatrixIterativeOptimizer:optimize ... \n");
  136. init();
  137. bool abort = false;
  138. /******************************
  139. start time criteria
  140. ******************************/
  141. m_startTime = clock();
  142. /******************************
  143. compute start value
  144. and first gradient
  145. ******************************/
  146. m_currentCostFunctionValue = evaluateCostFunction(m_parameters);
  147. m_gradient = (m_analyticalGradients == true &&
  148. (m_costFunction->hasAnalyticGradient() == true) ) ?
  149. getAnalyticalGradient(m_parameters) :
  150. getNumericalGradient(m_parameters, m_stepSize);
  151. m_hk = m_gradient * -1.0;
  152. matrix_type hk_norm;
  153. double factor = m_hk.Norm(0);
  154. if(fabs(factor) > 1.0e-12)
  155. {
  156. hk_norm = m_hk * (1.0/ factor);
  157. }
  158. matrix_type grad_norm;
  159. double factor2 = m_gradient.Norm(0);
  160. if(fabs(factor2) > 1.0e-12)
  161. {
  162. grad_norm = m_gradient * (1.0/ factor2);
  163. }
  164. /*******************************
  165. optimize step length
  166. *******************************/
  167. /// ARMIJO
  168. //m_lin.setXkGkDk(m_parameters, m_gradient, hk_norm);
  169. m_lin.setXkGkDk(m_parameters, grad_norm, hk_norm);
  170. m_lambdak = m_lin.optimize();
  171. if(m_verbose == true)
  172. {
  173. std::cout << "stepsize lambda: " << m_lambdak << std::endl;
  174. std::cout << "in direction: " << std::endl;
  175. for(int r = 0; r < static_cast<int>(m_numberOfParameters); r++)
  176. {
  177. std::cout<< hk_norm[r][0] << " ";
  178. }
  179. std::cout << std::endl;
  180. }
  181. /*******************************
  182. update the parameters
  183. *******************************/
  184. m_oldParams = m_parameters;
  185. m_parameters = m_parameters + hk_norm * m_lambdak;
  186. /******************************
  187. check abort criteria
  188. for gradient
  189. ******************************/
  190. if(m_gradientTolActive)
  191. {
  192. if(m_gradient.Norm(0) < m_gradientTol){
  193. m_returnReason = SUCCESS_GRADIENTTOL;
  194. abort = true;
  195. }
  196. }
  197. /* do the optimization loop */
  198. while(abort == false)
  199. {
  200. /******************************
  201. increase iteration
  202. counter
  203. ******************************/
  204. m_numIter++;
  205. /******************************
  206. cout actual iteration
  207. ******************************/
  208. if(m_verbose == true)
  209. {
  210. std::cout << "MatrixIterativeOptimizer :: Iteration: " << m_numIter << " : current parameters : ";
  211. for(int r = 0; r < static_cast<int>(m_numberOfParameters); r++)
  212. {
  213. std::cout<< m_parameters[r][0] << "\t ";
  214. }
  215. std::cout << " value: " << m_currentCostFunctionValue << std::endl;
  216. }
  217. /******************************
  218. Check iteration limit
  219. ******************************/
  220. if(m_maxNumIterActive)
  221. {
  222. if(m_numIter >= m_maxNumIter)
  223. {
  224. if(m_verbose == true)
  225. {
  226. std::cout << "Gradient Descenct :: aborting because of numIter" << std::endl;
  227. }
  228. /* set according return status and return */
  229. m_returnReason = SUCCESS_MAXITER;
  230. abort = true;
  231. continue;
  232. }
  233. }
  234. /******************************
  235. get the new Gradient
  236. ******************************/
  237. m_oldGradient = m_gradient;
  238. m_gradient = (m_analyticalGradients == true &&
  239. (m_costFunction->hasAnalyticGradient() == true) ) ?
  240. getAnalyticalGradient(m_parameters) :
  241. getNumericalGradient(m_parameters, m_stepSize);
  242. /******************************
  243. check gradient tol
  244. ******************************/
  245. if(m_gradientTolActive)
  246. {
  247. if(m_gradient.Norm(0) < m_gradientTol)
  248. {
  249. if(m_verbose == true)
  250. {
  251. std::cout << "MatrixIterativeOptimizer :: aborting because of GradientTol" << std::endl;
  252. }
  253. m_returnReason = SUCCESS_GRADIENTTOL;
  254. abort = true;
  255. continue;
  256. }
  257. }
  258. /******************************
  259. Update the Iteration
  260. Matrix
  261. ******************************/
  262. //matrix_type oldMatrix = m_IterationMatrix;
  263. updateIterationMatrix();
  264. /******************************
  265. compute a descent
  266. direction
  267. ******************************/
  268. computeDescentDirection();
  269. // check if descent direction is really a descent direction
  270. // if not: reset the iteration matrix and take
  271. // the gradient as iteration direction
  272. if( (!m_hk *m_gradient)[0][0] > 0.0)
  273. {
  274. cout << " resetting matrix" << endl;
  275. resetMatrix();
  276. m_hk=m_gradient * -1.0;
  277. }
  278. double factor = m_hk.Norm(0);
  279. if(fabs(factor) > 1.0e-12)
  280. {
  281. hk_norm = m_hk * (1.0/ factor);
  282. }
  283. matrix_type grad_norm;
  284. double factor2 = m_gradient.Norm(0);
  285. if(fabs(factor2) > 1.0e-12)
  286. {
  287. grad_norm = m_gradient * (1.0/ factor2);
  288. }
  289. /*******************************
  290. optimize step length
  291. *******************************/
  292. //m_lin.setXkGkDk(m_parameters, m_gradient, hk_norm);
  293. m_lin.setXkGkDk(m_parameters, grad_norm, hk_norm);
  294. m_lambdak = m_lin.optimize();
  295. /*******************************
  296. update the parameters
  297. *******************************/
  298. m_oldParams = m_parameters;
  299. m_parameters = m_parameters + hk_norm * m_lambdak;
  300. if(m_verbose == true)
  301. {
  302. //matrix_type hk_normu=S2U(hk_norm);
  303. std::cout << "MatrixIterativeOptimizer :: Iterating with lambda = " << m_lambdak << std::endl;
  304. std::cout << "MatrixIterativeOptimizer :: in direction : ";
  305. for(int mu = 0; mu < static_cast<int>(m_numberOfParameters); mu++)
  306. {
  307. //std::cout<< hk_normu[mu][0] << "\t ";
  308. std::cout<< hk_norm[mu][0] << "\t ";
  309. }
  310. std::cout << std::endl;
  311. //matrix_type m_gradientu=S2U(m_gradient);
  312. std::cout << "MatrixIterativeOptimizer :: Gradient was ";
  313. for(int nu = 0; nu < static_cast<int>(m_numberOfParameters); nu++)
  314. {
  315. //std::cout<< m_gradientu[nu][0] << "\t ";
  316. std::cout<< m_gradient[nu][0] << "\t ";
  317. }
  318. std::cout << std::endl;
  319. }
  320. /*******************************
  321. Check new parameters
  322. are it is in bounds,
  323. paramTol,
  324. funcTol,
  325. maxSeconds
  326. *******************************/
  327. if(!checkParameters(m_parameters))
  328. {
  329. if(m_verbose == true)
  330. {
  331. std::cout << "Gradient Descenct :: aborting because of parameter Bounds" << std::endl;
  332. }
  333. /* set according return status and the last parameters and return */
  334. m_returnReason = ERROR_XOUTOFBOUNDS;
  335. m_parameters = m_oldParams;
  336. abort = true;
  337. continue;
  338. }
  339. /*
  340. Get new Costfunction Value
  341. */
  342. double oldFuncValue = m_currentCostFunctionValue;
  343. m_currentCostFunctionValue = evaluateCostFunction(m_parameters);
  344. /*
  345. Check ParamTol
  346. */
  347. if(m_paramTolActive)
  348. {
  349. if( (m_parameters - m_oldParams).Norm(0) < m_paramTol)
  350. {
  351. if(m_verbose == true)
  352. {
  353. std::cout << "Gradient Descenct :: aborting because of param Tol" << std::endl;
  354. }
  355. /* set according return status and the last parameters and return */
  356. m_returnReason = SUCCESS_PARAMTOL;
  357. /*m_parameters = oldParams;*/
  358. abort = true;
  359. continue;
  360. }
  361. }
  362. if(m_funcTolActive)
  363. {
  364. if( abs((m_currentCostFunctionValue - oldFuncValue)) < m_funcTol)
  365. {
  366. if(m_verbose == true)
  367. {
  368. std::cout << "MatrixIterativeOptimizer :: aborting because of Func Tol" << std::endl;
  369. }
  370. /* set according return status and the last parameters and return */
  371. m_returnReason = SUCCESS_FUNCTOL;
  372. abort = true;
  373. continue;
  374. }
  375. }
  376. /*
  377. Check Optimization Timilimit, if active
  378. */
  379. if(m_maxSecondsActive)
  380. {
  381. m_currentTime = clock();
  382. /* time limit exceeded ? */
  383. if(((float)(m_currentTime - m_startTime )/CLOCKS_PER_SEC) >= m_maxSeconds )
  384. {
  385. if(m_verbose == true)
  386. {
  387. std::cout << "MatrixIterativeOptimizer :: aborting because of Time Limit" << std::endl;
  388. }
  389. /* set according return status and the last parameters and return */
  390. m_returnReason = SUCCESS_TIMELIMIT;
  391. m_parameters = m_oldParams;
  392. abort = true;
  393. continue;
  394. }
  395. }
  396. }
  397. if(m_verbose)
  398. {
  399. std::cout << "MatrixIterativeOptimizer :: RESULT: ";
  400. for(int r = 0; r < static_cast<int>(m_numberOfParameters); r++)
  401. {
  402. std::cout<< m_parameters[r][0] << " ";
  403. }
  404. std::cout << " value: " << m_currentCostFunctionValue << std::endl;
  405. }
  406. if(m_loger)
  407. m_loger->logTrace("leaving MatrixIterativeOptimizer:optimize ... \n");
  408. return m_returnReason;
  409. }