Optimizer.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // Optimizer.cpp: Implementation of the Optimizer class.
  4. //
  5. // Written by Matthias Wacker
  6. //
  7. //////////////////////////////////////////////////////////////////////
  8. #include "optimization/Optimizer.h"
  9. #include <iostream>
  10. #include <cassert>
  11. //Optimizer::Optimizer(unsigned int numOfParameters, CostFunction *objectiveFunction, OptLogBase *loger)
  12. Optimizer::Optimizer(OptLogBase *loger)
  13. {
  14. m_costFunction = NULL;
  15. // may be NULL
  16. m_loger = loger;
  17. // SET defaults
  18. m_numberOfParameters = 0;
  19. m_maximize = false;
  20. m_lowerParameterBoundActive = false;
  21. m_upperParameterBoundActive = false;
  22. m_funcTolActive = false;
  23. m_paramTolActive = false;
  24. m_maxNumIterActive = false;
  25. m_maxSecondsActive = false;
  26. m_verbose = false;
  27. m_paramTol = 0.0001;
  28. m_funcTol = 0.000001;
  29. }
  30. Optimizer::Optimizer( const Optimizer &opt)
  31. {
  32. // no deep copies!
  33. m_costFunction = opt.m_costFunction;
  34. m_loger = opt.m_loger;
  35. //
  36. m_numberOfParameters = opt.m_numberOfParameters;
  37. m_parameters = opt.m_parameters;
  38. m_scales = opt.m_scales;
  39. m_upperParameterBound = opt.m_upperParameterBound;
  40. m_lowerParameterBound = opt.m_lowerParameterBound;
  41. m_funcTol = opt.m_funcTol;
  42. m_paramTol = opt.m_paramTol;
  43. m_maxNumIter = opt.m_maxNumIter;
  44. m_maxSeconds = opt.m_maxSeconds;
  45. m_maximize = opt.m_maximize;
  46. m_funcTolActive = opt.m_funcTolActive;
  47. m_paramTolActive = opt.m_paramTolActive;
  48. m_maxNumIterActive = opt.m_maxNumIterActive;
  49. m_maxSecondsActive = opt.m_maxSecondsActive;
  50. m_lowerParameterBoundActive = opt.m_lowerParameterBoundActive;
  51. m_upperParameterBoundActive = opt.m_upperParameterBoundActive;
  52. m_verbose = opt.m_verbose;
  53. }
  54. Optimizer & Optimizer::operator=(const Optimizer &opt)
  55. {
  56. /*
  57. avoid self-copy
  58. */
  59. if(this != &opt)
  60. {
  61. //
  62. // =operator of SuperClass
  63. //
  64. //SuperClass::operator=(opt);
  65. //
  66. // own values:
  67. //
  68. // no deep copies!
  69. m_costFunction = opt.m_costFunction;
  70. m_loger = opt.m_loger;
  71. //
  72. m_numberOfParameters = opt.m_numberOfParameters;
  73. m_parameters = opt.m_parameters;
  74. m_scales = opt.m_scales;
  75. m_upperParameterBound = opt.m_upperParameterBound;
  76. m_lowerParameterBound = opt.m_lowerParameterBound;
  77. m_funcTol = opt.m_funcTol;
  78. m_paramTol = opt.m_paramTol;
  79. m_maxNumIter = opt.m_maxNumIter;
  80. m_maxSeconds = opt.m_maxSeconds;
  81. m_maximize = opt.m_maximize;
  82. m_funcTolActive = opt.m_funcTolActive;
  83. m_paramTolActive = opt.m_paramTolActive;
  84. m_maxNumIterActive = opt.m_maxNumIterActive;
  85. m_maxSecondsActive = opt.m_maxSecondsActive;
  86. m_lowerParameterBoundActive = opt.m_lowerParameterBoundActive;
  87. m_upperParameterBoundActive = opt.m_upperParameterBoundActive;
  88. m_verbose = opt.m_verbose;
  89. }
  90. return *this;
  91. }
  92. Optimizer::~Optimizer()
  93. {
  94. }
  95. void Optimizer::setMaximize(bool maximize)
  96. {
  97. m_maximize = maximize;
  98. }
  99. void Optimizer::setParameters(const matrix_type & startParameters)
  100. {
  101. if(checkParameters(startParameters) == true)
  102. {
  103. m_parameters = startParameters;
  104. }
  105. else
  106. {
  107. if(m_loger)
  108. m_loger->logError("the parameters that were tried to set are not within the Bounds. Startparameters are set to the lower Bound");
  109. assert(false); // :)
  110. m_parameters = m_lowerParameterBound;
  111. }
  112. }
  113. bool Optimizer::checkParameters(const matrix_type & parameters)
  114. {
  115. assert( (parameters.rows() == static_cast<int>(m_numberOfParameters)) && (parameters.cols() == 1 ) );
  116. if(m_lowerParameterBoundActive || m_upperParameterBoundActive)
  117. {
  118. for(int i=0; i < static_cast<int>(m_numberOfParameters); i++)
  119. {
  120. if( m_upperParameterBoundActive)
  121. {
  122. if(parameters[i][0] >= m_upperParameterBound[i][0])
  123. {
  124. return false;
  125. }
  126. }
  127. if( m_lowerParameterBoundActive)
  128. {
  129. if(parameters[i][0] <= m_lowerParameterBound[i][0])
  130. {
  131. return false;
  132. }
  133. }
  134. }
  135. }
  136. return true;
  137. }
  138. void Optimizer::setScales(const matrix_type & scales)
  139. {
  140. assert (scales.rows() == static_cast<int>(m_numberOfParameters) && (scales.cols() == 1 ) );
  141. m_scales=scales;
  142. }
  143. void Optimizer::setUpperParameterBound(bool active, const matrix_type & upperBound)
  144. {
  145. m_upperParameterBoundActive = active;
  146. m_upperParameterBound = upperBound;
  147. }
  148. void Optimizer::setLowerParameterBound(bool active, const matrix_type & lowerBound)
  149. {
  150. m_lowerParameterBoundActive = active;
  151. m_lowerParameterBound = lowerBound;
  152. }
  153. void Optimizer::setFuncTol(bool active, double difference)
  154. {
  155. m_funcTolActive = active;
  156. m_funcTol = difference;
  157. }
  158. void Optimizer::setParamTol(bool active, double difference)
  159. {
  160. m_paramTolActive = active;
  161. m_paramTol = difference;
  162. }
  163. void Optimizer::setMaxNumIter(bool active, unsigned int num)
  164. {
  165. m_maxNumIterActive = active;
  166. m_maxNumIter = num;
  167. }
  168. void Optimizer::setTimeLimit(bool active, double seconds)
  169. {
  170. m_maxSecondsActive = active;
  171. m_maxSeconds = seconds;
  172. }
  173. void Optimizer::setLoger(OptLogBase *loger)
  174. {
  175. m_loger = loger;
  176. if(loger)
  177. {
  178. loger->init();
  179. }
  180. }
  181. void Optimizer::changeCostFunction(CostFunction* costFunction)
  182. {
  183. m_costFunction = costFunction;
  184. if (m_costFunction)
  185. {
  186. m_costFunction->init();
  187. }
  188. }
  189. double Optimizer::evaluateCostFunction(const matrix_type & x)
  190. {
  191. return ( m_maximize == true ? (-1.0 * m_costFunction->evaluate(x)):(m_costFunction->evaluate(x)));
  192. }
  193. void Optimizer::init()
  194. {
  195. /* Check costfunctin and loger */
  196. if( !m_costFunction)
  197. {
  198. if(m_loger)
  199. m_loger->logError("Optimizer init() failed because of missing costfunction");
  200. assert(false); // :)
  201. }
  202. else
  203. {
  204. m_costFunction->init();
  205. }
  206. m_numIter = 0;
  207. }
  208. /*!
  209. function that calls costfunction->evaluate for every column in x and inverts the sign in neccesary
  210. */
  211. Optimizer::matrix_type Optimizer::evaluateSetCostFunction(const matrix_type & xSet)
  212. {
  213. return ( m_maximize == true ? (m_costFunction->evaluateSet(xSet) * -1.0):(m_costFunction->evaluateSet(xSet)));
  214. }