MROB
factor_graph_solve.hpp
1 /* Copyright (c) 2022, Gonzalo Ferrer
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  *
16  * factor_graph_solve.hpp
17  *
18  * Created on: Mar 23, 2018
19  * Author: Gonzalo Ferrer
20  * g.ferrer@skoltech.ru
21  * Mobile Robotics Lab, Skoltech
22  */
23 
24 #ifndef SRC_FACTOR_GRAPH_SOLVE_HPP_
25 #define SRC_FACTOR_GRAPH_SOLVE_HPP_
26 
27 
28 #include "mrob/factor_graph.hpp"
29 #include "mrob/time_profiling.hpp"
30 #include <unordered_map>
31 
32 namespace mrob {
33 
34 
74 class FGraphSolve: public FGraph
75 {
76 public:
81  enum matrixMethod{ADJ=0, SCHUR};
87  enum optimMethod{GN=0, LM, LM_ELLIPS};
88 
89  FGraphSolve(matrixMethod method = ADJ);
90  virtual ~FGraphSolve();
91 
100  uint_t solve(optimMethod method = GN, uint_t maxIters = 20, matData_t lambda = 1e-6, matData_t solutionTolerance = 1e-2);
108  matData_t chi2(bool evaluateResidualsFlag = true);
113  std::vector<MatX> get_estimated_state();
114 
118  void set_build_matrix_method(matrixMethod method) {matrixMethod_ = method;};
119  matrixMethod get_build_matrix_method() { return matrixMethod_;};
120 
127  SMatCol get_information_matrix() { return L_;}
133  SMatCol get_adjacency_matrix() { return A_;}
139  SMatCol get_W_matrix() { return W_;}
144  MatX1 get_vector_b() { return b_;}
148  MatX1 get_chi2_array();
149 
150 protected:
160  void build_problem(bool useLambda = false);
167  void build_adjacency();
173  void build_info_adjacency();
179  void build_info_EF();
180  void build_schur(); // TODO
181 
189  void optimize_gauss_newton(bool useLambda = false);
190 
206  uint_t optimize_levenberg_marquardt(uint_t maxIters);
207 
212  void update_nodes();
213 
221 
229 
238 
239  // Variables for solving the FGraph
240  matrixMethod matrixMethod_;
241  optimMethod optimMethod_;
242 
243 
244  factor_id_t N_; // total number of state variables
245  factor_id_t M_; // total number of observation variables
246 
247  std::unordered_map<factor_id_t, factor_id_t > indNodesMatrix_;
248 
249  SMatRow A_; //Adjacency matrix, as a Row sparse matrix. The reason is for filling in row-fashion for each factor
250  SMatRow W_; //A block diagonal information matrix. For types Adjacency it calculates its block transposed squared root
251  MatX1 r_; // Residuals as given by the factors
252 
253  SMatCol L_; //Information matrix. For Eigen Cholesky AMD Ordering it is necessary Col convention for compilation.
254  MatX1 b_; // Post-processed residuals, A'*W*r
255 
256  // Correction deltas
257  MatX1 dx_;
258 
259  // Particular parameters for Levenberg-Marquard
260  matData_t lambda_; // current value of lambda
261  matData_t solutionTolerance_;
262  MatX1 diagL_; //diagonal matrix (vector) of L to update it efficiently
263 
264 
265  // Methods for handling Eigen factors. If not used, no problem
266  SMatCol hessianEF_;
267  MatX1 gradientEF_;
268  bool buildAdjacencyFlag_;
269 
270  // time profiling
271  TimeProfiling time_profiles_;
272 };
273 
274 
275 }
276 
277 
278 #endif /* SRC_FACTOR_GRAPH_SOLVE_HPP_ */
SMatCol get_adjacency_matrix()
Definition: factor_graph_solve.hpp:133
MatX1 get_vector_b()
Definition: factor_graph_solve.hpp:144
std::vector< MatX > get_estimated_state()
Definition: factor_graph_solve.cpp:497
void synchronize_nodes_state()
Definition: factor_graph_solve.cpp:490
void build_info_adjacency()
Definition: factor_graph_solve.cpp:359
SMatCol get_W_matrix()
Definition: factor_graph_solve.hpp:139
Definition: factor_graph_solve.hpp:74
void update_nodes()
Definition: factor_graph_solve.cpp:468
Definition: factor_graph.hpp:57
void build_problem(bool useLambda=false)
Definition: factor_graph_solve.cpp:95
matData_t chi2(bool evaluateResidualsFlag=true)
Definition: factor_graph_solve.cpp:442
void build_index_nodes_matrix()
Definition: factor_graph_solve.cpp:233
optimMethod
Definition: factor_graph_solve.hpp:87
MatX1 get_chi2_array()
Definition: factor_graph_solve.cpp:512
Definition: time_profiling.hpp:40
void set_build_matrix_method(matrixMethod method)
Definition: factor_graph_solve.hpp:118
void build_info_EF()
Definition: factor_graph_solve.cpp:394
void build_adjacency()
Definition: factor_graph_solve.cpp:246
matrixMethod
Definition: factor_graph_solve.hpp:81
void synchronize_nodes_auxiliary_state()
Definition: factor_graph_solve.cpp:483
Special Euclidean (group) in 3d Is the group representing rotations and translations, that is, rigid body transformations. SE3 = {T = [R t] | R SO3 , t Re^3 } [0 1] Associated to the groups of RBT, there is the Lie algebra se3 representing the same transformation in the tangent space around the identity. Particularly, xi =[w , v] Re^6, where w Re^3 represents the rotation and v the translation. We will preserve this order in this class.
Definition: matrix_base.hpp:36
SMatCol get_information_matrix()
Definition: factor_graph_solve.hpp:127
uint_t solve(optimMethod method=GN, uint_t maxIters=20, matData_t lambda=1e-6, matData_t solutionTolerance=1e-2)
Definition: factor_graph_solve.cpp:50
void optimize_gauss_newton(bool useLambda=false)
Definition: factor_graph_solve.cpp:133
uint_t optimize_levenberg_marquardt(uint_t maxIters)
Definition: factor_graph_solve.cpp:160