PowellBrentOptimizer.cpp 7.3 KB

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