testGaussianConditional.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 
20 
21 #include <gtsam/base/Matrix.h>
23 #include <gtsam/inference/Key.h>
27 
28 #include <boost/assign/std/list.hpp>
29 #include <boost/assign/std/vector.hpp>
30 #include <boost/assign/list_inserter.hpp>
31 #include <boost/make_shared.hpp>
32 #include <boost/assign/list_of.hpp>
33 
34 #include <iostream>
35 #include <sstream>
36 #include <vector>
37 
38 using namespace gtsam;
39 using namespace std;
40 using namespace boost::assign;
41 
42 static const double tol = 1e-5;
43 
44 static Matrix R = (Matrix(2, 2) <<
45  -12.1244, -5.1962,
46  0., 4.6904).finished();
47 
48 /* ************************************************************************* */
50 {
51  Matrix S1 = (Matrix(2, 2) <<
52  -5.2786, -8.6603,
53  5.0254, 5.5432).finished();
54  Matrix S2 = (Matrix(2, 2) <<
55  -10.5573, -5.9385,
56  5.5737, 3.0153).finished();
57  Matrix S3 = (Matrix(2, 2) <<
58  -11.3820, -7.2581,
59  -3.0153, -3.5635).finished();
60 
61  Vector d = Vector2(1.0, 2.0);
63 
64  vector<pair<Key, Matrix> > terms = pair_list_of
65  (1, R)
66  (3, S1)
67  (5, S2)
68  (7, S3);
69 
70  GaussianConditional actual(terms, 1, d, s);
71 
73  EXPECT(assert_equal(Key(1), *it));
74  EXPECT(assert_equal(R, actual.R()));
75  ++ it;
76  EXPECT(it == actual.endFrontals());
77 
78  it = actual.beginParents();
79  EXPECT(assert_equal(Key(3), *it));
80  EXPECT(assert_equal(S1, actual.S(it)));
81 
82  ++ it;
83  EXPECT(assert_equal(Key(5), *it));
84  EXPECT(assert_equal(S2, actual.S(it)));
85 
86  ++ it;
87  EXPECT(assert_equal(Key(7), *it));
88  EXPECT(assert_equal(S3, actual.S(it)));
89 
90  ++it;
91  EXPECT(it == actual.endParents());
92 
93  EXPECT(assert_equal(d, actual.d()));
94  EXPECT(assert_equal(*s, *actual.get_model()));
95 
96  // test copy constructor
97  GaussianConditional copied(actual);
98  EXPECT(assert_equal(d, copied.d()));
99  EXPECT(assert_equal(*s, *copied.get_model()));
100  EXPECT(assert_equal(R, copied.R()));
101 }
102 
103 /* ************************************************************************* */
105 {
106  // create a conditional gaussian node
107  Matrix A1(2,2);
108  A1(0,0) = 1 ; A1(1,0) = 2;
109  A1(0,1) = 3 ; A1(1,1) = 4;
110 
111  Matrix A2(2,2);
112  A2(0,0) = 6 ; A2(1,0) = 0.2;
113  A2(0,1) = 8 ; A2(1,1) = 0.4;
114 
115  Matrix R(2,2);
116  R(0,0) = 0.1 ; R(1,0) = 0.3;
117  R(0,1) = 0.0 ; R(1,1) = 0.34;
118 
120 
121  Vector d = Vector2(0.2, 0.5);
122 
124  expected(1, d, R, 2, A1, 10, A2, model),
125  actual(1, d, R, 2, A1, 10, A2, model);
126 
127  EXPECT( expected.equals(actual) );
128 }
129 
130 /* ************************************************************************* */
132 {
133  //expected solution
134  Vector expectedX(2);
135  expectedX(0) = 20-3-11 ; expectedX(1) = 40-7-15;
136 
137  // create a conditional Gaussian node
138  Matrix R = (Matrix(2, 2) << 1., 0.,
139  0., 1.).finished();
140 
141  Matrix A1 = (Matrix(2, 2) << 1., 2.,
142  3., 4.).finished();
143 
144  Matrix A2 = (Matrix(2, 2) << 5., 6.,
145  7., 8.).finished();
146 
147  Vector d(2); d << 20.0, 40.0;
148 
149  GaussianConditional cg(1, d, R, 2, A1, 10, A2);
150 
151  Vector sx1(2); sx1 << 1.0, 1.0;
152  Vector sl1(2); sl1 << 1.0, 1.0;
153 
154  VectorValues expected = map_list_of
155  (1, expectedX)
156  (2, sx1)
157  (10, sl1);
158 
159  VectorValues solution = map_list_of
160  (2, sx1) // parents
161  (10, sl1);
162  solution.insert(cg.solve(solution));
163 
164  EXPECT(assert_equal(expected, solution, tol));
165 }
166 
167 /* ************************************************************************* */
168 TEST( GaussianConditional, solve_simple )
169 {
170  // 2 variables, frontal has dim=4
171  VerticalBlockMatrix blockMatrix(list_of(4)(2)(1), 4);
172  blockMatrix.matrix() <<
173  1.0, 0.0, 2.0, 0.0, 3.0, 0.0, 0.1,
174  0.0, 1.0, 0.0, 2.0, 0.0, 3.0, 0.2,
175  0.0, 0.0, 3.0, 0.0, 4.0, 0.0, 0.3,
176  0.0, 0.0, 0.0, 3.0, 0.0, 4.0, 0.4;
177 
178  // solve system as a non-multifrontal version first
179  GaussianConditional cg(list_of(1)(2), 1, blockMatrix);
180 
181  // partial solution
182  Vector sx1 = Vector2(9.0, 10.0);
183 
184  // elimination order: 1, 2
185  VectorValues actual = map_list_of
186  (2, sx1); // parent
187 
188  VectorValues expected = map_list_of<Key, Vector>
189  (2, sx1)
190  (1, (Vector(4) << -3.1,-3.4,-11.9,-13.2).finished());
191 
192  // verify indices/size
193  EXPECT_LONGS_EQUAL(2, (long)cg.size());
194  EXPECT_LONGS_EQUAL(4, (long)cg.rows());
195 
196  // solve and verify
197  actual.insert(cg.solve(actual));
198  EXPECT(assert_equal(expected, actual, tol));
199 }
200 
201 /* ************************************************************************* */
202 TEST( GaussianConditional, solve_multifrontal )
203 {
204  // create full system, 3 variables, 2 frontals, all 2 dim
205  VerticalBlockMatrix blockMatrix(list_of(2)(2)(2)(1), 4);
206  blockMatrix.matrix() <<
207  1.0, 0.0, 2.0, 0.0, 3.0, 0.0, 0.1,
208  0.0, 1.0, 0.0, 2.0, 0.0, 3.0, 0.2,
209  0.0, 0.0, 3.0, 0.0, 4.0, 0.0, 0.3,
210  0.0, 0.0, 0.0, 3.0, 0.0, 4.0, 0.4;
211 
212  // 3 variables, all dim=2
213  GaussianConditional cg(list_of(1)(2)(10), 2, blockMatrix);
214 
215  EXPECT(assert_equal(Vector(blockMatrix.full().rightCols(1)), cg.d()));
216 
217  // partial solution
218  Vector sl1 = Vector2(9.0, 10.0);
219 
220  // elimination order; _x_, _x1_, _l1_
221  VectorValues actual = map_list_of
222  (10, sl1); // parent
223 
224  VectorValues expected = map_list_of<Key, Vector>
225  (1, Vector2(-3.1,-3.4))
226  (2, Vector2(-11.9,-13.2))
227  (10, sl1);
228 
229  // verify indices/size
230  EXPECT_LONGS_EQUAL(3, (long)cg.size());
231  EXPECT_LONGS_EQUAL(4, (long)cg.rows());
232 
233  // solve and verify
234  actual.insert(cg.solve(actual));
235  EXPECT(assert_equal(expected, actual, tol));
236 
237 }
238 
239 /* ************************************************************************* */
240 TEST( GaussianConditional, solveTranspose ) {
246  Matrix R11 = (Matrix(1, 1) << 1.0).finished(), S12 = (Matrix(1, 1) << 1.0).finished();
247  Matrix R22 = (Matrix(1, 1) << 1.0).finished();
248  Vector d1(1), d2(1);
249  d1(0) = 9;
250  d2(0) = 5;
251 
252  // define nodes and specify in reverse topological sort (i.e. parents last)
253  GaussianBayesNet cbn = list_of
254  (GaussianConditional(1, d1, R11, 2, S12))
255  (GaussianConditional(1, d2, R22));
256 
257  // x=R'*y, y=inv(R')*x
258  // 2 = 1 2
259  // 5 1 1 3
260 
262  x = map_list_of<Key, Vector>
263  (1, (Vector(1) << 2.).finished())
264  (2, (Vector(1) << 5.).finished()),
265  y = map_list_of<Key, Vector>
266  (1, (Vector(1) << 2.).finished())
267  (2, (Vector(1) << 3.).finished());
268 
269  // test functional version
270  VectorValues actual = cbn.backSubstituteTranspose(x);
271  CHECK(assert_equal(y, actual));
272 }
273 
274 /* ************************************************************************* */
275 TEST( GaussianConditional, information ) {
276 
277  // Create R matrix
278  Matrix R(4,4); R <<
279  1, 2, 3, 4,
280  0, 5, 6, 7,
281  0, 0, 8, 9,
282  0, 0, 0, 10;
283 
284  // Create conditional
285  GaussianConditional conditional(0, Vector::Zero(4), R);
286 
287  // Expected information matrix (using permuted R)
288  Matrix IExpected = R.transpose() * R;
289 
290  // Actual information matrix (conditional should permute R)
291  Matrix IActual = conditional.information();
292  EXPECT(assert_equal(IExpected, IActual));
293 }
294 
295 /* ************************************************************************* */
296 TEST( GaussianConditional, isGaussianFactor ) {
297 
298  // Create R matrix
299  Matrix R(4,4); R <<
300  1, 2, 3, 4,
301  0, 5, 6, 7,
302  0, 0, 8, 9,
303  0, 0, 0, 10;
304 
305  // Create a conditional
306  GaussianConditional conditional(0, Vector::Zero(4), R);
307 
308  // Expected information matrix computed by conditional
309  Matrix IExpected = conditional.information();
310 
311  // Expected information matrix computed by a factor
312  JacobianFactor jf = conditional;
313  Matrix IActual = jf.information();
314 
315  EXPECT(assert_equal(IExpected, IActual));
316 }
317 
318 /* ************************************************************************* */
319 int main() { TestResult tr; return TestRegistry::runAllTests(tr);}
320 /* ************************************************************************* */
Provides additional testing facilities for common data structures.
#define CHECK(condition)
Definition: Test.h:109
Scalar * y
static int runAllTests(TestResult &result)
noiseModel::Diagonal::shared_ptr model
Matrix expected
Definition: testMatrix.cpp:974
FACTOR::const_iterator endParents() const
Definition: Conditional.h:114
const Matrix & matrix() const
A matrix with column blocks of pre-defined sizes. Used in JacobianFactor and GaussianConditional.
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:43
Symmetric group.
Definition: testGroup.cpp:27
size_t rows() const
Definition: Half.h:150
FACTOR::const_iterator beginParents() const
Definition: Conditional.h:111
Eigen::VectorXd Vector
Definition: Vector.h:38
#define EXPECT(condition)
Definition: Test.h:151
FACTOR::const_iterator endFrontals() const
Definition: Conditional.h:108
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
Conditional Gaussian Base class.
const constBVector d() const
static Matrix R
const SharedDiagonal & get_model() const
noiseModel::Diagonal::shared_ptr SharedDiagonal
Definition: NoiseModel.h:736
traits
Definition: chartTesting.h:28
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:42
FACTOR::const_iterator beginFrontals() const
Definition: Conditional.h:105
Eigen::Vector2d Vector2
Definition: Vector.h:42
size_t size() const
Definition: Factor.h:135
#define EXPECT_LONGS_EQUAL(expected, actual)
Definition: Test.h:155
static shared_ptr Sigmas(const Vector &sigmas, bool smart=true)
Definition: NoiseModel.cpp:270
VectorValues solve(const VectorValues &parents) const
TEST(LPInitSolver, InfiniteLoopSingleVar)
Chordal Bayes Net, the result of eliminating a factor graph.
const G double tol
Definition: Group.h:83
KeyVector::const_iterator const_iterator
Const iterator over keys.
Definition: Factor.h:67
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 x
iterator insert(const std::pair< Key, Vector > &key_value)
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
Matrix information() const override
VectorValues backSubstituteTranspose(const VectorValues &gx) const
Symmetric< 3 > S3
Definition: testGroup.cpp:88


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:38