gtsam  4.1.0
gtsam
NonlinearOptimizerParams.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 
22 #pragma once
23 
26 #include <boost/optional.hpp>
27 #include <string>
28 
29 namespace gtsam {
30 
34 class GTSAM_EXPORT NonlinearOptimizerParams {
35 public:
37  enum Verbosity {
38  SILENT, TERMINATION, ERROR, VALUES, DELTA, LINEAR
39  };
40 
41  size_t maxIterations = 100;
42  double relativeErrorTol = 1e-5;
43  double absoluteErrorTol = 1e-5;
44  double errorTol = 0.0;
45  Verbosity verbosity = SILENT;
46  Ordering::OrderingType orderingType = Ordering::COLAMD;
47 
48  size_t getMaxIterations() const { return maxIterations; }
49  double getRelativeErrorTol() const { return relativeErrorTol; }
50  double getAbsoluteErrorTol() const { return absoluteErrorTol; }
51  double getErrorTol() const { return errorTol; }
52  std::string getVerbosity() const { return verbosityTranslator(verbosity); }
53 
54  void setMaxIterations(int value) { maxIterations = value; }
55  void setRelativeErrorTol(double value) { relativeErrorTol = value; }
56  void setAbsoluteErrorTol(double value) { absoluteErrorTol = value; }
57  void setErrorTol(double value) { errorTol = value; }
58  void setVerbosity(const std::string& src) {
59  verbosity = verbosityTranslator(src);
60  }
61 
62  static Verbosity verbosityTranslator(const std::string &s) ;
63  static std::string verbosityTranslator(Verbosity value) ;
64 
67  using IterationHook = std::function<
68  void(size_t /*iteration*/, double/*errorBefore*/, double/*errorAfter*/)>;
69 
95 
98  MULTIFRONTAL_CHOLESKY,
99  MULTIFRONTAL_QR,
100  SEQUENTIAL_CHOLESKY,
101  SEQUENTIAL_QR,
102  Iterative, /* Experimental Flag */
103  CHOLMOD, /* Experimental Flag */
104  };
105 
106  LinearSolverType linearSolverType = MULTIFRONTAL_CHOLESKY;
107  boost::optional<Ordering> ordering;
108  IterativeOptimizationParameters::shared_ptr iterativeParams;
109 
110  NonlinearOptimizerParams() = default;
111  virtual ~NonlinearOptimizerParams() {
112  }
113 
114  virtual void print(const std::string& str = "") const;
115 
116  inline bool isMultifrontal() const {
117  return (linearSolverType == MULTIFRONTAL_CHOLESKY)
118  || (linearSolverType == MULTIFRONTAL_QR);
119  }
120 
121  inline bool isSequential() const {
122  return (linearSolverType == SEQUENTIAL_CHOLESKY)
123  || (linearSolverType == SEQUENTIAL_QR);
124  }
125 
126  inline bool isCholmod() const {
127  return (linearSolverType == CHOLMOD);
128  }
129 
130  inline bool isIterative() const {
131  return (linearSolverType == Iterative);
132  }
133 
134  GaussianFactorGraph::Eliminate getEliminationFunction() const {
135  switch (linearSolverType) {
136  case MULTIFRONTAL_CHOLESKY:
137  case SEQUENTIAL_CHOLESKY:
138  return EliminatePreferCholesky;
139 
140  case MULTIFRONTAL_QR:
141  case SEQUENTIAL_QR:
142  return EliminateQR;
143 
144  default:
145  throw std::runtime_error(
146  "Nonlinear optimization parameter \"factorization\" is invalid");
147  }
148  }
149 
150  std::string getLinearSolverType() const {
151  return linearSolverTranslator(linearSolverType);
152  }
153 
154  void setLinearSolverType(const std::string& solver) {
155  linearSolverType = linearSolverTranslator(solver);
156  }
157 
158  void setIterativeParams(const boost::shared_ptr<IterativeOptimizationParameters> params);
159 
160  void setOrdering(const Ordering& ordering) {
161  this->ordering = ordering;
162  this->orderingType = Ordering::CUSTOM;
163  }
164 
165  std::string getOrderingType() const {
166  return orderingTypeTranslator(orderingType);
167  }
168 
169  // Note that if you want to use a custom ordering, you must set the ordering directly, this will switch to custom type
170  void setOrderingType(const std::string& ordering){
171  orderingType = orderingTypeTranslator(ordering);
172  }
173 
174 private:
175  std::string linearSolverTranslator(LinearSolverType linearSolverType) const;
176  LinearSolverType linearSolverTranslator(const std::string& linearSolverType) const;
177  std::string orderingTypeTranslator(Ordering::OrderingType type) const;
178  Ordering::OrderingType orderingTypeTranslator(const std::string& type) const;
179 };
180 
181 // For backward compatibility:
182 typedef NonlinearOptimizerParams SuccessiveLinearizationParams;
183 
184 } /* namespace gtsam */
gtsam::NonlinearOptimizerParams::iterationHook
IterationHook iterationHook
Optional user-provided iteration hook to be called after each optimization iteration (Default: none).
Definition: NonlinearOptimizerParams.h:94
gtsam::Ordering::OrderingType
OrderingType
Type of ordering to use.
Definition: Ordering.h:41
GaussianFactorGraph.h
Linear Factor Graph where all factors are Gaussians.
gtsam::NonlinearOptimizerParams::ordering
boost::optional< Ordering > ordering
The optional variable elimination ordering, or empty to use COLAMD (default: empty)
Definition: NonlinearOptimizerParams.h:107
gtsam::NonlinearOptimizerParams::iterativeParams
IterativeOptimizationParameters::shared_ptr iterativeParams
The container for iterativeOptimization parameters. used in CG Solvers.
Definition: NonlinearOptimizerParams.h:108
gtsam::EliminateQR
std::pair< GaussianConditional::shared_ptr, JacobianFactor::shared_ptr > EliminateQR(const GaussianFactorGraph &factors, const Ordering &keys)
Multiply all factors and eliminate the given keys from the resulting factor using a QR variant that h...
Definition: JacobianFactor.cpp:797
gtsam::NonlinearOptimizerParams
The common parameters for Nonlinear optimizers.
Definition: NonlinearOptimizerParams.h:34
gtsam::NonlinearOptimizerParams::IterationHook
std::function< void(size_t, double, double)> IterationHook
Type for an optional user-provided hook to be called after each internal optimizer iteration.
Definition: NonlinearOptimizerParams.h:68
gtsam::EliminateableFactorGraph< GaussianFactorGraph >::Eliminate
boost::function< EliminationResult(const FactorGraphType &, const Ordering &)> Eliminate
The function type that does a single dense elimination step on a subgraph.
Definition: EliminateableFactorGraph.h:89
gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
SubgraphSolver.h
Subgraph Solver from IROS 2010.
gtsam::NonlinearOptimizerParams::Verbosity
Verbosity
See NonlinearOptimizerParams::verbosity.
Definition: NonlinearOptimizerParams.h:37
gtsam::NonlinearOptimizerParams::LinearSolverType
LinearSolverType
See NonlinearOptimizerParams::linearSolverType.
Definition: NonlinearOptimizerParams.h:97