GammaLib 2.1.0.dev
Loading...
Searching...
No Matches
GOptimizerLM.hpp
Go to the documentation of this file.
1/***************************************************************************
2 * GOptimizerLM.hpp - Levenberg Marquardt optimizer *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2009-2023 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"
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 ***************************************************************************/
51class GOptimizerLM : public GOptimizer {
52
53public:
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
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 const int& npars(void) const;
86 const int& nfree(void) const;
87 const int& max_iter(void) const;
88 const int& max_stalls(void) const;
89 const int& max_boundary_hits(void) const;
90 const double& lambda_start(void) const;
91 const double& lambda_inc(void) const;
92 const double& lambda_dec(void) const;
93 const double& lambda(void) const;
94 const double& eps(void) const;
95 const double& accept_dec(void) const;
96 std::string status_string(void) const;
97
98protected:
99 // Protected methods
100 void init_members(void);
101 void copy_members(const GOptimizerLM& opt);
102 void free_members(void);
103 double iteration(GOptimizerFunction& fct, GOptimizerPars& pars);
104 double step_size(const GVector& grad, const GOptimizerPars& pars);
105
106 // Protected members
107 int m_npars; //!< Number of parameters
108 int m_nfree; //!< Number of free parameters
109 double m_lambda_start; //!< Initial start value
110 double m_lambda_inc; //!< Lambda increase
111 double m_lambda_dec; //!< Lambda decrease
112 double m_eps; //!< Absolute precision
113 double m_accept_dec; //!< Acceptable function decrease
114 int m_max_iter; //!< Maximum number of iterations
115 int m_max_stall; //!< Maximum number of stalls
116 int m_max_hit; //!< Maximum number of successive hits
117 int m_max_dec; //!< Maximum number of function decrease
118 bool m_step_adjust; //!< Adjust step size to boundaries
119 std::vector<bool> m_hit_boundary; //!< Bookkeeping array for boundary hits
120 std::vector<int> m_hit_minimum; //!< Bookkeeping of successive minimum hits
121 std::vector<int> m_hit_maximum; //!< Bookkeeping of successive maximum hits
122 std::vector<bool> m_par_freeze; //!< Bookkeeping of parameter freeze
123 std::vector<bool> m_par_remove; //!< Bookkeeping of parameter removal
124 double m_lambda; //!< Actual lambda
125 double m_value; //!< Actual function value
126 double m_delta; //!< Function improvement
127 int m_status; //!< Fit status
128 int m_iter; //!< Iteration
129 int m_num_dec; //!< Number of function decreases
130 GLog* m_logger; //!< Pointer to optional logger
131
132};
133
134
135/***********************************************************************//**
136 * @brief Return class name
137 *
138 * @return String containing the class name ("GOptimizerLM").
139 ***************************************************************************/
140inline
141std::string GOptimizerLM::classname(void) const
142{
143 return ("GOptimizerLM");
144}
145
146
147/***********************************************************************//**
148 * @brief Return function value
149 *
150 * @return Function value.
151 ***************************************************************************/
152inline
153double GOptimizerLM::value(void) const
154{
155 return (m_value);
156}
157
158
159/***********************************************************************//**
160 * @brief Return optimizer status
161 *
162 * @return Optimizer status.
163 ***************************************************************************/
164inline
165int GOptimizerLM::status(void) const
166{
167 return (m_status);
168}
169
170
171/***********************************************************************//**
172 * @brief Return number of iterations
173 *
174 * @return Number of iterations.
175 ***************************************************************************/
176inline
177int GOptimizerLM::iter(void) const
178{
179 return (m_iter);
180}
181
182
183/***********************************************************************//**
184 * @brief Set logger
185 *
186 * @param[in] log Logger to use in optimizer.
187 *
188 * Set the logger into which the optimizer will dump any output.
189 ***************************************************************************/
190inline
192{
193 m_logger = log;
194 return;
195}
196
197
198/***********************************************************************//**
199 * @brief Set maximum number of iterations
200 *
201 * @param[in] max_iter Maximum number of iterations.
202 ***************************************************************************/
203inline
204void GOptimizerLM::max_iter(const int& max_iter)
205{
207 return;
208}
209
210
211/***********************************************************************//**
212 * @brief Set maximum number of allowed subsequent stalls
213 *
214 * @param[in] max_stalls Maximum number of allowed subsequent stalls.
215 ***************************************************************************/
216inline
217void GOptimizerLM::max_stalls(const int& max_stalls)
218{
220 return;
221}
222
223
224/***********************************************************************//**
225 * @brief Set maximum number of parameter boundary hits
226 *
227 * @param[in] max_hit Maximum number of parameter boundary hits.
228 ***************************************************************************/
229inline
230void GOptimizerLM::max_boundary_hits(const int& max_hit)
231{
232 m_max_hit = max_hit;
233 return;
234}
235
236
237/***********************************************************************//**
238 * @brief Set lambda starting value
239 *
240 * @param[in] value Lambda starting value.
241 ***************************************************************************/
242inline
243void GOptimizerLM::lambda_start(const double& value)
244{
246 return;
247}
248
249
250/***********************************************************************//**
251 * @brief Set lambda increment value
252 *
253 * @param[in] value Lambda increment value.
254 ***************************************************************************/
255inline
256void GOptimizerLM::lambda_inc(const double& value)
257{
259 return;
260}
261
262
263/***********************************************************************//**
264 * @brief Set lambda decrement value
265 *
266 * @param[in] value Lambda decrement value.
267 ***************************************************************************/
268inline
269void GOptimizerLM::lambda_dec(const double& value)
270{
272 return;
273}
274
275
276/***********************************************************************//**
277 * @brief Set requested absolute convergence precision
278 *
279 * @param[in] eps Requested absolute convergence precision.
280 ***************************************************************************/
281inline
282void GOptimizerLM::eps(const double& eps)
283{
284 m_eps = eps;
285 return;
286}
287
288
289/***********************************************************************//**
290 * @brief Set acceptable function decrease
291 *
292 * @param[in] value Acceptable function decrease.
293 *
294 * Sets the acceptable function decrease value for which the new solution
295 * will be kept and the iterations continue. This strategy provides better
296 * convergence in case that a function decrease is encountered.
297 ***************************************************************************/
298inline
299void GOptimizerLM::accept_dec(const double& value)
300{
302 return;
303}
304
305
306/***********************************************************************//**
307 * @brief Return number of model parameters
308 *
309 * @return Number of model parameters.
310 ***************************************************************************/
311inline
312const int& GOptimizerLM::npars(void) const
313{
314 return (m_npars);
315}
316
317
318/***********************************************************************//**
319 * @brief Return number of free model parameters
320 *
321 * @return Number of free model parameters.
322 ***************************************************************************/
323inline
324const int& GOptimizerLM::nfree(void) const
325{
326 return (m_nfree);
327}
328
329
330/***********************************************************************//**
331 * @brief Return maximum number of iterations
332 *
333 * @return Maximum number of iterations.
334 ***************************************************************************/
335inline
336const int& GOptimizerLM::max_iter(void) const
337{
338 return (m_max_iter);
339}
340
341
342/***********************************************************************//**
343 * @brief Return maximum number of allowed subsequent stalls
344 *
345 * @return Maximum number of allowed subsequent stalls.
346 ***************************************************************************/
347inline
348const int& GOptimizerLM::max_stalls(void) const
349{
350 return (m_max_stall);
351}
352
353
354/***********************************************************************//**
355 * @brief Return maximum number of parameter boundary hits
356 *
357 * @return Maximum number of parameter boundary hits.
358 ***************************************************************************/
359inline
361{
362 return (m_max_hit);
363}
364
365
366/***********************************************************************//**
367 * @brief Return lambda starting value
368 *
369 * @return Lambda starting value.
370 ***************************************************************************/
371inline
372const double& GOptimizerLM::lambda_start(void) const
373{
374 return (m_lambda_start);
375}
376
377
378/***********************************************************************//**
379 * @brief Return lambda increment value
380 *
381 * @return Lambda increment value.
382 ***************************************************************************/
383inline
384const double& GOptimizerLM::lambda_inc(void) const
385{
386 return (m_lambda_inc);
387}
388
389
390/***********************************************************************//**
391 * @brief Return lambda decrement value
392 *
393 * @return Lambda decrement value.
394 ***************************************************************************/
395inline
396const double& GOptimizerLM::lambda_dec(void) const
397{
398 return (m_lambda_dec);
399}
400
401
402/***********************************************************************//**
403 * @brief Return lambda value
404 *
405 * @return Lambda value.
406 ***************************************************************************/
407inline
408const double& GOptimizerLM::lambda(void) const
409{
410 return (m_lambda);
411}
412
413
414/***********************************************************************//**
415 * @brief Return requested absolute convergence precision
416 *
417 * @return Requested absolute convergence precision.
418 ***************************************************************************/
419inline
420const double& GOptimizerLM::eps(void) const
421{
422 return (m_eps);
423}
424
425
426/***********************************************************************//**
427 * @brief Return acceptable function decrease
428 *
429 * @return Acceptable function decrease.
430 ***************************************************************************/
431inline
432const double& GOptimizerLM::accept_dec(void) const
433{
434 return (m_accept_dec);
435}
436
437#endif /* GOPTIMIZERLM_HPP */
Information logger class definition.
Optimizer function abstract base class.
Abstract optimizer abstract base class interface definition.
GChatter
Definition GTypemaps.hpp:33
@ NORMAL
Definition GTypemaps.hpp:36
GVector log(const GVector &vector)
Computes natural logarithm of vector elements.
Definition GVector.cpp:1342
Information logger interface definition.
Definition GLog.hpp:62
Optimizer function abstract base class.
Levenberg Marquardt optimizer class.
bool m_step_adjust
Adjust step size to boundaries.
int m_npars
Number of parameters.
int m_max_iter
Maximum number of iterations.
GLog * m_logger
Pointer to optional logger.
double m_lambda_inc
Lambda increase.
std::string status_string(void) const
Set fit status string.
int m_max_dec
Maximum number of function decrease.
std::vector< int > m_hit_minimum
Bookkeeping of successive minimum hits.
int m_num_dec
Number of function decreases.
std::vector< bool > m_hit_boundary
Bookkeeping array for boundary hits.
virtual GOptimizerLM * clone(void) const
Clone object.
void init_members(void)
Initialise class members.
GOptimizerLM & operator=(const GOptimizerLM &opt)
Assignment operator.
const int & max_stalls(void) const
Return maximum number of allowed subsequent stalls.
void logger(GLog *log)
Set logger.
void free_members(void)
Delete class members.
const double & lambda_inc(void) const
Return lambda increment value.
double m_value
Actual function value.
int m_max_stall
Maximum number of stalls.
double m_lambda_dec
Lambda decrease.
virtual int iter(void) const
Return number of iterations.
const int & max_boundary_hits(void) const
Return maximum number of parameter boundary hits.
std::vector< bool > m_par_freeze
Bookkeeping of parameter freeze.
GOptimizerLM(void)
Void constructor.
double step_size(const GVector &grad, const GOptimizerPars &pars)
Return LM step size.
const double & eps(void) const
Return requested absolute convergence precision.
virtual void errors(GOptimizerFunction &fct, GOptimizerPars &pars)
Compute parameter uncertainties.
double iteration(GOptimizerFunction &fct, GOptimizerPars &pars)
Perform one LM iteration.
void copy_members(const GOptimizerLM &opt)
Copy class members.
double m_eps
Absolute precision.
virtual std::string print(const GChatter &chatter=NORMAL) const
Print optimizer information.
virtual void optimize(GOptimizerFunction &fct, GOptimizerPars &pars)
Optimize function parameters.
std::vector< int > m_hit_maximum
Bookkeeping of successive maximum hits.
int m_status
Fit status.
std::vector< bool > m_par_remove
Bookkeeping of parameter removal.
double m_lambda_start
Initial start value.
const double & lambda(void) const
Return lambda value.
int m_nfree
Number of free parameters.
const int & max_iter(void) const
Return maximum number of iterations.
virtual std::string classname(void) const
Return class name.
double m_delta
Function improvement.
virtual void clear(void)
Clear object.
int m_iter
Iteration.
const int & nfree(void) const
Return number of free model parameters.
const double & lambda_dec(void) const
Return lambda decrement value.
int m_max_hit
Maximum number of successive hits.
const double & lambda_start(void) const
Return lambda starting value.
double m_accept_dec
Acceptable function decrease.
virtual double value(void) const
Return function value.
virtual ~GOptimizerLM(void)
Destructor.
double m_lambda
Actual lambda.
virtual int status(void) const
Return optimizer status.
const double & accept_dec(void) const
Return acceptable function decrease.
const int & npars(void) const
Return number of model parameters.
Optimizer parameter container class.
Abstract optimizer abstract base class.
Vector class.
Definition GVector.hpp:46