gtsam
Loading...
Searching...
No Matches
RISAMGraduatedFactor.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
19#pragma once
21#include <gtsam/sam/RISAMGraduationScheduler.h>
22
23#include <cmath>
24
25namespace gtsam {
26
28class GTSAM_EXPORT GraduatedFactor {
31 public:
32 typedef std::shared_ptr<GraduatedFactor> shared_ptr;
33 typedef noiseModel::mEstimator::Base RobustLoss;
35
38 protected:
40 RobustLoss::shared_ptr robustLoss_;
43
45 std::shared_ptr<double> mu_;
47
49 friend class RISAM;
50
53 public:
59 GraduatedFactor(RobustLoss::shared_ptr loss,
62 mu_ = std::make_shared<double>(scheduler_->muInit());
63 }
64
68 mu_ = std::make_shared<double>(*(other.mu_));
69 }
70
71 virtual ~GraduatedFactor() = default;
72
77 const Values& currentEstimate) const = 0;
78
80 virtual double residual(const Values& currentEstimate) const = 0;
81
83 virtual double robustLoss(const Values& currentEstimate) const = 0;
84
86 const RobustLoss::shared_ptr& loss() const { return robustLoss_; }
87
90 return scheduler_;
91 }
92
95 virtual NonlinearFactor::shared_ptr cloneUngraduated() const = 0;
97};
98
100template <class FACTOR_TYPE>
101class GenericGraduatedFactor : public FACTOR_TYPE, public GraduatedFactor {
102 static_assert(std::is_base_of<NonlinearFactor, FACTOR_TYPE>::value,
103 "GraduatedFactor Must be instantiated with a Factor Derived "
104 "from NonlinearFactor.");
105
108 public:
109 typedef std::shared_ptr<GenericGraduatedFactor<FACTOR_TYPE>> shared_ptr;
111
114 public:
120 template <class... Args>
121 GenericGraduatedFactor(RobustLoss::shared_ptr loss,
123 Args&&... args)
124 : FACTOR_TYPE(std::forward<Args>(args)...),
126
131 : FACTOR_TYPE(other), GraduatedFactor(other) {}
132
134 NonlinearFactor::shared_ptr clone() const override {
135 return std::static_pointer_cast<NonlinearFactor>(
136 NonlinearFactor::shared_ptr(
138 }
139
142 const Values& currentEstimate) const override {
143 double residual = this->residual(currentEstimate);
144
145 // Use base factor to linearize
146 auto whitenedLinearSystem = FACTOR_TYPE::linearize(currentEstimate);
147 auto [A, b] = whitenedLinearSystem->jacobian();
148 const size_t outputDim = b.size();
149
150 // Extract the non-dense linear system
151 auto keys = whitenedLinearSystem->keys();
152 std::vector<Matrix> Ablocks;
153 size_t indexStart = 0;
154 for (const auto& key : keys) {
155 size_t d = currentEstimate.at(key).dim();
156 Ablocks.push_back(A.block(0, indexStart, outputDim, d));
157 indexStart += d;
158 }
159
160 // Weight the Linearized Blocks
161 const double sqrtWeight =
162 std::sqrt(robustLoss_->graduatedWeight(residual, *mu_));
163 for (Matrix& Aj : Ablocks) {
164 Aj *= sqrtWeight;
165 }
166 b *= sqrtWeight;
167
168 // Construct a jacobian factor from the weighted system
169 FastMap<Key, Matrix> AblockMap;
170 for (size_t i = 0; i < Ablocks.size(); i++) {
171 AblockMap[keys[i]] = Ablocks[i];
172 }
173 return std::make_shared<JacobianFactor>(AblockMap, b);
174 }
175
178 const Values& currentEstimate) const override {
179 // Delegate to linearizeGraduated which is required by the GraduatedFactor
180 // Interface
181 return linearizeGraduated(currentEstimate);
182 }
183
185 double error(const Values& values) const override {
186 return robustLoss(values);
187 }
188
189
193 double residual(const Values& currentEstimate) const override {
194 return std::sqrt(2.0 * FACTOR_TYPE::error(currentEstimate));
195 }
196
198 double robustLoss(const Values& currentEstimate) const override {
199 return robustLoss_->graduatedLoss(residual(currentEstimate), *mu_);
200 }
201
203 NonlinearFactor::shared_ptr cloneUngraduated() const override {
204 return FACTOR_TYPE::clone();
205 }
206
207};
208} // namespace gtsam
Non-linear factor base classes.
STL namespace.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
FastMap is a thin wrapper around std::map that uses the boost fast_pool_allocator instead of the defa...
Definition FastMap.h:40
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition GaussianFactor.h:42
Pure virtual class for all robust error function classes.
Definition LossFunctions.h:81
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65
const ValueType at(Key j) const
Retrieve a variable by key j.
Definition Values-inl.h:260
Graduated Factor for riSAM base class.
Definition RISAMGraduatedFactor.h:28
GraduatedFactor(RobustLoss::shared_ptr loss, GraduationScheduler::shared_ptr scheduler)
Constructor.
Definition RISAMGraduatedFactor.h:59
GraduationScheduler::shared_ptr scheduler_
The control param ($\mu$) scheduler for this factor.
Definition RISAMGraduatedFactor.h:42
const RobustLoss::shared_ptr & loss() const
Returns the robust loss for this graduated factor.
Definition RISAMGraduatedFactor.h:86
const GraduationScheduler::shared_ptr & scheduler() const
Returns the graduation scheduler for this factor.
Definition RISAMGraduatedFactor.h:89
friend class RISAM
Befriend RISAM to give access to these protected values.
Definition RISAMGraduatedFactor.h:49
std::shared_ptr< double > mu_
The unique mu control parameter for this factor.
Definition RISAMGraduatedFactor.h:45
virtual double robustLoss(const Values &currentEstimate) const =0
returns the graduated robust loss \rho_\mu(r) of the factor
RobustLoss::shared_ptr robustLoss_
The robust loss for this factor.
Definition RISAMGraduatedFactor.h:40
GraduatedFactor(const GraduatedFactor &other)
Copy constructor.
Definition RISAMGraduatedFactor.h:66
virtual GaussianFactor::shared_ptr linearizeGraduated(const Values &currentEstimate) const =0
Linearize this factor using the convexification parameter mu.
virtual double residual(const Values &currentEstimate) const =0
returns the residual of the factor
virtual NonlinearFactor::shared_ptr cloneUngraduated() const =0
Copies this factor as an instance of its base type without the graduated robust loss or scheduler.
double residual(const Values &currentEstimate) const override
returns the residual of the factor
Definition RISAMGraduatedFactor.h:193
GenericGraduatedFactor(const GenericGraduatedFactor< FACTOR_TYPE > &other)
Copy Constructor Delegates to GraduatedFactor's copy constructor so the copy preserves the current gr...
Definition RISAMGraduatedFactor.h:130
GenericGraduatedFactor(RobustLoss::shared_ptr loss, GraduationScheduler::shared_ptr scheduler, Args &&... args)
Constructor.
Definition RISAMGraduatedFactor.h:121
NonlinearFactor::shared_ptr cloneUngraduated() const override
See GraduatedFactor::cloneUngraduated.
Definition RISAMGraduatedFactor.h:203
NonlinearFactor::shared_ptr clone() const override
Makes a deep copy of the factor.
Definition RISAMGraduatedFactor.h:134
GaussianFactor::shared_ptr linearize(const Values &currentEstimate) const override
Linearize the System.
Definition RISAMGraduatedFactor.h:177
double robustLoss(const Values &currentEstimate) const override
See GraduatedFactor::robustLoss.
Definition RISAMGraduatedFactor.h:198
GaussianFactor::shared_ptr linearizeGraduated(const Values &currentEstimate) const override
Linearizes the factor using the current value of mu.
Definition RISAMGraduatedFactor.h:141
double error(const Values &values) const override
Returns the graduated robust loss \rho_\mu(r).
Definition RISAMGraduatedFactor.h:185
std::shared_ptr< GraduationScheduler > shared_ptr
Shortcut for shared pointer.
Definition RISAMGraduationScheduler.h:47