PowellBrentOptimizer.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // PowellBrentOptimizer.cpp: Implementation of the
  4. // ADRS Optimizer
  5. //
  6. // Written by Matthias Wacker
  7. //
  8. //
  9. //
  10. //////////////////////////////////////////////////////////////////////
  11. #include "optimization/PowellBrentOptimizer.h"
  12. #include "optimization/Opt_Namespace.h"
  13. //#include <iostream>
  14. PowellBrentOptimizer::PowellBrentOptimizer(OptLogBase *loger) : SuperClass(loger)
  15. {
  16. //xi = matrix_type(m_numberOfParameters, m_numberOfParameters);
  17. //m_lin = BrentLineSearcher(m_costFunction,loger);
  18. m_lin_Lower = -5.0;
  19. m_lin_Upper = 5.0;
  20. m_lin_Eps = 1.0e-3;//1.0e-2;
  21. }
  22. PowellBrentOptimizer::PowellBrentOptimizer( const PowellBrentOptimizer &opt) : SuperClass(opt)
  23. {
  24. m_lin= opt.m_lin;
  25. xi= opt.xi;
  26. m_lin_Lower= opt.m_lin_Lower;
  27. m_lin_Upper= opt.m_lin_Upper;
  28. m_lin_Eps= opt.m_lin_Eps;
  29. }
  30. /*
  31. operator=
  32. */
  33. PowellBrentOptimizer & PowellBrentOptimizer::operator=(const PowellBrentOptimizer &opt)
  34. {
  35. /*
  36. avoid self-copy
  37. */
  38. if(this != &opt)
  39. {
  40. /*
  41. =operator of SuperClass
  42. */
  43. SuperClass::operator=(opt);
  44. /*
  45. own values:
  46. */
  47. m_lin= opt.m_lin;
  48. xi= opt.xi;
  49. m_lin_Lower= opt.m_lin_Lower;
  50. m_lin_Upper= opt.m_lin_Upper;
  51. m_lin_Eps= opt.m_lin_Eps;
  52. }
  53. return *this;
  54. }
  55. PowellBrentOptimizer::~PowellBrentOptimizer()
  56. {
  57. }
  58. void PowellBrentOptimizer::init()
  59. {
  60. SuperClass::init();
  61. //xi =0.0;
  62. xi = matrix_type(m_numberOfParameters, m_numberOfParameters);
  63. m_lin = BrentLineSearcher(m_costFunction,m_loger);
  64. for(uint i = 0; i < m_numberOfParameters;i++)
  65. {
  66. for(uint j = 0; j < m_numberOfParameters; j++)
  67. {
  68. if(i == j)
  69. {
  70. //xi(i,j) = 1.0;
  71. xi[i][j] = 1.0;
  72. }
  73. }
  74. }
  75. if(m_funcTolActive)
  76. m_lin.setEps(m_funcTol);
  77. if(m_maximize == true)
  78. m_lin.setMaximize(true);
  79. //if(m_verbose)
  80. // m_lin.setVerbose(true);
  81. }
  82. void PowellBrentOptimizer::setLineSearchOptions(double lower, double upper, double eps)
  83. {
  84. m_lin_Lower= lower;
  85. m_lin_Upper= upper;
  86. m_lin_Eps= eps;
  87. m_lin.setBounds(lower, upper);
  88. m_lin.setEps(eps);
  89. }
  90. int PowellBrentOptimizer::optimize()
  91. {
  92. this->init();
  93. /*
  94. start time criteria
  95. */
  96. m_startTime = clock();
  97. int n = m_numberOfParameters;
  98. double ftol = m_funcTol;
  99. double TINY = 1.0e-25;
  100. bool abort = false;
  101. int i,ibig, j;
  102. double del, fp, fptt, t;
  103. matrix_type pt(m_numberOfParameters,1);
  104. matrix_type ptt(m_numberOfParameters,1);
  105. matrix_type xit(m_numberOfParameters,1);
  106. /*
  107. eval at parameters
  108. */
  109. double fret = evaluateCostFunction(m_parameters);
  110. /*
  111. save the initial point
  112. */
  113. pt = m_parameters;
  114. if(m_verbose)
  115. {
  116. for(uint r = 0; r < m_numberOfParameters; r++)
  117. {
  118. std::cout<< m_parameters[r][0] << " ";
  119. }
  120. std::cout<< fret << std::endl;
  121. }
  122. while(abort == false)
  123. {
  124. /*
  125. Check iteration limit
  126. */
  127. if(m_maxNumIterActive)
  128. {
  129. if(m_numIter >= m_maxNumIter)
  130. {
  131. /* set according return status and return */
  132. m_returnReason = SUCCESS_MAXITER;
  133. abort = true;
  134. continue;
  135. }
  136. }
  137. /*
  138. increase iteration counter
  139. */
  140. ++m_numIter;
  141. fp = fret;
  142. ibig = 0;
  143. del = 0.0;
  144. for(i = 0; i < n;i++)
  145. {
  146. for(j = 0; j<n;j++)
  147. {
  148. //xit(j,0) = xi(j,i) * m_scales(j,0);
  149. xit[j][0] = xi[j][i] * m_scales[j][0];
  150. }
  151. fptt=fret;
  152. /* do a one dimensional line search */
  153. m_lin.setBounds(m_lin_Lower ,m_lin_Upper);
  154. m_lin.setEps(m_lin_Eps);
  155. m_lin.setX0(m_parameters);
  156. m_lin.setH0(xit);
  157. double lambda = m_lin.optimize();
  158. fret = m_lin.evaluateSub(lambda);
  159. xit = xit * lambda;
  160. // if value improved, accept new point
  161. if(fret < fptt )
  162. {
  163. m_parameters= m_parameters + xit;
  164. }
  165. if(m_verbose)
  166. {
  167. for(uint r = 0; r < m_numberOfParameters; r++)
  168. {
  169. std::cout<< m_parameters[r][0] << " ";
  170. }
  171. std::cout <<fret<< std::endl;
  172. }
  173. if(fptt - fret > del)
  174. {
  175. del = fptt - fret;
  176. ibig = i;
  177. }
  178. }
  179. // check for improvement -> if there is none, abort iteration!
  180. if(fret >= fp)
  181. {
  182. m_returnReason=SUCCESS_NO_BETTER_POINT;
  183. abort=true;
  184. continue;
  185. }
  186. if(m_funcTolActive)
  187. {
  188. /*
  189. a recommended abort criteria
  190. */
  191. if(2.0 * (fp - fret ) <= ftol *(fabs(fp) + fabs(fret) ) + TINY)
  192. {
  193. m_returnReason = SUCCESS_FUNCTOL;
  194. abort = true;
  195. continue;
  196. }
  197. }
  198. /*
  199. construct extrapolated point and the average direction moved
  200. save old starting point
  201. */
  202. for (j=0; j<n;j++)
  203. {
  204. //ptt(j,0)= 2.0*m_parameters(j,0)-pt(j,0);
  205. //xit(j,0)= m_parameters(j,0)-pt(j,0);
  206. //pt(j,0) = m_parameters(j,0);
  207. ptt[j][0]= 2.0*m_parameters[j][0]-pt[j][0];
  208. xit[j][0]= m_parameters[j][0]-pt[j][0];
  209. pt[j][0] = m_parameters[j][0];
  210. }
  211. fptt = evaluateCostFunction(ptt);
  212. if(fptt < fp)
  213. {
  214. t = 2.0*(fp-2.0*fret+fptt)*sqrt(fp-fret-del) - del *sqrt(fp-fptt);
  215. if(t < 0.0)
  216. {
  217. /*
  218. move to minimum of new
  219. direction and save the new direction
  220. */
  221. m_lin.setBounds(m_lin_Lower,m_lin_Upper);
  222. m_lin.setX0(m_parameters);
  223. m_lin.setH0(xit);
  224. double lambda = m_lin.optimize();
  225. fret = m_lin.evaluateSub(lambda);
  226. xit= xit*lambda;
  227. m_parameters= m_parameters+ xit;
  228. // normalize direction
  229. xit = xit * (1.0 / xit.Norm(0));
  230. for(j=0; j < n ;j++)
  231. {
  232. xi[j][ibig] = xi[j][n-1];
  233. xi[j][n-1]= xit[j][0];
  234. }
  235. }
  236. }
  237. /*
  238. Check Optimization Timilimit, if active
  239. */
  240. if(m_maxSecondsActive)
  241. {
  242. m_currentTime = clock();
  243. /* time limit exceeded ? */
  244. if(((float)(m_currentTime - m_startTime )/CLOCKS_PER_SEC) >= m_maxSeconds )
  245. {
  246. if(m_verbose == true)
  247. {
  248. std::cout << "# aborting because of Time Limit" << std::endl;
  249. }
  250. /* set according return status and the last parameters and return */
  251. m_returnReason = SUCCESS_TIMELIMIT;
  252. abort = true;
  253. continue;
  254. }
  255. }
  256. /*
  257. Check Bounds if Active
  258. */
  259. if(!checkParameters(m_parameters))
  260. {
  261. if(m_verbose == true)
  262. {
  263. std::cout << "PowellBrent :: aborting because of parameter Bounds" << std::endl;
  264. }
  265. /* set according return status and the last parameters and return */
  266. m_returnReason = ERROR_XOUTOFBOUNDS;
  267. abort = true;
  268. continue;
  269. }
  270. /*if(m_verbose)
  271. {
  272. for(int r = 0; r < m_numberOfParameters; r++)
  273. {
  274. std::cout<< m_parameters(r,0) << " ";
  275. }
  276. std::cout << std::endl;
  277. }*/
  278. }
  279. return m_returnReason;
  280. }