CostFunction.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // CostFunction.cpp: implementation of the CostFunction class.
  4. //
  5. // Written By: Matthias Wacker
  6. //
  7. //////////////////////////////////////////////////////////////////////
  8. #include "optimization/CostFunction.h"
  9. using namespace opt;
  10. CostFunction::CostFunction() : SuperClass()
  11. {
  12. m_hasAnalyticGradient = false;
  13. m_hasAnalyticHessian = false;
  14. m_numEval = 0;
  15. }
  16. CostFunction::CostFunction(unsigned int numOfParameters) : SuperClass(numOfParameters)
  17. {
  18. m_hasAnalyticGradient = false;
  19. m_hasAnalyticHessian = false;
  20. m_numEval = 0;
  21. }
  22. CostFunction::CostFunction(const CostFunction &func) : SuperClass(func)
  23. {
  24. m_hasAnalyticGradient = func.m_hasAnalyticGradient;
  25. m_hasAnalyticHessian = func.m_hasAnalyticHessian;
  26. m_numEval = func.m_numEval;
  27. }
  28. CostFunction::~CostFunction()
  29. {
  30. }
  31. CostFunction &CostFunction::operator=(const CostFunction &func)
  32. {
  33. /*
  34. avoid self-copy
  35. */
  36. if(this != &func)
  37. {
  38. /*
  39. =operator of SuperClass
  40. */
  41. SuperClass::operator=(func);
  42. /*
  43. own values:
  44. */
  45. m_hasAnalyticGradient = func.m_hasAnalyticGradient;
  46. m_hasAnalyticHessian = func.m_hasAnalyticHessian;
  47. m_numEval = func.m_numEval;
  48. }
  49. return *this;
  50. }
  51. const matrix_type CostFunction::getAnalyticGradient(const matrix_type &x)
  52. {
  53. /*
  54. FIXME: WACKER: Throw exception as default behaviour ?
  55. */
  56. return x * 0;
  57. }
  58. void CostFunction::init()
  59. {
  60. }
  61. const matrix_type CostFunction::getAnalyticHessian(const matrix_type &x)
  62. {
  63. /*
  64. FIXME: WACKER: Throw exception as default behaviour
  65. */
  66. return x * !x *0.0;
  67. }
  68. bool CostFunction::setX0(const matrix_type &x0)
  69. {
  70. if(x0.rows() == static_cast<int>(m_numOfParameters) && x0.cols() == 1)
  71. {
  72. m_x_0 = x0;
  73. return true;
  74. }
  75. else
  76. return false;
  77. }
  78. bool CostFunction::setH0(const matrix_type &H0)
  79. {
  80. if(H0.rows() == static_cast<int>(m_numOfParameters) && H0.cols() == 1)
  81. {
  82. m_h_0 = H0;
  83. return true;
  84. }
  85. else
  86. return false;
  87. }
  88. double CostFunction::evaluateSub(double lambda)
  89. {
  90. return evaluate(m_x_0 + m_h_0 * lambda);
  91. }