LinearContainerFactor.cpp
Go to the documentation of this file.
1 
13 
14 namespace gtsam {
15 
16 /* ************************************************************************* */
18  if (!linearizationPoint.empty()) {
20  for (Key key : keys()) {
21  linearizationPoint_->insert(key, linearizationPoint.at(key));
22  }
23  } else {
24  linearizationPoint_ = boost::none;
25  }
26 }
27 
28 /* ************************************************************************* */
30  const boost::optional<Values>& linearizationPoint)
31 : NonlinearFactor(factor->keys()), factor_(factor), linearizationPoint_(linearizationPoint) {
32 }
33 
34 /* ************************************************************************* */
37 : NonlinearFactor(factor.keys()), factor_(factor.clone()) {
38  initializeLinearizationPoint(linearizationPoint);
39 }
40 
41 /* ************************************************************************* */
44 : NonlinearFactor(factor.keys()), factor_(factor.clone()) {
45  initializeLinearizationPoint(linearizationPoint);
46 }
47 
48 /* ************************************************************************* */
51 : NonlinearFactor(factor->keys()), factor_(factor->clone()) {
52  initializeLinearizationPoint(linearizationPoint);
53 }
54 
55 /* ************************************************************************* */
56 void LinearContainerFactor::print(const std::string& s, const KeyFormatter& keyFormatter) const {
57  Base::print(s+"LinearContainerFactor", keyFormatter);
58  if (factor_)
59  factor_->print(" Stored Factor", keyFormatter);
61  linearizationPoint_->print(" LinearizationPoint", keyFormatter);
62 }
63 
64 /* ************************************************************************* */
65 bool LinearContainerFactor::equals(const NonlinearFactor& f, double tol) const {
66  const LinearContainerFactor* jcf = dynamic_cast<const LinearContainerFactor*>(&f);
67  if (!jcf || !factor_->equals(*jcf->factor_, tol) || !NonlinearFactor::equals(f))
68  return false;
70  return true;
72  return linearizationPoint_->equals(*jcf->linearizationPoint_, tol);
73  return false;
74 }
75 
76 /* ************************************************************************* */
77 double LinearContainerFactor::error(const Values& c) const {
79  return 0;
80 
81  // Extract subset of values for comparison
82  Values csub;
83  for (Key key : keys())
84  csub.insert(key, c.at(key));
85 
86  // create dummy ordering for evaluation
87  VectorValues delta = linearizationPoint_->localCoordinates(csub);
88 
89  // compute error
90  double error = factor_->error(delta);
91 
92  return error;
93 }
94 
95 /* ************************************************************************* */
97  if (isJacobian())
98  return toJacobian()->get_model()->dim();
99  else
100  return 1; // Hessians don't have true dimension
101 }
102 
103 /* ************************************************************************* */
105  // Clone factor and update as necessary
106  GaussianFactor::shared_ptr linFactor = factor_->clone();
107  if (!hasLinearizationPoint())
108  return linFactor;
109 
110  // Extract subset of values
111  Values subsetC;
112  for (Key key : keys())
113  subsetC.insert(key, c.at(key));
114 
115  // Determine delta between linearization points using new ordering
116  VectorValues delta = linearizationPoint_->localCoordinates(subsetC);
117 
118  // Apply changes due to relinearization
119  if (isJacobian()) {
120  JacobianFactor::shared_ptr jacFactor = boost::dynamic_pointer_cast<JacobianFactor>(linFactor);
121  jacFactor->getb() = -jacFactor->unweighted_error(delta);
122  } else {
123  HessianFactor::shared_ptr hesFactor = boost::dynamic_pointer_cast<HessianFactor>(linFactor);
124 
125  const auto view = hesFactor->informationView();
126  Vector deltaVector = delta.vector(keys());
127  Vector G_delta = view * deltaVector;
128  hesFactor->constantTerm() += deltaVector.dot(G_delta) - 2.0 * deltaVector.dot(hesFactor->linearTerm().col(0));
129  hesFactor->linearTerm() -= G_delta;
130  }
131 
132  return linFactor;
133 }
134 
135 /* ************************************************************************* */
137  return dynamic_cast<const JacobianFactor*>(factor_.get()) != 0;
138 }
139 
140 /* ************************************************************************* */
142  return dynamic_cast<const HessianFactor*>(factor_.get()) != 0;
143 }
144 
145 /* ************************************************************************* */
147  return boost::dynamic_pointer_cast<JacobianFactor>(factor_);
148 }
149 
150 /* ************************************************************************* */
152  return boost::dynamic_pointer_cast<HessianFactor>(factor_);
153 }
154 
155 /* ************************************************************************* */
158  return result;
159 }
160 
161 /* ************************************************************************* */
163  GaussianFactor::shared_ptr antifactor = factor_->negate(); // already has keys in place
165 }
166 
167 /* ************************************************************************* */
169  const GaussianFactorGraph& linear_graph, const Values& linearizationPoint) {
171  result.reserve(linear_graph.size());
172  for (const auto& f : linear_graph)
173  if (f)
174  result += boost::make_shared<LinearContainerFactor>(f, linearizationPoint);
175  return result;
176 }
177 
178 /* ************************************************************************* */
179 } // \namespace gtsam
180 
size_t size() const
Definition: FactorGraph.h:306
Wrap Jacobian and Hessian linear factors to allow simple injection into a nonlinear graph...
void insert(Key j, const Value &val)
Definition: Values.cpp:140
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set view
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
bool empty() const
Definition: Values.h:239
GTSAM_EXPORT boost::shared_ptr< HessianFactor > toHessian() const
const GaussianFactor::shared_ptr & factor() const
GaussianFactor::shared_ptr factor_
GTSAM_EXPORT GaussianFactor::shared_ptr negateToGaussian() const
Factor Graph Values.
Eigen::VectorXd Vector
Definition: Vector.h:38
Values result
const ValueType at(Key j) const
Definition: Values-inl.h:342
const constBVector getb() const
GTSAM_EXPORT bool isHessian() const
const boost::optional< Values > & linearizationPoint() const
boost::shared_ptr< This > shared_ptr
GTSAM_EXPORT boost::shared_ptr< JacobianFactor > toJacobian() const
boost::shared_ptr< This > shared_ptr
A shared_ptr to this class.
GTSAM_EXPORT size_t dim() const override
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Contains the HessianFactor class, a general quadratic factor.
RealScalar s
Linear Factor Graph where all factors are Gaussians.
virtual bool equals(const NonlinearFactor &f, double tol=1e-9) const
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
GTSAM_EXPORT void print(const std::string &s="", const KeyFormatter &keyFormatter=gtsam::DefaultKeyFormatter) const override
GTSAM_EXPORT NonlinearFactor::shared_ptr negateToNonlinear() const
GTSAM_EXPORT bool equals(const NonlinearFactor &f, double tol=1e-9) const override
traits
Definition: chartTesting.h:28
std::vector< float > Values
const KeyVector & keys() const
Access the factor&#39;s involved variable keys.
Definition: Factor.h:124
NonlinearFactor::shared_ptr clone() const override
GTSAM_EXPORT bool isJacobian() const
GTSAM_EXPORT void initializeLinearizationPoint(const Values &linearizationPoint)
boost::optional< Values > linearizationPoint_
A Gaussian factor using the canonical parameters (information form)
void reserve(size_t size)
Definition: FactorGraph.h:162
const G double tol
Definition: Group.h:83
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
static GTSAM_EXPORT NonlinearFactorGraph ConvertLinearGraph(const GaussianFactorGraph &linear_graph, const Values &linearizationPoint=Values())
GTSAM_EXPORT GaussianFactor::shared_ptr linearize(const Values &c) const override
GTSAM_EXPORT double error(const Values &c) const override
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
Vector vector() const


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:31