yat  0.21pre
Minimizer.h
1 #ifndef theplu_yat_utility_minimizer_
2 #define theplu_yat_utility_minimizer_
3 
4 // $Id: Minimizer.h 4252 2022-11-18 02:54:04Z peter $
5 
6 /*
7  Copyright (C) 2022 Peter Johansson
8 
9  This file is part of the yat library, https://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <https://www.gnu.org/licenses/>.
23 */
24 
25 #include "Exception.h"
26 #include "yat_assert.h"
27 #include "univariable/f.h"
28 
29 #include <gsl/gsl_min.h>
30 #include <gsl/gsl_errno.h>
31 
32 #include <limits>
33 #include <memory>
34 #include <sstream>
35 
36 namespace theplu {
37 namespace yat {
38 namespace utility {
39 
48  class Minimizer
49  {
50  public:
52  Minimizer(const Minimizer&) = delete;
53 
55  Minimizer& operator=(const Minimizer&) = delete;
56 
60  unsigned int epochs(void) const;
61 
64  class Stopper
65  {
66  public:
68  virtual bool operator()(const gsl_min_fminimizer*)=0;
69  };
70 
73  class Interval : public Stopper
74  {
75  public:
80  Interval(double epsabs, double epsrel);
81 
88  bool operator()(const gsl_min_fminimizer*);
89  private:
90  double epsabs_;
91  double epsrel_;
92  };
93 
102  template<class FUNC>
103  double operator()(FUNC& func, double guess, double min, double max,
104  Stopper&& stopper);
105 
113  template<class FUNC>
114  double operator()(FUNC& func, double guess, double min, double max,
115  Stopper&& stopper, unsigned int max_epochs);
116 
117  protected:
123  Minimizer(const gsl_min_fminimizer_type* t);
124  private:
125  const gsl_min_fminimizer_type* type_;
126  struct GslFree
127  {
128  void operator()(gsl_min_fminimizer*) const;
129  };
130  std::unique_ptr<gsl_min_fminimizer, GslFree> solver_;
131  unsigned int epochs_;
132  double epsabs_;
133  double epsrel_;
134  };
135 
136 
137  // template implementation
138 
139  template<class FUNC>
140  double Minimizer::operator()(FUNC& func, double guess, double min,double max,
141  Minimizer::Stopper&& stopper)
142  {
143  unsigned int max_epochs = std::numeric_limits<unsigned int>::max();
144  return (*this)(func, guess, min, max, std::move(stopper), max_epochs);
145  }
146 
147 
148  template<class FUNC>
149  double Minimizer::operator()(FUNC& func, double guess, double min,
150  double max, Minimizer::Stopper&& stopper,
151  unsigned int max_epochs)
152  {
153  gsl_function gsl_func;
154  gsl_func.function = univariable::f<FUNC>;
155  gsl_func.params = &func;
156  gsl_min_fminimizer_set(solver_.get(), &gsl_func, guess, min, max);
157 
158  int status = GSL_CONTINUE;
159  for (epochs_=0; epochs_<max_epochs && !stopper(solver_.get()); ++epochs_) {
160  status = gsl_min_fminimizer_iterate(solver_.get());
161  if (status) {
162  // GSL returns GSL_FAILURE when the function cannot be improved
163  if (status == GSL_FAILURE)
164  break;
165  if (status == GSL_EBADFUNC) {
166  std::ostringstream ss;
167  ss << "Minimizer: at x = "
168  << gsl_min_fminimizer_x_minimum(solver_.get());
169  throw GSL_error(ss.str(), status);
170  }
171  }
172  }
173 
174  return gsl_min_fminimizer_x_minimum(solver_.get());
175  }
176 
177 }}}
178 #endif
Interval(double epsabs, double epsrel)
virtual bool operator()(const gsl_min_fminimizer *)=0
return true is search should stop
unsigned int epochs(void) const
Number of epochs (iterations) used in last minimisation.
Class for errors reported from underlying GSL calls.
Definition: Exception.h:102
The Department of Theoretical Physics namespace as we define it.
double operator()(FUNC &func, double guess, double min, double max, Stopper &&stopper)
Definition: Minimizer.h:140
T max(const T &a, const T &b, const T &c)
Definition: stl_utility.h:699
Wrapper class around gsl_min_fminimizer in GSL.
Definition: Minimizer.h:48
bool operator()(const gsl_min_fminimizer *)
Minimizer(const Minimizer &)=delete
Copy is not allowed.
Minimizer & operator=(const Minimizer &)=delete
Assignment is not allowed.

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14