CostFunction.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // CostFunction.h: interface of the CostFunction class.
  4. //
  5. // Written By: Matthias Wacker
  6. //
  7. //////////////////////////////////////////////////////////////////////
  8. #ifndef _COST_FUNCTION_H_
  9. #define _COST_FUNCTION_H_
  10. #include "optimization/Optimizable.h"
  11. #include "optimization/Opt_Namespace.h"
  12. namespace opt=optimization;
  13. /*!
  14. class Abstract base class for all cost functions.
  15. */
  16. class CostFunction : public Optimizable
  17. {
  18. public:
  19. typedef Optimizable SuperClass;
  20. typedef opt::matrix_type matrix_type;
  21. /*!
  22. DefaultConstructor
  23. */
  24. CostFunction();
  25. /*!
  26. Constructor.
  27. \param numOfParameters
  28. */
  29. CostFunction(unsigned int numOfParamters);
  30. /*
  31. Copy constructor
  32. */
  33. CostFunction(const CostFunction &func);
  34. /*!
  35. Destructor.
  36. */
  37. virtual ~CostFunction();
  38. /*!
  39. =operator
  40. */
  41. CostFunction &operator=(const CostFunction &costFunc);
  42. inline bool hasAnalyticGradient(){return m_hasAnalyticGradient;}
  43. /*!
  44. get the analytic gradient
  45. */
  46. virtual const opt::matrix_type getAnalyticGradient(const opt::matrix_type &x);
  47. inline bool hasAnalyticHessian(){return m_hasAnalyticHessian;}
  48. /*!
  49. get the analytic hessian
  50. */
  51. virtual const opt::matrix_type getAnalyticHessian(const opt::matrix_type & x);
  52. /*!
  53. get number of Evaluations
  54. */
  55. inline unsigned int getNumberOfEvaluations(){return m_numEval;};
  56. /*!
  57. reset the evaluation counter
  58. */
  59. inline void resetNumberOfEvaluations(){m_numEval = 0;};
  60. /*!
  61. Initialization for the cost function
  62. */
  63. virtual void init();
  64. /*!
  65. set an x0 for 1dim line search
  66. */
  67. bool setX0(const opt::matrix_type &x0);
  68. /*!
  69. set an h0 for 1dim line search
  70. */
  71. bool setH0(const opt::matrix_type &H0);
  72. /*!
  73. evaluate 1dimension sub function fsub(lambda) = f(x0 + lambda * h0)
  74. */
  75. double evaluateSub(double lambda);
  76. /*!
  77. * This function returns the full parameter vector. If the costfunction performs dimension reduction
  78. * this method will return the current parameter vector with full dimension. Otherwise the costfunction works with
  79. * the full parameter vector in any case and it is returned unchanged.
  80. * @param x current parameter vector (from optimizer)
  81. * @return full parameter vector
  82. */
  83. virtual opt::matrix_type getFullParamsFromSubParams(const opt::matrix_type &x)
  84. {
  85. matrix_type fullparamvec(x);
  86. return x;
  87. }
  88. protected:
  89. /*!
  90. has analytic gradient ?
  91. */
  92. bool m_hasAnalyticGradient;
  93. /*!
  94. has analytic hessian ?
  95. */
  96. bool m_hasAnalyticHessian;
  97. /*!
  98. x_0
  99. */
  100. opt::matrix_type m_x_0;
  101. /*!
  102. direction h_0
  103. */
  104. opt::matrix_type m_h_0;
  105. /*!
  106. number of evaluations
  107. */
  108. unsigned int m_numEval;
  109. };
  110. #endif