GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GOptimizerLM.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GOptimizerLM.hpp - Levenberg Marquardt optimizer *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2009-2017 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GOptimizerLM.hpp
23  * @brief Levenberg Marquardt optimizer class interface definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GOPTIMIZERLM_HPP
28 #define GOPTIMIZERLM_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <vector>
32 #include "GOptimizer.hpp"
33 #include "GOptimizerFunction.hpp"
34 #include "GLog.hpp"
35 
36 /* __ Definitions ________________________________________________________ */
37 #define G_LM_CONVERGED 0
38 #define G_LM_STALLED 1
39 #define G_LM_SINGULAR 2
40 #define G_LM_NOT_POSTIVE_DEFINITE 3
41 #define G_LM_BAD_ERRORS 4
42 
43 
44 /***********************************************************************//**
45  * @class GOptimizerLM
46  *
47  * @brief Levenberg Marquardt optimizer class
48  *
49  * This method implements an Levenberg Marquardt optimizer.
50  ***************************************************************************/
51 class GOptimizerLM : public GOptimizer {
52 
53 public:
54 
55  // Constructors and destructors
56  GOptimizerLM(void);
57  explicit GOptimizerLM(GLog* log);
58  GOptimizerLM(const GOptimizerLM& opt);
59  virtual ~GOptimizerLM(void);
60 
61  // Operators
62  GOptimizerLM& operator=(const GOptimizerLM& opt);
63 
64  // Implemented pure virtual base class methods
65  virtual void clear(void);
66  virtual GOptimizerLM* clone(void) const;
67  virtual std::string classname(void) const;
68  virtual void optimize(GOptimizerFunction& fct, GOptimizerPars& pars);
69  virtual void errors(GOptimizerFunction& fct, GOptimizerPars& pars);
70  virtual double value(void) const;
71  virtual int status(void) const;
72  virtual int iter(void) const;
73  virtual std::string print(const GChatter& chatter = NORMAL) const;
74 
75  // Methods
76  void logger(GLog* log);
77  void max_iter(const int& max_iter);
78  void max_stalls(const int& max_stalls);
79  void max_boundary_hits(const int& max_hit);
80  void lambda_start(const double& value);
81  void lambda_inc(const double& value);
82  void lambda_dec(const double& value);
83  void eps(const double& eps);
84  void accept_dec(const double& value);
85  int max_iter(void) const;
86  int max_stalls(void) const;
87  int max_boundary_hits(void) const;
88  const double& lambda_start(void) const;
89  const double& lambda_inc(void) const;
90  const double& lambda_dec(void) const;
91  const double& lambda(void) const;
92  const double& eps(void) const;
93  const double& accept_dec(void) const;
94 
95 protected:
96  // Protected methods
97  void init_members(void);
98  void copy_members(const GOptimizerLM& opt);
99  void free_members(void);
100  double iteration(GOptimizerFunction& fct, GOptimizerPars& pars);
101  double step_size(const GVector& grad, const GOptimizerPars& pars);
102 
103  // Protected members
104  int m_npars; //!< Number of parameters
105  int m_nfree; //!< Number of free parameters
106  double m_lambda_start; //!< Initial start value
107  double m_lambda_inc; //!< Lambda increase
108  double m_lambda_dec; //!< Lambda decrease
109  double m_eps; //!< Absolute precision
110  double m_accept_dec; //!< Acceptable function decrease
111  int m_max_iter; //!< Maximum number of iterations
112  int m_max_stall; //!< Maximum number of stalls
113  int m_max_hit; //!< Maximum number of successive hits
114  int m_max_dec; //!< Maximum number of function decrease
115  bool m_step_adjust; //!< Adjust step size to boundaries
116  std::vector<bool> m_hit_boundary; //!< Bookkeeping array for boundary hits
117  std::vector<int> m_hit_minimum; //!< Bookkeeping of successive minimum hits
118  std::vector<int> m_hit_maximum; //!< Bookkeeping of successive maximum hits
119  std::vector<bool> m_par_freeze; //!< Bookkeeping of parameter freeze
120  std::vector<bool> m_par_remove; //!< Bookkeeping of parameter removal
121  double m_lambda; //!< Actual lambda
122  double m_value; //!< Actual function value
123  double m_delta; //!< Function improvement
124  int m_status; //!< Fit status
125  int m_iter; //!< Iteration
126  int m_num_dec; //!< Number of function decreases
127  GLog* m_logger; //!< Pointer to optional logger
128 
129 };
130 
131 
132 /***********************************************************************//**
133  * @brief Return class name
134  *
135  * @return String containing the class name ("GOptimizerLM").
136  ***************************************************************************/
137 inline
138 std::string GOptimizerLM::classname(void) const
139 {
140  return ("GOptimizerLM");
141 }
142 
143 
144 /***********************************************************************//**
145  * @brief Return function value
146  *
147  * @return Function value.
148  ***************************************************************************/
149 inline
150 double GOptimizerLM::value(void) const
151 {
152  return (m_value);
153 }
154 
155 
156 /***********************************************************************//**
157  * @brief Return optimizer status
158  *
159  * @return Optimizer status.
160  ***************************************************************************/
161 inline
162 int GOptimizerLM::status(void) const
163 {
164  return (m_status);
165 }
166 
167 
168 /***********************************************************************//**
169  * @brief Return number of iterations
170  *
171  * @return Number of iterations.
172  ***************************************************************************/
173 inline
174 int GOptimizerLM::iter(void) const
175 {
176  return (m_iter);
177 }
178 
179 
180 /***********************************************************************//**
181  * @brief Set logger
182  *
183  * @param[in] log Logger to use in optimizer.
184  *
185  * Set the logger into which the optimizer will dump any output.
186  ***************************************************************************/
187 inline
189 {
190  m_logger = log;
191  return;
192 }
193 
194 
195 /***********************************************************************//**
196  * @brief Set maximum number of iterations
197  *
198  * @param[in] max_iter Maximum number of iterations.
199  ***************************************************************************/
200 inline
201 void GOptimizerLM::max_iter(const int& max_iter)
202 {
204  return;
205 }
206 
207 
208 /***********************************************************************//**
209  * @brief Set maximum number of allowed subsequent stalls
210  *
211  * @param[in] max_stalls Maximum number of allowed subsequent stalls.
212  ***************************************************************************/
213 inline
214 void GOptimizerLM::max_stalls(const int& max_stalls)
215 {
217  return;
218 }
219 
220 
221 /***********************************************************************//**
222  * @brief Set maximum number of parameter boundary hits
223  *
224  * @param[in] max_hit Maximum number of parameter boundary hits.
225  ***************************************************************************/
226 inline
227 void GOptimizerLM::max_boundary_hits(const int& max_hit)
228 {
229  m_max_hit = max_hit;
230  return;
231 }
232 
233 
234 /***********************************************************************//**
235  * @brief Set lambda starting value
236  *
237  * @param[in] value Lambda starting value.
238  ***************************************************************************/
239 inline
240 void GOptimizerLM::lambda_start(const double& value)
241 {
243  return;
244 }
245 
246 
247 /***********************************************************************//**
248  * @brief Set lambda increment value
249  *
250  * @param[in] value Lambda increment value.
251  ***************************************************************************/
252 inline
253 void GOptimizerLM::lambda_inc(const double& value)
254 {
256  return;
257 }
258 
259 
260 /***********************************************************************//**
261  * @brief Set lambda decrement value
262  *
263  * @param[in] value Lambda decrement value.
264  ***************************************************************************/
265 inline
266 void GOptimizerLM::lambda_dec(const double& value)
267 {
269  return;
270 }
271 
272 
273 /***********************************************************************//**
274  * @brief Set requested absolute convergence precision
275  *
276  * @param[in] eps Requested absolute convergence precision.
277  ***************************************************************************/
278 inline
279 void GOptimizerLM::eps(const double& eps)
280 {
281  m_eps = eps;
282  return;
283 }
284 
285 
286 /***********************************************************************//**
287  * @brief Set acceptable function decrease
288  *
289  * @param[in] value Acceptable function decrease.
290  *
291  * Sets the acceptable function decrease value for which the new solution
292  * will be kept and the iterations continue. This strategy provides better
293  * convergence in case that a function decrease is encountered.
294  ***************************************************************************/
295 inline
296 void GOptimizerLM::accept_dec(const double& value)
297 {
299  return;
300 }
301 
302 
303 
304 
305 /***********************************************************************//**
306  * @brief Return maximum number of iterations
307  *
308  * @return Maximum number of iterations.
309  ***************************************************************************/
310 inline
311 int GOptimizerLM::max_iter(void) const
312 {
313  return (m_max_iter);
314 }
315 
316 
317 /***********************************************************************//**
318  * @brief Return maximum number of allowed subsequent stalls
319  *
320  * @return Maximum number of allowed subsequent stalls.
321  ***************************************************************************/
322 inline
324 {
325  return (m_max_stall);
326 }
327 
328 
329 /***********************************************************************//**
330  * @brief Return maximum number of parameter boundary hits
331  *
332  * @return Maximum number of parameter boundary hits.
333  ***************************************************************************/
334 inline
336 {
337  return (m_max_hit);
338 }
339 
340 
341 /***********************************************************************//**
342  * @brief Return lambda starting value
343  *
344  * @return Lambda starting value.
345  ***************************************************************************/
346 inline
347 const double& GOptimizerLM::lambda_start(void) const
348 {
349  return (m_lambda_start);
350 }
351 
352 
353 /***********************************************************************//**
354  * @brief Return lambda increment value
355  *
356  * @return Lambda increment value.
357  ***************************************************************************/
358 inline
359 const double& GOptimizerLM::lambda_inc(void) const
360 {
361  return (m_lambda_inc);
362 }
363 
364 
365 /***********************************************************************//**
366  * @brief Return lambda decrement value
367  *
368  * @return Lambda decrement value.
369  ***************************************************************************/
370 inline
371 const double& GOptimizerLM::lambda_dec(void) const
372 {
373  return (m_lambda_dec);
374 }
375 
376 
377 /***********************************************************************//**
378  * @brief Return lambda value
379  *
380  * @return Lambda value.
381  ***************************************************************************/
382 inline
383 const double& GOptimizerLM::lambda(void) const
384 {
385  return (m_lambda);
386 }
387 
388 
389 /***********************************************************************//**
390  * @brief Return requested absolute convergence precision
391  *
392  * @return Requested absolute convergence precision.
393  ***************************************************************************/
394 inline
395 const double& GOptimizerLM::eps(void) const
396 {
397  return (m_eps);
398 }
399 
400 
401 /***********************************************************************//**
402  * @brief Return acceptable function decrease
403  *
404  * @return Acceptable function decrease.
405  ***************************************************************************/
406 inline
407 const double& GOptimizerLM::accept_dec(void) const
408 {
409  return (m_accept_dec);
410 }
411 
412 #endif /* GOPTIMIZERLM_HPP */
GOptimizerLM & operator=(const GOptimizerLM &opt)
Assignment operator.
const double & lambda_inc(void) const
Return lambda increment value.
double step_size(const GVector &grad, const GOptimizerPars &pars)
Return LM step size.
Optimizer function abstract base class.
void init_members(void)
Initialise class members.
virtual double value(void) const
Return function value.
Optimizer parameter container class.
virtual void optimize(GOptimizerFunction &fct, GOptimizerPars &pars)
Optimize function parameters.
double m_lambda_start
Initial start value.
double m_lambda
Actual lambda.
int max_boundary_hits(void) const
Return maximum number of parameter boundary hits.
void copy_members(const GOptimizerLM &opt)
Copy class members.
int m_iter
Iteration.
double m_eps
Absolute precision.
std::vector< bool > m_hit_boundary
Bookkeeping array for boundary hits.
int m_npars
Number of parameters.
const double & lambda(void) const
Return lambda value.
GOptimizerLM(void)
Void constructor.
std::vector< bool > m_par_remove
Bookkeeping of parameter removal.
int m_max_dec
Maximum number of function decrease.
double m_value
Actual function value.
Abstract optimizer abstract base class interface definition.
const double & lambda_start(void) const
Return lambda starting value.
GLog * m_logger
Pointer to optional logger.
Information logger interface definition.
Definition: GLog.hpp:62
Optimizer function abstract base class.
virtual GOptimizerLM * clone(void) const
Clone object.
int m_nfree
Number of free parameters.
bool m_step_adjust
Adjust step size to boundaries.
int m_status
Fit status.
virtual void clear(void)
Clear object.
double m_delta
Function improvement.
double m_accept_dec
Acceptable function decrease.
GVector log(const GVector &vector)
Computes natural logarithm of vector elements.
Definition: GVector.cpp:1184
virtual ~GOptimizerLM(void)
Destructor.
double m_lambda_dec
Lambda decrease.
Abstract optimizer abstract base class.
Definition: GOptimizer.hpp:54
const double & lambda_dec(void) const
Return lambda decrement value.
std::vector< bool > m_par_freeze
Bookkeeping of parameter freeze.
GChatter
Definition: GTypemaps.hpp:33
double m_lambda_inc
Lambda increase.
const double & accept_dec(void) const
Return acceptable function decrease.
int m_num_dec
Number of function decreases.
void free_members(void)
Delete class members.
int m_max_iter
Maximum number of iterations.
int m_max_hit
Maximum number of successive hits.
std::vector< int > m_hit_maximum
Bookkeeping of successive maximum hits.
void logger(GLog *log)
Set logger.
Information logger class definition.
virtual int iter(void) const
Return number of iterations.
const double & eps(void) const
Return requested absolute convergence precision.
virtual void errors(GOptimizerFunction &fct, GOptimizerPars &pars)
Compute parameter uncertainties.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print optimizer information.
Vector class.
Definition: GVector.hpp:46
std::vector< int > m_hit_minimum
Bookkeeping of successive minimum hits.
virtual std::string classname(void) const
Return class name.
Levenberg Marquardt optimizer class.
int m_max_stall
Maximum number of stalls.
int max_stalls(void) const
Return maximum number of allowed subsequent stalls.
virtual int status(void) const
Return optimizer status.
int max_iter(void) const
Return maximum number of iterations.
double iteration(GOptimizerFunction &fct, GOptimizerPars &pars)
Perform one LM iteration.