yat  0.20.3pre
MultiMinimizer.h
1 #ifndef theplu_yat_utility_multi_minimizer_
2 #define theplu_yat_utility_multi_minimizer_
3 
4 // $Id: MultiMinimizer.h 4248 2022-09-26 08:52:29Z 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 "multivariable/f.h"
27 
28 #include "Vector.h"
29 #include "yat_assert.h"
30 
31 #include <gsl/gsl_multimin.h>
32 
33 #include <memory>
34 
35 namespace theplu {
36 namespace yat {
37 namespace utility {
38 
48  {
49  public:
51  MultiMinimizer(const MultiMinimizer&) = delete;
52 
54  MultiMinimizer& operator=(const MultiMinimizer&) = delete;
55 
59  unsigned int epochs(void) const;
60 
66  const yat::utility::Vector& step(void) const;
67 
75 
78  class Stopper
79  {
80  public:
82  virtual bool operator()(const gsl_multimin_fminimizer*)=0;
83  };
84 
85 
90  class Size : public Stopper
91  {
92  public:
94  explicit Size(double epsabs);
95 
103  bool operator()(const gsl_multimin_fminimizer*);
104  private:
105  double epsabs_;
106  };
107 
117  template<class FUNC>
118  void operator()(yat::utility::VectorMutable& x, FUNC& func,
119  Stopper&& stopper);
120 
126  template<class FUNC>
127  void operator()(yat::utility::VectorMutable&, FUNC& func,
128  Stopper&& stopper, unsigned int max_epochs);
129  protected:
136  MultiMinimizer(const gsl_multimin_fminimizer_type* t, size_t size);
137  private:
138  const gsl_multimin_fminimizer_type* type_;
139  size_t size_;
140  struct GslFree
141  {
142  void operator()(gsl_multimin_fminimizer*) const;
143  };
144  std::unique_ptr<gsl_multimin_fminimizer, GslFree> solver_;
145  yat::utility::Vector step_size_;
146  unsigned int epochs_;
147  };
148 
149 
150  // template implementation
151 
152  template<class FUNC>
154  FUNC& func,
155  MultiMinimizer::Stopper&& stopper)
156  {
157  unsigned int max_epochs = std::numeric_limits<unsigned int>::max();
158  (*this)(x, func, std::move(stopper), max_epochs);
159  }
160 
161 
162  template<class FUNC>
164  FUNC& func,
165  MultiMinimizer::Stopper&& stopper,
166  unsigned int max_epochs)
167  {
168  YAT_ASSERT(size_ == x.size());
169  gsl_multimin_function gsl_func;
170  gsl_func.n = x.size();
171  gsl_func.f = multivariable::f<FUNC>;
172  gsl_func.params = &func;
173 
174  gsl_multimin_fminimizer_set(solver_.get(), &gsl_func, x.gsl_vector_p(),
175  step_size_.gsl_vector_p());
176 
177  int status = 0;
178  for (epochs_=0; epochs_<max_epochs; ++epochs_) {
179  status = gsl_multimin_fminimizer_iterate(solver_.get());
180  if (status) {
181  if (status == GSL_ENOPROG)
182  break;
183  throw GSL_error("MultiMinimizer", status);
184  }
185  if (stopper(solver_.get()))
186  break;
187  //double size = gsl_multimin_fminimizer_size(solver_.get());
188  //if (gsl_multimin_test_size(size, epsabs) != GSL_CONTINUE)
189  //break;
190  }
191 
192  // copy result to passed x
193  yat::utility::VectorConstView view(solver_->x);
194  x = view;
195  }
196 
197 }}}
198 #endif
Definition: MultiMinimizer.h:78
Class for errors reported from underlying GSL calls.
Definition: Exception.h:102
The Department of Theoretical Physics namespace as we define it.
virtual bool operator()(const gsl_multimin_fminimizer *)=0
return true when search should stop
MultiMinimizer & operator=(const MultiMinimizer &)=delete
Assignment is not allowed.
bool operator()(const gsl_multimin_fminimizer *)
const yat::utility::Vector & step(void) const
The size of the first step.
Wrapper class around gsl_multimin_fminimizer in GSL.
Definition: MultiMinimizer.h:47
MultiMinimizer(const MultiMinimizer &)=delete
Copy is not allowed.
unsigned int epochs(void) const
Number of epochs (iterations) used in last minimisation.
T max(const T &a, const T &b, const T &c)
Definition: stl_utility.h:699
Read-only view.
Definition: VectorConstView.h:56
This is the yat interface to GSL vector.
Definition: Vector.h:59
void operator()(yat::utility::VectorMutable &x, FUNC &func, Stopper &&stopper)
Definition: MultiMinimizer.h:153
This is the mutable interface to GSL vector.
Definition: VectorMutable.h:56
Definition: MultiMinimizer.h:90

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