yat  0.20.3pre
MultiMinimizerDerivative.h
1 #ifndef theplu_yat_utility_multi_minimizer_derivative
2 #define theplu_yat_utility_multi_minimizer_derivative
3 
4 // $Id: MultiMinimizerDerivative.h 4248 2022-09-26 08:52:29Z peter $
5 //
6 // Copyright (C) 2022 Peter Johansson
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program. If not, see <https://www.gnu.org/licenses/>.
20 
21 #include "multivariable/df.h"
22 #include "multivariable/f.h"
23 
24 #include <yat/utility/Vector.h>
25 #include <yat/utility/VectorMutable.h>
26 #include <yat/utility/version.h>
27 
28 #include <gsl/gsl_multimin.h>
29 
30 #include <cassert>
31 #include <memory>
32 
33 namespace theplu {
34 namespace yat {
35 namespace utility {
36 
44  {
45  public:
48 
51  operator=(const MultiMinimizerDerivative&) = delete;
52 
56  unsigned int epochs(void) const;
57 
63  double step_size(void) const;
64 
70  void step_size(double ss);
71 
78  double tolerance(void) const;
79 
86  void tolerance(double tol);
87 
90  class Stopper
91  {
92  public:
94  virtual bool operator()(const gsl_multimin_fdfminimizer*)=0;
95  };
96 
97 
100  class Gradient : public Stopper
101  {
102  public:
104  explicit Gradient(double epsabs);
105 
113  bool operator()(const gsl_multimin_fdfminimizer*);
114  private:
115  double epsabs_;
116  };
117 
118 
132  template<class FUNC>
133  void operator()(yat::utility::VectorMutable&, FUNC& func,
135 
141  template<class FUNC>
142  void operator()(yat::utility::VectorMutable&, FUNC& func,
144  unsigned int max_epochs);
145  protected:
152  MultiMinimizerDerivative(const gsl_multimin_fdfminimizer_type* t,
153  size_t size);
154  private:
155  const gsl_multimin_fdfminimizer_type* type_;
156  unsigned int epochs_;
157  size_t size_;
158  struct GslFree
159  {
160  void operator()(gsl_multimin_fdfminimizer*) const;
161  };
162  std::unique_ptr<gsl_multimin_fdfminimizer, GslFree> solver_;
163  double step_size_;
164  double tol_;
165  };
166 
167 
168  // template implementation
169 
170  template<class FUNC>
171  void
173  FUNC& func,
175  {
176  unsigned int max_epochs = std::numeric_limits<unsigned int>::max();
177  (*this)(x, func, std::move(stopper), max_epochs);
178  }
179 
180 
181  template<class FUNC>
182  void
184  FUNC& func,
186  unsigned int max_epochs)
187  {
188  assert(size_ == x.size());
189  gsl_multimin_function_fdf gsl_func;
190  gsl_func.n = size_;
191  gsl_func.f = multivariable::f<FUNC>;
192  gsl_func.df = multivariable::df<FUNC>;
193  gsl_func.fdf = multivariable::fdf<FUNC>;
194  gsl_func.params = &func;
195 
196  gsl_multimin_fdfminimizer_set(solver_.get(), &gsl_func, x.gsl_vector_p(),
197  step_size_, tol_);
198 
199  int status = 0;
200  for (epochs_=0; epochs_<max_epochs; ++epochs_) {
201  status = gsl_multimin_fdfminimizer_iterate(solver_.get());
202  if (status) {
203  if (status == GSL_ENOPROG)
204  break;
205  throw yat::utility::GSL_error("MultiMinimizerDerivative", status);
206  }
207 
208  if (stopper(solver_.get()))
209  break;
210  //if (gsl_multimin_test_gradient(solver_->gradient,epsabs) != GSL_CONTINUE)
211  //break;
212  }
213 
214  // copy result to passed x
215  yat::utility::VectorConstView view(solver_->x);
216  x = view;
217  }
218 
219 }}}
220 #endif
Class for errors reported from underlying GSL calls.
Definition: Exception.h:102
The Department of Theoretical Physics namespace as we define it.
unsigned int epochs(void) const
Number of epochs (iterations) used in last minimisation.
Definition: MultiMinimizerDerivative.h:100
T max(const T &a, const T &b, const T &c)
Definition: stl_utility.h:699
Read-only view.
Definition: VectorConstView.h:56
bool operator()(const gsl_multimin_fdfminimizer *)
Wrapper class around gsl_multimin_fdfminimizer in GSL.
Definition: MultiMinimizerDerivative.h:43
This is the mutable interface to GSL vector.
Definition: VectorMutable.h:56
Definition: MultiMinimizerDerivative.h:90
void operator()(yat::utility::VectorMutable &, FUNC &func, MultiMinimizerDerivative::Stopper &&stopper)
Definition: MultiMinimizerDerivative.h:172
MultiMinimizerDerivative(const MultiMinimizerDerivative &)=delete
Copy is not allowed.
virtual bool operator()(const gsl_multimin_fdfminimizer *)=0
return true is search should stop
MultiMinimizerDerivative & operator=(const MultiMinimizerDerivative &)=delete
Assignment not allowed.

Generated on Sat Feb 18 2023 03:31:44 for yat by  doxygen 1.8.14