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