Optimizer.h 11 KB

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