Plotter.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/Plotter.h"
  18. #include "core/optimization/blackbox/Definitions_core_opt.h"
  19. #include <iostream>
  20. #include <fstream>
  21. using namespace OPTIMIZATION;
  22. Plotter::Plotter() : m_absoluteBounds(true),
  23. m_pCostfunc(NULL)
  24. {
  25. }
  26. Plotter::Plotter(const Plotter& plotter)
  27. {
  28. m_pCostfunc= plotter.m_pCostfunc;
  29. m_absoluteBounds= plotter.m_absoluteBounds;
  30. }
  31. Plotter::~Plotter()
  32. {
  33. }
  34. Plotter& Plotter::operator=(const Plotter & plotter)
  35. {
  36. //avoid self-copy
  37. if(this != &plotter)
  38. {
  39. m_pCostfunc= plotter.m_pCostfunc;
  40. m_absoluteBounds= plotter.m_absoluteBounds;
  41. }
  42. return *this;
  43. }
  44. void Plotter::setOptProblem(const SimpleOptProblem& optprob)
  45. {
  46. m_pOptprob= &optprob;
  47. // Note: not nice, but a CostFunction cannot be const, as evaluate() is not const and cannot be made const
  48. m_pCostfunc= const_cast<SimpleOptProblem&>(optprob).getOriginalCostFunction();
  49. }
  50. void Plotter::plot1D(int paramnum, const float from, const float to, const float stepwidth, const char* path)
  51. {
  52. // if the parameter number is < 0 or has a higher dimension then the optimization problem, throw an assertion
  53. assert(paramnum >= 0 && paramnum < m_pOptprob->getNumParams());
  54. // open plot file for writing
  55. std::ofstream plotfile;
  56. plotfile.open(path);
  57. // take actual parameter values from the optimization problem
  58. matrix_type tmpParams= m_pOptprob->getAllCurrentParams();
  59. // start and end values for costfunction evaluation
  60. float start,end;
  61. // if absolute bounds are used
  62. if(m_absoluteBounds)
  63. {
  64. start= from;
  65. end= to;
  66. }
  67. // else compute relative bounds
  68. else
  69. {
  70. start= tmpParams(paramnum,0) + from;
  71. end= tmpParams(paramnum,0) + to;
  72. }
  73. // setting relevant parameter to start value
  74. tmpParams(paramnum,0)= start;
  75. while(tmpParams(paramnum,0) <= end)
  76. {
  77. // evaluate costfunction and write to plotfile
  78. plotfile << tmpParams(paramnum,0) << " " << m_pCostfunc->evaluate(tmpParams) << std::endl;
  79. // increment evaluation value
  80. tmpParams(paramnum,0)+= stepwidth;
  81. }
  82. plotfile.close();
  83. }
  84. void Plotter::plot2D(int paramnum1, const float from1, const float to1, const float stepwidth1, int paramnum2, const float from2, const float to2, const float stepwidth2, const char* path)
  85. {
  86. // if the parameter number is < 0 or has a higher dimension then the optimization problem, throw an assertion
  87. assert(paramnum1 >= 0 && paramnum1 < m_pOptprob->getNumParams() && paramnum2 >= 0 && paramnum2 < m_pOptprob->getNumParams() );
  88. // open plot file for writing
  89. std::ofstream plotfile;
  90. plotfile.open(path);
  91. // take actual parameter values from the optimization problem
  92. matrix_type tmpParams= m_pOptprob->getAllCurrentParams();
  93. // start and end values for costfunction evaluation
  94. float start1,end1,start2,end2;
  95. // if absolute bounds are used
  96. if(m_absoluteBounds)
  97. {
  98. start1= from1;
  99. end1= to1;
  100. start2= from2;
  101. end2= to2;
  102. }
  103. // else compute relative bounds
  104. else
  105. {
  106. start1= tmpParams(paramnum1,0) + from1;
  107. end1= tmpParams(paramnum1,0) + to1;
  108. start2= tmpParams(paramnum2,0) + from2;
  109. end2= tmpParams(paramnum2,0) + to2;
  110. }
  111. // setting relevant parameter to start value
  112. tmpParams(paramnum1,0)= start1;
  113. tmpParams(paramnum2,0)= start2;
  114. while(tmpParams(paramnum1,0) <= end1)
  115. {
  116. while(tmpParams(paramnum2,0) <= end2)
  117. {
  118. // evaluate costfunction and write to plotfile
  119. plotfile << tmpParams(paramnum1,0) << " " << tmpParams(paramnum2,0) << " " << m_pCostfunc->evaluate(tmpParams) << std::endl;
  120. // increment evaluation value of parameter 2
  121. tmpParams(paramnum2,0)+= stepwidth2;
  122. }
  123. plotfile<<std::endl;
  124. // reset inner loop
  125. tmpParams(paramnum2,0)= start2;
  126. // increment evaluation value of parameter 1
  127. tmpParams(paramnum1,0)+= stepwidth1;
  128. }
  129. plotfile.close();
  130. }
  131. void Plotter::plot3D(int paramnum1, const float from1, const float to1, const float stepwidth1, int paramnum2, const float from2, const float to2, const float stepwidth2, int paramnum3, const float from3, const float to3, const float stepwidth3, const char* path)
  132. {
  133. // if the parameter number is < 0 or has a higher dimension then the optimization problem, throw an assertion
  134. assert(paramnum1 >= 0 && paramnum1 < m_pOptprob->getNumParams() && paramnum2 >= 0 && paramnum2 < m_pOptprob->getNumParams() && paramnum3 >= 0 && paramnum3 < m_pOptprob->getNumParams());
  135. // take actual parameter values from the optimization problem
  136. matrix_type tmpParams= m_pOptprob->getAllCurrentParams();
  137. // start and end values for costfunction evaluation
  138. float start1,end1;
  139. // if absolute bounds are used
  140. if(m_absoluteBounds)
  141. {
  142. start1= from1;
  143. end1= to1;
  144. }
  145. // else compute relative bounds
  146. else
  147. {
  148. start1= tmpParams(paramnum1,0) + from1;
  149. end1= tmpParams(paramnum1,0) + to1;
  150. }
  151. // iterating over the first parameter
  152. tmpParams(paramnum1,0)= start1;
  153. std::string filename(const_cast<char*>(path));
  154. // cutting suffix ".txt", if there
  155. if(filename.find(".txt") != std::string::npos)
  156. {
  157. filename.erase(filename.find(".txt"),4);
  158. }
  159. while(tmpParams(paramnum1,0) <= end1)
  160. {
  161. // generate name of plotfile
  162. char number[5];
  163. sprintf(number,"%f",tmpParams(paramnum1,0));
  164. std::string numfilename= filename + "_" + number + ".txt";
  165. // open plot file for writing
  166. std::ofstream plotfile;
  167. plotfile.open(numfilename.c_str());
  168. // start and end values for costfunction evaluation
  169. float start2,end2,start3,end3;
  170. if(m_absoluteBounds)
  171. {
  172. start2= from2;
  173. end2= to2;
  174. start3= from3;
  175. end3= to3;
  176. }
  177. // else compute relative bounds
  178. else
  179. {
  180. start2= tmpParams(paramnum2,0) + from2;
  181. end2= tmpParams(paramnum2,0) + to2;
  182. start3= tmpParams(paramnum3,0) + from3;
  183. end3= tmpParams(paramnum3,0) + to3;
  184. }
  185. // setting relevant parameter to start value
  186. tmpParams(paramnum2,0)= start2;
  187. tmpParams(paramnum3,0)= start3;
  188. while(tmpParams(paramnum2,0) <= end2)
  189. {
  190. while(tmpParams(paramnum3,0) <= end3)
  191. {
  192. // evaluate costfunction and write to plotfile
  193. plotfile << tmpParams(paramnum2,0) << " " << tmpParams(paramnum3,0) << " " << m_pCostfunc->evaluate(tmpParams) << std::endl;
  194. // increment evaluation value of parameter 3
  195. tmpParams(paramnum3,0)+= stepwidth3;
  196. }
  197. plotfile<<std::endl;
  198. // reset inner loop
  199. tmpParams(paramnum3,0)= start3;
  200. // increment evaluation value of parameter 2
  201. tmpParams(paramnum2,0)+= stepwidth2;
  202. }
  203. // close plotfile
  204. plotfile.close();
  205. // increment parameter 1
  206. tmpParams(paramnum1,0)+= stepwidth1;
  207. }
  208. }
  209. void Plotter::setBoundInterpretationStatus(bool absolute)
  210. {
  211. m_absoluteBounds= absolute;
  212. }
  213. bool Plotter::getBoundInterpretationStatus()
  214. {
  215. return m_absoluteBounds;
  216. }