File: smallExample.h

package info (click to toggle)
gtsam 4.2.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 46,108 kB
  • sloc: cpp: 127,191; python: 14,312; xml: 8,442; makefile: 252; sh: 119; ansic: 101
file content (703 lines) | stat: -rw-r--r-- 21,292 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/* ----------------------------------------------------------------------------

 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
 * Atlanta, Georgia 30332-0415
 * All Rights Reserved
 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)

 * See LICENSE for the license information

 * -------------------------------------------------------------------------- */

/**
 * @file    smallExample.h
 * @brief   Create small example with two poses and one landmark
 * @brief   smallExample
 * @author  Carlos Nieto
 */

// \callgraph


#pragma once

#include <tests/simulated2D.h>
#include <gtsam/inference/Symbol.h>
#include <gtsam/nonlinear/NonlinearFactorGraph.h>
#include <gtsam/linear/GaussianBayesNet.h>
#include <gtsam/linear/GaussianFactorGraph.h>

namespace gtsam {
namespace example {

/**
 * Create small example for non-linear factor graph
 */
// inline boost::shared_ptr<const NonlinearFactorGraph> sharedNonlinearFactorGraph();
// inline NonlinearFactorGraph createNonlinearFactorGraph();

/**
 * Create values structure to go with it
 * The ground truth values structure for the example above
 */
// inline Values createValues();

/** Vector Values equivalent */
// inline VectorValues createVectorValues();

/**
 * create a noisy values structure for a nonlinear factor graph
 */
// inline boost::shared_ptr<const Values> sharedNoisyValues();
// inline Values createNoisyValues();

/**
 * Zero delta config
 */
// inline VectorValues createZeroDelta();

/**
 * Delta config that, when added to noisyValues, returns the ground truth
 */
// inline VectorValues createCorrectDelta();

/**
 * create a linear factor graph
 * The non-linear graph above evaluated at NoisyValues
 */
// inline GaussianFactorGraph createGaussianFactorGraph();

/**
 * create small Chordal Bayes Net x <- y
 */
// inline GaussianBayesNet createSmallGaussianBayesNet();

/**
 * Create really non-linear factor graph (cos/sin)
 */
// inline boost::shared_ptr<const NonlinearFactorGraph>
//sharedReallyNonlinearFactorGraph();
// inline NonlinearFactorGraph createReallyNonlinearFactorGraph();

/**
 * Create a full nonlinear smoother
 * @param T number of time-steps
 */
// inline std::pair<NonlinearFactorGraph, Values> createNonlinearSmoother(int T);

/**
 * Create a Kalman smoother by linearizing a non-linear factor graph
 * @param T number of time-steps
 */
// inline GaussianFactorGraph createSmoother(int T);

/* ******************************************************* */
// Linear Constrained Examples
/* ******************************************************* */

/**
 * Creates a simple constrained graph with one linear factor and
 * one binary equality constraint that sets x = y
 */
// inline GaussianFactorGraph createSimpleConstraintGraph();
// inline VectorValues createSimpleConstraintValues();

/**
 * Creates a simple constrained graph with one linear factor and
 * one binary constraint.
 */
// inline GaussianFactorGraph createSingleConstraintGraph();
// inline VectorValues createSingleConstraintValues();

/**
 * Creates a constrained graph with a linear factor and two
 * binary constraints that share a node
 */
// inline GaussianFactorGraph createMultiConstraintGraph();
// inline VectorValues createMultiConstraintValues();

/* ******************************************************* */
// Planar graph with easy subtree for SubgraphPreconditioner
/* ******************************************************* */

/*
 * Create factor graph with N^2 nodes, for example for N=3
 *  x13-x23-x33
 *   |   |   |
 *  x12-x22-x32
 *   |   |   |
 * -x11-x21-x31
 * with x11 clamped at (1,1), and others related by 2D odometry.
 */
// inline std::pair<GaussianFactorGraph, VectorValues> planarGraph(size_t N);

/*
 * Create canonical ordering for planar graph that also works for tree
 * With x11 the root, e.g. for N=3
 * x33 x23 x13 x32 x22 x12 x31 x21 x11
 */
// inline Ordering planarOrdering(size_t N);

/*
 * Split graph into tree and loop closing constraints, e.g., with N=3
 *  x13-x23-x33
 *   |
 *  x12-x22-x32
 *   |
 * -x11-x21-x31
 */
// inline std::pair<GaussianFactorGraph, GaussianFactorGraph > splitOffPlanarTree(
//    size_t N, const GaussianFactorGraph& original);



// Implementations

//  using namespace gtsam::noiseModel;

namespace impl {
typedef boost::shared_ptr<NonlinearFactor> shared_nlf;

static SharedDiagonal kSigma1_0 = noiseModel::Isotropic::Sigma(2,1.0);
static SharedDiagonal kSigma0_1 = noiseModel::Isotropic::Sigma(2,0.1);
static SharedDiagonal kSigma0_2 = noiseModel::Isotropic::Sigma(2,0.2);
static SharedDiagonal kConstrainedModel = noiseModel::Constrained::All(2);

static const Key _l1_=0, _x1_=1, _x2_=2;
static const Key _x_=0, _y_=1, _z_=2;
} // \namespace impl


/* ************************************************************************* */
inline boost::shared_ptr<const NonlinearFactorGraph>
sharedNonlinearFactorGraph(const SharedNoiseModel &noiseModel1 = impl::kSigma0_1,
                           const SharedNoiseModel &noiseModel2 = impl::kSigma0_2) {
  using namespace impl;
  using symbol_shorthand::L;
  using symbol_shorthand::X;
  // Create
  boost::shared_ptr<NonlinearFactorGraph> nlfg(new NonlinearFactorGraph);

  // prior on x1
  Point2 mu(0, 0);
  shared_nlf f1(new simulated2D::Prior(mu, noiseModel1, X(1)));
  nlfg->push_back(f1);

  // odometry between x1 and x2
  Point2 z2(1.5, 0);
  shared_nlf f2(new simulated2D::Odometry(z2, noiseModel1, X(1), X(2)));
  nlfg->push_back(f2);

  // measurement between x1 and l1
  Point2 z3(0, -1);
  shared_nlf f3(new simulated2D::Measurement(z3, noiseModel2, X(1), L(1)));
  nlfg->push_back(f3);

  // measurement between x2 and l1
  Point2 z4(-1.5, -1.);
  shared_nlf f4(new simulated2D::Measurement(z4, noiseModel2, X(2), L(1)));
  nlfg->push_back(f4);

  return nlfg;
}

/* ************************************************************************* */
inline NonlinearFactorGraph
createNonlinearFactorGraph(const SharedNoiseModel &noiseModel1 = impl::kSigma0_1,
                           const SharedNoiseModel &noiseModel2 = impl::kSigma0_2) {
  return *sharedNonlinearFactorGraph(noiseModel1, noiseModel2);
}

/* ************************************************************************* */
inline Values createValues() {
  using symbol_shorthand::X;
  using symbol_shorthand::L;
  Values c;
  c.insert(X(1), Point2(0.0, 0.0));
  c.insert(X(2), Point2(1.5, 0.0));
  c.insert(L(1), Point2(0.0, -1.0));
  return c;
}

/* ************************************************************************* */
inline VectorValues createVectorValues() {
  using namespace impl;
  VectorValues c {{_l1_, Vector2(0.0, -1.0)},
                  {_x1_, Vector2(0.0, 0.0)},
                  {_x2_, Vector2(1.5, 0.0)}};
  return c;
}

/* ************************************************************************* */
inline boost::shared_ptr<const Values> sharedNoisyValues() {
  using symbol_shorthand::X;
  using symbol_shorthand::L;
  boost::shared_ptr<Values> c(new Values);
  c->insert(X(1), Point2(0.1, 0.1));
  c->insert(X(2), Point2(1.4, 0.2));
  c->insert(L(1), Point2(0.1, -1.1));
  return c;
}

/* ************************************************************************* */
inline Values createNoisyValues() {
  return *sharedNoisyValues();
}

/* ************************************************************************* */
inline VectorValues createCorrectDelta() {
  using symbol_shorthand::X;
  using symbol_shorthand::L;
  VectorValues c;
  c.insert(L(1), Vector2(-0.1, 0.1));
  c.insert(X(1), Vector2(-0.1, -0.1));
  c.insert(X(2), Vector2(0.1, -0.2));
  return c;
}

/* ************************************************************************* */
inline VectorValues createZeroDelta() {
  using symbol_shorthand::X;
  using symbol_shorthand::L;
  VectorValues c;
  c.insert(L(1), Z_2x1);
  c.insert(X(1), Z_2x1);
  c.insert(X(2), Z_2x1);
  return c;
}

/* ************************************************************************* */
inline GaussianFactorGraph createGaussianFactorGraph() {
  using symbol_shorthand::X;
  using symbol_shorthand::L;
  // Create empty graph
  GaussianFactorGraph fg;

  // linearized prior on x1: c[_x1_]+x1=0 i.e. x1=-c[_x1_]
  fg += JacobianFactor(X(1), 10*I_2x2, -1.0*Vector::Ones(2));

  // odometry between x1 and x2: x2-x1=[0.2;-0.1]
  fg += JacobianFactor(X(1), -10*I_2x2, X(2), 10*I_2x2, Vector2(2.0, -1.0));

  // measurement between x1 and l1: l1-x1=[0.0;0.2]
  fg += JacobianFactor(X(1), -5*I_2x2, L(1), 5*I_2x2, Vector2(0.0, 1.0));

  // measurement between x2 and l1: l1-x2=[-0.2;0.3]
  fg += JacobianFactor(X(2), -5*I_2x2, L(1), 5*I_2x2, Vector2(-1.0, 1.5));

  return fg;
}

/* ************************************************************************* */
/** create small Chordal Bayes Net x <- y
 * x y d
 * 1 1 9
 *   1 5
 */
inline GaussianBayesNet createSmallGaussianBayesNet() {
  using namespace impl;
  Matrix R11 = (Matrix(1, 1) << 1.0).finished(), S12 = (Matrix(1, 1) << 1.0).finished();
  Matrix R22 = (Matrix(1, 1) << 1.0).finished();
  Vector d1(1), d2(1);
  d1(0) = 9;
  d2(0) = 5;

  // define nodes and specify in reverse topological sort (i.e. parents last)
  GaussianConditional::shared_ptr Px_y(new GaussianConditional(_x_, d1, R11, _y_, S12));
  GaussianConditional::shared_ptr Py(new GaussianConditional(_y_, d2, R22));
  GaussianBayesNet cbn;
  cbn.push_back(Px_y);
  cbn.push_back(Py);

  return cbn;
}

/* ************************************************************************* */
// Some nonlinear functions to optimize
/* ************************************************************************* */
namespace smallOptimize {

inline Point2 h(const Point2& v) {
  return Point2(cos(v.x()), sin(v.y()));
}

inline Matrix H(const Point2& v) {
  return (Matrix(2, 2) <<
      -sin(v.x()), 0.0,
      0.0, cos(v.y())).finished();
}

struct UnaryFactor: public gtsam::NoiseModelFactorN<Point2> {

