AdaptiveDirectionRandomSearchOptimizer.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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 "optimization/Opt_Namespace.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 = 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. ice::Randomize();
  150. /*
  151. check if bounds are active! bounds are needed
  152. to generate usefull random start points
  153. */
  154. if(!(m_upperParameterBoundActive == true && m_lowerParameterBoundActive == true))
  155. {
  156. if(m_loger)
  157. {
  158. 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");
  159. }
  160. m_lowerParameterBound = m_parameters;
  161. m_upperParameterBound = m_parameters;
  162. m_lowerParameterBoundActive = true;
  163. m_upperParameterBoundActive = true;
  164. }
  165. /*
  166. generate random start points
  167. */
  168. if(m_advancedInit == false)
  169. {
  170. for(int k = 0; k < static_cast<int>(m_numberOfParameters);k++)
  171. {
  172. for(int l = 0; l < static_cast<int>(m_numberOfParallelPoints);l++)
  173. {
  174. m_Parameters[k][l] = m_parameters[k][0] + m_scales[k][0] *2.0* (ice::RandomD() - 0.5);
  175. }
  176. }
  177. }
  178. else
  179. {
  180. // dimension check
  181. assert(m_advancedinitLowerBounds.rows() == (int)m_numberOfParameters && m_advancedinitUpperBounds.rows() == (int)m_numberOfParameters);
  182. for(int k = 0; k < static_cast<int>(m_numberOfParameters);k++)
  183. {
  184. for(int l = 0; l < static_cast<int>(m_numberOfParallelPoints);l++)
  185. {
  186. m_Parameters[k][l] = m_advancedinitLowerBounds[k][0] +ice::RandomD() * (m_advancedinitUpperBounds[k][0] - m_advancedinitLowerBounds[k][0] ) ;
  187. }
  188. }
  189. }
  190. /*
  191. evaluate SET !!
  192. */
  193. m_CurrentCostFunctionValues = evaluateSetCostFunction(m_Parameters);
  194. /*
  195. If the threshold was set, check if all points are below the threshold
  196. */
  197. if(m_initFuncThreshActive)
  198. {
  199. bool pointOk=false;
  200. for(int u = 0; u < static_cast<int>(m_numberOfParallelPoints); u++)
  201. {
  202. /*
  203. if the are not ok ... generate new points for those, who arent..
  204. */
  205. if(m_CurrentCostFunctionValues[u][0] < m_initFuncTresh)
  206. {
  207. pointOk = true;
  208. }
  209. else
  210. {
  211. pointOk = false;
  212. }
  213. while(pointOk == false)
  214. {
  215. for(int k = 0; k < static_cast<int>(m_numberOfParameters);k++)
  216. {
  217. m_Parameters[k][u] = m_parameters[k][0] + m_scales[k][0] * 2.0*(ice::RandomD() - 0.5);
  218. }
  219. /*
  220. reevaluate the value and check against threshold
  221. */
  222. //double tmpNewValue = evaluateCostFunction(m_Parameters.Sub(m_numberOfParameters,1,0,u));
  223. double tmpNewValue = evaluateCostFunction(m_Parameters(0,u,m_numberOfParameters-1,u));
  224. /*
  225. if point is ok now go to next point
  226. */
  227. if(tmpNewValue < m_initFuncTresh)
  228. {
  229. m_CurrentCostFunctionValues[u][0] = tmpNewValue;
  230. pointOk = true;
  231. }
  232. } // while point not ok
  233. } // for all points
  234. } // if threshold active
  235. /*if(m_verbose)
  236. {
  237. std::cout << "AdaptiveDirectionRandomSearch :: Initial parameterSet: ";
  238. for(int l=0;l<m_numberOfParallelPoints;l++)
  239. {
  240. for(int r = 0; r < static_cast<int>(m_numberOfParameters); r++)
  241. {
  242. std::cout<< m_Parameters[r][l] << " ";
  243. }
  244. std::cout << m_CurrentCostFunctionValues[l][0] << std::endl;
  245. }
  246. std::cout << std::endl;
  247. std::cout << "Number of Evaluations needed for a proper initilization: " << m_costFunction->getNumberOfEvaluations() << std::endl;
  248. }*/
  249. /*
  250. (re)set m_bk
  251. */
  252. for(int j = 0; j < static_cast<int>(m_numberOfParallelPoints); j++)
  253. {
  254. m_bk[j] = m_b0;
  255. }
  256. }
  257. bool AdaptiveDirectionRandomSearchOptimizer::setControlSeqParams(double b0, double bfac,
  258. unsigned int bThresTimesNotDecreased,double bBreak)
  259. {
  260. if(b0 <= 0 || bfac <= 0 || bfac > 1 || bThresTimesNotDecreased == 0 || bBreak <= 0)
  261. {
  262. return false;
  263. }
  264. m_b0 = b0;
  265. m_bfac = bfac;
  266. m_bThresTimesNotDecreased = bThresTimesNotDecreased;
  267. m_bBreak = bBreak;
  268. return true;
  269. }
  270. void AdaptiveDirectionRandomSearchOptimizer::activateAdvancedInit(bool enable, matrix_type& lowerBounds, matrix_type& upperBounds)
  271. {
  272. m_advancedInit= enable;
  273. m_advancedinitLowerBounds= lowerBounds;
  274. m_advancedinitUpperBounds= upperBounds;
  275. }
  276. matrix_type AdaptiveDirectionRandomSearchOptimizer::generatePoint()
  277. {
  278. matrix_type newPoint(m_numberOfParameters,1);
  279. for(int i = 0; i < static_cast<int>(m_numberOfParameters); i++)
  280. {
  281. if(m_scales[i][0] > 0.0 )
  282. {
  283. newPoint[i][0] = m_scales[i][0] * 2.0*(ice::RandomD() - 0.5);
  284. }
  285. }
  286. //double div=newPoint.Norm(0);
  287. double div=newPoint.Norm(0);
  288. if (div > 1.0e-50)
  289. {
  290. newPoint = newPoint * (1.0/div);
  291. }
  292. else
  293. {
  294. newPoint=this->generatePoint();
  295. }
  296. return newPoint;
  297. }
  298. matrix_type AdaptiveDirectionRandomSearchOptimizer::generatePoints()
  299. {
  300. matrix_type newPoints(m_numberOfParameters,m_numberOfParallelPoints);
  301. matrix_type newPoint(m_numberOfParameters,1);
  302. for(int j = 0; j < static_cast<int>(m_numberOfParallelPoints);j++)
  303. {
  304. newPoint = this->generatePoint();
  305. for(int i = 0; i < static_cast<int>(m_numberOfParameters); i++)
  306. {
  307. newPoints[i][j] = newPoint[i][0];
  308. }
  309. }
  310. return newPoints;
  311. }
  312. bool AdaptiveDirectionRandomSearchOptimizer::setRecallParams(
  313. double c0s,
  314. double c1s,
  315. double c0f,
  316. double c1f,
  317. double D)
  318. {
  319. if (c0s < 0 ||
  320. c0s >=1 ||
  321. c1s <0 ||
  322. c0s+c1s <= 1 ||
  323. c0f <= 0 ||
  324. c0f >= 1 ||
  325. c1f >= 0 ||
  326. c0f + c1f < -1.0||
  327. c0f + c1f > 1.0 ||
  328. D <= 0.0)
  329. {
  330. return false;
  331. }
  332. m_c0s = c0s;
  333. m_c1s = c1s;
  334. m_c0f = c0f;
  335. m_c1f = c1f;
  336. m_D = D;
  337. return true;
  338. }
  339. void AdaptiveDirectionRandomSearchOptimizer::acceptPoints(matrix_type oldValues, matrix_type newValues)
  340. {
  341. for(int i = 0;i< static_cast<int>(m_numberOfParallelPoints);i++)
  342. {
  343. if(newValues[i][0] < oldValues[i][0])
  344. {
  345. m_pointsAccepted[i]=true;
  346. }
  347. else
  348. {
  349. m_pointsAccepted[i]=false;
  350. }
  351. }
  352. }
  353. int AdaptiveDirectionRandomSearchOptimizer::optimize()
  354. {
  355. init();
  356. if(m_loger)
  357. m_loger->logTrace("ADRS: starting optimization ... \n");
  358. /*
  359. start time criteria
  360. */
  361. m_startTime = clock();
  362. bool abort = false;
  363. /*
  364. declare and initialize algorithm specific local variables
  365. */
  366. matrix_type newPoints;
  367. matrix_type newValues;
  368. //matrix_type oldValues;
  369. matrix_type deltaX(m_numberOfParameters,m_numberOfParallelPoints);
  370. matrix_type deltaXold(m_numberOfParameters,m_numberOfParallelPoints);
  371. matrix_type dk(m_numberOfParameters,m_numberOfParallelPoints);
  372. matrix_type dkold(m_numberOfParameters,m_numberOfParallelPoints);
  373. int i = 0;
  374. int j = 0;
  375. /*
  376. begin with the first step outside the loop
  377. */
  378. m_numIter++;
  379. unsigned int *timesNotDecreased = new unsigned int[m_numberOfParallelPoints];
  380. for(int k = 0; k< static_cast<int>(m_numberOfParallelPoints);k++)
  381. {
  382. timesNotDecreased[k] = 0;
  383. }
  384. /*
  385. generate a new delta X (potential step)
  386. */
  387. matrix_type tmp = generatePoints();
  388. for(j = 0; j< static_cast<int>(m_numberOfParallelPoints);j++)
  389. {
  390. for(i = 0;i < static_cast<int>(m_numberOfParameters);i++)
  391. {
  392. deltaX[i][j] = tmp[i][j] * m_bk[j] ;
  393. }
  394. }
  395. /*
  396. check costfunction at potential new point
  397. */
  398. newPoints = m_Parameters + deltaX;
  399. newValues = evaluateSetCostFunction(newPoints);
  400. /*
  401. are the new points better?
  402. */
  403. acceptPoints(m_CurrentCostFunctionValues,newValues);
  404. for(j = 0; j < static_cast<int>(m_numberOfParallelPoints);j++)
  405. {
  406. if(m_pointsAccepted[j] == true)
  407. {
  408. for(i =0;i < static_cast<int>(m_numberOfParameters);i++)
  409. {
  410. /*
  411. set the new point
  412. */
  413. m_Parameters[i][j] = newPoints[i][j];
  414. /*
  415. update the recall factor
  416. */
  417. dk[i][j] = dkold[i][j] * m_c0s + deltaXold[i][j] * m_c1s;
  418. }
  419. m_CurrentCostFunctionValues[j][0] = newValues[j][0];
  420. /*
  421. reset the counter for failed attempts
  422. */
  423. timesNotDecreased[j] = 0;
  424. }
  425. else
  426. {
  427. for(i =0; i < static_cast<int>(m_numberOfParameters);i++)
  428. {
  429. /*
  430. update the recall factor
  431. */
  432. dk[i][j] = dkold[i][j] * m_c0f + deltaXold[i][j] * m_c1f;
  433. }
  434. /*
  435. raise the counter for failed attempts
  436. */
  437. timesNotDecreased[j] ++;
  438. }
  439. }
  440. /*
  441. do the optimization in the main loop
  442. */
  443. while(abort == false)
  444. {
  445. /*
  446. increase iteration counter
  447. */
  448. m_numIter++;
  449. /*
  450. Check iteration limit
  451. */
  452. if(m_maxNumIterActive)
  453. {
  454. if(m_numIter >= m_maxNumIter)
  455. {
  456. /* set according return status and return */
  457. m_returnReason = SUCCESS_MAXITER;
  458. abort = true;
  459. continue;
  460. }
  461. }
  462. /*
  463. save the old deltaX
  464. */
  465. deltaXold = deltaX;
  466. /*
  467. generate a new delta X (potential step)
  468. */
  469. matrix_type tmp = generatePoints();
  470. for(j = 0; j< static_cast<int>(m_numberOfParallelPoints);j++)
  471. {
  472. for(i = 0; i < static_cast<int>(m_numberOfParameters);i++)
  473. {
  474. deltaX[i][j] = dk[i][j] + tmp[i][j] * m_bk[j] ;
  475. }
  476. }
  477. /*
  478. check costfunction at potential new point
  479. */
  480. newPoints = m_Parameters + deltaX;
  481. newValues = evaluateSetCostFunction(newPoints);
  482. /*
  483. save the old dk
  484. */
  485. dkold = dk;
  486. /*
  487. are the new points better?
  488. */
  489. acceptPoints(m_CurrentCostFunctionValues,newValues);
  490. for(j = 0; j < static_cast<int>(m_numberOfParallelPoints);j++)
  491. {
  492. if(m_pointsAccepted[j] == true)
  493. {
  494. for(i =0; i < static_cast<int>(m_numberOfParameters);i++)
  495. {
  496. /*
  497. set the new point
  498. */
  499. m_Parameters[i][j] = newPoints[i][j];
  500. /*
  501. update the recall factor
  502. */
  503. dk[i][j] = dkold[i][j] * m_c0s + deltaXold[i][j] * m_c1s;
  504. }
  505. m_CurrentCostFunctionValues[j][0] = newValues[j][0];
  506. /*
  507. reset the counter for failed attempts
  508. */
  509. timesNotDecreased[j] = 0;
  510. }
  511. else
  512. {
  513. for(i =0;i < static_cast<int>(m_numberOfParameters);i++)
  514. {
  515. /*
  516. update the recall factor
  517. */
  518. dk[i][j] = dkold[i][j] * m_c0f + deltaXold[i][j] * m_c1f;
  519. }
  520. /*
  521. raise the counter for failed attempts
  522. */
  523. timesNotDecreased[j] ++;
  524. }
  525. /*
  526. scaledown m_bk if there was no improvement for a certain time
  527. */
  528. if(timesNotDecreased[j] >= m_bThresTimesNotDecreased)
  529. {
  530. m_bk[j] = m_bk[j] * m_bfac;
  531. timesNotDecreased[j] = 0;
  532. }
  533. /*
  534. if m_bk < m_bBreak ..
  535. */
  536. if( m_bk[j] < m_bBreak )
  537. {
  538. /* */
  539. if(m_backupActive)
  540. {
  541. if(m_CurrentCostFunctionValues[j][0] < m_backupPointValue)
  542. {
  543. //m_backupPoint = m_Parameters.Sub(m_numberOfParameters,1,0,j);
  544. m_backupPoint = m_Parameters(0,j,m_numberOfParameters-1,j);
  545. m_backupPointValue = m_CurrentCostFunctionValues[j][0];
  546. }
  547. }
  548. else
  549. {
  550. //m_backupPoint = m_Parameters.Sub(m_numberOfParameters,1,0,j);
  551. m_backupPoint = m_Parameters(0,j,m_numberOfParameters-1,j);
  552. m_backupPointValue = m_CurrentCostFunctionValues[j][0];
  553. m_backupActive = true;
  554. }
  555. /*
  556. reset counters
  557. */
  558. m_bk[j] = m_b0;
  559. timesNotDecreased[j] = 0;
  560. matrix_type resProb = m_CurrentCostFunctionValues;
  561. double maxVal=m_CurrentCostFunctionValues.MaxVal();
  562. for(int i=0; i < (int)m_numberOfParallelPoints; ++i)
  563. {
  564. resProb[i][0] -= maxVal;
  565. }
  566. double denom = MatrixSum(resProb);
  567. /*
  568. ensure numerical stability
  569. */
  570. if( fabs(denom) < 1.0e-50)
  571. {
  572. denom = denom < 0.0 ? -1.0e-50 : 1.0e-50;
  573. }
  574. resProb = resProb * (1.0/denom);
  575. double sum = 0.0;
  576. for(int u = 0; u < static_cast<int>(m_numberOfParallelPoints); u++)
  577. {
  578. sum += resProb[u][0];
  579. resProb[u][0] = sum;
  580. }
  581. /*
  582. generate random number [0,1]
  583. */
  584. double choice = ice::RandomD();
  585. int u_chosen = 0;
  586. for(int u = 0; u < static_cast<int>(m_numberOfParallelPoints); u++)
  587. {
  588. u_chosen = u;
  589. if( choice < resProb[u][0] )
  590. {
  591. break;
  592. }
  593. }
  594. /*
  595. set m_parameters
  596. */
  597. for(int v = 0; v < static_cast<int>(m_numberOfParameters); v++)
  598. {
  599. m_Parameters[v][j]=m_Parameters[v][u_chosen];
  600. }
  601. m_CurrentCostFunctionValues[j][0] = m_CurrentCostFunctionValues[u_chosen][0];
  602. }
  603. /*
  604. dk has to be <= D * m_bk
  605. */
  606. //double norm= dk(0,j,m_numberOfParameters-1,j).Norm(0);
  607. double norm= dk(0,j,m_numberOfParameters-1,j).Norm(0);
  608. if( norm >= m_D * m_bk[j])
  609. {
  610. if(norm < 1.0e-50)
  611. {
  612. //m_loger->logWarning("ADRS Optimizer: Warning Computation gets unstable");
  613. norm = 1.0e-50;
  614. }
  615. for(i =0;i < static_cast<int>(m_numberOfParameters);i++)
  616. {
  617. /*
  618. update the recall factor
  619. */
  620. dk[i][j] = dk[i][j] * 1.0/norm;
  621. }
  622. }
  623. }
  624. if(m_verbose)
  625. {
  626. std::cout << "# AdaptiveDirectionRandomSearch :: parameterSet: ";
  627. for(int l=0;l < static_cast<int>(m_numberOfParallelPoints);l++)
  628. {
  629. for(int r = 0; r < static_cast<int>(m_numberOfParameters); r++)
  630. {
  631. std::cout<< m_Parameters[r][l] << " ";
  632. }
  633. std::cout << m_bk[l] << " "<<
  634. m_CurrentCostFunctionValues[l][0] << std::endl;
  635. }
  636. std::cout <<"# "<< std::endl;
  637. }
  638. // fixme wacker for plotting
  639. /*
  640. for(int l=0;l<m_numberOfParallelPoints;l++)
  641. {
  642. for(int r = 0; r < 2; r++)
  643. {
  644. std::cout<< m_Parameters[r][l] << " ";
  645. }
  646. std::cout << m_CurrentCostFunctionValues[l][0] << std::endl;
  647. }
  648. */
  649. /*
  650. Check if it is in bounds, maxSeconds
  651. */
  652. /*
  653. if(!checkParameters(m_parameters))
  654. {
  655. // set according return status and the last parameters and return
  656. m_returnReason = ERROR_XOUTOFBOUNDS;
  657. abort = true;
  658. }*/
  659. /*
  660. check kind of paramtol
  661. */
  662. if(m_paramTolActive)
  663. {
  664. if(m_numberOfParallelPoints > 1 )
  665. {
  666. /*
  667. check if distance from one point to all others is below a the threshold
  668. */
  669. matrix_type paramSet = m_Parameters;
  670. bool abortNow = true;
  671. for(int e = 0; e < static_cast<int>(m_numberOfParallelPoints);e++)
  672. {
  673. if( (paramSet(0,e,m_numberOfParameters-1,e) - paramSet(0,0,m_numberOfParameters-1,0)).Norm(0) > m_paramTol)
  674. {
  675. abortNow = false;
  676. }
  677. }
  678. if(abortNow)
  679. {
  680. abort = true;
  681. m_returnReason = SUCCESS_PARAMTOL;
  682. }
  683. }
  684. }
  685. /*
  686. Check Optimization Timelimit, if active
  687. */
  688. if(m_maxSecondsActive)
  689. {
  690. m_currentTime = clock();
  691. /* time limit exceeded ? */
  692. if(((float)(m_currentTime - m_startTime )/CLOCKS_PER_SEC) >= m_maxSeconds )
  693. {
  694. /* set according return status and the last parameters and return */
  695. m_returnReason = SUCCESS_TIMELIMIT;
  696. abort = true;
  697. continue;
  698. }
  699. }
  700. }
  701. /*
  702. find the best value..
  703. */
  704. unsigned int u_best = 0;
  705. for(int u = 0; u < static_cast<int>(m_numberOfParallelPoints); u++)
  706. {
  707. if( m_CurrentCostFunctionValues[u][0] < m_CurrentCostFunctionValues[u_best][0] )
  708. {
  709. u_best = u;
  710. }
  711. }
  712. /*
  713. regular points include the best one
  714. */
  715. //m_parameters = m_Parameters.Sub(m_numberOfParameters,1,0,u_best);
  716. m_parameters = m_Parameters(0,u_best,m_numberOfParameters-1,u_best);
  717. m_currentCostFunctionValue = m_CurrentCostFunctionValues[u_best][0];
  718. if (m_backupActive)
  719. {
  720. /*
  721. compare with backup point
  722. */
  723. if( m_backupPointValue < m_CurrentCostFunctionValues[u_best][0] )
  724. {
  725. /*
  726. backup point is best
  727. */
  728. m_parameters = m_backupPoint;
  729. m_currentCostFunctionValue = m_backupPointValue;
  730. }
  731. }
  732. delete[] timesNotDecreased;
  733. return m_returnReason;
  734. }