SimpleOptProblem.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. This library is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Library General Public
  4. License version 2 as published by the Free Software Foundation.
  5. This library is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  8. Library General Public License for more details.
  9. You should have received a copy of the GNU Library General Public License
  10. along with this library; see the file COPYING.LIB. If not, write to
  11. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  12. Boston, MA 02110-1301, USA.
  13. ---
  14. Copyright (C) 2009, Esther-Sabrina Platzer <esther.platzer@uni-jena.de>
  15. Matthias Wacker <matthias.wacker@mti.uni-jena.de>
  16. */
  17. /// @class SimpleOptProblem
  18. /// This class encapsulats simple optimization problems. Parameter values, scales and upper and lower bounds can be managed. Furthermore individual parameters can be deactivated,
  19. /// which allows an optimization of just a parameter subset. Activation and Deactivation can also be done with help of the class interface.
  20. ///
  21. /// @author Esther-Sabrina Platzer, Matthias Wacker
  22. /// @date 2009-07-02
  23. #ifndef _SimpleOptProblem_h_
  24. #define _SimpleOptProblem_h_
  25. #include <assert.h>
  26. #include "core/optimization/blackbox/Definitions_core_opt.h"
  27. #include "core/optimization/blackbox/CostFunction.h"
  28. #include "core/optimization/blackbox/DimWrapperCostFunction.h"
  29. namespace OPTIMIZATION {
  30. class SimpleOptProblem
  31. {
  32. public:
  33. ///
  34. /// Standard Constructor
  35. ///
  36. SimpleOptProblem( );
  37. ///
  38. /// Parametrized Constructor allows to specify the initial parameter values, the parameter scales and the costfunction which should be used. Furthermore it is possible to optimize just a subset of the parameter set. For this the wanted parameters have to be activated and the unwanted must be deactivated. By usage of the allactive flag it is possible to activate/deactivate the initial parameter setting in advance. By default all parameters are activeted.
  39. ///
  40. /// @param costfunc pointer to costfunction
  41. /// @param initialParams matrix containing the initial parameter values
  42. /// @param scales matrix containing the scales for every parameter
  43. /// @param allactive boolean flag for activating (true) or deactivating (false) all parameters
  44. ///
  45. SimpleOptProblem(CostFunction* costfunc, OPTIMIZATION::matrix_type& initialParams, OPTIMIZATION::matrix_type& scales, bool allactive=true);
  46. ///
  47. /// Copy Constructor
  48. ///
  49. /// @param opt the optimization problem to copy
  50. ///
  51. SimpleOptProblem(const SimpleOptProblem& opt);
  52. ///
  53. /// Destructor
  54. ///
  55. ~SimpleOptProblem();
  56. ///
  57. /// Assignment Operator
  58. /// @param opt the optimization problem to assign
  59. /// @return SimpleOptProblem
  60. ///
  61. SimpleOptProblem& operator=(const SimpleOptProblem& opt);
  62. ///
  63. /// Depending on the value of status all parameters (the whole parameter set) is activated (status=true) or deactivated (status= false)
  64. ///
  65. /// @param status boolean activation flag
  66. ///
  67. void activateAllParams(bool status);
  68. ///
  69. /// Given the number of a certain parameter it status (activated/deactivated) can be set individually
  70. ///
  71. /// @param paramnum number of the parameter in the overall parameter set
  72. /// @param status activation status (true= activated, false= deactivated)
  73. ///
  74. void activateParam(int paramnum, bool status);
  75. ///
  76. /// Returns the status of individual parameters
  77. ///
  78. /// @param paramnum parameter number in the overall parameter set
  79. /// @retval true, if the actual parameter is active
  80. /// @retval false, if the actual parameter is inactive
  81. ///
  82. bool isActive(int paramnum) const;
  83. ///
  84. /// Returns wether all params are active or not
  85. /// @retval true, if all parameters are active (m_numParams == m_numActiveParams)
  86. /// @retval false, if none or not all parameters are active (m_numParams != m_numActiveParams)
  87. bool allActive() const;
  88. ///
  89. /// @name Getters
  90. ///
  91. ///
  92. /// Returns the size of the overall parameter set
  93. ///
  94. int getNumParams() const;
  95. ///
  96. /// Returns the number of active parameters
  97. ///
  98. int getNumActiveParams() const;
  99. ///
  100. /// Returns the current parameter set
  101. ///
  102. /// @return current parameter matrix
  103. ///
  104. OPTIMIZATION::matrix_type getAllCurrentParams() const;
  105. ///
  106. /// Returns the current parameter set
  107. ///
  108. /// @return current parameter matrix
  109. ///
  110. OPTIMIZATION::matrix_type getActiveCurrentParams() const;
  111. ///
  112. /// Returns the current parameter scales
  113. ///
  114. /// @return current scale matrix
  115. ///
  116. OPTIMIZATION::matrix_type getAllScales() const;
  117. ///
  118. /// Returns the current parameter scales
  119. ///
  120. /// @return current scale matrix
  121. ///
  122. OPTIMIZATION::matrix_type getActiveScales() const;
  123. ///
  124. /// Returns the upper bounds (if no upper bounds are active, the matrix just contains infinity)
  125. ///
  126. /// @return upper bound matrix
  127. ///
  128. OPTIMIZATION::matrix_type getAllUpperBounds() const;
  129. ///
  130. /// Returns the upper bounds (if no upper bounds are active, the matrix just contains infinity)
  131. ///
  132. /// @return upper bound matrix
  133. ///
  134. OPTIMIZATION::matrix_type getActiveUpperBounds() const;
  135. ///
  136. /// Returns the lower bounds (if no lower bounds are active, the matrix just contains -infinity)
  137. ///
  138. /// @return lower bound matrix
  139. ///
  140. OPTIMIZATION::matrix_type getAllLowerBounds() const;
  141. ///
  142. /// Returns the lower bounds (if no lower bounds are active, the matrix just contains -infinity)
  143. ///
  144. /// @return lower bound matrix
  145. ///
  146. OPTIMIZATION::matrix_type getActiveLowerBounds() const;
  147. ///
  148. /// Returns a pointer to the CURRENT CostFunction with regard to the actual parameter selection
  149. ///
  150. /// @return CostFunction pointer
  151. ///
  152. CostFunction* getCostFunction();
  153. ///
  154. /// Returns a pointer to the ORIGINAL CostFunction ignoring the acutal parameter selection
  155. ///
  156. /// @return CostFunction pointer
  157. ///
  158. CostFunction* getOriginalCostFunction() const;
  159. ///
  160. /// Current description of the optimization strategy
  161. ///
  162. /// @retval true, if a maximization of the CostFunction is performed
  163. /// @retval false, if a minimization of the CostFunction is performed
  164. ///
  165. bool getMaximize() const;
  166. ///
  167. /// Returns the current status of lower bounds
  168. ///
  169. /// @retval true, if at least on lower bound (different from -infinity) is set
  170. /// @retval false, if no lower bounds are set (lower bound matrix just contains -infinity)
  171. ///
  172. bool lowerBoundsActive() const;
  173. ///
  174. /// Returns the current status of upper bounds
  175. ///
  176. /// @retval true, if at least on upper bound (different from infinity) is set
  177. /// @retval false, if no upper bounds are set (lower bound matrix just contains infinity)
  178. ///
  179. bool upperBoundsActive() const;
  180. ///
  181. /// @name Setters
  182. ///
  183. ///
  184. /// (Re)initialization of the current parameter values
  185. ///
  186. /// @param params parameter value matrix (must be m_numParams x 1)
  187. ///
  188. void setAllCurrentParameters(OPTIMIZATION::matrix_type& params);
  189. ///
  190. /// (Re)initialization of the current parameter values of active parameters
  191. ///
  192. /// @param params parameter value matrix (must be m_numActiveParams x 1)
  193. ///
  194. void setActiveCurrentParameters(OPTIMIZATION::matrix_type& params);
  195. ///
  196. /// (Re)initialization of the current scale setting
  197. ///
  198. /// @param scales parameter scale matrix (must be m_numParams x 1)
  199. ///
  200. void setAllScales(OPTIMIZATION::matrix_type& scales);
  201. ///
  202. /// (Re)initialization of the current scale setting for active parameters
  203. ///
  204. /// @param scales parameter scale matrix (must be m_numActiveParams x 1)
  205. ///
  206. void setActiveScales(OPTIMIZATION::matrix_type& scales);
  207. ///
  208. /// Definition of a lower bound for an individual parameter
  209. ///
  210. /// @param paramnumber number of the parameter in the overall parameter set
  211. /// @param lowerbound the lower bound for this parameter
  212. ///
  213. void setLowerBound(int paramnumber, float lowerbound);
  214. ///
  215. /// Definition of an upper bound for an individual parameter
  216. ///
  217. /// @param paramnumber number of the parameter in the overall parameter set
  218. /// @param upperbound the upper bound for this parameter
  219. ///
  220. void setUpperBound(int paramnumber, float upperbound);
  221. ///
  222. /// Deactivates all lower bounds resulting in a lower bound matrix just containing -infinity.
  223. ///
  224. void resetLowerBounds();
  225. ///
  226. /// Deactivates upper bounds and results in an upper bound matrix just containing infinity
  227. ///
  228. void resetUpperBounds();
  229. ///
  230. /// Exchange of CostFunction.
  231. ///
  232. /// @param costfunc new CostFunction pointer
  233. ///
  234. void changeCostFunc(CostFunction* costfunc);
  235. ///
  236. /// Definition of the optimization strategy.
  237. ///
  238. /// @param maximize if maximize=true a maximization of the CostFunction will be performed, otherwise it is minimized (Default)
  239. void setMaximize(bool maximize);
  240. protected:
  241. private:
  242. ///
  243. /// Computes a selection matrix out of the selection vector used for selecting active parameters which can be used to compute smaller dimension matrices containing just the active params (or scales and bounds for active params)
  244. ///
  245. /// @return selection matrix with dimension m_numActiveParams x m_numParams
  246. OPTIMIZATION::matrix_type computeSelectionMatrix() const;
  247. //! cost function pointer
  248. CostFunction* m_costfunc;
  249. //! number of parameters
  250. int m_numParams;
  251. //! number of active parameters (active parameters are the ones, which currently should be optimized)
  252. int m_numActiveParams;
  253. //! holds the current parameters as parameter vector
  254. OPTIMIZATION::matrix_type m_currentparams;
  255. //! holds the current scales for the parameters
  256. OPTIMIZATION::matrix_type m_scales;
  257. //! upper bounds for parameters
  258. OPTIMIZATION::matrix_type m_upperBounds;
  259. //! lower bounds for parameters
  260. OPTIMIZATION::matrix_type m_lowerBounds;
  261. //! selection matrix: defines the active parameters
  262. OPTIMIZATION::matrix_type m_selection;
  263. //! flag defining wether the optimization problem is a maximization problem (true) or a minimization problem (false)
  264. bool m_maximize;
  265. //! flag defining wether lowerBounds are set
  266. bool m_lowerBoundsActive;
  267. //! flag defining wether upperBounds are set
  268. bool m_upperBoundsActive;
  269. //! pointer to dimension wrapper function
  270. CostFunction* m_dimwrapper;
  271. };
  272. } //namespace
  273. #endif /* _SimpleOptProblem_h_ */