jack06215

[dlib] non-linear optimisation using some strategy

Feb 25th, 2020
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. #include "dlib/optimization.h"
  2. #include "dlib/global_optimization.h"
  3.  
  4. #include <iostream>
  5. #include <exception>
  6.  
  7. // variable length column vector of doubles
  8. typedef dlib::matrix<double, 0, 1> column_vector;
  9.  
  10. /*
  11.     Rosenbrock's function.  
  12.     It is a function of two input variables and has a global minimum at (1,1).
  13. */
  14. double rosen(const column_vector& m) {
  15.     const double x = m(0);
  16.     const double y = m(1);
  17.  
  18.     return 100.0 * std::pow(y - x * x, 2) + std::pow(1 - x, 2);
  19. }
  20.  
  21. // helper function used while optimizing the rosen() function
  22. const column_vector rosen_derivative(const column_vector& m) {
  23. /*!
  24.     ensures
  25.     - returns the gradient vector for the rosen function
  26. !*/
  27.     const double x = m(0);
  28.     const double y = m(1);
  29.  
  30.     // column vector of length 2
  31.     column_vector res(2);
  32.  
  33.     // gradient vector (i.e. fist partial derivative)
  34.     res(0) = -400 * x * (y - x * x) - 2 * (1 - x);      // with respect to x
  35.     res(1) = 200 * (y - x * x);                         // with respect to y
  36.    
  37.     return res;
  38. }
  39.  
  40. // Hessian matrix (i.e. second partial derivatives)
  41. dlib::matrix<double> rosen_hessian(const column_vector& m)
  42. {
  43.     const double x = m(0);      // derivative w.r.t x
  44.     const double y = m(1);      // derivative w.r.t y
  45.  
  46.     dlib::matrix<double> res(2, 2);
  47.  
  48.     // compute partial second derivatives
  49.     res(0, 0) = 1200 * x * x - 400 * y + 2; // second derivative with respect to x
  50.     res(1, 0) = res(0, 1) = -400 * x;       // derivative with respect to x and y
  51.     res(1, 1) = 200;                        // second derivative with respect to y
  52.     return res;
  53. }
  54.  
  55. class rosen_model {
  56.     /*!
  57.         This object is a "function model" which can be used with the
  58.         find_min_trust_region() routine.
  59.     !*/
  60. public:
  61.     typedef ::column_vector column_vector;
  62.     typedef dlib::matrix<double> general_matrix;
  63.  
  64.     // f(x) to be optimised
  65.     double operator() (const column_vector& x) const {
  66.         return rosen(x);
  67.     }
  68.  
  69.     // partial f'(x) and f''(x)
  70.     void get_derivative_and_hessian(const column_vector& x,
  71.                                     column_vector& der,
  72.                                     general_matrix& hess) const {
  73.         der = rosen_derivative(x);  // 1st partial derivative
  74.         hess = rosen_hessian(x);    // 2nd partial derivative
  75.     }
  76. };
  77.  
  78. int main() {
  79.    
  80.     // Set the starting point to (4,8).  This is the point the optimization algorithm
  81.     // will start out from and it will move it closer and closer to the function's
  82.     // minimum point. So generally you want to try and compute a good guess that is
  83.     // somewhat near the actual optimum value.
  84.     column_vector starting_point = { 4, 8 };
  85.  
  86.     // Check the difference between our derivative and the numerical approximation. Since
  87.     // it is very easy to make a mistake while coding a function like rosen_derivative() it is a good idea
  88.     // to compare your derivative function against a numerical approximation and see if
  89.     // the results are similar. So the first thing we do is compare the results at a test point:
  90.     std::cout << "Derivative function check:  " <<
  91.         dlib::length(dlib::derivative(rosen)(starting_point) - rosen_derivative(starting_point)) << std::endl;
  92.    
  93.    
  94.    
  95.     // 1. find_min() using some "_search_strategy"
  96.     // http://dlib.net/optimization.html
  97.     dlib::find_min( dlib::bfgs_search_strategy(),                   // BFGS search strategy
  98.                     dlib::objective_delta_stop_strategy(1e-7),      // Stop when rosen() is less than 10^-7
  99.                     rosen, rosen_derivative, starting_point, -1);   // our function to be optimised
  100.  
  101.    
  102.     // 2. trust-region method. "operator()" and "get_derivative_and_hessian()" is call used during the routine.
  103.     dlib::find_min_trust_region(dlib::objective_delta_stop_strategy(1e-7),
  104.                                 rosen_model(),
  105.                                 starting_point,
  106.                                 10);
  107.     std::cout << "rosen solution: \n" << starting_point << std::endl;
  108.    
  109.    
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment