MyCostFunction.cpp 895 B

12345678910111213141516171819202122232425262728293031323334
  1. #include "MyCostFunction.h"
  2. double MyCostFunction::evaluate(const optimization::matrix_type & x)
  3. {
  4. double f;
  5. if (m_bVerbose)
  6. std::cerr << x.rows() << " x " << x.cols() << std::endl;
  7. if ( x.rows() == 1 )
  8. {
  9. if (m_bVerbose)
  10. std::cerr << "current position: " << x[0][0] << std::endl;
  11. //our cost function is f(x) = (x-5)^2
  12. f = pow(x[0][0] - 4.2, 2.0);
  13. if (m_bVerbose)
  14. std::cerr << "function value: " << f << std::endl;
  15. }
  16. //two-dimensional data
  17. else {
  18. if (m_bVerbose)
  19. std::cerr << "current position: " << x[0][0] << " " << x[1][0] << std::endl;
  20. //our cost function is f(x,y) = (x-4.7)^2 + (y-1.1)^2
  21. f = pow(x[0][0] - 4.7, 2.0) + pow( x[1][0] - 1.1, 2.0 );
  22. if (m_bVerbose)
  23. std::cerr << "function value: " << f << std::endl;
  24. }
  25. return f;
  26. }