SimpleOptProblem.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 "optimization/Opt_Namespace.h"
  27. #include "optimization/CostFunction.h"
  28. #include "optimization/DimWrapperCostFunction.h"
  29. class SimpleOptProblem
  30. {
  31. public:
  32. ///
  33. /// Standard Constructor
  34. ///
  35. SimpleOptProblem( );
  36. ///
  37. /// 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.
  38. ///
  39. /// @param costfunc pointer to costfunction
  40. /// @param initialParams matrix containing the initial parameter values
  41. /// @param scales matrix containing the scales for every parameter
  42. /// @param allactive boolean flag for activating (true) or deactivating (false) all parameters
  43. ///
  44. SimpleOptProblem(CostFunction* costfunc, optimization::matrix_type& initialParams, optimization::matrix_type& scales, bool allactive=true);
  45. ///
  46. /// Copy Constructor
  47. ///
  48. /// @param opt the optimization problem to copy
  49. ///
  50. SimpleOptProblem(const SimpleOptProblem& opt);
  51. ///
  52. /// Destructor
  53. ///
  54. ~SimpleOptProblem();
  55. ///
  56. /// Assignment Operator
  57. /// @param opt the optimization problem to assign
  58. /// @return SimpleOptProblem
  59. ///
  60. SimpleOptProblem& operator=(const SimpleOptProblem& opt);
  61. ///
  62. /// Depending on the value of status all parameters (the whole parameter set) is activated (status=true) or deactivated (status= false)
  63. ///
  64. /// @param status boolean activation flag
  65. ///
  66. void activateAllParams(bool status);
  67. ///
  68. /// Given the number of a certain parameter it status (activated/deactivated) can be set individually
  69. ///
  70. /// @param paramnum number of the parameter in the overall parameter set
  71. /// @param status activation status (true= activated, false= deactivated)
  72. ///
  73. void activateParam(int paramnum, bool status);
  74. ///
  75. /// Returns the status of individual parameters
  76. ///
  77. /// @param paramnum parameter number in the overall parameter set
  78. /// @retval true, if the actual parameter is active
  79. /// @retval false, if the actual parameter is inactive
  80. ///
  81. bool isActive(int paramnum) const;
  82. ///
  83. /// Returns wether all params are active or not
  84. /// @retval true, if all parameters are active (m_numParams == m_numActiveParams)
  85. /// @retval false, if none or not all parameters are active (m_numParams != m_numActiveParams)
  86. bool allActive() const;
  87. ///
  88. /// @name Getters
  89. ///
  90. ///
  91. /// Returns the size of the overall parameter set
  92. ///
  93. int getNumParams() const;
  94. ///
  95. /// Returns the number of active parameters
  96. ///
  97. int getNumActiveParams() const;
  98. ///
  99. /// Returns the current parameter set
  100. ///
  101. /// @return current parameter matrix
  102. ///
  103. optimization::matrix_type getAllCurrentParams() const;
  104. ///
  105. /// Returns the current parameter set
  106. ///
  107. /// @return current parameter matrix
  108. ///
  109. optimization::matrix_type getActiveCurrentParams() const;
  110. ///
  111. /// Returns the current parameter scales
  112. ///
  113. /// @return current scale matrix
  114. ///
  115. optimization::matrix_type getAllScales() const;
  116. ///
  117. /// Returns the current parameter scales
  118. ///
  119. /// @return current scale matrix
  120. ///
  121. optimization::matrix_type getActiveScales() const;
  122. ///
  123. /// Returns the upper bounds (if no upper bounds are active, the matrix just contains infinity)
  124. ///
  125. /// @return upper bound matrix
  126. ///
  127. optimization::matrix_type getAllUpperBounds() const;
  128. ///
  129. /// Returns the upper bounds (if no upper bounds are active, the matrix just contains infinity)
  130. ///
  131. /// @return upper bound matrix
  132. ///
  133. optimization::matrix_type getActiveUpperBounds() const;
  134. ///
  135. /// Returns the lower bounds (if no lower bounds are active, the matrix just contains -infinity)
  136. ///
  137. /// @return lower bound matrix
  138. ///
  139. optimization::matrix_type getAllLowerBounds() const;
  140. ///
  141. /// Returns the lower bounds (if no lower bounds are active, the matrix just contains -infinity)
  142. ///
  143. /// @return lower bound matrix
  144. ///
  145. optimization::matrix_type getActiveLowerBounds() const;
  146. ///
  147. /// Returns a pointer to the CURRENT CostFunction with regard to the actual parameter selection
  148. ///
  149. /// @return CostFunction pointer
  150. ///
  151. CostFunction* getCostFunction();
  152. ///
  153. /// Returns a pointer to the ORIGINAL CostFunction ignoring the acutal parameter selection
  154. ///
  155. /// @return CostFunction pointer
  156. ///
  157. CostFunction* getOriginalCostFunction() const;
  158. ///
  159. /// Current description of the optimization strategy
  160. ///
  161. /// @retval true, if a maximization of the CostFunction is performed
  162. /// @retval false, if a minimization of the CostFunction is performed
  163. ///
  164. bool getMaximize() const;
  165. ///
  166. /// Returns the current status of lower bounds
  167. ///
  168. /// @retval true, if at least on lower bound (different from -infinity) is set
  169. /// @retval false, if no lower bounds are set (lower bound matrix just contains -infinity)
  170. ///
  171. bool lowerBoundsActive() const;
  172. ///
  173. /// Returns the current status of upper bounds
  174. ///
  175. /// @retval true, if at least on upper bound (different from infinity) is set
  176. /// @retval false, if no upper bounds are set (lower bound matrix just contains infinity)
  177. ///
  178. bool upperBoundsActive() const;
  179. ///
  180. /// @name Setters
  181. ///
  182. ///
  183. /// (Re)initialization of the current parameter values
  184. ///
  185. /// @param params parameter value matrix (must be m_numParams x 1)
  186. ///
  187. void setAllCurrentParameters(optimization::matrix_type& params);
  188. ///
  189. /// (Re)initialization of the current parameter values of active parameters
  190. ///
  191. /// @param params parameter value matrix (must be m_numActiveParams x 1)
  192. ///
  193. void setActiveCurrentParameters(optimization::matrix_type& params);
  194. ///
  195. /// (Re)initialization of the current scale setting
  196. ///
  197. /// @param scales parameter scale matrix (must be m_numParams x 1)
  198. ///
  199. void setAllScales(optimization::matrix_type& scales);
  200. ///
  201. /// (Re)initialization of the current scale setting for active parameters
  202. ///
  203. /// @param scales parameter scale matrix (must be m_numActiveParams x 1)
  204. ///
  205. void setActiveScales(optimization::matrix_type& scales);
  206. ///
  207. /// Definition of a lower bound for an individual parameter
  208. ///
  209. /// @param paramnumber number of the parameter in the overall parameter set
  210. /// @param lowerbound the lower bound for this parameter
  211. ///
  212. void setLowerBound(int paramnumber, float lowerbound);
  213. ///
  214. /// Definition of an upper bound for an individual parameter
  215. ///
  216. /// @param paramnumber number of the parameter in the overall parameter set
  217. /// @param upperbound the upper bound for this parameter
  218. ///
  219. void setUpperBound(int paramnumber, float upperbound);
  220. ///
  221. /// Deactivates all lower bounds resulting in a lower bound matrix just containing -infinity.
  222. ///
  223. void resetLowerBounds();
  224. ///
  225. /// Deactivates upper bounds and results in an upper bound matrix just containing infinity
  226. ///
  227. void resetUpperBounds();
  228. ///
  229. /// Exchange of CostFunction.
  230. ///
  231. /// @param costfunc new CostFunction pointer
  232. ///
  233. void changeCostFunc(CostFunction* costfunc);
  234. ///
  235. /// Definition of the optimization strategy.
  236. ///
  237. /// @param maximize if maximize=true a maximization of the CostFunction will be performed, otherwise it is minimized (Default)
  238. void setMaximize(bool maximize);
  239. protected:
  240. private:
  241. ///
  242. /// 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)
  243. ///
  244. /// @return selection matrix with dimension m_numActiveParams x m_numParams
  245. optimization::matrix_type computeSelectionMatrix() const;
  246. //! cost function pointer
  247. CostFunction* m_costfunc;
  248. //! number of parameters
  249. int m_numParams;
  250. //! number of active parameters (active parameters are the ones, which currently should be optimized)
  251. int m_numActiveParams;
  252. //! holds the current parameters as parameter vector
  253. optimization::matrix_type m_currentparams;
  254. //! holds the current scales for the parameters
  255. optimization::matrix_type m_scales;
  256. //! upper bounds for parameters
  257. optimization::matrix_type m_upperBounds;
  258. //! lower bounds for parameters
  259. optimization::matrix_type m_lowerBounds;
  260. //! selection matrix: defines the active parameters
  261. optimization::matrix_type m_selection;
  262. //! flag defining wether the optimization problem is a maximization problem (true) or a minimization problem (false)
  263. bool m_maximize;
  264. //! flag defining wether lowerBounds are set
  265. bool m_lowerBoundsActive;
  266. //! flag defining wether upperBounds are set
  267. bool m_upperBoundsActive;
  268. //! pointer to dimension wrapper function
  269. CostFunction* m_dimwrapper;
  270. };
  271. #endif /* _SimpleOptProblem_h_ */