Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "dlib/optimization.h"
- #include "dlib/global_optimization.h"
- #include <iostream>
- #include <exception>
- // variable length column vector of doubles
- typedef dlib::matrix<double, 0, 1> column_vector;
- /*
- Rosenbrock's function.
- It is a function of two input variables and has a global minimum at (1,1).
- */
- double rosen(const column_vector& m) {
- const double x = m(0);
- const double y = m(1);
- return 100.0 * std::pow(y - x * x, 2) + std::pow(1 - x, 2);
- }
- // helper function used while optimizing the rosen() function
- const column_vector rosen_derivative(const column_vector& m) {
- /*!
- ensures
- - returns the gradient vector for the rosen function
- !*/
- const double x = m(0);
- const double y = m(1);
- // column vector of length 2
- column_vector res(2);
- // gradient vector (i.e. fist partial derivative)
- res(0) = -400 * x * (y - x * x) - 2 * (1 - x); // with respect to x
- res(1) = 200 * (y - x * x); // with respect to y
- return res;
- }
- // Hessian matrix (i.e. second partial derivatives)
- dlib::matrix<double> rosen_hessian(const column_vector& m)
- {
- const double x = m(0); // derivative w.r.t x
- const double y = m(1); // derivative w.r.t y
- dlib::matrix<double> res(2, 2);
- // compute partial second derivatives
- res(0, 0) = 1200 * x * x - 400 * y + 2; // second derivative with respect to x
- res(1, 0) = res(0, 1) = -400 * x; // derivative with respect to x and y
- res(1, 1) = 200; // second derivative with respect to y
- return res;
- }
- class rosen_model {
- /*!
- This object is a "function model" which can be used with the
- find_min_trust_region() routine.
- !*/
- public:
- typedef ::column_vector column_vector;
- typedef dlib::matrix<double> general_matrix;
- // f(x) to be optimised
- double operator() (const column_vector& x) const {
- return rosen(x);
- }
- // partial f'(x) and f''(x)
- void get_derivative_and_hessian(const column_vector& x,
- column_vector& der,
- general_matrix& hess) const {
- der = rosen_derivative(x); // 1st partial derivative
- hess = rosen_hessian(x); // 2nd partial derivative
- }
- };
- int main() {
- // Set the starting point to (4,8). This is the point the optimization algorithm
- // will start out from and it will move it closer and closer to the function's
- // minimum point. So generally you want to try and compute a good guess that is
- // somewhat near the actual optimum value.
- column_vector starting_point = { 4, 8 };
- // Check the difference between our derivative and the numerical approximation. Since
- // it is very easy to make a mistake while coding a function like rosen_derivative() it is a good idea
- // to compare your derivative function against a numerical approximation and see if
- // the results are similar. So the first thing we do is compare the results at a test point:
- std::cout << "Derivative function check: " <<
- dlib::length(dlib::derivative(rosen)(starting_point) - rosen_derivative(starting_point)) << std::endl;
- // 1. find_min() using some "_search_strategy"
- // http://dlib.net/optimization.html
- dlib::find_min( dlib::bfgs_search_strategy(), // BFGS search strategy
- dlib::objective_delta_stop_strategy(1e-7), // Stop when rosen() is less than 10^-7
- rosen, rosen_derivative, starting_point, -1); // our function to be optimised
- // 2. trust-region method. "operator()" and "get_derivative_and_hessian()" is call used during the routine.
- dlib::find_min_trust_region(dlib::objective_delta_stop_strategy(1e-7),
- rosen_model(),
- starting_point,
- 10);
- std::cout << "rosen solution: \n" << starting_point << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment