Optimizer.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. ///
  2. /// @file Optimizer.h: interface of the Optimizer class.
  3. /// @author Matthias Wacker, Esther Platzer
  4. ///
  5. #ifndef _OPTIMIZER_H_
  6. #define _OPTIMIZER_H_
  7. #include "optimization/CostFunction.h"
  8. #include <time.h>
  9. #include "optimization/OptLogBase.h"
  10. #include "optimization/Opt_Namespace.h"
  11. /*!
  12. Abstract base class of all optimizers.
  13. */
  14. class Optimizer
  15. {
  16. public:
  17. typedef optimization::matrix_type matrix_type;
  18. ///
  19. /// Constructor (also Default constructor)
  20. /// @param optProb pointer to optimization problem object
  21. /// @param loger OptLogBase * to existing log class or NULL
  22. ///
  23. Optimizer(OptLogBase *loger=NULL);
  24. ///
  25. /// Copy constructor
  26. /// @param opt .. optimizer to copy
  27. ///
  28. Optimizer( const Optimizer &opt);
  29. ///
  30. /// assignment opterator
  31. /// @param opt optimizer to "copy"
  32. /// @return self-reference
  33. ///
  34. Optimizer & operator=(const Optimizer &opt);
  35. ///
  36. /// Destructor.
  37. ///
  38. virtual ~Optimizer();
  39. ///
  40. /// enumeration for the return reasons of an optimizer
  41. ///
  42. enum {
  43. SUCCESS_FUNCTOL,
  44. SUCCESS_PARAMTOL,
  45. SUCCESS_MAXITER,
  46. SUCCESS_TIMELIMIT,
  47. ERROR_XOUTOFBOUNDS,
  48. ERROR_COMPUTATION_UNSTABLE,
  49. _to_continue_
  50. };
  51. ///
  52. /// setScales means the parameters are internally handled scaled to compensate for illposed
  53. /// Parameter relations.
  54. /// The scales are used multiplicative: higher scales mean bigger steps
  55. /// (For Example rotation has much more effect than translation,
  56. /// so steps should be much smaller than for translation, i.g. use small scales for the
  57. /// rotation parameters and higher scales for the translation parameters)
  58. ///
  59. /// @brief Set the scaling vector for the parameters.
  60. /// @param scales vector for the scales (i-th scale for i-th parameter dimension)
  61. ///
  62. void setScales(const optimization::matrix_type & scales);
  63. ///
  64. /// @brief Get the scales
  65. ///
  66. /// @return matrix with the scales
  67. ///
  68. inline const optimization::matrix_type & getScales(){return m_scales;};
  69. ///
  70. /// @brief Set bounds for x
  71. ///
  72. /// Sets an upper bound for the parameter space. Optimization aborts if it iterates over these given bound.
  73. ///
  74. void setUpperParameterBound(bool active, const optimization::matrix_type & upperBound);
  75. ///
  76. /// @brief Set bounds for x.
  77. ///
  78. /// Sets a lower bound for the parameter space. Optimization aborts if it iterates over these given bound.
  79. ///
  80. void setLowerParameterBound(bool active, const optimization::matrix_type & lowerBound);
  81. ///
  82. /// @brief get the upper bound
  83. /// @return matrix containing the upper bound
  84. ///
  85. inline const optimization::matrix_type & getUpperParameterBound(){ return m_upperParameterBound;};
  86. ///
  87. /// @brief get the lower parameter bound
  88. /// @return matrix containing the lower bound
  89. ///
  90. inline const optimization::matrix_type & getLowerParameterBound(){return m_lowerParameterBound;};
  91. ///
  92. /// @brief Set function tolerance abort criteria
  93. ///
  94. /// Set function tolerance abort criteria. While iterating, if the change of the objective function between
  95. /// two iterations is below the given difference threshold. The optimization stops and returns SUCCESS if
  96. /// active is 'true'
  97. ///
  98. /// @param active bool to activate the criteria (true == active..)
  99. /// @param difference double to set the threshold
  100. ///
  101. void setFuncTol(bool active, double difference);
  102. ///
  103. /// @brief Get the function tolerance abort criteria
  104. /// @return double representing the threshold
  105. ///
  106. inline double getFuncTol(){return m_funcTol;};
  107. ///
  108. /// @brief Set parameter tolerance abort criteria
  109. ///
  110. /// Set parameter tolerance abort criteria. While iterating, if the change of the parameters between
  111. /// two iterations is below the given difference threshold. The optimization stops and returns SUCCESS if
  112. /// active is 'true'
  113. ///
  114. /// @param active : bool to activate the criteria (true == active..)
  115. /// @param difference : representing the threshold
  116. ///
  117. void setParamTol(bool active, double difference);
  118. ///
  119. /// @brief Get the parameter tolerance abort criteria
  120. /// @return double representing the threshold
  121. ///
  122. inline double getParamTol(){return m_paramTol;};
  123. ///
  124. /// @brief Set maximum number of Iterations
  125. /// @param active : bool to enable the criteria
  126. /// @param num : unsigned int -> max number of iterations
  127. ///
  128. void setMaxNumIter(bool active, unsigned int num);
  129. ///
  130. /// @brief get maximum number of Iterations
  131. /// @return unsigned int representing the max number of iterations
  132. ///
  133. inline unsigned int getMaxNumIter(){return m_maxNumIter;};
  134. ///
  135. /// @brief get actual Iteration count
  136. /// @return unsigned int represention the actual iteration count
  137. ///
  138. inline unsigned int getNumIter(){return m_numIter;};
  139. ///
  140. /// @brief set time limit in sec
  141. /// @param active: bool to set the timelimit active or not
  142. /// @param seconds: double representing the timelimit in seconds
  143. ///
  144. void setTimeLimit(bool active, double seconds);
  145. ///
  146. /// @brief get time limit in sec
  147. /// @return time limit
  148. ///
  149. inline double getTimeLimit(){return m_maxSeconds;};
  150. ///
  151. /// @brief get return reason
  152. /// @return returns an int representing the return reason. Compare with enumeration
  153. ///
  154. inline int getReturnReason(){return m_returnReason;};
  155. ///
  156. /// @brief setMaximize 'true' Changes Optimization to maximization instead of the default: 'false' ^= minimization.
  157. /// @param maximize maximize?
  158. ///
  159. void setMaximize(bool maximize);
  160. ///
  161. /// @brief set loger
  162. /// @param loger OptLogBase * to an instance of a loger
  163. ///
  164. void setLoger(OptLogBase *loger);
  165. ///
  166. /// @brief setVerbose = true to enable a lot of outputs
  167. ///
  168. inline void setVerbose(bool verbose){m_verbose = verbose;};
  169. protected:
  170. // pure virtual definitions
  171. ///
  172. /// @brief virtual function that will start the optimization
  173. /// @return integer representing abortion status from enum
  174. ///
  175. virtual int optimize() = 0;
  176. ///
  177. /// @brief Initializations
  178. ///
  179. void init();
  180. // end of pure virtual definitions
  181. ///
  182. /// @brief Checks if parameters are valid
  183. /// @param parameters : parameters to check.
  184. ///
  185. bool checkParameters(const optimization::matrix_type & parameters);
  186. ///
  187. /// @brief Get the last parameters
  188. /// @return optimization::matrix_type containing the last parameters used in Optimization
  189. ///
  190. inline optimization::matrix_type getLastParameters(){return m_parameters;}
  191. ///
  192. /// @brief get number of paramters
  193. ///
  194. inline unsigned int getNumberOfParameters(){return m_numberOfParameters;}
  195. ///
  196. /// @brief Sets parameters used as initial point for Optimization
  197. /// @param startParameters : used as initial point
  198. ///
  199. void setParameters(const optimization::matrix_type & startParameters);
  200. ///
  201. /// @brief get loger
  202. /// @return pointer to the loger
  203. ///
  204. inline OptLogBase * getLoger(){return m_loger;};
  205. ///
  206. /// @brief Change cost function.
  207. ///
  208. void changeCostFunction(CostFunction* costFunction);
  209. //////////////////////////////////////////
  210. // //
  211. // member definitions //
  212. // //
  213. //////////////////////////////////////////
  214. ///
  215. /// pointer to cost function
  216. ///
  217. CostFunction* m_costFunction;
  218. ///
  219. /// number of parameters to be optimized
  220. ///
  221. unsigned int m_numberOfParameters;
  222. ///
  223. /// parameter vector
  224. ///
  225. optimization::matrix_type m_parameters;
  226. ///
  227. /// function value corresponding to m_parameters
  228. ///
  229. double m_currentCostFunctionValue;
  230. ///
  231. /// scales vector
  232. ///
  233. optimization::matrix_type m_scales;
  234. ///
  235. /// parameters tolerance threshold
  236. ///
  237. double m_paramTol;
  238. ///
  239. /// parameters tolerance active
  240. ///
  241. bool m_paramTolActive;
  242. ///
  243. /// function tolerance threshold
  244. ///
  245. double m_funcTol;
  246. ///
  247. /// function tolerance active
  248. ///
  249. bool m_funcTolActive;
  250. ///
  251. /// Maximum number of Iterations
  252. ///
  253. unsigned int m_maxNumIter;
  254. ///
  255. /// Iteration limit active
  256. ///
  257. bool m_maxNumIterActive;
  258. ///
  259. /// Iteration counter
  260. ///
  261. unsigned int m_numIter;
  262. ///
  263. /// Time limit in seconds
  264. ///
  265. double m_maxSeconds;
  266. ///
  267. /// Time limit active
  268. ///
  269. bool m_maxSecondsActive;
  270. ///
  271. /// start time sturct
  272. ///
  273. clock_t m_startTime;
  274. ///
  275. /// current time struct
  276. ///
  277. clock_t m_currentTime;
  278. ///
  279. /// container to store the return reason until it is returned from optimize()
  280. ///
  281. int m_returnReason ;
  282. ///
  283. /// upper parameter bound
  284. ///
  285. optimization::matrix_type m_upperParameterBound;
  286. ///
  287. /// upper parameter bound active
  288. ///
  289. bool m_upperParameterBoundActive;
  290. ///
  291. /// lower parameter bound
  292. ///
  293. optimization::matrix_type m_lowerParameterBound;
  294. ///
  295. /// lower parameter bound active
  296. ///
  297. bool m_lowerParameterBoundActive;
  298. ///
  299. /// maximize
  300. ///
  301. bool m_maximize;
  302. ///
  303. /// log class OptLogBase
  304. ///
  305. OptLogBase *m_loger;
  306. ///
  307. /// bool to enable a bunch of outputs
  308. ///
  309. bool m_verbose;
  310. ///
  311. /// function that calls costfunction->evaluate but inverts the sign if neccesary (min or max..)
  312. ///
  313. double evaluateCostFunction(const optimization::matrix_type & x);
  314. ///
  315. /// function that calls costfunction->evaluate for every column in x and inverts the sign in neccesary
  316. ///
  317. optimization::matrix_type evaluateSetCostFunction(const optimization::matrix_type & xSet);
  318. };
  319. #endif