NonlinearConjugateGradientOptimizer.cpp
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 #include <gtsam/nonlinear/Values.h>
24 
25 #include <cmath>
26 
27 namespace gtsam {
28 
29 typedef internal::NonlinearOptimizerState State;
30 
38  const Values &values) {
39  // Linearize graph
40  GaussianFactorGraph::shared_ptr linear = nfg.linearize(values);
41  return linear->gradientAtZero();
42 }
43 
45  const NonlinearFactorGraph& graph, const Values& initialValues, const Parameters& params)
46  : Base(graph, std::unique_ptr<State>(new State(initialValues, graph.error(initialValues)))),
47  params_(params) {}
48 
50  return graph_.error(state);
51 }
52 
54  const State &state) const {
55  return gradientInPlace(graph_, state);
56 }
57 
59  const State &current, const double alpha, const Gradient &g) const {
60  Gradient step = g;
61  step *= alpha;
62  return current.retract(step);
63 }
64 
66  const auto [newValues, dummy] = nonlinearConjugateGradient<System, Values>(
67  System(graph_), state_->values, params_, true /* single iteration */);
68  state_.reset(new State(newValues, graph_.error(newValues), state_->iterations + 1));
69 
70  // NOTE(frank): We don't linearize this system, so we must return null here.
71  return nullptr;
72 }
73 
75  // Optimize until convergence
76  System system(graph_);
77  const auto [newValues, iterations] =
78  nonlinearConjugateGradient(system, state_->values, params_, false);
79  state_.reset(new State(std::move(newValues), graph_.error(newValues), iterations));
80  return state_->values;
81 }
82 
83 } /* namespace gtsam */
84 
def step(data, isam, result, truth, currPoseIndex)
Definition: visual_isam.py:82
A non-templated config holding any types of Manifold-group elements.
leaf::MyValues values
size_t iterations() const
return number of iterations in current optimizer state
Definition: BFloat16.h:88
NonlinearFactorGraph graph
void g(const string &key, int i)
Definition: testBTree.cpp:41
static const SmartProjectionParams params
Values retract(const VectorValues &delta) const
Definition: Values.cpp:98
std::shared_ptr< GaussianFactorGraph > linearize(const Values &linearizationPoint) const
Linearize a nonlinear factor graph.
std::unique_ptr< internal::NonlinearOptimizerState > state_
PIMPL&#39;d state.
Factor Graph Values.
GaussianFactorGraph::shared_ptr iterate() override
std::tuple< V, int > nonlinearConjugateGradient(const S &system, const V &initial, const NonlinearOptimizerParams &params, const bool singleIteration, const bool gradientDescent=false)
RealScalar alpha
Linear Factor Graph where all factors are Gaussians.
std::shared_ptr< This > shared_ptr
shared_ptr to this class
double error(const Values &values) const
internal::DoglegState State
NonlinearConjugateGradientOptimizer(const NonlinearFactorGraph &graph, const Values &initialValues, const Parameters &params=Parameters())
Constructor.
traits
Definition: chartTesting.h:28
static VectorValues gradientInPlace(const NonlinearFactorGraph &nfg, const Values &values)
Return the gradient vector of a nonlinear factor graph.
State advance(const State &current, const double alpha, const Gradient &g) const
NonlinearFactorGraph graph_
The graph with nonlinear factors.
Private class for NonlinearOptimizer state.
static double error
Definition: testRot3.cpp:37
Simple non-linear optimizer that solves using non-preconditioned CG.


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:57