CombinatorialOptimizer.cpp 6.6 KB

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