jack06215

[dlib] BOBYQA with cost function

Feb 19th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <dlib/optimization.h>
  2. #include <dlib/global_optimization.h>
  3. #include <iostream>
  4.  
  5. // variable length column vector
  6. typedef dlib::matrix<double, 0, 1> column_vector;
  7.  
  8. class cost_fuction
  9. {
  10. public:
  11.     cost_fuction(double a, double b, double c, double d)
  12.     { // run all variable initialisation here
  13.         target = { a, b, c, d };
  14.         optimise_target = dlib::log10(target*100);
  15.     }
  16.  
  17.     double operator()(const column_vector& arg) const
  18.     { // this will be called repeatedly during the optimization process
  19.         return dlib::mean(dlib::squared(arg - optimise_target));
  20.     }
  21. private:
  22.     column_vector target;
  23.     column_vector optimise_target;
  24. };
  25.  
  26. int main()
  27. {
  28.     column_vector starting_point = { dlib::pi / 4, dlib::pi / 4, dlib::pi / 4, dlib::pi / 4 };
  29.     dlib::find_min_bobyqa(cost_fuction(3, 5, 1, 7),
  30.         starting_point,
  31.         9,                                              // number of interpolation points
  32.         dlib::uniform_matrix<double>(4, 1, -1e100),     // lower bound constraint
  33.         dlib::uniform_matrix<double>(4, 1, 1e100),      // upper bound constraint
  34.         10,                                             // initial trust region radius
  35.         1e-6,                                           // stopping trust region radius
  36.         100                                             // max number of objective function evaluations
  37.     );
  38.  
  39.     std::cout << "find_min_bobyqa() solution:\n" << starting_point << std::endl;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment