Bayeux  3.4.1
Core Foundation library for SuperNEMO
multimin.h
Go to the documentation of this file.
1 
3 #ifndef MYGSL_MULTIMIN_H
4 #define MYGSL_MULTIMIN_H 1
5 
6 // Standard library:
7 #include <iostream>
8 #include <string>
9 #include <vector>
10 
11 // Third party:
12 // - GSL:
13 #include <gsl/gsl_multimin.h>
14 #include <gsl/gsl_vector.h>
15 
16 // This project:
17 #include <mygsl/i_unary_function.h>
18 
19 namespace mygsl {
20 
22  {
23  public:
24 
26  {
27  public:
28  enum limit_t
29  {
30  LIMIT_NO = 0x0,
31  LIMIT_MIN = 0x1,
32  LIMIT_MAX = 0x2,
34  };
35  enum type_t
36  {
40  };
41 
42  public:
43 
44  std::string name;
45  double value;
46  int type;
47  int limit;
48  double min;
49  double max;
50  double step;
51 
52  private:
53 
54  param_entry ()
55  {
56  value=0.1;
57  type =TYPE_FREE;
59  min=0.0;
60  max=1.0;
61  step=0.01;
62  name="parameter";
63  }
64 
65  public:
66 
67  bool is_fixed () const { return type==TYPE_FIXED; }
68 
69  bool is_free () const { return type==TYPE_FREE; }
70 
71  bool is_auto () const { return type==TYPE_AUTO; }
72 
73  bool has_limit () const {return limit!=LIMIT_NO; }
74 
75  bool has_no_limit () const {return limit==LIMIT_NO; }
76 
77  bool has_min () const {return limit&LIMIT_MIN; }
78 
79  bool has_max () const {return limit&LIMIT_MAX; }
80 
81  bool is_in_range () const;
82 
83  double get_dist_to_min () const;
84 
85  double get_dist_to_max () const;
86 
87  double get_dist_to_limit () const;
88 
89  double get_value () const;
90 
91  double get_step () const;
92 
93  double get_min () const;
94 
95  double get_max () const;
96 
97  void set_value (double value_);
98 
99  void set_value_no_check (double value_);
100 
102  const std::string & name_);
103 
105  const std::string & name_ ,
106  double value_);
107 
109  const std::string & name_ ,
110  double value_ ,
111  double min_ ,
112  double max_ ,
113  double step_ ,
114  bool fixed_ = false);
115  };
116 
117  // predicate:
118  struct param_has_name : std::unary_function<bool,param_entry>
119  {
120  std::string _name_;
121  param_has_name (const std::string & name_) : _name_ (name_)
122  {
123  return;
124  }
125  bool operator () (const param_entry & pe_)
126  {
127  return pe_.name == _name_;
128  }
129  };
130 
131  // predicate:
132  struct param_is_free : std::unary_function<bool,param_entry>
133  {
135  {
136  return;
137  }
138  bool operator () (const param_entry & pe_)
139  {
140  return pe_.is_free ();
141  }
142  };
143 
144  // predicate:
145  struct param_is_fixed : std::unary_function<bool,param_entry>
146  {
148  {
149  return;
150  }
151  bool operator () (const param_entry & pe_)
152  {
153  return pe_.is_fixed ();
154  }
155  };
156 
157  // predicate:
158  struct param_is_auto : std::unary_function<bool,param_entry>
159  {
161  {
162  return;
163  }
164  bool operator () (const param_entry & pe_)
165  {
166  return pe_.is_auto ();
167  }
168  };
169 
170  protected:
171 
173  std::vector<param_entry> _params;
174 
175  private:
176 
177  size_t _free_dimension_;
178  size_t _auto_dimension_;
179 
180  void _update_free_dimension_ ();
181 
182  void _update_auto_dimension_ ();
183 
184  void _update_dimensions_ ();
185 
186  public:
187 
188  void lock_params ();
189 
190  void unlock_params ();
191 
192  bool is_lock_params () const;
193 
194  void print_params (std::ostream &) const;
195 
196  void print_params2 (std::ostream &) const;
197 
198  const param_entry & get_param (int i_) const;
199 
200  void set_param_value (int i_, double val_);
201 
202  void set_param_value_no_check (int i_, double val_);
203 
204  bool is_param_fixed (int i_) const;
205 
206  bool is_param_free (int i_) const;
207 
208  bool is_param_auto (int i_) const;
209 
210  double get_param_value (int i_) const;
211 
212  double get_param_min (int i_) const;
213 
214  double get_param_max (int i_) const;
215 
216  double get_param_step (int i_) const;
217 
218  const std::string & get_param_name (int i_) const;
219 
220  void add_param (const param_entry & pe_);
221 
222  size_t get_dimension () const;
223 
224  size_t get_free_dimension () const;
225 
226  size_t get_auto_dimension () const;
227 
228  protected:
229 
230  virtual int _prepare_values ();
231 
232  virtual int _auto_values ();
233 
234  virtual int _eval_f (double & f_) = 0;
235 
236  virtual int _eval_df (double * gradient_) = 0;
237 
238  public:
239 
240  virtual int prepare_values ();
241 
242  virtual int auto_values ();
243 
244  virtual int eval_f (const double * x_ , double & f_);
245 
246  virtual int eval_df (const double * x_ , double * gradient_);
247 
248  virtual int eval_fdf (const double * x_ ,
249  double & f_ ,
250  double * gradient_);
251 
252  void to_double_star (double * pars_ ,
253  size_t dimension_) const;
254 
255  virtual void from_double_star (const double * pars_ ,
256  size_t dimension_);
257 
258  multimin_system ();
259 
260  virtual ~multimin_system ();
261 
262  void dump (std::ostream & out_) const;
263 
264  };
265 
266  class multimin
267  {
268  public:
269  static const size_t DEFAULT_MAX_ITER;
270  static const bool DEFAULT_VERBOSE;
271 
272  static double multimin_f (const gsl_vector * v_ , void * params_);
273 
274  static void multimin_df (const gsl_vector * v_ , void * params_ ,
275  gsl_vector * gradient_);
276 
277  static void multimin_fdf (const gsl_vector * v_ , void * params_ ,
278  double * f_ , gsl_vector * gradient_);
279 
280  static bool name_is_valid (const std::string & name_);
281 
283  {
284  MODE_FDF = 0,
285  MODE_F = 1
286  };
287 
289  {
292  };
293 
295  {
296 
297  public:
298  virtual void action (int status_ ,
299  size_t iter_ ,
300  double * x_ ,
301  size_t dim_ ,
302  double f_) = 0;
303  void operator () (int status_ ,
304  size_t iter_ ,
305  double * x_ ,
306  size_t dim_ ,
307  double f_)
308  {
309  action (status_,iter_,x_,dim_,f_);
310  }
311 
312  };
313 
315  {
316  virtual void action (int status_ ,
317  size_t iter_ ,
318  double * x_ ,
319  size_t dim_ ,
320  double f_);
321  };
322 
324 
325  private:
326 
327  bool _verbose_;
328  int _mode_;
329  const gsl_multimin_fdfminimizer_type * _algo_fdf_;
330  const gsl_multimin_fminimizer_type * _algo_f_;
331  gsl_multimin_fdfminimizer * _fdfmin_;
332  gsl_multimin_fminimizer * _fmin_;
333  gsl_multimin_function _f_;
334  gsl_multimin_function_fdf _fdf_;
335  gsl_vector * _x_;
336  gsl_vector * _ss_;
337  multimin_system * _sys_;
338 
339  double _fdf_step_size_;
340  double _fdf_tol_;
341  size_t _max_iter_;
342  size_t _n_iter_;
343 
344  double _fval_;
345  int _stopping_;
346  double _epsabs_;
347 
348  // hook step function:
349  at_step_action * _at_step_action_;
350 
351  void _init_algorithm_ (const std::string & name_);
352 
353  void _set_mode_ (int mode_);
354 
355  public:
356 
357  static void print_types (std::ostream &);
358 
359  size_t get_n_iter () const;
360 
361  double get_fval () const;
362 
363  void unset_step_action ();
364 
365  void set_default_step_action ();
366 
367  void set_step_action (at_step_action & asd_);
368 
369  std::string get_name () const;
370 
371  void init (const std::string & name_ ,
372  multimin_system & ms_);
373 
374  void reset ();
375 
376  multimin ();
377 
378  virtual ~multimin ();
379 
380  protected:
381 
382  void _at_step_hook (int status_ ,
383  size_t iter_ ,
384  double * x_ ,
385  size_t dim_ ,
386  double f_);
387 
388  public:
389 
390  int minimize (double epsabs_);
391 
392  void devel_dump_x () const;
393 
394  };
395 
396 } // namespace mygsl
397 
398 #endif // MYGSL_MULTIMIN_H
399 
400 /* Local Variables: */
401 /* mode: c++ */
402 /* coding: utf-8 */
403 /* End: */
Definition: multimin.h:132
void _at_step_hook(int status_, size_t iter_, double *x_, size_t dim_, double f_)
bool is_auto() const
Definition: multimin.h:71
void unset_step_action()
static default_step_action _default_step_action_
Definition: multimin.h:323
double get_value() const
Definition: multimin.h:145
static param_entry make_param_entry_auto(const std::string &name_)
Definition: multimin.h:30
virtual int eval_fdf(const double *x_, double &f_, double *gradient_)
virtual int _eval_df(double *gradient_)=0
double get_step() const
double get_param_value(int i_) const
std::string get_name() const
void devel_dump_x() const
void print_params2(std::ostream &) const
void dump(std::ostream &out_) const
mode_type
Definition: multimin.h:282
static void multimin_fdf(const gsl_vector *v_, void *params_, double *f_, gsl_vector *gradient_)
bool operator()(const param_entry &pe_)
Definition: multimin.h:138
Definition: multimin.h:284
double get_dist_to_max() const
int minimize(double epsabs_)
bool is_param_auto(int i_) const
static void print_types(std::ostream &)
void set_param_value(int i_, double val_)
type_t
Definition: multimin.h:35
double get_dist_to_limit() const
virtual void from_double_star(const double *pars_, size_t dimension_)
void add_param(const param_entry &pe_)
static const bool DEFAULT_VERBOSE
Definition: multimin.h:270
void set_param_value_no_check(int i_, double val_)
double get_dist_to_min() const
bool is_param_fixed(int i_) const
bool has_max() const
Definition: multimin.h:79
Definition: multimin.h:291
static double multimin_f(const gsl_vector *v_, void *params_)
limit_t
Definition: multimin.h:28
virtual void action(int status_, size_t iter_, double *x_, size_t dim_, double f_)=0
virtual void action(int status_, size_t iter_, double *x_, size_t dim_, double f_)
Definition: multimin.h:33
std::vector< param_entry > _params
Definition: multimin.h:173
Definition: multimin.h:314
param_is_fixed()
Definition: multimin.h:147
static const size_t DEFAULT_MAX_ITER
Definition: multimin.h:269
bool has_no_limit() const
Definition: multimin.h:75
double min
Definition: multimin.h:48
void set_default_step_action()
bool has_limit() const
Definition: multimin.h:73
double get_max() const
Definition: multimin.h:32
Definition: multimin.h:25
Definition: multimin.h:285
double get_param_max(int i_) const
bool has_min() const
Definition: multimin.h:77
bool is_param_free(int i_) const
stopping_t
Definition: multimin.h:288
void operator()(int status_, size_t iter_, double *x_, size_t dim_, double f_)
Definition: multimin.h:303
param_is_free()
Definition: multimin.h:134
std::string _name_
Definition: multimin.h:120
bool is_fixed() const
Definition: multimin.h:67
bool is_in_range() const
std::string name
Definition: multimin.h:44
void to_double_star(double *pars_, size_t dimension_) const
Definition: multimin.h:31
static bool name_is_valid(const std::string &name_)
void print_params(std::ostream &) const
void set_value(double value_)
size_t get_dimension() const
virtual int auto_values()
Definition: multimin.h:290
Top-level namespace of the Bayeux/mygsl module library.
Definition: base_decay_driver.h:47
bool operator()(const param_entry &pe_)
Definition: multimin.h:164
Definition: multimin.h:266
virtual ~multimin()
Definition: multimin.h:39
virtual int _prepare_values()
Definition: multimin.h:37
void init(const std::string &name_, multimin_system &ms_)
Definition: multimin.h:38
Definition: multimin.h:294
virtual int eval_df(const double *x_, double *gradient_)
bool is_lock_params() const
bool _lock_params
Definition: multimin.h:172
const param_entry & get_param(int i_) const
bool operator()(const param_entry &pe_)
Definition: multimin.h:151
const std::string & get_param_name(int i_) const
static param_entry make_param_entry_ranged(const std::string &name_, double value_, double min_, double max_, double step_, bool fixed_=false)
virtual int _eval_f(double &f_)=0
virtual int eval_f(const double *x_, double &f_)
Definition: multimin.h:21
virtual int prepare_values()
size_t get_n_iter() const
Definition: multimin.h:118
param_is_auto()
Definition: multimin.h:160
int limit
Definition: multimin.h:47
double get_param_min(int i_) const
int type
Definition: multimin.h:46
bool operator()(const param_entry &pe_)
Definition: multimin.h:125
double get_fval() const
virtual int _auto_values()
double max
Definition: multimin.h:49
Definition: multimin.h:158
void set_value_no_check(double value_)
double step
Definition: multimin.h:50
double get_param_step(int i_) const
bool is_free() const
Definition: multimin.h:69
static void multimin_df(const gsl_vector *v_, void *params_, gsl_vector *gradient_)
param_has_name(const std::string &name_)
Definition: multimin.h:121
size_t get_auto_dimension() const
void set_step_action(at_step_action &asd_)
static param_entry make_param_entry_fixed(const std::string &name_, double value_)
double value
Definition: multimin.h:45
double get_min() const
size_t get_free_dimension() const