AdaptiveDirectionRandomSearchOptimizer.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // AdaptiveDirectionRandomSearchOptimizer.cpp: Implementation of the
  4. // ADRS Optimizer
  5. //
  6. //
  7. //////////////////////////////////////////////////////////////////////
  8. #include "AdaptiveDirectionRandomSearchOptimizer.h"
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include <iostream>
  12. //#include "numbase.h" // ice random
  13. #include "optimization/AdditionalIceUtils.h"
  14. #include "core/optimization/blackbox/Definitions_core_opt.h"
  15. using namespace OPTIMIZATION;
  16. AdaptiveDirectionRandomSearchOptimizer::AdaptiveDirectionRandomSearchOptimizer(
  17. unsigned int numOfPoints,OptLogBase *loger) : SuperClass(loger)
  18. {
  19. m_numberOfParallelPoints = numOfPoints;
  20. m_b0 = 1.0;
  21. m_bfac = 0.5;
  22. m_bThresTimesNotDecreased = 0;
  23. m_bk = new double[m_numberOfParallelPoints];
  24. m_pointsAccepted = new bool[m_numberOfParallelPoints];
  25. for(int j = 0; j < static_cast<int>(m_numberOfParallelPoints); j++)
  26. {
  27. m_bk[j] = m_b0;
  28. m_pointsAccepted[j]= false;
  29. }
  30. m_bBreak = 0.1;
  31. m_backupActive = false;
  32. m_c0f = 0.75;
  33. m_c0s = 0.75;
  34. m_c1f = -0.75;
  35. m_c1s = 1.25;
  36. m_D = 3;
  37. m_initFuncThreshActive = false;
  38. m_initFuncTresh = 0.0;
  39. // advanced initialization is turned off per default
  40. m_advancedInit= false;
  41. }
  42. AdaptiveDirectionRandomSearchOptimizer::AdaptiveDirectionRandomSearchOptimizer( const AdaptiveDirectionRandomSearchOptimizer &opt) : SuperClass(opt)
  43. {
  44. m_numberOfParallelPoints = opt.m_numberOfParallelPoints;
  45. m_Parameters = opt.m_Parameters;
  46. m_b0 = opt.m_b0;
  47. m_bfac = opt.m_bfac;
  48. m_bk = new double[m_numberOfParallelPoints];
  49. for(int j = 0; j < static_cast<int>(m_numberOfParallelPoints); j++)
  50. {
  51. m_bk[j] = opt.m_bk[j];
  52. }
  53. m_pointsAccepted = new bool[m_numberOfParallelPoints];
  54. for(int j = 0; j < static_cast<int>(m_numberOfParallelPoints); j++)
  55. {
  56. m_pointsAccepted[j] = opt.m_pointsAccepted[j];
  57. }
  58. m_bBreak = opt.m_bBreak;
  59. m_backupActive = opt.m_backupActive;
  60. m_backupPoint = opt.m_backupPoint;
  61. m_backupPointValue = opt.m_backupPointValue;
  62. m_bThresTimesNotDecreased = opt.m_bThresTimesNotDecreased;
  63. m_c0f = opt.m_c0f;
  64. m_c0s = opt.m_c0s;
  65. m_c1f = opt.m_c1f;
  66. m_c1s = opt.m_c1s;
  67. m_D = opt.m_D;
  68. m_initFuncThreshActive = opt.m_initFuncThreshActive;
  69. m_initFuncTresh = opt.m_initFuncTresh;
  70. // advanced init setup
  71. m_advancedInit= opt.m_advancedInit;
  72. m_advancedinitLowerBounds= opt.m_advancedinitLowerBounds;
  73. m_advancedinitUpperBounds= opt.m_advancedinitUpperBounds;
  74. }
  75. /*
  76. operator=
  77. */
  78. AdaptiveDirectionRandomSearchOptimizer & AdaptiveDirectionRandomSearchOptimizer::operator=(const AdaptiveDirectionRandomSearchOptimizer &opt)
  79. {
  80. /*
  81. avoid self-copy
  82. */
  83. if(this != &opt)
  84. {
  85. delete[] m_bk;
  86. delete[] m_pointsAccepted;
  87. /*
  88. =operator of SuperClass
  89. */
  90. SuperClass::operator=(opt);
  91. /*
  92. own values:
  93. */
  94. m_numberOfParallelPoints = opt.m_numberOfParallelPoints;
  95. m_Parameters = opt.m_Parameters;
  96. m_b0 = opt.m_b0;
  97. m_bfac = opt.m_bfac;
  98. m_bBreak = opt.m_bBreak;
  99. m_backupActive = opt.m_backupActive;
  100. m_backupPoint = opt.m_backupPoint;
  101. m_backupPointValue = opt.m_backupPointValue;
  102. m_bk = new double[m_numberOfParallelPoints];
  103. for(int j = 0; j < static_cast<int>(m_numberOfParallelPoints); j++)
  104. {
  105. m_bk[j] = opt.m_bk[j];
  106. }
  107. m_pointsAccepted = new bool[m_numberOfParallelPoints];
  108. for(int j = 0; j < static_cast<int>(m_numberOfParallelPoints); j++)
  109. {
  110. m_pointsAccepted[j] = opt.m_pointsAccepted[j];
  111. }
  112. m_bThresTimesNotDecreased = opt.m_bThresTimesNotDecreased;
  113. m_c0f = opt.m_c0f;
  114. m_c0s = opt.m_c0s;
  115. m_c1f = opt.m_c1f;
  116. m_c1s = opt.m_c1s;
  117. m_D = opt.m_D;
  118. m_initFuncThreshActive = opt.m_initFuncThreshActive;
  119. m_initFuncTresh = opt.m_initFuncTresh;
  120. // advanced init setup
  121. m_advancedInit= opt.m_advancedInit;
  122. m_advancedinitLowerBounds= opt.m_advancedinitLowerBounds;
  123. m_advancedinitUpperBounds= opt.m_advancedinitUpperBounds;
  124. }
  125. return *this;
  126. }
  127. AdaptiveDirectionRandomSearchOptimizer::~AdaptiveDirectionRandomSearchOptimizer()
  128. {
  129. delete[] m_bk;
  130. delete[] m_pointsAccepted;
  131. }
  132. void AdaptiveDirectionRandomSearchOptimizer::init()
  133. {
  134. m_Parameters = OPTIMIZATION::matrix_type(m_numberOfParameters,m_numberOfParallelPoints);
  135. // if not set before, set default value
  136. if(m_bThresTimesNotDecreased == 0)
  137. m_bThresTimesNotDecreased = static_cast<unsigned int>(m_numberOfParameters * m_numberOfParameters* 5.0);
  138. SuperClass::init();
  139. // "reset"
  140. for(int j = 0; j < static_cast<int>(m_numberOfParallelPoints); j++)
  141. {
  142. m_bk[j] = m_b0;
  143. m_pointsAccepted[j]= false;
  144. }
  145. m_backupActive = false;
  146. /*
  147. seed rand
  148. */
  149. #ifdef NICE_USE_ICE
  150. ice::Randomize();
  151. #else
  152. srand(0);
  153. #endif
  154. /*
  155. check if bounds are active! bounds are needed
  156. to generate usefull random start points
  157. */
  158. if(!(m_upperParameterBoundActive == true && m_lowerParameterBoundActive == true))
  159. {
  160. if(m_loger)
  161. {
  162. m_loger->logError("parameter bounds are not active! Please set proper parameter bounds for the random start point generation. This event is has no further exception handling");
  163. }
  164. m_lowerParameterBound = m_parameters;
  165. m_upperParameterBound = m_parameters;
  166. m_lowerParameterBoundActive = true;
  167. m_upperParameterBoundActive = true;
  168. }
  169. /*
  170. generate random start points
  171. */
  172. if(m_advancedInit == false)
  173. {
  174. for(int k = 0; k < static_cast<int>(m_numberOfParameters);k++)
  175. {
  176. for(int l = 0; l < static_cast<int>(m_numberOfParallelPoints);l++)
  177. {
  178. #ifdef NICE_USE_ICE
  179. m_Parameters(k,l) = m_parameters(k,0) + m_scales(k,0) *2.0* (ice::RandomD() - 0.5);
  180. #else
  181. m_Parameters(k,l) = m_parameters(k,0) + m_scales(k,0) *2.0* (drand48() - 0.5);
  182. #endif
  183. }
  184. }
  185. }
  186. else
  187. {
  188. // dimension check
  189. assert(m_advancedinitLowerBounds.rows() == (int)m_numberOfParameters && m_advancedinitUpperBounds.rows() == (int)m_numberOfParameters);
  190. for(int k = 0; k < static_cast<int>(m_numberOfParameters);k++)
  191. {
  192. for(int l = 0; l < static_cast<int>(m_numberOfParallelPoints);l++)
  193. {
  194. #ifdef NICE_USE_ICE
  195. m_Parameters(k,l) = m_advancedinitLowerBounds(k,0) +ice::RandomD() * (m_advancedinitUpperBounds(k,0) - m_advancedinitLowerBounds(k,0) ) ;
  196. #else
  197. m_Parameters(k,l) = m_advancedinitLowerBounds(k,0) +drand48()* (m_advancedinitUpperBounds(k,0) - m_advancedinitLowerBounds(k,0) ) ;
  198. #endif
  199. }
  200. }
  201. }
  202. /*
  203. evaluate SET !!
  204. */
  205. m_CurrentCostFunctionValues = evaluateSetCostFunction(m_Parameters);
  206. /*
  207. If the threshold was set, check if all points are below the threshold
  208. */
  209. if(m_initFuncThreshActive)
  210. {
  211. bool pointOk=false;
  212. for(int u = 0; u < static_cast<int>(m_numberOfParallelPoints); u++)
  213. {
  214. /*
  215. if the are not ok ... generate new points for those, who arent..
  216. */
  217. if(m_CurrentCostFunctionValues(u,0) < m_initFuncTresh)
  218. {
  219. pointOk = true;
  220. }
  221. else
  222. {
  223. pointOk = false;
  224. }
  225. while(pointOk == false)
  226. {
  227. for(int k = 0; k < static_cast<int>(m_numberOfParameters);k++)
  228. {
  229. #ifdef NICE_USE_ICE
  230. m_Parameters(k,u) = m_parameters(k,0) + m_scales(k,0) * 2.0*(ice::RandomD() - 0.5);
  231. #else
  232. m_Parameters(k,u) = m_parameters(k,0) + m_scales(k,0) * 2.0*(drand48() - 0.5);
  233. #endif
  234. }
  235. /*
  236. reevaluate the value and check against threshold
  237. */
  238. //double tmpNewValue = evaluateCostFunction(m_Parameters.Sub(m_numberOfParameters,1,0,u));
  239. double tmpNewValue = evaluateCostFunction(m_Parameters(0,u,m_numberOfParameters-1,u));
  240. /*
  241. if point is ok now go to next point
  242. */
  243. if(tmpNewValue < m_initFuncTresh)
  244. {
  245. m_CurrentCostFunctionValues(u,0) = tmpNewValue;
  246. pointOk = true;
  247. }
  248. } // while point not ok
  249. } // for all points
  250. } // if threshold active
  251. /*if(m_verbose)
  252. {
  253. std::cout << "AdaptiveDirectionRandomSearch :: Initial parameterSet: ";
  254. for(int l=0;l<m_numberOfParallelPoints;l++)
  255. {
  256. for(int r = 0; r < static_cast<int>(m_numberOfParameters); r++)
  257. {
  258. std::cout<< m_Parameters(r,l) << " ";
  259. }
  260. std::cout << m_CurrentCostFunctionValues(l,0) << std::endl;
  261. }
  262. std::cout << std::endl;
  263. std::cout << "Number of Evaluations needed for a proper initilization: " << m_costFunction->getNumberOfEvaluations() << std::endl;
  264. }*/
  265. /*
  266. (re)set m_bk
  267. */
  268. for(int j = 0; j < static_cast<int>(m_numberOfParallelPoints); j++)
  269. {
  270. m_bk[j] = m_b0;
  271. }
  272. }
  273. bool AdaptiveDirectionRandomSearchOptimizer::setControlSeqParams(double b0, double bfac,
  274. unsigned int bThresTimesNotDecreased,double bBreak)
  275. {
  276. if(b0 <= 0 || bfac <= 0 || bfac > 1 || bThresTimesNotDecreased == 0 || bBreak <= 0)
  277. {
  278. return false;
  279. }
  280. m_b0 = b0;
  281. m_bfac = bfac;
  282. m_bThresTimesNotDecreased = bThresTimesNotDecreased;
  283. m_bBreak = bBreak;
  284. return true;
  285. }
  286. void AdaptiveDirectionRandomSearchOptimizer::activateAdvancedInit(bool enable, OPTIMIZATION::matrix_type& lowerBounds, OPTIMIZATION::matrix_type& upperBounds)
  287. {
  288. m_advancedInit= enable;
  289. m_advancedinitLowerBounds= lowerBounds;
  290. m_advancedinitUpperBounds= upperBounds;
  291. }
  292. OPTIMIZATION::matrix_type AdaptiveDirectionRandomSearchOptimizer::generatePoint()
  293. {
  294. OPTIMIZATION::matrix_type newPoint(m_numberOfParameters,1);
  295. for(int i = 0; i < static_cast<int>(m_numberOfParameters); i++)
  296. {
  297. if(m_scales(i,0) > 0.0 )
  298. {
  299. #ifdef NICE_USE_ICE
  300. newPoint(i,0) = m_scales(i,0) * 2.0*(ice::RandomD() - 0.5);
  301. #else
  302. newPoint(i,0) = m_scales(i,0) * 2.0*(drand48() - 0.5);
  303. #endif
  304. }
  305. }
  306. // double div=newPoint.frobeniusNorm();
  307. double div=newPoint.frobeniusNorm();
  308. if (div > 1.0e-50)
  309. {
  310. newPoint = newPoint * (1.0/div);
  311. }
  312. else
  313. {
  314. newPoint=this->generatePoint();
  315. }
  316. return newPoint;
  317. }
  318. OPTIMIZATION::matrix_type AdaptiveDirectionRandomSearchOptimizer::generatePoints()
  319. {
  320. OPTIMIZATION::matrix_type newPoints(m_numberOfParameters,m_numberOfParallelPoints);
  321. OPTIMIZATION::matrix_type newPoint(m_numberOfParameters,1);
  322. for(int j = 0; j < static_cast<int>(m_numberOfParallelPoints);j++)
  323. {
  324. newPoint = this->generatePoint();
  325. for(int i = 0; i < static_cast<int>(m_numberOfParameters); i++)
  326. {
  327. newPoints(i,j) = newPoint(i,0);
  328. }
  329. }
  330. return newPoints;
  331. }
  332. bool AdaptiveDirectionRandomSearchOptimizer::setRecallParams(
  333. double c0s,
  334. double c1s,
  335. double c0f,
  336. double c1f,
  337. double D)
  338. {
  339. if (c0s < 0 ||
  340. c0s >=1 ||
  341. c1s <0 ||
  342. c0s+c1s <= 1 ||
  343. c0f <= 0 ||
  344. c0f >= 1 ||
  345. c1f >= 0 ||
  346. c0f + c1f < -1.0||
  347. c0f + c1f > 1.0 ||
  348. D <= 0.0)
  349. {
  350. return false;
  351. }
  352. m_c0s = c0s;
  353. m_c1s = c1s;
  354. m_c0f = c0f;
  355. m_c1f = c1f;
  356. m_D = D;
  357. return true;
  358. }
  359. void AdaptiveDirectionRandomSearchOptimizer::acceptPoints(OPTIMIZATION::matrix_type oldValues, OPTIMIZATION::matrix_type newValues)
  360. {
  361. for(int i = 0;i< static_cast<int>(m_numberOfParallelPoints);i++)
  362. {
  363. if(newValues(i,0) < oldValues(i,0))
  364. {
  365. m_pointsAccepted[i]=true;
  366. }
  367. else
  368. {
  369. m_pointsAccepted[i]=false;
  370. }
  371. }
  372. }
  373. int AdaptiveDirectionRandomSearchOptimizer::optimize()
  374. {
  375. init();
  376. if(m_loger)
  377. m_loger->logTrace("ADRS: starting optimization ... \n");
  378. /*
  379. start time criteria
  380. */
  381. m_startTime = clock();
  382. bool abort = false;
  383. /*
  384. declare and initialize algorithm specific local variables
  385. */
  386. OPTIMIZATION::matrix_type newPoints;
  387. OPTIMIZATION::matrix_type newValues;
  388. //OPTIMIZATION::matrix_type oldValues;
  389. OPTIMIZATION::matrix_type deltaX(m_numberOfParameters,m_numberOfParallelPoints);
  390. OPTIMIZATION::matrix_type deltaXold(m_numberOfParameters,m_numberOfParallelPoints);
  391. OPTIMIZATION::matrix_type dk(m_numberOfParameters,m_numberOfParallelPoints);
  392. OPTIMIZATION::matrix_type dkold(m_numberOfParameters,m_numberOfParallelPoints);
  393. int i = 0;
  394. int j = 0;
  395. /*
  396. begin with the first step outside the loop
  397. */
  398. m_numIter++;
  399. unsigned int *timesNotDecreased = new unsigned int[m_numberOfParallelPoints];
  400. for(int k = 0; k< static_cast<int>(m_numberOfParallelPoints);k++)
  401. {
  402. timesNotDecreased[k] = 0;
  403. }
  404. /*
  405. generate a new delta X (potential step)
  406. */
  407. OPTIMIZATION::matrix_type tmp = generatePoints();
  408. for(j = 0; j< static_cast<int>(m_numberOfParallelPoints);j++)
  409. {
  410. for(i = 0;i < static_cast<int>(m_numberOfParameters);i++)
  411. {
  412. deltaX(i,j) = tmp(i,j) * m_bk[j] ;
  413. }
  414. }
  415. /*
  416. check costfunction at potential new point
  417. */
  418. newPoints = m_Parameters + deltaX;
  419. newValues = evaluateSetCostFunction(newPoints);
  420. /*
  421. are the new points better?
  422. */
  423. acceptPoints(m_CurrentCostFunctionValues,newValues);
  424. for(j = 0; j < static_cast<int>(m_numberOfParallelPoints);j++)
  425. {
  426. if(m_pointsAccepted[j] == true)
  427. {
  428. for(i =0;i < static_cast<int>(m_numberOfParameters);i++)
  429. {
  430. /*
  431. set the new point
  432. */
  433. m_Parameters(i,j) = newPoints(i,j);
  434. /*
  435. update the recall factor
  436. */
  437. dk(i,j) = dkold(i,j) * m_c0s + deltaXold(i,j) * m_c1s;
  438. }
  439. m_CurrentCostFunctionValues(j,0) = newValues(j,0);
  440. /*
  441. reset the counter for failed attempts
  442. */
  443. timesNotDecreased[j] = 0;
  444. }
  445. else
  446. {
  447. for(i =0; i < static_cast<int>(m_numberOfParameters);i++)
  448. {
  449. /*
  450. update the recall factor
  451. */
  452. dk(i,j) = dkold(i,j) * m_c0f + deltaXold(i,j) * m_c1f;
  453. }
  454. /*
  455. raise the counter for failed attempts
  456. */
  457. timesNotDecreased[j] ++;
  458. }
  459. }
  460. /*
  461. do the optimization in the main loop
  462. */
  463. while(abort == false)
  464. {
  465. /*
  466. increase iteration counter
  467. */
  468. m_numIter++;
  469. /*
  470. Check iteration limit
  471. */
  472. if(m_maxNumIterActive)
  473. {
  474. if(m_numIter >= m_maxNumIter)
  475. {
  476. /* set according return status and return */
  477. m_returnReason = SUCCESS_MAXITER;
  478. abort = true;
  479. continue;
  480. }
  481. }
  482. /*
  483. save the old deltaX
  484. */
  485. deltaXold = deltaX;
  486. /*
  487. generate a new delta X (potential step)
  488. */
  489. OPTIMIZATION::matrix_type tmp = generatePoints();
  490. for(j = 0; j< static_cast<int>(m_numberOfParallelPoints);j++)
  491. {
  492. for(i = 0; i < static_cast<int>(m_numberOfParameters);i++)
  493. {
  494. deltaX(i,j) = dk(i,j) + tmp(i,j) * m_bk[j] ;
  495. }
  496. }
  497. /*
  498. check costfunction at potential new point
  499. */
  500. newPoints = m_Parameters + deltaX;
  501. newValues = evaluateSetCostFunction(newPoints);
  502. /*
  503. save the old dk
  504. */
  505. dkold = dk;
  506. /*
  507. are the new points better?
  508. */
  509. acceptPoints(m_CurrentCostFunctionValues,newValues);
  510. for(j = 0; j < static_cast<int>(m_numberOfParallelPoints);j++)
  511. {
  512. if(m_pointsAccepted[j] == true)
  513. {
  514. for(i =0; i < static_cast<int>(m_numberOfParameters);i++)
  515. {
  516. /*
  517. set the new point
  518. */
  519. m_Parameters(i,j) = newPoints(i,j);
  520. /*
  521. update the recall factor
  522. */
  523. dk(i,j) = dkold(i,j) * m_c0s + deltaXold(i,j) * m_c1s;
  524. }
  525. m_CurrentCostFunctionValues(j,0) = newValues(j,0);
  526. /*
  527. reset the counter for failed attempts
  528. */
  529. timesNotDecreased[j] = 0;
  530. }
  531. else
  532. {
  533. for(i =0;i < static_cast<int>(m_numberOfParameters);i++)
  534. {
  535. /*
  536. update the recall factor
  537. */
  538. dk(i,j) = dkold(i,j) * m_c0f + deltaXold(i,j) * m_c1f;
  539. }
  540. /*
  541. raise the counter for failed attempts
  542. */
  543. timesNotDecreased[j] ++;
  544. }
  545. /*
  546. scaledown m_bk if there was no improvement for a certain time
  547. */
  548. if(timesNotDecreased[j] >= m_bThresTimesNotDecreased)
  549. {
  550. m_bk[j] = m_bk[j] * m_bfac;
  551. timesNotDecreased[j] = 0;
  552. }
  553. /*
  554. if m_bk < m_bBreak ..
  555. */
  556. if( m_bk[j] < m_bBreak )
  557. {
  558. /* */
  559. if(m_backupActive)
  560. {
  561. if(m_CurrentCostFunctionValues(j,0) < m_backupPointValue)
  562. {
  563. //m_backupPoint = m_Parameters.Sub(m_numberOfParameters,1,0,j);
  564. m_backupPoint = m_Parameters(0,j,m_numberOfParameters-1,j);
  565. m_backupPointValue = m_CurrentCostFunctionValues(j,0);
  566. }
  567. }
  568. else
  569. {
  570. //m_backupPoint = m_Parameters.Sub(m_numberOfParameters,1,0,j);
  571. m_backupPoint = m_Parameters(0,j,m_numberOfParameters-1,j);
  572. m_backupPointValue = m_CurrentCostFunctionValues(j,0);
  573. m_backupActive = true;
  574. }
  575. /*
  576. reset counters
  577. */
  578. m_bk[j] = m_b0;
  579. timesNotDecreased[j] = 0;
  580. OPTIMIZATION::matrix_type resProb = m_CurrentCostFunctionValues;
  581. double maxVal=m_CurrentCostFunctionValues.Max();
  582. for(int i=0; i < (int)m_numberOfParallelPoints; ++i)
  583. {
  584. resProb(i,0) -= maxVal;
  585. }
  586. double denom = MatrixSum(resProb);
  587. /*
  588. ensure numerical stability
  589. */
  590. if( fabs(denom) < 1.0e-50)
  591. {
  592. denom = denom < 0.0 ? -1.0e-50 : 1.0e-50;
  593. }
  594. resProb = resProb * (1.0/denom);
  595. double sum = 0.0;
  596. for(int u = 0; u < static_cast<int>(m_numberOfParallelPoints); u++)
  597. {
  598. sum += resProb(u,0);
  599. resProb(u,0) = sum;
  600. }
  601. /*
  602. generate random number [0,1]
  603. */
  604. #ifdef NICE_USE_ICE
  605. double choice = ice::RandomD();
  606. #else
  607. double choice = drand48();
  608. #endif
  609. int u_chosen = 0;
  610. for(int u = 0; u < static_cast<int>(m_numberOfParallelPoints); u++)
  611. {
  612. u_chosen = u;
  613. if( choice < resProb(u,0) )
  614. {
  615. break;
  616. }
  617. }
  618. /*
  619. set m_parameters
  620. */
  621. for(int v = 0; v < static_cast<int>(m_numberOfParameters); v++)
  622. {
  623. m_Parameters(v,j)=m_Parameters(v,u_chosen);
  624. }
  625. m_CurrentCostFunctionValues(j,0) = m_CurrentCostFunctionValues(u_chosen,0);
  626. }
  627. /*
  628. dk has to be <= D * m_bk
  629. */
  630. // double norm= dk(0,j,m_numberOfParameters-1,j).frobeniusNorm();
  631. double norm= dk(0,j,m_numberOfParameters-1,j).frobeniusNorm();
  632. if( norm >= m_D * m_bk[j])
  633. {
  634. if(norm < 1.0e-50)
  635. {
  636. //m_loger->logWarning("ADRS Optimizer: Warning Computation gets unstable");
  637. norm = 1.0e-50;
  638. }
  639. for(i =0;i < static_cast<int>(m_numberOfParameters);i++)
  640. {
  641. /*
  642. update the recall factor
  643. */
  644. dk(i,j) = dk(i,j) * 1.0/norm;
  645. }
  646. }
  647. }
  648. if(m_verbose)
  649. {
  650. std::cout << "# AdaptiveDirectionRandomSearch :: parameterSet: ";
  651. for(int l=0;l < static_cast<int>(m_numberOfParallelPoints);l++)
  652. {
  653. for(int r = 0; r < static_cast<int>(m_numberOfParameters); r++)
  654. {
  655. std::cout<< m_Parameters(r,l) << " ";
  656. }
  657. std::cout << m_bk[l] << " "<<
  658. m_CurrentCostFunctionValues(l,0) << std::endl;
  659. }
  660. std::cout <<"# "<< std::endl;
  661. }
  662. // fixme wacker for plotting
  663. /*
  664. for(int l=0;l<m_numberOfParallelPoints;l++)
  665. {
  666. for(int r = 0; r < 2; r++)
  667. {
  668. std::cout<< m_Parameters(r,l) << " ";
  669. }
  670. std::cout << m_CurrentCostFunctionValues(l,0) << std::endl;
  671. }
  672. */
  673. /*
  674. Check if it is in bounds, maxSeconds
  675. */
  676. /*
  677. if(!checkParameters(m_parameters))
  678. {
  679. // set according return status and the last parameters and return
  680. m_returnReason = ERROR_XOUTOFBOUNDS;
  681. abort = true;
  682. }*/
  683. /*
  684. check kind of paramtol
  685. */
  686. if(m_paramTolActive)
  687. {
  688. if(m_numberOfParallelPoints > 1 )
  689. {
  690. /*
  691. check if distance from one point to all others is below a the threshold
  692. */
  693. OPTIMIZATION::matrix_type paramSet = m_Parameters;
  694. bool abortNow = true;
  695. for(int e = 0; e < static_cast<int>(m_numberOfParallelPoints);e++)
  696. {
  697. if( (paramSet(0,e,m_numberOfParameters-1,e) - paramSet(0,0,m_numberOfParameters-1,0)).frobeniusNorm() > m_paramTol)
  698. {
  699. abortNow = false;
  700. }
  701. }
  702. if(abortNow)
  703. {
  704. abort = true;
  705. m_returnReason = SUCCESS_PARAMTOL;
  706. }
  707. }
  708. }
  709. /*
  710. Check Optimization Timelimit, if active
  711. */
  712. if(m_maxSecondsActive)
  713. {
  714. m_currentTime = clock();
  715. /* time limit exceeded ? */
  716. if(((float)(m_currentTime - m_startTime )/CLOCKS_PER_SEC) >= m_maxSeconds )
  717. {
  718. /* set according return status and the last parameters and return */
  719. m_returnReason = SUCCESS_TIMELIMIT;
  720. abort = true;
  721. continue;
  722. }
  723. }
  724. }
  725. /*
  726. find the best value..
  727. */
  728. unsigned int u_best = 0;
  729. for(int u = 0; u < static_cast<int>(m_numberOfParallelPoints); u++)
  730. {
  731. if( m_CurrentCostFunctionValues(u,0) < m_CurrentCostFunctionValues(u_best,0) )
  732. {
  733. u_best = u;
  734. }
  735. }
  736. /*
  737. regular points include the best one
  738. */
  739. //m_parameters = m_Parameters.Sub(m_numberOfParameters,1,0,u_best);
  740. m_parameters = m_Parameters(0,u_best,m_numberOfParameters-1,u_best);
  741. m_currentCostFunctionValue = m_CurrentCostFunctionValues(u_best,0);
  742. if (m_backupActive)
  743. {
  744. /*
  745. compare with backup point
  746. */
  747. if( m_backupPointValue < m_CurrentCostFunctionValues(u_best,0) )
  748. {
  749. /*
  750. backup point is best
  751. */
  752. m_parameters = m_backupPoint;
  753. m_currentCostFunctionValue = m_backupPointValue;
  754. }
  755. }
  756. delete[] timesNotDecreased;
  757. return m_returnReason;
  758. }