CombinatorialOptimizer.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // CombinatorialOptimizer.cpp: Implementation of the
  4. // CombinatorialOptimizer class.
  5. //
  6. //////////////////////////////////////////////////////////////////////
  7. #include "optimization/CombinatorialOptimizer.h"
  8. #include "core/basics/Exception.h"
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #ifdef NICE_USE_ICE
  12. #include "numbase.h" // ice random
  13. #endif
  14. #include <iostream>
  15. using namespace OPTIMIZATION;
  16. CombinatorialOptimizer::CombinatorialOptimizer(OptLogBase *loger): SuperClass(loger)
  17. {
  18. m_mode = 1;
  19. m_T0 = 1;
  20. m_Tk = m_T0;
  21. m_alpha = 0.95;
  22. //m_stepLength = 1.0;
  23. m_fast = false;
  24. }
  25. CombinatorialOptimizer::CombinatorialOptimizer( const CombinatorialOptimizer &opt) : SuperClass(opt)
  26. {
  27. m_mode = opt.m_mode;
  28. m_T0 = opt.m_T0;
  29. m_Tk = opt.m_Tk;
  30. m_alpha = opt.m_alpha;
  31. //m_stepLength = opt.m_stepLength;
  32. m_fast = opt.m_fast;
  33. }
  34. /*
  35. operator=
  36. */
  37. CombinatorialOptimizer & CombinatorialOptimizer::operator=(const CombinatorialOptimizer &opt)
  38. {
  39. /*
  40. avoid self-copy
  41. */
  42. if(this != &opt)
  43. {
  44. /*
  45. =operator of SuperClass
  46. */
  47. SuperClass::operator=(opt);
  48. /*
  49. own values:
  50. */
  51. m_mode = opt.m_mode;
  52. m_T0 = opt.m_T0;
  53. m_Tk = opt.m_Tk;
  54. m_alpha = opt.m_alpha;
  55. //m_stepLength = opt.m_stepLength;
  56. m_fast = opt.m_fast;
  57. }
  58. return *this;
  59. }
  60. CombinatorialOptimizer::~CombinatorialOptimizer()
  61. {
  62. }
  63. void CombinatorialOptimizer::init()
  64. {
  65. SuperClass::init();
  66. m_currentCostFunctionValue = evaluateCostFunction(m_parameters);
  67. /*
  68. seed rand
  69. */
  70. #ifdef NICE_USE_ICE
  71. ice::Randomize();
  72. #else
  73. srand(0);
  74. #endif
  75. /*
  76. (re)set m_Tk
  77. */
  78. m_Tk = m_T0;
  79. }
  80. bool CombinatorialOptimizer::setMode(int mode)
  81. {
  82. switch(mode)
  83. {
  84. case 1:
  85. case 2:
  86. case 3:
  87. case 4:
  88. m_mode = mode;
  89. return true;
  90. default:
  91. return false;
  92. }
  93. }
  94. bool CombinatorialOptimizer::setControlSeqParam(double T0,double alpha)
  95. {
  96. if(T0 < 0)
  97. return false;
  98. m_T0 = T0;
  99. if(alpha < 0 || alpha > 1)
  100. return false;
  101. return true;
  102. }
  103. bool CombinatorialOptimizer::accepptPoint(double oldValue, double newValue)
  104. {
  105. bool accept = false;
  106. //cout<<"old val: "<<oldValue<<endl;
  107. // cout<<"new val: "<<newValue<<endl;
  108. // cout<<endl;
  109. switch(m_mode)
  110. {
  111. case 1:
  112. {
  113. /*
  114. simulated annealing
  115. *****************************/
  116. if(newValue <= oldValue)
  117. {
  118. accept = true;
  119. break;
  120. }
  121. /*
  122. generate uniform distrubuted random number
  123. */
  124. #ifdef NICE_USE_ICE
  125. double lambda = ice::RandomD();
  126. #else
  127. double lambda = drand48();
  128. #endif
  129. if(lambda <= exp(-(newValue - oldValue)/((m_Tk < 1.0e-20)? 1.0e-20 :m_Tk)))
  130. {
  131. accept =true;
  132. }
  133. break;
  134. }
  135. case 2:
  136. /*
  137. threshold acceptance
  138. *****************************/
  139. if((newValue - oldValue) < m_Tk)
  140. {
  141. accept = true;
  142. }
  143. break;
  144. case 3:
  145. /*
  146. great deluge algorithm
  147. *****************************/
  148. if(newValue <= m_Tk)
  149. {
  150. accept = true;
  151. }
  152. break;
  153. case 4:
  154. /*
  155. stochastic relaxation
  156. *****************************/
  157. if(newValue <= oldValue)
  158. {
  159. accept = true;
  160. }
  161. case 5:
  162. {
  163. /*
  164. annealing & theshold
  165. *****************************/
  166. /*
  167. accept better values
  168. */
  169. if(newValue <= oldValue)
  170. {
  171. accept = true;
  172. break;
  173. }
  174. /*
  175. dont accept values bigger than m_threshold
  176. */
  177. if(newValue > m_threshold )
  178. {
  179. accept = false;
  180. break;
  181. }
  182. /*
  183. generate uniform distrubuted random number
  184. */
  185. #ifdef NICE_USE_ICE
  186. double lambda = ice::RandomD();
  187. #else
  188. double lambda = drand48();
  189. #endif
  190. if(lambda <= exp(-(newValue - oldValue)/((m_Tk < 1.0e-20)? 1.0e-20 :m_Tk)))
  191. {
  192. accept =true;
  193. }
  194. else
  195. accept = false;
  196. break;
  197. }
  198. default:
  199. accept = false;
  200. }
  201. return accept;
  202. }
  203. matrix_type CombinatorialOptimizer::generatePoint()
  204. {
  205. matrix_type newPoint(m_parameters);
  206. for(int i = 0; i < static_cast<int>(m_numberOfParameters); i++)
  207. {
  208. if(m_scales(i,0) > 0.0 )
  209. {
  210. if(m_fast == true)
  211. {
  212. //newPoint(i,0) = m_parameters(i,0) + m_stepLength * m_Tk * ice::GaussRandom(1.0) ;
  213. #ifdef NICE_USE_ICE
  214. newPoint(i,0) = m_parameters(i,0) + m_scales(i,0) * m_Tk * ice::GaussRandom(1.0) ;
  215. #else
  216. fthrow ( NICE::Exception, "CombinatorialOptimizer::generatePoint(): this functions needs the ICE library (NICE_USE_ICE)" );
  217. #endif
  218. }
  219. else
  220. {
  221. #ifdef NICE_USE_ICE
  222. newPoint(i,0) = m_parameters(i,0) + m_scales(i,0) * ice::GaussRandom(1.0) ;
  223. #else
  224. fthrow ( NICE::Exception, "CombinatorialOptimizer::generatePoint(): this functions needs the ICE library (NICE_USE_ICE)" );
  225. #endif
  226. }
  227. }
  228. }
  229. //cout<<"new Point: "<<newPoint<<endl;
  230. return newPoint;
  231. }
  232. //bool CombinatorialOptimizer::setSteplength(double steplength)
  233. //{
  234. // if(steplength <= 0)
  235. // return false;
  236. //
  237. // m_stepLength = steplength;
  238. //
  239. // return true;
  240. //}
  241. int CombinatorialOptimizer::optimize()
  242. {
  243. // init
  244. init();
  245. /*
  246. start time criteria
  247. */
  248. m_startTime = clock();
  249. bool abort = false;
  250. matrix_type newPoint;
  251. double newValue;
  252. /*
  253. do the optimization
  254. */
  255. while(abort == false)
  256. {
  257. /*
  258. increase iteration counter
  259. */
  260. m_numIter++;
  261. /*
  262. Check iteration limit
  263. */
  264. if(m_maxNumIterActive)
  265. {
  266. if(m_numIter >= m_maxNumIter)
  267. {
  268. /* set according return status and return */
  269. m_returnReason = SUCCESS_MAXITER;
  270. abort = true;
  271. continue;
  272. }
  273. }
  274. /*
  275. update control seq.
  276. */
  277. m_Tk *= m_alpha;
  278. /*
  279. generate new point
  280. */
  281. newPoint = generatePoint();
  282. /*
  283. evaluate cost function
  284. */
  285. newValue = evaluateCostFunction(newPoint);
  286. /*
  287. accept new point
  288. */
  289. if( accepptPoint(m_currentCostFunctionValue,newValue) )
  290. {
  291. m_parameters = newPoint;
  292. m_currentCostFunctionValue = newValue;
  293. }
  294. if(m_verbose)
  295. {
  296. std::cout << "CombinatorialOptimizer :: parameter: ";
  297. for(int r = 0; r < static_cast<int>(m_numberOfParameters); r++)
  298. {
  299. std::cout<< m_parameters(r,0) << " ";
  300. }
  301. std::cout << m_currentCostFunctionValue << std::endl;
  302. }
  303. /*
  304. Check if it is in bounds, paramTol, funcTol, NumIter, gradienttol, maxSeconds
  305. */
  306. if(!checkParameters(m_parameters))
  307. {
  308. /* set according return status and the last parameters and return */
  309. m_returnReason = ERROR_XOUTOFBOUNDS;
  310. abort = true;
  311. }
  312. /*
  313. Check Optimization Timelimit, if active
  314. */
  315. if(m_maxSecondsActive)
  316. {
  317. m_currentTime = clock();
  318. /* time limit exceeded ? */
  319. if(((float)(m_currentTime - m_startTime )/CLOCKS_PER_SEC) >= m_maxSeconds )
  320. {
  321. /* set according return status and the last parameters and return */
  322. m_returnReason = SUCCESS_TIMELIMIT;
  323. abort = true;
  324. continue;
  325. }
  326. }
  327. }
  328. return m_returnReason;
  329. }