SimpleOptProblem.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #include "optimization/SimpleOptProblem.h"
  18. using namespace optimization;
  19. // note: all matrices are of size 0x0 meaning that they are not initialised!
  20. SimpleOptProblem::SimpleOptProblem() : m_costfunc(NULL),
  21. m_numParams(0),
  22. m_numActiveParams(0),
  23. m_maximize(false),
  24. m_lowerBoundsActive(false),
  25. m_upperBoundsActive(false),
  26. m_dimwrapper(NULL)
  27. {
  28. }
  29. SimpleOptProblem::SimpleOptProblem(CostFunction* costfunc, optimization::matrix_type& initialParams, optimization::matrix_type& scales, bool allactive)
  30. {
  31. // if dimension of initial parameter matrix and/or scale matrix do not fit the given dimension, stop
  32. assert(initialParams.rows() == (int)costfunc->getNumOfParameters() && scales.rows() == (int)costfunc->getNumOfParameters());
  33. // pointer to costfunction
  34. m_costfunc= costfunc;
  35. // full dimension of optimization problem
  36. m_numParams= m_costfunc->getNumOfParameters();
  37. matrix_type tmp(m_numParams,1);
  38. m_selection= tmp;
  39. m_upperBounds= tmp;
  40. m_lowerBounds= tmp;
  41. // number of active parameters (all or none?)
  42. if(allactive)
  43. {
  44. m_numActiveParams= m_numParams;
  45. for(int i= 0; i< m_numParams; ++i)
  46. m_selection[i][0]= 1;
  47. }
  48. else
  49. {
  50. m_numActiveParams= 0;
  51. for(int i= 0; i< m_numParams; ++i)
  52. m_selection[i][0]= 0;
  53. }
  54. // init parameters and scales
  55. m_currentparams= initialParams;
  56. m_scales= scales;
  57. // init upper and lower bounds with infitniy and -infinity
  58. for(int i= 0; i< m_numParams; ++i)
  59. {
  60. m_lowerBounds[i][0]= -1.0*numeric_limits<double>::max( );
  61. // -1.0*numeric_limits<double>::infinity( );//-1.0*numeric_limits<float>::max( );
  62. m_upperBounds[i][0]= numeric_limits<double>::max( );
  63. //numeric_limits<double>::infinity( );//numeric_limits<float>::max( );
  64. }
  65. // per default minimization will be perfomed and no bounds are active
  66. m_maximize= false;
  67. m_lowerBoundsActive= false;
  68. m_upperBoundsActive= false;
  69. m_dimwrapper= NULL;
  70. }
  71. SimpleOptProblem::SimpleOptProblem(const SimpleOptProblem& opt)
  72. {
  73. m_costfunc= opt.getOriginalCostFunction();
  74. m_numParams= opt.getNumParams();
  75. m_numActiveParams= opt.getNumActiveParams();
  76. m_currentparams= opt.getAllCurrentParams();
  77. m_scales= opt.getAllScales();
  78. m_selection=opt.m_selection; // direct member access
  79. m_maximize= opt.getMaximize();
  80. m_lowerBoundsActive= opt.lowerBoundsActive();
  81. m_upperBoundsActive= opt.upperBoundsActive();
  82. m_lowerBounds= opt.getAllLowerBounds();
  83. m_upperBounds= opt.getAllUpperBounds();
  84. m_dimwrapper= NULL;
  85. }
  86. SimpleOptProblem::~SimpleOptProblem()
  87. {
  88. if(m_dimwrapper != NULL)
  89. delete m_dimwrapper;
  90. }
  91. SimpleOptProblem& SimpleOptProblem::operator=(const SimpleOptProblem& opt)
  92. {
  93. m_costfunc= opt.m_costfunc;
  94. m_selection=opt.m_selection; // direct member access
  95. m_numParams= opt.getNumParams();
  96. m_numActiveParams= opt.getNumActiveParams();
  97. m_currentparams= opt.getAllCurrentParams();
  98. m_scales= opt.getAllScales();
  99. m_maximize= opt.getMaximize();
  100. m_lowerBoundsActive= opt.lowerBoundsActive();
  101. m_upperBoundsActive= opt.upperBoundsActive();
  102. m_lowerBounds= opt.getAllLowerBounds();
  103. m_upperBounds= opt.getAllUpperBounds();
  104. m_dimwrapper= NULL;
  105. return *this;
  106. }
  107. void SimpleOptProblem::activateAllParams(bool status)
  108. {
  109. for(int i= 0; i< this->getNumParams(); ++i)
  110. {
  111. m_selection[i][0]= (int)(status);
  112. }
  113. if(status == true)
  114. {
  115. m_numActiveParams=m_numParams;
  116. }
  117. else
  118. {
  119. m_numActiveParams=0;
  120. }
  121. }
  122. void SimpleOptProblem::activateParam(int paramnum, bool status)
  123. {
  124. assert(paramnum < m_numParams && paramnum >= 0);
  125. m_selection[paramnum][0]= (int)(status);
  126. int count = 0;
  127. for(int i =0; i < m_numParams; ++i)
  128. {
  129. if(m_selection[i][0]==1)
  130. {
  131. count++;
  132. }
  133. }
  134. m_numActiveParams=count;
  135. }
  136. bool SimpleOptProblem::isActive(int paramnum) const
  137. {
  138. assert(paramnum < m_numParams && paramnum >= 0);
  139. if(m_selection[paramnum][0] == 1)
  140. return true;
  141. else
  142. return false;
  143. }
  144. bool SimpleOptProblem::allActive() const
  145. {
  146. if(m_numActiveParams == m_numParams)
  147. return true;
  148. else
  149. return false;
  150. }
  151. int SimpleOptProblem::getNumParams() const
  152. {
  153. return m_numParams;
  154. }
  155. int SimpleOptProblem::getNumActiveParams() const
  156. {
  157. return m_numActiveParams;
  158. }
  159. matrix_type SimpleOptProblem::getAllCurrentParams() const
  160. {
  161. return m_currentparams;
  162. }
  163. matrix_type SimpleOptProblem::getActiveCurrentParams() const
  164. {
  165. //compute selection matrix (X x m_numParamsx)
  166. matrix_type selmatrix= this->computeSelectionMatrix();
  167. return (selmatrix*m_currentparams);
  168. }
  169. matrix_type SimpleOptProblem::getAllScales() const
  170. {
  171. return m_scales;
  172. }
  173. matrix_type SimpleOptProblem::getActiveScales() const
  174. {
  175. //compute selection matrix (X x m_numParamsx)
  176. matrix_type selmatrix= this->computeSelectionMatrix();
  177. return(selmatrix* m_scales);
  178. }
  179. matrix_type SimpleOptProblem::getAllUpperBounds() const
  180. {
  181. return m_upperBounds;
  182. }
  183. matrix_type SimpleOptProblem::getActiveUpperBounds() const
  184. {
  185. //compute selection matrix (X x m_numParamsx)
  186. matrix_type selmatrix= this->computeSelectionMatrix();
  187. return (selmatrix*m_upperBounds);
  188. }
  189. matrix_type SimpleOptProblem::getAllLowerBounds() const
  190. {
  191. return m_lowerBounds;
  192. }
  193. matrix_type SimpleOptProblem::getActiveLowerBounds() const
  194. {
  195. //compute selection matrix (X x m_numParamsx)
  196. matrix_type selmatrix= this->computeSelectionMatrix();
  197. return (selmatrix*m_lowerBounds);
  198. }
  199. CostFunction* SimpleOptProblem::getCostFunction()
  200. {
  201. // if number of active params is less then m_numParams we need a Wrapper function
  202. if(m_numActiveParams < m_numParams)
  203. {
  204. // if there is still existing an old dimwrapper function
  205. /// @todo little problem: if the opt problem was not changed concerning its selection and a dimension reduced version is already existing it will be deleted and newly allocated nevertheless; that is not so nice :(
  206. if(m_dimwrapper != NULL)
  207. delete m_dimwrapper;
  208. // new dimension reduction, wrapper function
  209. m_dimwrapper= new DimWrapperCostFunction(m_costfunc, m_selection, m_currentparams);
  210. return m_dimwrapper;
  211. }
  212. // else we return just the normal costfunction
  213. return m_costfunc;
  214. }
  215. CostFunction* SimpleOptProblem::getOriginalCostFunction() const
  216. {
  217. return m_costfunc;
  218. }
  219. bool SimpleOptProblem::getMaximize() const
  220. {
  221. return m_maximize;
  222. }
  223. bool SimpleOptProblem::lowerBoundsActive() const
  224. {
  225. return m_lowerBoundsActive;
  226. }
  227. bool SimpleOptProblem::upperBoundsActive() const
  228. {
  229. return m_upperBoundsActive;
  230. }
  231. void SimpleOptProblem::setAllCurrentParameters(matrix_type& params)
  232. {
  233. assert(params.rows() == m_numParams && params.cols() == 1);
  234. m_currentparams= params;
  235. }
  236. void SimpleOptProblem::setActiveCurrentParameters(matrix_type& params)
  237. {
  238. assert(params.rows() == m_numActiveParams && params.cols() == 1);
  239. int paramcopied= 0;
  240. for(int i= 0; i< m_numParams; ++i)
  241. {
  242. if(m_selection[i][0] == 1)
  243. {
  244. m_currentparams[i][0]= params[paramcopied][0];
  245. paramcopied++;
  246. }
  247. }
  248. }
  249. void SimpleOptProblem::setAllScales(matrix_type& scales)
  250. {
  251. assert(scales.rows() == m_numParams && scales.cols() == 1);
  252. m_scales= scales;
  253. }
  254. void SimpleOptProblem::setActiveScales(matrix_type& scales)
  255. {
  256. assert(scales.rows() == m_numActiveParams && scales.cols() == 1);
  257. int scalecopied= 0;
  258. for(int i= 0; i< m_numParams; ++i)
  259. {
  260. if(m_selection[i][0] == 1)
  261. {
  262. m_scales[i][0]= scales[scalecopied][0];
  263. scalecopied++;
  264. }
  265. }
  266. }
  267. void SimpleOptProblem::setLowerBound(int paramnumber, float lowerbound)
  268. {
  269. assert(paramnumber >= 0 && paramnumber < m_numParams);
  270. m_lowerBoundsActive= true;
  271. m_lowerBounds[paramnumber][0]= lowerbound;
  272. }
  273. void SimpleOptProblem::setUpperBound(int paramnumber, float upperbound)
  274. {
  275. assert(paramnumber >= 0 && paramnumber < m_numParams);
  276. m_upperBoundsActive= true;
  277. m_upperBounds[paramnumber][0]= upperbound;
  278. }
  279. void SimpleOptProblem::resetLowerBounds()
  280. {
  281. m_lowerBoundsActive= false;
  282. for(int i= 0; i< m_numParams; ++i)
  283. {
  284. m_lowerBounds[i][0]= -1.0*numeric_limits<double>::infinity( );
  285. }
  286. }
  287. void SimpleOptProblem::resetUpperBounds()
  288. {
  289. m_upperBoundsActive= false;
  290. for(int i= 0; i< m_numParams; ++i)
  291. {
  292. m_upperBounds[i][0]= numeric_limits<double>::infinity( );
  293. }
  294. }
  295. void SimpleOptProblem::changeCostFunc(CostFunction* costfunc)
  296. {
  297. m_costfunc= costfunc;
  298. }
  299. void SimpleOptProblem::setMaximize(bool maximize)
  300. {
  301. m_maximize=maximize;
  302. }
  303. matrix_type SimpleOptProblem::computeSelectionMatrix() const
  304. {
  305. matrix_type selectionmatrix(m_numActiveParams,m_numParams,0);
  306. int index= 0;
  307. for(int i= 0; i < m_numParams; ++i)
  308. {
  309. if(m_selection[i][0] == 1)
  310. {
  311. selectionmatrix[index][i]= 1.0;
  312. index++;
  313. }
  314. }
  315. return selectionmatrix;
  316. }