BubbleProfiler  0.3.0
by Peter Athron, Csaba Balazs, Michael Bardsley, Andrew Fowlie, Dylan Harries & Graham White
nlopt_optimizer.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of BubbleProfiler.
3  *
4  * BubbleProfiler is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * BubbleProfiler is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with BubbleProfiler. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef BUBBLEPROFILER_NLOPT_OPTIMIZER_HPP_INCLUDED
19 #define BUBBLEPROFILER_NLOPT_OPTIMIZER_HPP_INCLUDED
20 
21 #include <nlopt.hpp>
22 
23 #include <Eigen/Core>
24 
25 namespace BubbleProfiler {
26 
30 bool optimization_succeeded(nlopt::result);
31 
36 public:
37  using Index = Eigen::VectorXd::Index;
38  using Function = std::function<double(const Eigen::VectorXd&)>;
39 
40  enum class Extremum_type { MIN, MAX};
41 
42  explicit NLopt_optimizer(Index);
43  template <typename F>
44  NLopt_optimizer(F&&, Index);
45 
46  void set_algorithm(nlopt::algorithm a) { algorithm = a; }
48  template <typename F>
49  void set_function(F&& f) { function = std::forward<F>(f); }
50 
51  void set_lower_bounds(double lb);
52  void set_lower_bounds(Index i, double lb);
53  void set_lower_bounds(const Eigen::VectorXd& lb);
54  void set_upper_bounds(double ub);
55  void set_upper_bounds(Index i, double ub);
56  void set_upper_bounds(const Eigen::VectorXd& ub);
57  void set_xtol_rel(double xtol_rel_);
58  void set_ftol_rel(double ftol_rel_);
59 
67  void set_max_time(double maxtime);
68 
69  Eigen::VectorXd get_extremum_location() const { return extremum; }
70  double get_extremum_value() const { return extremum_value; }
71 
77  nlopt::result optimize(const Eigen::VectorXd& guess);
78 
79 private:
81  nlopt::algorithm algorithm{nlopt::GN_DIRECT_L};
83  Eigen::VectorXd lower_bounds{};
84  Eigen::VectorXd upper_bounds{};
85  double maxtime{0.};
86  Eigen::VectorXd extremum{};
87  double extremum_value{0.};
88  Function function{nullptr};
89  double xtol_rel{2. * std::numeric_limits<double>::epsilon()};
90  double ftol_rel{2. * std::numeric_limits<double>::epsilon()};
91 
92  static double nlopt_function(const std::vector<double>&,
93  std::vector<double>&, void*);
94 };
95 
96 template <typename F>
98  : n_dims(n)
99  , lower_bounds(Eigen::VectorXd::Zero(n))
100  , upper_bounds(Eigen::VectorXd::Zero(n))
101  , function(std::forward<F>(f))
102 {
103 }
104 
105 } // namespace BubbleProfiler
106 
107 #endif
Eigen::VectorXd::Index Index
nlopt::result optimize(const Eigen::VectorXd &guess)
bool optimization_succeeded(nlopt::result)
std::function< double(const Eigen::VectorXd &)> Function
void set_extremum_type(Extremum_type e)
Eigen::VectorXd get_extremum_location() const
void set_ftol_rel(double ftol_rel_)
void set_algorithm(nlopt::algorithm a)
static double nlopt_function(const std::vector< double > &, std::vector< double > &, void *)
void set_xtol_rel(double xtol_rel_)