CostFunction_ndim_2ndOrder.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // CostFunction_ndim_2ndOrder.cpp: implementation of a test
  4. // CostFunction class.
  5. //
  6. // Written By: Matthias Wacker
  7. //
  8. //////////////////////////////////////////////////////////////////////
  9. #include "CostFunction_ndim_2ndOrder.h"
  10. using namespace OPTIMIZATION;
  11. CostFunction_ndim_2ndOrder::CostFunction_ndim_2ndOrder() : SuperClass()
  12. {
  13. }
  14. CostFunction_ndim_2ndOrder::CostFunction_ndim_2ndOrder(unsigned int dim) : SuperClass(dim)
  15. {
  16. m_hasAnalyticGradient = true;
  17. m_hasAnalyticHessian = false; // not implemented
  18. m_A = matrix_type(dim,dim);
  19. m_bt = matrix_type(1,dim);
  20. m_c = 0.0;
  21. }
  22. CostFunction_ndim_2ndOrder::CostFunction_ndim_2ndOrder(const CostFunction_ndim_2ndOrder &func):SuperClass(func)
  23. {
  24. m_A = func.m_A;
  25. m_bt = func.m_bt;
  26. m_c = func.m_c;
  27. }
  28. CostFunction_ndim_2ndOrder & CostFunction_ndim_2ndOrder::operator=(const CostFunction_ndim_2ndOrder & func)
  29. {
  30. /*
  31. avoid self-copy
  32. */
  33. if(this != &func)
  34. {
  35. /*
  36. =operator of SuperClass
  37. */
  38. SuperClass::operator=(func);
  39. /*
  40. own values:
  41. */
  42. m_A = func.m_A;
  43. m_bt = func.m_bt;
  44. m_c = func.m_c;
  45. }
  46. return *this;
  47. }
  48. CostFunction_ndim_2ndOrder::~CostFunction_ndim_2ndOrder()
  49. {
  50. }
  51. void CostFunction_ndim_2ndOrder::init()
  52. {
  53. }
  54. int CostFunction_ndim_2ndOrder::setAbc(const matrix_type &A,const matrix_type &b, double c)
  55. {
  56. if(A.rows() == static_cast<int>(m_numOfParameters) && A.cols() == static_cast<int>(m_numOfParameters) &&
  57. b.rows() == static_cast<int>(m_numOfParameters) && b.cols() == 1)
  58. {
  59. m_A = A;
  60. m_bt = b.transpose();
  61. m_c = c;
  62. return 0;
  63. }
  64. else
  65. {
  66. return -1;
  67. }
  68. }
  69. double CostFunction_ndim_2ndOrder::evaluate(const matrix_type &parameter)
  70. {
  71. //return ( !parameter * 0.5 * m_A * parameter + m_bt * parameter + m_c)(0,0);
  72. return ( parameter.transpose() * 0.5 * m_A * parameter + m_bt * parameter)(0,0) + m_c;
  73. }
  74. const matrix_type CostFunction_ndim_2ndOrder::getAnalyticGradient(const matrix_type &parameter)
  75. {
  76. matrix_type tmp = parameter * 0;
  77. tmp = m_A * parameter + m_bt.transpose();
  78. return tmp;
  79. }