gtsam
Loading...
Searching...
No Matches
GncParams.h
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
33
34#pragma once
35
38
39namespace gtsam {
40
41/* ************************************************************************* */
44 GM /*Geman McClure*/,
45 TLS /*Truncated least squares*/
46};
47
50enum class GncScheduler {
51 Linear,
52 SuperLinear
53};
54
55template<class BaseOptimizerParameters>
56class GncParams {
57 public:
59 typedef typename BaseOptimizerParameters::OptimizerType OptimizerType;
60
62 enum Verbosity {
63 SILENT = 0,
64 SUMMARY,
65 LAMBDA,
66 WEIGHTS,
67 VALUES,
68#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
72 MU = LAMBDA,
73#endif
74 };
75
77 GncParams(const BaseOptimizerParameters& baseOptimizerParams)
79 }
80
85
87 BaseOptimizerParameters baseOptimizerParams;
90 GncScheduler scheduler = GncScheduler::Linear;
91 size_t maxIterations = 100;
92 double lambdaStep = 1.4;
93 double relativeCostTol = 1e-5;
94 double weightsTol = 1e-4;
95 double lambdaMax = 1e16;
98
104 IndexVector knownOutliers;
105
107 void setLossType(const GncLossType type) {
108 lossType = type;
109 }
110
113 scheduler = s;
114 }
115
117 void setMaxIterations(const size_t maxIter) {
118 std::cout
119 << "setMaxIterations: changing the max nr of iters might lead to less accurate solutions and is not recommended! "
120 << std::endl;
121 maxIterations = maxIter;
122 }
123
125 void setLambdaStep(const double step) {
126 lambdaStep = step;
127 }
128
130 void setRelativeCostTol(double value) {
131 relativeCostTol = value;
132 }
133
135 void setWeightsTol(double value) {
136 weightsTol = value;
137 }
138
140 void setVerbosityGNC(const Verbosity value) {
141 verbosity = value;
142 }
143
150 void setKnownInliers(const IndexVector& knownIn) {
151 for (size_t i = 0; i < knownIn.size(); i++){
152 knownInliers.push_back(knownIn[i]);
153 }
154 std::sort(knownInliers.begin(), knownInliers.end());
155 }
156
161 void setKnownOutliers(const IndexVector& knownOut) {
162 for (size_t i = 0; i < knownOut.size(); i++){
163 knownOutliers.push_back(knownOut[i]);
164 }
165 std::sort(knownOutliers.begin(), knownOutliers.end());
166 }
167
168 void setAllowNonNoiseModelFactors(bool allow) {
170 }
171
172#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
175 void setMuStep(const double step) { setLambdaStep(step); }
176#endif
177
179 bool equals(const GncParams& other, double tol = 1e-9) const {
180 return baseOptimizerParams.equals(other.baseOptimizerParams)
181 && lossType == other.lossType && maxIterations == other.maxIterations
182 && std::fabs(lambdaStep - other.lambdaStep) <= tol
183 && scheduler == other.scheduler
184 && verbosity == other.verbosity && knownInliers == other.knownInliers
185 && knownOutliers == other.knownOutliers
187 }
188
190 void print(const std::string& str) const {
191 std::cout << str << "\n";
192 switch (lossType) {
193 case GM:
194 std::cout << "lossType: Geman McClure" << "\n";
195 break;
196 case TLS:
197 std::cout << "lossType: Truncated Least-squares" << "\n";
198 break;
199 default:
200 throw std::runtime_error("GncParams::print: unknown loss type.");
201 }
202 switch (scheduler) {
203 case GncScheduler::Linear:
204 std::cout << "scheduler: Linear" << "\n";
205 break;
206 case GncScheduler::SuperLinear:
207 std::cout << "scheduler: SuperLinear" << "\n";
208 break;
209 default:
210 throw std::runtime_error("GncParams::print: unknown scheduler type.");
211 }
212 std::cout << "maxIterations: " << maxIterations << "\n";
213 std::cout << "lambdaStep: " << lambdaStep << "\n";
214 std::cout << "relativeCostTol: " << relativeCostTol << "\n";
215 std::cout << "weightsTol: " << weightsTol << "\n";
216 std::cout << "verbosity: " << verbosity << "\n";
217 for (size_t i = 0; i < knownInliers.size(); i++)
218 std::cout << "knownInliers: " << knownInliers[i] << "\n";
219 for (size_t i = 0; i < knownOutliers.size(); i++)
220 std::cout << "knownOutliers: " << knownOutliers[i] << "\n";
221 std::cout << "allowNonNoiseModelFactors: " << allowNonNoiseModelFactors << "\n";
222 baseOptimizerParams.print("Base optimizer params: ");
223 }
224};
225
226}
A nonlinear optimizer that uses the Levenberg-Marquardt trust-region scheme.
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
Global functions in a separate testing namespace.
Definition chartTesting.h:28
GncLossType
Choice of robust loss function for GNC.
Definition GncParams.h:43
GncScheduler
Choice of GNC scheduling strategy.
Definition GncParams.h:50
BaseOptimizerParameters baseOptimizerParams
GNC parameters.
Definition GncParams.h:87
double lambdaMax
Maximum value of lambda in GNC, acts as a cap (only for TLS).
Definition GncParams.h:95
IndexVector knownInliers
Slots in the factor graph corresponding to measurements that we know are outliers.
Definition GncParams.h:102
GncParams()
Default constructor.
Definition GncParams.h:82
BaseOptimizerParameters::OptimizerType OptimizerType
For each parameter, specify the corresponding optimizer: e.g., GaussNewtonParams -> GaussNewtonOptimi...
Definition GncParams.h:59
void setKnownInliers(const IndexVector &knownIn)
(Optional) Provide a vector of measurements that must be considered inliers.
Definition GncParams.h:150
void print(const std::string &str) const
Print.
Definition GncParams.h:190
double lambdaStep
Multiplicative factor to reduce/increase the lambda in gnc.
Definition GncParams.h:92
FastVector< uint64_t > IndexVector
Use IndexVector for inliers and outliers since it is fast.
Definition GncParams.h:100
GncParams(const BaseOptimizerParameters &baseOptimizerParams)
Constructor.
Definition GncParams.h:77
void setLambdaStep(const double step)
Set the graduated non-convexity step: at each GNC iteration, lambda is updated as lambda <- lambda * ...
Definition GncParams.h:125
double weightsTol
If the weights are within weightsTol from being binary, stop iterating (only for TLS).
Definition GncParams.h:94
void setKnownOutliers(const IndexVector &knownOut)
(Optional) Provide a vector of measurements that must be considered outliers.
Definition GncParams.h:161
Verbosity verbosity
Verbosity level.
Definition GncParams.h:96
bool equals(const GncParams &other, double tol=1e-9) const
Equals.
Definition GncParams.h:179
void setRelativeCostTol(double value)
Set the maximum relative difference in lambda values to stop iterating.
Definition GncParams.h:130
GncLossType lossType
any other specific GNC parameters:
Definition GncParams.h:89
bool allowNonNoiseModelFactors
If true, factors without noise model are not reweighted and not not included in lambda calculation.
Definition GncParams.h:97
size_t maxIterations
Maximum number of iterations.
Definition GncParams.h:91
void setLossType(const GncLossType type)
Set the robust loss function to be used in GNC (chosen among the ones in GncLossType).
Definition GncParams.h:107
GncScheduler scheduler
Default scheduler.
Definition GncParams.h:90
double relativeCostTol
If relative cost change is below this threshold, stop iterating.
Definition GncParams.h:93
void setVerbosityGNC(const Verbosity value)
Set the verbosity level.
Definition GncParams.h:140
void setMaxIterations(const size_t maxIter)
Set the maximum number of iterations in GNC (changing the max nr of iters might lead to less accurate...
Definition GncParams.h:117
Verbosity
Verbosity levels.
Definition GncParams.h:62
void setWeightsTol(double value)
Set the maximum difference between the weights and their rounding in {0,1} to stop iterating.
Definition GncParams.h:135
void setScheduler(const GncScheduler s)
Set the scheduler type.
Definition GncParams.h:112