MyCostFunction.cpp 920 B

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