testBasisFactors.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  * -------------------------------1-------------------------------------------
11  */
12 
21 #include <gtsam/base/Testable.h>
23 #include <gtsam/base/Vector.h>
24 #include <gtsam/basis/Basis.h>
26 #include <gtsam/basis/Chebyshev2.h>
27 #include <gtsam/geometry/Pose2.h>
28 #include <gtsam/inference/Symbol.h>
32 
33 using gtsam::Chebyshev2;
38 using gtsam::Pose2;
39 using gtsam::Values;
40 using gtsam::Vector;
42 
43 constexpr size_t N = 2;
44 
45 // Key used in all tests
46 const gtsam::Symbol key('X', 0);
47 
48 //******************************************************************************
49 TEST(BasisFactors, EvaluationFactor) {
51 
52  double measured = 0;
53 
54  auto model = Isotropic::Sigma(1, 1.0);
55  EvaluationFactor<Chebyshev2> factor(key, measured, model, N, 0);
56 
58  graph.add(factor);
59 
60  Vector functionValues(N);
61  functionValues.setZero();
62 
64  initial.insert<Vector>(key, functionValues);
65 
67  parameters.setMaxIterations(20);
68  Values result =
69  LevenbergMarquardtOptimizer(graph, initial, parameters).optimize();
70 
71  EXPECT_DOUBLES_EQUAL(0, graph.error(result), 1e-9);
72 }
73 
74 //******************************************************************************
75 TEST(BasisFactors, VectorEvaluationFactor) {
77  const size_t M = 4;
78 
79  const Vector measured = Vector::Zero(M);
80 
81  auto model = Isotropic::Sigma(M, 1.0);
82  VectorEvaluationFactor<Chebyshev2> factor(key, measured, model, M, N, 0);
83 
85  graph.add(factor);
86 
87  gtsam::Matrix stateMatrix = gtsam::Matrix::Zero(M, N);
88 
90  initial.insert<gtsam::Matrix>(key, stateMatrix);
91 
93  parameters.setMaxIterations(20);
94  Values result =
95  LevenbergMarquardtOptimizer(graph, initial, parameters).optimize();
96 
97  EXPECT_DOUBLES_EQUAL(0, graph.error(result), 1e-9);
98 }
99 
100 //******************************************************************************
101 TEST(BasisFactors, Print) {
103  const size_t M = 1;
104 
105  const Vector measured = Vector::Ones(M) * 42;
106 
107  auto model = Isotropic::Sigma(M, 1.0);
108  VectorEvaluationFactor<Chebyshev2> factor(key, measured, model, M, N, 0);
109 
110  std::string expected =
111  " keys = { X0 }\n"
112  " noise model: unit (1) \n"
113  "FunctorizedFactor(X0)\n"
114  " measurement: [\n"
115  " 42\n"
116  "]\n"
117  " noise model sigmas: 1\n";
118 
119  EXPECT(assert_print_equal(expected, factor));
120 }
121 
122 //******************************************************************************
123 TEST(BasisFactors, VectorComponentFactor) {
125  const int P = 4;
126  const size_t i = 2;
127  const double measured = 0.0, t = 3.0, a = 2.0, b = 4.0;
128  auto model = Isotropic::Sigma(1, 1.0);
129  VectorComponentFactor<Chebyshev2> factor(key, measured, model, P, N, i, t, a,
130  b);
131 
133  graph.add(factor);
134 
135  gtsam::Matrix stateMatrix = gtsam::Matrix::Zero(P, N);
136 
137  Values initial;
138  initial.insert<gtsam::Matrix>(key, stateMatrix);
139 
141  parameters.setMaxIterations(20);
142  Values result =
143  LevenbergMarquardtOptimizer(graph, initial, parameters).optimize();
144 
145  EXPECT_DOUBLES_EQUAL(0, graph.error(result), 1e-9);
146 }
147 
148 //******************************************************************************
149 TEST(BasisFactors, ManifoldEvaluationFactor) {
151  const Pose2 measured;
152  const double t = 3.0, a = 2.0, b = 4.0;
153  auto model = Isotropic::Sigma(3, 1.0);
154  ManifoldEvaluationFactor<Chebyshev2, Pose2> factor(key, measured, model, N, t,
155  a, b);
156 
158  graph.add(factor);
159 
160  gtsam::Matrix stateMatrix = gtsam::Matrix::Zero(3, N);
161 
162  Values initial;
163  initial.insert<gtsam::Matrix>(key, stateMatrix);
164 
166  parameters.setMaxIterations(20);
167  Values result =
168  LevenbergMarquardtOptimizer(graph, initial, parameters).optimize();
169 
170  EXPECT_DOUBLES_EQUAL(0, graph.error(result), 1e-9);
171  // Check Jacobians
172  EXPECT_CORRECT_FACTOR_JACOBIANS(factor, initial, 1e-7, 1e-5);
173 }
174 
175 //******************************************************************************
176 TEST(BasisFactors, VecDerivativePrior) {
178  const size_t M = 4;
179 
180  const Vector measured = Vector::Zero(M);
181  auto model = Isotropic::Sigma(M, 1.0);
182  VectorDerivativeFactor<Chebyshev2> vecDPrior(key, measured, model, M, N, 0);
183 
185  graph.add(vecDPrior);
186 
187  gtsam::Matrix stateMatrix = gtsam::Matrix::Zero(M, N);
188 
189  Values initial;
190  initial.insert<gtsam::Matrix>(key, stateMatrix);
191 
193  parameters.setMaxIterations(20);
194  Values result =
195  LevenbergMarquardtOptimizer(graph, initial, parameters).optimize();
196 
197  EXPECT_DOUBLES_EQUAL(0, graph.error(result), 1e-9);
198 }
199 
200 //******************************************************************************
201 TEST(BasisFactors, ComponentDerivativeFactor) {
203  const size_t M = 4;
204 
205  double measured = 0;
206  auto model = Isotropic::Sigma(1, 1.0);
207  ComponentDerivativeFactor<Chebyshev2> controlDPrior(key, measured, model, M,
208  N, 0, 0);
209 
211  graph.add(controlDPrior);
212 
213  Values initial;
214  gtsam::Matrix stateMatrix = gtsam::Matrix::Zero(M, N);
215  initial.insert<gtsam::Matrix>(key, stateMatrix);
216 
218  parameters.setMaxIterations(20);
219  Values result =
220  LevenbergMarquardtOptimizer(graph, initial, parameters).optimize();
221 
222  EXPECT_DOUBLES_EQUAL(0, graph.error(result), 1e-9);
223 }
224 
225 /* ************************************************************************* */
226 int main() {
227  TestResult tr;
228  return TestRegistry::runAllTests(tr);
229 }
230 /* ************************************************************************* */
const gtsam::Symbol key('X', 0)
Point2 measured(-17, 30)
Provides additional testing facilities for common data structures.
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51
virtual const Values & optimize()
Scalar * b
Definition: benchVecAdd.cpp:17
Concept check for values that can be used in unit tests.
static int runAllTests(TestResult &result)
bool assert_print_equal(const std::string &expected, const V &actual, const std::string &s="")
noiseModel::Diagonal::shared_ptr model
Matrix expected
Definition: testMatrix.cpp:971
Factor definitions for various Basis functors.
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
Values initial
Evaluate derivatives of a nonlinear factor numerically.
NonlinearFactorGraph graph
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
Definition: Test.h:161
TEST(BasisFactors, EvaluationFactor)
IsDerived< DERIVEDFACTOR > add(std::shared_ptr< DERIVEDFACTOR > factor)
add is a synonym for push_back.
Definition: FactorGraph.h:214
constexpr size_t N
Eigen::VectorXd Vector
Definition: Vector.h:38
Values result
Factor for enforcing the scalar value of the polynomial BASIS representation at x is the same as the ...
Definition: BasisFactors.h:39
Compute an interpolating basis.
#define EXPECT(condition)
Definition: Test.h:150
A nonlinear optimizer that uses the Levenberg-Marquardt trust-region scheme.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Pseudo-spectral parameterization for Chebyshev polynomials of the second kind.
double error(const Values &values) const
static ConjugateGradientParameters parameters
typedef and functions to augment Eigen&#39;s VectorXd
std::vector< float > Values
#define EXPECT_CORRECT_FACTOR_JACOBIANS(factor, values, numerical_derivative_step, tolerance)
Check the Jacobians produced by a factor against finite differences.
int main()
void insert(Key j, const Value &val)
Definition: Values.cpp:155
2D Pose
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
void Print(const CONTAINER &keys, const string &s, const KeyFormatter &keyFormatter)
Definition: Key.cpp:62
Point2 t(10, 10)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:52