  Point2 z_;

  UnaryFactor(const Point2& z, const SharedNoiseModel& model, Key key) :
    gtsam::NoiseModelFactorN<Point2>(model, key), z_(z) {
  }

  Vector evaluateError(const Point2& x, boost::optional<Matrix&> A = boost::none) const override {
    if (A) *A = H(x);
    return (h(x) - z_);
  }

  gtsam::NonlinearFactor::shared_ptr clone() const override {
        return boost::static_pointer_cast<gtsam::NonlinearFactor>(
            gtsam::NonlinearFactor::shared_ptr(new UnaryFactor(*this))); }
};

}

/* ************************************************************************* */
inline NonlinearFactorGraph nonlinearFactorGraphWithGivenSigma(const double sigma) {
  using symbol_shorthand::X;
  using symbol_shorthand::L;
  boost::shared_ptr<NonlinearFactorGraph> fg(new NonlinearFactorGraph);
  Point2 z(1.0, 0.0);
  boost::shared_ptr<smallOptimize::UnaryFactor> factor(
      new smallOptimize::UnaryFactor(z, noiseModel::Isotropic::Sigma(2,sigma), X(1)));
  fg->push_back(factor);
  return *fg;
}

/* ************************************************************************* */
inline boost::shared_ptr<const NonlinearFactorGraph> sharedReallyNonlinearFactorGraph() {
  using symbol_shorthand::X;
  using symbol_shorthand::L;
  boost::shared_ptr<NonlinearFactorGraph> fg(new NonlinearFactorGraph);
  Point2 z(1.0, 0.0);
  double sigma = 0.1;
  boost::shared_ptr<smallOptimize::UnaryFactor> factor(
      new smallOptimize::UnaryFactor(z, noiseModel::Isotropic::Sigma(2,sigma), X(1)));
  fg->push_back(factor);
  return fg;
}

inline NonlinearFactorGraph createReallyNonlinearFactorGraph() {
  return *sharedReallyNonlinearFactorGraph();
}

/* ************************************************************************* */
inline NonlinearFactorGraph sharedNonRobustFactorGraphWithOutliers() {
  using symbol_shorthand::X;
  boost::shared_ptr<NonlinearFactorGraph> fg(new NonlinearFactorGraph);
  Point2 z(0.0, 0.0);
  double sigma = 0.1;

  boost::shared_ptr<PriorFactor<Point2>> factor(
      new PriorFactor<Point2>(X(1), z, noiseModel::Isotropic::Sigma(2,sigma)));
  // 3 noiseless inliers
  fg->push_back(factor);
  fg->push_back(factor);
  fg->push_back(factor);

  // 1 outlier
  Point2 z_out(1.0, 0.0);
  boost::shared_ptr<PriorFactor<Point2>> factor_out(
      new PriorFactor<Point2>(X(1), z_out, noiseModel::Isotropic::Sigma(2,sigma)));
  fg->push_back(factor_out);

  return *fg;
}

/* ************************************************************************* */
inline NonlinearFactorGraph sharedRobustFactorGraphWithOutliers() {
  using symbol_shorthand::X;
  boost::shared_ptr<NonlinearFactorGraph> fg(new NonlinearFactorGraph);
  Point2 z(0.0, 0.0);
  double sigma = 0.1;
  auto gmNoise = noiseModel::Robust::Create(
            noiseModel::mEstimator::GemanMcClure::Create(1.0), noiseModel::Isotropic::Sigma(2,sigma));
  boost::shared_ptr<PriorFactor<Point2>> factor(
      new PriorFactor<Point2>(X(1), z, gmNoise));
  // 3 noiseless inliers
  fg->push_back(factor);
  fg->push_back(factor);
  fg->push_back(factor);

  // 1 outlier
  Point2 z_out(1.0, 0.0);
  boost::shared_ptr<PriorFactor<Point2>> factor_out(
      new PriorFactor<Point2>(X(1), z_out, gmNoise));
  fg->push_back(factor_out);

  return *fg;
}


/* ************************************************************************* */
inline std::pair<NonlinearFactorGraph, Values> createNonlinearSmoother(int T) {
  using namespace impl;
  using symbol_shorthand::X;
  using symbol_shorthand::L;

  // Create
  NonlinearFactorGraph nlfg;
  Values poses;

  // prior on x1
  Point2 x1(1.0, 0.0);
  shared_nlf prior(new simulated2D::Prior(x1, kSigma1_0, X(1)));
  nlfg.push_back(prior);
  poses.insert(X(1), x1);

  for (int t = 2; t <= T; t++) {
    // odometry between x_t and x_{t-1}
    Point2 odo(1.0, 0.0);
    shared_nlf odometry(new simulated2D::Odometry(odo, kSigma1_0, X(t - 1), X(t)));
    nlfg.push_back(odometry);

    // measurement on x_t is like perfect GPS
    Point2 xt(t, 0);
    shared_nlf measurement(new simulated2D::Prior(xt, kSigma1_0, X(t)));
    nlfg.push_back(measurement);

    // initial estimate
    poses.insert(X(t), xt);
  }

  return std::make_pair(nlfg, poses);
}

/* ************************************************************************* */
inline GaussianFactorGraph createSmoother(int T) {
  NonlinearFactorGraph nlfg;
  Values poses;
  std::tie(nlfg, poses) = createNonlinearSmoother(T);

  return *nlfg.linearize(poses);
}

/* ************************************************************************* */
inline GaussianFactorGraph createSimpleConstraintGraph() {
  using namespace impl;
  // create unary factor
  // prior on _x_, mean = [1,-1], sigma=0.1
  Matrix Ax = I_2x2;
  Vector b1(2);
  b1(0) = 1.0;
  b1(1) = -1.0;
  JacobianFactor::shared_ptr f1(new JacobianFactor(_x_, Ax, b1, kSigma0_1));

  // create binary constraint factor
  // between _x_ and _y_, that is going to be the only factor on _y_
  // |1 0||x_1| + |-1  0||y_1| = |0|
  // |0 1||x_2|   | 0 -1||y_2|   |0|
  Matrix Ax1 = I_2x2;
  Matrix Ay1 = I_2x2 * -1;
  Vector b2 = Vector2(0.0, 0.0);
  JacobianFactor::shared_ptr f2(new JacobianFactor(_x_, Ax1, _y_, Ay1, b2,
      kConstrainedModel));

  // construct the graph
  GaussianFactorGraph fg;
  fg.push_back(f1);
  fg.push_back(f2);

  return fg;
}

/* ************************************************************************* */
inline VectorValues createSimpleConstraintValues() {
  using namespace impl;
  using symbol_shorthand::X;
  using symbol_shorthand::L;
  VectorValues config;
  Vector v = Vector2(1.0, -1.0);
  config.insert(_x_, v);
  config.insert(_y_, v);
  return config;
}

/* ************************************************************************* */
inline GaussianFactorGraph createSingleConstraintGraph() {
  using namespace impl;
  // create unary factor
  // prior on _x_, mean = [1,-1], sigma=0.1
  Matrix Ax = I_2x2;
  Vector b1(2);
  b1(0) = 1.0;
  b1(1) = -1.0;
  //GaussianFactor::shared_ptr f1(new JacobianFactor(_x_, kSigma0_1->Whiten(Ax), kSigma0_1->whiten(b1), kSigma0_1));
  JacobianFactor::shared_ptr f1(new JacobianFactor(_x_, Ax, b1, kSigma0_1));

  // create binary constraint factor
  // between _x_ and _y_, that is going to be the only factor on _y_
  // |1 2||x_1| + |10 0||y_1| = |1|
  // |2 1||x_2|   |0 10||y_2|   |2|
  Matrix Ax1(2, 2);
  Ax1(0, 0) = 1.0;
  Ax1(0, 1) = 2.0;
  Ax1(1, 0) = 2.0;
  Ax1(1, 1) = 1.0;
  Matrix Ay1 = I_2x2 * 10;
  Vector b2 = Vector2(1.0, 2.0);
  JacobianFactor::shared_ptr f2(new JacobianFactor(_x_, Ax1, _y_, Ay1, b2,
      kConstrainedModel));

  // construct the graph
  GaussianFactorGraph fg;
  fg.push_back(f1);
  fg.push_back(f2);

  return fg;
}

/* ************************************************************************* */
inline VectorValues createSingleConstraintValues() {
  using namespace impl;
  VectorValues config{{_x_, Vector2(1.0, -1.0)}, {_y_, Vector2(0.2, 0.1)}};
  return config;
}

/* ************************************************************************* */
inline GaussianFactorGraph createMultiConstraintGraph() {
  using namespace impl;
  // unary factor 1
  Matrix A = I_2x2;
  Vector b = Vector2(-2.0, 2.0);
  JacobianFactor::shared_ptr lf1(new JacobianFactor(_x_, A, b, kSigma0_1));

  // constraint 1
  Matrix A11(2, 2);
  A11(0, 0) = 1.0;
  A11(0, 1) = 2.0;
  A11(1, 0) = 2.0;
  A11(1, 1) = 1.0;

  Matrix A12(2, 2);
  A12(0, 0) = 10.0;
  A12(0, 1) = 0.0;
  A12(1, 0) = 0.0;
  A12(1, 1) = 10.0;

  Vector b1(2);
  b1(0) = 1.0;
  b1(1) = 2.0;
  JacobianFactor::shared_ptr lc1(new JacobianFactor(_x_, A11, _y_, A12, b1,
      kConstrainedModel));

  // constraint 2
  Matrix A21(2, 2);
  A21(0, 0) = 3.0;
  A21(0, 1) = 4.0;
  A21(1, 0) = -1.0;
  A21(1, 1) = -2.0;

  Matrix A22(2, 2);
  A22(0, 0) = 1.0;
  A22(0, 1) = 1.0;
  A22(1, 0) = 1.0;
  A22(1, 1) = 2.0;

  Vector b2(2);
  b2(0) = 3.0;
  b2(1) = 4.0;
  JacobianFactor::shared_ptr lc2(new JacobianFactor(_x_, A21, _z_, A22, b2,
      kConstrainedModel));

  // construct the graph
  GaussianFactorGraph fg;
  fg.push_back(lf1);
  fg.push_back(lc1);
  fg.push_back(lc2);

  return fg;
}

/* ************************************************************************* */
inline VectorValues createMultiConstraintValues() {
  using namespace impl;
  VectorValues config{{_x_, Vector2(-2.0, 2.0)},
                      {_y_, Vector2(-0.1, 0.4)},
                      {_z_, Vector2(-4.0, 5.0)}};
  return config;
}

/* ************************************************************************* */
// Create key for simulated planar graph
namespace impl {
inline Symbol key(size_t x, size_t y) {
  using symbol_shorthand::X;
  return X(1000*x+y);
}
} // \namespace impl

/* ************************************************************************* */
inline std::pair<GaussianFactorGraph, VectorValues> planarGraph(size_t N) {
  using namespace impl;

  // create empty graph
  NonlinearFactorGraph nlfg;

  // Create almost hard constraint on x11, sigma=0 will work for PCG not for normal
  shared_nlf constraint(new simulated2D::Prior(Point2(1.0, 1.0), noiseModel::Isotropic::Sigma(2,1e-3), key(1,1)));
  nlfg.push_back(constraint);

  // Create horizontal constraints, 1...N*(N-1)
  Point2 z1(1.0, 0.0); // move right
  for (size_t x = 1; x < N; x++)
    for (size_t y = 1; y <= N; y++) {
      shared_nlf f(new simulated2D::Odometry(z1, noiseModel::Isotropic::Sigma(2,0.01), key(x, y), key(x + 1, y)));
      nlfg.push_back(f);
    }

  // Create vertical constraints, N*(N-1)+1..2*N*(N-1)
  Point2 z2(0.0, 1.0); // move up
  for (size_t x = 1; x <= N; x++)
    for (size_t y = 1; y < N; y++) {
      shared_nlf f(new simulated2D::Odometry(z2, noiseModel::Isotropic::Sigma(2,0.01), key(x, y), key(x, y + 1)));
      nlfg.push_back(f);
    }

  // Create linearization and ground xtrue config
  Values zeros;
  for (size_t x = 1; x <= N; x++)
    for (size_t y = 1; y <= N; y++)
      zeros.insert(key(x, y), Point2(0,0));
  VectorValues xtrue;
  for (size_t x = 1; x <= N; x++)
    for (size_t y = 1; y <= N; y++)
      xtrue.insert(key(x, y), Point2((double)x, (double)y));

  // linearize around zero
  boost::shared_ptr<GaussianFactorGraph> gfg = nlfg.linearize(zeros);
  return std::make_pair(*gfg, xtrue);
}

/* ************************************************************************* */
inline Ordering planarOrdering(size_t N) {
  Ordering ordering;
  for (size_t y = N; y >= 1; y--)
    for (size_t x = N; x >= 1; x--)
      ordering.push_back(impl::key(x, y));
  return ordering;
}

/* ************************************************************************* */
inline std::pair<GaussianFactorGraph, GaussianFactorGraph> splitOffPlanarTree(
    size_t N, const GaussianFactorGraph& original) {
  GaussianFactorGraph T, C;

  // Add the x11 constraint to the tree
  T.push_back(original[0]);

  // Add all horizontal constraints to the tree
  size_t i = 1;
  for (size_t x = 1; x < N; x++)
    for (size_t y = 1; y <= N; y++, i++) T.push_back(original[i]);

  // Add first vertical column of constraints to T, others to C
  for (size_t x = 1; x <= N; x++)
    for (size_t y = 1; y < N; y++, i++)
      if (x == 1)
        T.push_back(original[i]);
      else
        C.push_back(original[i]);

  return std::make_pair(T, C);
}

/* ************************************************************************* */

} // \namespace example
} // \namespace gtsam