Skip to content

Commit afd75ea

Browse files
[fix,upd] fixing elementwise opration worning and example of building jacobians
1 parent ffa37a4 commit afd75ea

2 files changed

Lines changed: 86 additions & 1 deletion

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
* example_solver_2d.cpp
17+
*
18+
* Created on: May 28, 2024
19+
* Author: Aleksei Panchenko
20+
* aleksei.panchenko@skoltech.ru
21+
* Mobile Robotics Lab, Skoltech
22+
*/
23+
24+
25+
#include "mrob/factor_graph_diff_solve.hpp"
26+
#include "mrob/factor_graph.hpp"
27+
#include "mrob/factors/factor1Pose2d_diff.hpp"
28+
#include "mrob/factors/factor2Poses2d_diff.hpp"
29+
#include "mrob/factors/nodePose2d.hpp"
30+
31+
32+
#include <iostream>
33+
# include <vector>
34+
35+
int main ()
36+
{
37+
38+
std::vector<mrob::factor_id_t> diff_factor_idx;
39+
40+
mrob::FGraphDiffSolve graph(mrob::FGraphDiffSolve::ADJ);
41+
42+
// Initial node is defined at 0,0,0, and anchor factor actually observing it at 0
43+
mrob::Mat31 x, obs;
44+
x = mrob::Mat31::Random()*0.1;
45+
obs = mrob::Mat31::Zero();
46+
// Nodes and factors are added to the graph using polymorphism. That is why
47+
// we need to declare here what specific kind of nodes or factors we use
48+
// while the definition is an abstract class (Node or DiffFactor)
49+
std::shared_ptr<mrob::Node> n1(new mrob::NodePose2d(x));
50+
graph.add_node(n1);
51+
52+
Mat3 obsInformation= Mat3::Identity();
53+
std::shared_ptr<mrob::DiffFactor> f1(new mrob::Factor1Pose2d_diff(obs,n1,obsInformation*1e6));
54+
diff_factor_idx.emplace_back(graph.add_factor(f1));
55+
56+
std::shared_ptr<mrob::Node> n2(new mrob::NodePose2d(x));
57+
graph.add_node(n2);
58+
59+
obs << -1 , -1 , 0;
60+
std::shared_ptr<mrob::DiffFactor> f2(new mrob::Factor2Poses2d_diff(obs,n2,n1,obsInformation));
61+
diff_factor_idx.emplace_back(graph.add_factor(f2));
62+
63+
obs << 1, 1, 0;
64+
std::shared_ptr<mrob::DiffFactor> gnss_2(new mrob::Factor1Pose2d_diff(obs,n2, obsInformation*1e4));
65+
diff_factor_idx.emplace_back(graph.add_factor(gnss_2));
66+
67+
std::shared_ptr<mrob::Node> n3(new mrob::NodePose2d(x));
68+
graph.add_node(n3);
69+
70+
obs << -1 , -1 , 0;
71+
std::shared_ptr<mrob::DiffFactor> f3(new mrob::Factor2Poses2d_diff(obs,n3,n2,obsInformation));
72+
diff_factor_idx.emplace_back(graph.add_factor(f3));
73+
74+
obs << 2, 2, 0;
75+
std::shared_ptr<mrob::DiffFactor> gnss_3(new mrob::Factor1Pose2d_diff(obs,n3,obsInformation*1e4));
76+
diff_factor_idx.emplace_back(graph.add_factor(gnss_3));
77+
78+
graph.build_jacobians();
79+
80+
std::cout << "\nA = \n" << MatX(graph.get_dx_dz()) << std::endl;
81+
82+
83+
84+
return 0;
85+
}

src/FGraphDiff/factor_graph_diff_solve.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ uint_t FGraphDiffSolve::optimize_levenberg_marquardt(uint_t maxIters)
222222
//modelFidelity = deltaChi2 / (dx_.dot(b_) - 0.5*dx_.dot(L_* dx_));
223223
// Update, according to H.B. Nielson, Damping Parameter In Marquardt’s Method, Technical Report IMM-REP-1999-05, Dept. of Mathematical Modeling, Technical University Denmark
224224
// (J'J ) dx = J'r , so this is substituted.
225-
modelFidelity = 2.0 * deltaChi2 / (dx_.dot(b_) - dx_.dot(lambda_ * diagL_* dx_));
225+
modelFidelity = 2.0 * deltaChi2 / (dx_.dot(b_) - dx_.dot(lambda_ * diagL_.cwiseProduct(dx_)));
226226
if (verbose_)
227227
{
228228
std::cout << "model fidelity = " << modelFidelity << " and m_k = " << dx_.dot(b_) << std::endl;

0 commit comments

Comments
 (0)