gtsam  4.1.0
gtsam
LevenbergMarquardtParams.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
21 #pragma once
22 
25 
26 namespace gtsam {
27 
34 
35 public:
37  enum VerbosityLM {
38  SILENT = 0, SUMMARY, TERMINATION, LAMBDA, TRYLAMBDA, TRYCONFIG, DAMPED, TRYDELTA
39  };
40 
41  static VerbosityLM verbosityLMTranslator(const std::string &s);
42  static std::string verbosityLMTranslator(VerbosityLM value);
43 
44 public:
45 
46  double lambdaInitial;
47  double lambdaFactor;
52  std::string logFile;
55  double minDiagonal;
56  double maxDiagonal;
57 
59  : verbosityLM(SILENT),
60  diagonalDamping(false),
61  minDiagonal(1e-6),
62  maxDiagonal(1e32) {
63  SetLegacyDefaults(this);
64  }
65 
66  static void SetLegacyDefaults(LevenbergMarquardtParams* p) {
67  // Relevant NonlinearOptimizerParams:
68  p->maxIterations = 100;
69  p->relativeErrorTol = 1e-5;
70  p->absoluteErrorTol = 1e-5;
71  // LM-specific:
72  p->lambdaInitial = 1e-5;
73  p->lambdaFactor = 10.0;
74  p->lambdaUpperBound = 1e5;
75  p->lambdaLowerBound = 0.0;
76  p->minModelFidelity = 1e-3;
77  p->diagonalDamping = false;
78  p->useFixedLambdaFactor = true;
79  }
80 
81  // these do seem to work better for SFM
82  static void SetCeresDefaults(LevenbergMarquardtParams* p) {
83  // Relevant NonlinearOptimizerParams:
84  p->maxIterations = 50;
85  p->absoluteErrorTol = 0; // No corresponding option in CERES
86  p->relativeErrorTol = 1e-6; // This is function_tolerance
87  // LM-specific:
88  p->lambdaUpperBound = 1e32;
89  p->lambdaLowerBound = 1e-16;
90  p->lambdaInitial = 1e-04;
91  p->lambdaFactor = 2.0;
92  p->minModelFidelity = 1e-3; // options.min_relative_decrease in CERES
93  p->diagonalDamping = true;
94  p->useFixedLambdaFactor = false; // This is important
95  }
96 
97  static LevenbergMarquardtParams LegacyDefaults() {
98  LevenbergMarquardtParams p;
99  SetLegacyDefaults(&p);
100  return p;
101  }
102 
103  static LevenbergMarquardtParams CeresDefaults() {
104  LevenbergMarquardtParams p;
105  SetCeresDefaults(&p);
106  return p;
107  }
108 
109  static LevenbergMarquardtParams EnsureHasOrdering(LevenbergMarquardtParams params,
110  const NonlinearFactorGraph& graph) {
111  if (!params.ordering)
112  params.ordering = Ordering::Create(params.orderingType, graph);
113  return params;
114  }
115 
116  static LevenbergMarquardtParams ReplaceOrdering(LevenbergMarquardtParams params,
117  const Ordering& ordering) {
118  params.ordering = ordering;
119  return params;
120  }
121 
122  virtual ~LevenbergMarquardtParams() {}
123  void print(const std::string& str = "") const override;
124 
127  bool getDiagonalDamping() const { return diagonalDamping; }
128  double getlambdaFactor() const { return lambdaFactor; }
129  double getlambdaInitial() const { return lambdaInitial; }
130  double getlambdaLowerBound() const { return lambdaLowerBound; }
131  double getlambdaUpperBound() const { return lambdaUpperBound; }
132  bool getUseFixedLambdaFactor() { return useFixedLambdaFactor; }
133  std::string getLogFile() const { return logFile; }
134  std::string getVerbosityLM() const { return verbosityLMTranslator(verbosityLM);}
135 
136  void setDiagonalDamping(bool flag) { diagonalDamping = flag; }
137  void setlambdaFactor(double value) { lambdaFactor = value; }
138  void setlambdaInitial(double value) { lambdaInitial = value; }
139  void setlambdaLowerBound(double value) { lambdaLowerBound = value; }
140  void setlambdaUpperBound(double value) { lambdaUpperBound = value; }
141  void setUseFixedLambdaFactor(bool flag) { useFixedLambdaFactor = flag;}
142  void setLogFile(const std::string& s) { logFile = s; }
143  void setVerbosityLM(const std::string& s) { verbosityLM = verbosityLMTranslator(s);}
144  // @}
147 
149  boost::shared_ptr<NonlinearOptimizerParams> clone() const {
150  return boost::shared_ptr<NonlinearOptimizerParams>(new LevenbergMarquardtParams(*this));
151  }
152 
154 };
155 
156 }
gtsam::NonlinearOptimizerParams::relativeErrorTol
double relativeErrorTol
The maximum relative error decrease to stop iterating (default 1e-5)
Definition: NonlinearOptimizerParams.h:42
gtsam::LevenbergMarquardtParams::lambdaLowerBound
double lambdaLowerBound
The minimum lambda used in LM (default: 0)
Definition: LevenbergMarquardtParams.h:49
gtsam::LevenbergMarquardtParams::useFixedLambdaFactor
bool useFixedLambdaFactor
if true applies constant increase (or decrease) to lambda according to lambdaFactor
Definition: LevenbergMarquardtParams.h:54
gtsam::NonlinearOptimizerParams
The common parameters for Nonlinear optimizers.
Definition: NonlinearOptimizerParams.h:34
NonlinearFactorGraph.h
Factor Graph Constsiting of non-linear factors.
gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
gtsam::NonlinearOptimizerParams::maxIterations
size_t maxIterations
The maximum iterations to stop iterating (default 100)
Definition: NonlinearOptimizerParams.h:41
gtsam::LevenbergMarquardtParams::logFile
std::string logFile
an optional CSV log file, with [iteration, time, error, lambda]
Definition: LevenbergMarquardtParams.h:52
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
gtsam::LevenbergMarquardtParams::verbosityLM
VerbosityLM verbosityLM
The verbosity level for Levenberg-Marquardt (default: SILENT), see also NonlinearOptimizerParams::ver...
Definition: LevenbergMarquardtParams.h:50
gtsam::NonlinearOptimizerParams::absoluteErrorTol
double absoluteErrorTol
The maximum absolute error decrease to stop iterating (default 1e-5)
Definition: NonlinearOptimizerParams.h:43
NonlinearOptimizerParams.h
Parameters for nonlinear optimization.
gtsam::LevenbergMarquardtParams::VerbosityLM
VerbosityLM
See LevenbergMarquardtParams::lmVerbosity.
Definition: LevenbergMarquardtParams.h:37
gtsam::LevenbergMarquardtParams::diagonalDamping
bool diagonalDamping
if true, use diagonal of Hessian
Definition: LevenbergMarquardtParams.h:53
gtsam::LevenbergMarquardtParams::minModelFidelity
double minModelFidelity
Lower bound for the modelFidelity to accept the result of an LM iteration.
Definition: LevenbergMarquardtParams.h:51
gtsam::LevenbergMarquardtParams
Parameters for Levenberg-Marquardt optimization.
Definition: LevenbergMarquardtParams.h:33
gtsam::LevenbergMarquardtParams::lambdaFactor
double lambdaFactor
The amount by which to multiply or divide lambda when adjusting lambda (default: 10....
Definition: LevenbergMarquardtParams.h:47
gtsam::LevenbergMarquardtParams::maxDiagonal
double maxDiagonal
when using diagonal damping saturates the maximum diagonal entries (default: 1e32)
Definition: LevenbergMarquardtParams.h:56
gtsam::LevenbergMarquardtParams::lambdaInitial
double lambdaInitial
The initial Levenberg-Marquardt damping term (default: 1e-5)
Definition: LevenbergMarquardtParams.h:46
gtsam::LevenbergMarquardtParams::clone
boost::shared_ptr< NonlinearOptimizerParams > clone() const
Definition: LevenbergMarquardtParams.h:149
gtsam::LevenbergMarquardtParams::lambdaUpperBound
double lambdaUpperBound
The maximum lambda to try before assuming the optimization has failed (default: 1e5)
Definition: LevenbergMarquardtParams.h:48
gtsam::LevenbergMarquardtParams::minDiagonal
double minDiagonal
when using diagonal damping saturates the minimum diagonal entries (default: 1e-6)
Definition: LevenbergMarquardtParams.h:55