123456789101112131415161718192021222324252627282930313233343536 |
- #include "MyCostFunction.h"
- using namespace OPTIMIZATION;
- double MyCostFunction::evaluate(const OPTIMIZATION::matrix_type & x)
- {
- double f;
- if (m_bVerbose)
- std::cerr << x.rows() << " x " << x.cols() << std::endl;
- if ( x.rows() == 1 )
- {
- if (m_bVerbose)
- std::cerr << "current position: " << x(0,0) << std::endl;
- //our cost function is f(x) = (x-5)^2
- f = pow(x(0,0) - 4.2, 2.0);
- if (m_bVerbose)
- std::cerr << "function value: " << f << std::endl;
- }
- //two-dimensional data
- else {
- if (m_bVerbose)
- std::cerr << "current position: " << x(0,0) << " " << x(1,0) << std::endl;
- //our cost function is f(x,y) = (x-4.7)^2 + (y-1.1)^2
- f = pow(x(0,0) - 4.7, 2.0) + pow( x(1,0) - 1.1, 2.0 );
- if (m_bVerbose)
- std::cerr << "function value: " << f << std::endl;
- }
- return f;
- }
|