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 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
|
/* ----------------------------------------------------------------------------
* 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 testNonlinearFactor.cpp
* @brief Unit tests for Non-Linear Factor,
* create a non linear factor graph and a values structure for it and
* calculate the error for the factor.
* @author Christian Potthast
**/
/*STL/C++*/
#include <iostream>
#include <CppUnitLite/TestHarness.h>
// TODO: DANGEROUS, create shared pointers
#define GTSAM_MAGIC_GAUSSIAN 2
#include <gtsam/base/Testable.h>
#include <gtsam/base/Matrix.h>
#include <tests/smallExample.h>
#include <tests/simulated2D.h>
#include <gtsam/linear/GaussianFactor.h>
#include <gtsam/nonlinear/NonlinearFactorGraph.h>
#include <gtsam/inference/Symbol.h>
using namespace std;
using namespace gtsam;
using namespace example;
// Convenience for named keys
using symbol_shorthand::X;
using symbol_shorthand::L;
typedef boost::shared_ptr<NonlinearFactor > shared_nlf;
/* ************************************************************************* */
TEST( NonlinearFactor, equals )
{
SharedNoiseModel sigma(noiseModel::Isotropic::Sigma(2,1.0));
// create two nonlinear2 factors
Point2 z3(0.,-1.);
simulated2D::Measurement f0(z3, sigma, X(1),L(1));
// measurement between x2 and l1
Point2 z4(-1.5, -1.);
simulated2D::Measurement f1(z4, sigma, X(2),L(1));
CHECK(assert_equal(f0,f0));
CHECK(f0.equals(f0));
CHECK(!f0.equals(f1));
CHECK(!f1.equals(f0));
}
/* ************************************************************************* */
TEST( NonlinearFactor, equals2 )
{
// create a non linear factor graph
NonlinearFactorGraph fg = createNonlinearFactorGraph();
// get two factors
NonlinearFactorGraph::sharedFactor f0 = fg[0], f1 = fg[1];
CHECK(f0->equals(*f0));
CHECK(!f0->equals(*f1));
CHECK(!f1->equals(*f0));
}
/* ************************************************************************* */
TEST( NonlinearFactor, NonlinearFactor )
{
// create a non linear factor graph
NonlinearFactorGraph fg = createNonlinearFactorGraph();
// create a values structure for the non linear factor graph
Values cfg = createNoisyValues();
// get the factor "f1" from the factor graph
NonlinearFactorGraph::sharedFactor factor = fg[0];
// calculate the error_vector from the factor "f1"
// error_vector = [0.1 0.1]
Vector actual_e = boost::dynamic_pointer_cast<NoiseModelFactor>(factor)->unwhitenedError(cfg);
CHECK(assert_equal(0.1*Vector::Ones(2),actual_e));
// error = 0.5 * [1 1] * [1;1] = 1
double expected = 1.0;
// calculate the error from the factor "f1"
double actual = factor->error(cfg);
DOUBLES_EQUAL(expected,actual,0.00000001);
}
/* ************************************************************************* */
TEST(NonlinearFactor, Weight) {
// create a values structure for the non linear factor graph
Values values;
// Instantiate a concrete class version of a NoiseModelFactor
PriorFactor<Point2> factor1(X(1), Point2(0, 0));
values.insert(X(1), Point2(0.1, 0.1));
CHECK(assert_equal(1.0, factor1.weight(values)));
// Factor with noise model
auto noise = noiseModel::Isotropic::Sigma(2, 0.2);
PriorFactor<Point2> factor2(X(2), Point2(1, 1), noise);
values.insert(X(2), Point2(1.1, 1.1));
CHECK(assert_equal(1.0, factor2.weight(values)));
Point2 estimate(3, 3), prior(1, 1);
double distance = (estimate - prior).norm();
auto gaussian = noiseModel::Isotropic::Sigma(2, 0.2);
PriorFactor<Point2> factor;
// vector to store all the robust models in so we can test iteratively.
vector<noiseModel::Robust::shared_ptr> robust_models;
// Fair noise model
auto fair = noiseModel::Robust::Create(
noiseModel::mEstimator::Fair::Create(1.3998), gaussian);
robust_models.push_back(fair);
// Huber noise model
auto huber = noiseModel::Robust::Create(
noiseModel::mEstimator::Huber::Create(1.345), gaussian);
robust_models.push_back(huber);
// Cauchy noise model
auto cauchy = noiseModel::Robust::Create(
noiseModel::mEstimator::Cauchy::Create(0.1), gaussian);
robust_models.push_back(cauchy);
// Tukey noise model
auto tukey = noiseModel::Robust::Create(
noiseModel::mEstimator::Tukey::Create(4.6851), gaussian);
robust_models.push_back(tukey);
// Welsch noise model
auto welsch = noiseModel::Robust::Create(
noiseModel::mEstimator::Welsch::Create(2.9846), gaussian);
robust_models.push_back(welsch);
// Geman-McClure noise model
auto gm = noiseModel::Robust::Create(
noiseModel::mEstimator::GemanMcClure::Create(1.0), gaussian);
robust_models.push_back(gm);
// DCS noise model
auto dcs = noiseModel::Robust::Create(
noiseModel::mEstimator::DCS::Create(1.0), gaussian);
robust_models.push_back(dcs);
// L2WithDeadZone noise model
auto l2 = noiseModel::Robust::Create(
noiseModel::mEstimator::L2WithDeadZone::Create(1.0), gaussian);
robust_models.push_back(l2);
for(auto&& model: robust_models) {
factor = PriorFactor<Point2>(X(3), prior, model);
values.clear();
values.insert(X(3), estimate);
CHECK(assert_equal(model->robust()->weight(distance), factor.weight(values)));
}
}
/* ************************************************************************* */
TEST( NonlinearFactor, linearize_f1 )
{
Values c = createNoisyValues();
// Grab a non-linear factor
NonlinearFactorGraph nfg = createNonlinearFactorGraph();
NonlinearFactorGraph::sharedFactor nlf = nfg[0];
// We linearize at noisy config from SmallExample
GaussianFactor::shared_ptr actual = nlf->linearize(c);
GaussianFactorGraph lfg = createGaussianFactorGraph();
GaussianFactor::shared_ptr expected = lfg[0];
CHECK(assert_equal(*expected,*actual));
// The error |A*dx-b| approximates (h(x0+dx)-z) = -error_vector
// Hence i.e., b = approximates z-h(x0) = error_vector(x0)
//CHECK(assert_equal(nlf->error_vector(c),actual->get_b()));
}
/* ************************************************************************* */
TEST( NonlinearFactor, linearize_f2 )
{
Values c = createNoisyValues();
// Grab a non-linear factor
NonlinearFactorGraph nfg = createNonlinearFactorGraph();
NonlinearFactorGraph::sharedFactor nlf = nfg[1];
// We linearize at noisy config from SmallExample
GaussianFactor::shared_ptr actual = nlf->linearize(c);
GaussianFactorGraph lfg = createGaussianFactorGraph();
GaussianFactor::shared_ptr expected = lfg[1];
CHECK(assert_equal(*expected,*actual));
}
/* ************************************************************************* */
TEST( NonlinearFactor, linearize_f3 )
{
// Grab a non-linear factor
NonlinearFactorGraph nfg = createNonlinearFactorGraph();
NonlinearFactorGraph::sharedFactor nlf = nfg[2];
// We linearize at noisy config from SmallExample
Values c = createNoisyValues();
GaussianFactor::shared_ptr actual = nlf->linearize(c);
GaussianFactorGraph lfg = createGaussianFactorGraph();
GaussianFactor::shared_ptr expected = lfg[2];
CHECK(assert_equal(*expected,*actual));
}
/* ************************************************************************* */
TEST( NonlinearFactor, linearize_f4 )
{
// Grab a non-linear factor
NonlinearFactorGraph nfg = createNonlinearFactorGraph();
NonlinearFactorGraph::sharedFactor nlf = nfg[3];
// We linearize at noisy config from SmallExample
Values c = createNoisyValues();
GaussianFactor::shared_ptr actual = nlf->linearize(c);
GaussianFactorGraph lfg = createGaussianFactorGraph();
GaussianFactor::shared_ptr expected = lfg[3];
CHECK(assert_equal(*expected,*actual));
}
/* ************************************************************************* */
TEST( NonlinearFactor, size )
{
// create a non linear factor graph
NonlinearFactorGraph fg = createNonlinearFactorGraph();
// create a values structure for the non linear factor graph
Values cfg = createNoisyValues();
// get some factors from the graph
NonlinearFactorGraph::sharedFactor factor1 = fg[0], factor2 = fg[1],
factor3 = fg[2];
CHECK(factor1->size() == 1);
CHECK(factor2->size() == 2);
CHECK(factor3->size() == 2);
}
/* ************************************************************************* */
TEST( NonlinearFactor, linearize_constraint1 )
{
SharedDiagonal constraint = noiseModel::Constrained::MixedSigmas(Vector2(0.2,0));
Point2 mu(1., -1.);
NonlinearFactorGraph::sharedFactor f0(new simulated2D::Prior(mu, constraint, X(1)));
Values config;
config.insert(X(1), Point2(1.0, 2.0));
GaussianFactor::shared_ptr actual = f0->linearize(config);
// create expected
Vector2 b(0., -3.);
JacobianFactor expected(X(1), (Matrix(2, 2) << 5.0, 0.0, 0.0, 1.0).finished(), b,
noiseModel::Constrained::MixedSigmas(Vector2(1,0)));
CHECK(assert_equal((const GaussianFactor&)expected, *actual));
}
/* ************************************************************************* */
TEST( NonlinearFactor, linearize_constraint2 )
{
SharedDiagonal constraint = noiseModel::Constrained::MixedSigmas(Vector2(0.2,0));
Point2 z3(1.,-1.);
simulated2D::Measurement f0(z3, constraint, X(1),L(1));
Values config;
config.insert(X(1), Point2(1.0, 2.0));
config.insert(L(1), Point2(5.0, 4.0));
GaussianFactor::shared_ptr actual = f0.linearize(config);
// create expected
Matrix2 A; A << 5.0, 0.0, 0.0, 1.0;
Vector2 b(-15., -3.);
JacobianFactor expected(X(1), -1*A, L(1), A, b,
noiseModel::Constrained::MixedSigmas(Vector2(1,0)));
CHECK(assert_equal((const GaussianFactor&)expected, *actual));
}
/* ************************************************************************* */
TEST( NonlinearFactor, cloneWithNewNoiseModel )
{
// create original factor
double sigma1 = 0.1;
NonlinearFactorGraph nfg = example::nonlinearFactorGraphWithGivenSigma(sigma1);
// create expected
double sigma2 = 10;
NonlinearFactorGraph expected = example::nonlinearFactorGraphWithGivenSigma(sigma2);
// create actual
NonlinearFactorGraph actual;
SharedNoiseModel noise2 = noiseModel::Isotropic::Sigma(2,sigma2);
actual.push_back( boost::dynamic_pointer_cast<NoiseModelFactor>(nfg[0])->cloneWithNewNoiseModel(noise2) );
// check it's all good
CHECK(assert_equal(expected, actual));
}
/* ************************************************************************* */
class TestFactor1 : public NoiseModelFactor1<double> {
static_assert(std::is_same<Base, NoiseModelFactor>::value, "Base type wrong");
static_assert(std::is_same<This, NoiseModelFactor1<double>>::value,
"This type wrong");
public:
typedef NoiseModelFactor1<double> Base;
TestFactor1() : Base(noiseModel::Diagonal::Sigmas(Vector1(2.0)), L(1)) {}
using Base::NoiseModelFactor1; // inherit constructors
Vector evaluateError(const double& x1, boost::optional<Matrix&> H1 =
boost::none) const override {
if (H1) *H1 = (Matrix(1, 1) << 1.0).finished();
return (Vector(1) << x1).finished();
}
gtsam::NonlinearFactor::shared_ptr clone() const override {
return boost::static_pointer_cast<gtsam::NonlinearFactor>(
gtsam::NonlinearFactor::shared_ptr(new TestFactor1(*this)));
}
};
/* ************************************ */
TEST(NonlinearFactor, NoiseModelFactor1) {
TestFactor1 tf;
Values tv;
tv.insert(L(1), double((1.0)));
EXPECT(assert_equal((Vector(1) << 1.0).finished(), tf.unwhitenedError(tv)));
DOUBLES_EQUAL(0.25 / 2.0, tf.error(tv), 1e-9);
JacobianFactor jf(
*boost::dynamic_pointer_cast<JacobianFactor>(tf.linearize(tv)));
LONGS_EQUAL((long)L(1), (long)jf.keys()[0]);
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 0.5).finished(),
jf.getA(jf.begin())));
EXPECT(assert_equal((Vector)(Vector(1) << -0.5).finished(), jf.getb()));
// Test all functions/types for backwards compatibility
static_assert(std::is_same<TestFactor1::X, double>::value,
"X type incorrect");
EXPECT(assert_equal(tf.key(), L(1)));
std::vector<Matrix> H = {Matrix()};
EXPECT(assert_equal(Vector1(1.0), tf.unwhitenedError(tv, H)));
// Test constructors
TestFactor1 tf2(noiseModel::Unit::Create(1), L(1));
TestFactor1 tf3(noiseModel::Unit::Create(1), {L(1)});
TestFactor1 tf4(noiseModel::Unit::Create(1), gtsam::Symbol('L', 1));
}
/* ************************************************************************* */
class TestFactor4 : public NoiseModelFactor4<double, double, double, double> {
static_assert(std::is_same<Base, NoiseModelFactor>::value, "Base type wrong");
static_assert(
std::is_same<This,
NoiseModelFactor4<double, double, double, double>>::value,
"This type wrong");
public:
typedef NoiseModelFactor4<double, double, double, double> Base;
TestFactor4() : Base(noiseModel::Diagonal::Sigmas((Vector(1) << 2.0).finished()), X(1), X(2), X(3), X(4)) {}
using Base::NoiseModelFactor4; // inherit constructors
Vector
evaluateError(const double& x1, const double& x2, const double& x3, const double& x4,
boost::optional<Matrix&> H1 = boost::none,
boost::optional<Matrix&> H2 = boost::none,
boost::optional<Matrix&> H3 = boost::none,
boost::optional<Matrix&> H4 = boost::none) const override {
if(H1) {
*H1 = (Matrix(1, 1) << 1.0).finished();
*H2 = (Matrix(1, 1) << 2.0).finished();
*H3 = (Matrix(1, 1) << 3.0).finished();
*H4 = (Matrix(1, 1) << 4.0).finished();
}
return (Vector(1) << x1 + 2.0 * x2 + 3.0 * x3 + 4.0 * x4).finished();
}
gtsam::NonlinearFactor::shared_ptr clone() const override {
return boost::static_pointer_cast<gtsam::NonlinearFactor>(
gtsam::NonlinearFactor::shared_ptr(new TestFactor4(*this))); }
};
/* ************************************ */
TEST(NonlinearFactor, NoiseModelFactor4) {
TestFactor4 tf;
Values tv;
tv.insert(X(1), double((1.0)));
tv.insert(X(2), double((2.0)));
tv.insert(X(3), double((3.0)));
tv.insert(X(4), double((4.0)));
EXPECT(assert_equal((Vector(1) << 30.0).finished(), tf.unwhitenedError(tv)));
DOUBLES_EQUAL(0.5 * 30.0 * 30.0 / 4.0, tf.error(tv), 1e-9);
JacobianFactor jf(*boost::dynamic_pointer_cast<JacobianFactor>(tf.linearize(tv)));
LONGS_EQUAL((long)X(1), (long)jf.keys()[0]);
LONGS_EQUAL((long)X(2), (long)jf.keys()[1]);
LONGS_EQUAL((long)X(3), (long)jf.keys()[2]);
LONGS_EQUAL((long)X(4), (long)jf.keys()[3]);
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 0.5).finished(), jf.getA(jf.begin())));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 1.0).finished(), jf.getA(jf.begin()+1)));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 1.5).finished(), jf.getA(jf.begin()+2)));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 2.0).finished(), jf.getA(jf.begin()+3)));
EXPECT(assert_equal((Vector)(Vector(1) << 0.5 * -30.).finished(), jf.getb()));
// Test all functions/types for backwards compatibility
static_assert(std::is_same<TestFactor4::X1, double>::value,
"X1 type incorrect");
static_assert(std::is_same<TestFactor4::X2, double>::value,
"X2 type incorrect");
static_assert(std::is_same<TestFactor4::X3, double>::value,
"X3 type incorrect");
static_assert(std::is_same<TestFactor4::X4, double>::value,
"X4 type incorrect");
EXPECT(assert_equal(tf.key1(), X(1)));
EXPECT(assert_equal(tf.key2(), X(2)));
EXPECT(assert_equal(tf.key3(), X(3)));
EXPECT(assert_equal(tf.key4(), X(4)));
std::vector<Matrix> H = {Matrix(), Matrix(), Matrix(), Matrix()};
EXPECT(assert_equal(Vector1(30.0), tf.unwhitenedError(tv, H)));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 1.).finished(), H.at(0)));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 2.).finished(), H.at(1)));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 3.).finished(), H.at(2)));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 4.).finished(), H.at(3)));
// And test "forward compatibility" using `key<N>` and `ValueType<N>` too
static_assert(std::is_same<TestFactor4::ValueType<1>, double>::value,
"ValueType<1> type incorrect");
static_assert(std::is_same<TestFactor4::ValueType<2>, double>::value,
"ValueType<2> type incorrect");
static_assert(std::is_same<TestFactor4::ValueType<3>, double>::value,
"ValueType<3> type incorrect");
static_assert(std::is_same<TestFactor4::ValueType<4>, double>::value,
"ValueType<4> type incorrect");
EXPECT(assert_equal(tf.key<1>(), X(1)));
EXPECT(assert_equal(tf.key<2>(), X(2)));
EXPECT(assert_equal(tf.key<3>(), X(3)));
EXPECT(assert_equal(tf.key<4>(), X(4)));
// Test constructors
TestFactor4 tf2(noiseModel::Unit::Create(1), L(1), L(2), L(3), L(4));
TestFactor4 tf3(noiseModel::Unit::Create(1), {L(1), L(2), L(3), L(4)});
TestFactor4 tf4(noiseModel::Unit::Create(1),
std::array<Key, 4>{L(1), L(2), L(3), L(4)});
std::vector<Key> keys = {L(1), L(2), L(3), L(4)};
TestFactor4 tf5(noiseModel::Unit::Create(1), keys);
}
/* ************************************************************************* */
class TestFactor5 : public NoiseModelFactor5<double, double, double, double, double> {
public:
typedef NoiseModelFactor5<double, double, double, double, double> Base;
TestFactor5() : Base(noiseModel::Diagonal::Sigmas((Vector(1) << 2.0).finished()), X(1), X(2), X(3), X(4), X(5)) {}
Vector
evaluateError(const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5,
boost::optional<Matrix&> H1 = boost::none,
boost::optional<Matrix&> H2 = boost::none,
boost::optional<Matrix&> H3 = boost::none,
boost::optional<Matrix&> H4 = boost::none,
boost::optional<Matrix&> H5 = boost::none) const override {
if(H1) {
*H1 = (Matrix(1, 1) << 1.0).finished();
*H2 = (Matrix(1, 1) << 2.0).finished();
*H3 = (Matrix(1, 1) << 3.0).finished();
*H4 = (Matrix(1, 1) << 4.0).finished();
*H5 = (Matrix(1, 1) << 5.0).finished();
}
return (Vector(1) << x1 + 2.0 * x2 + 3.0 * x3 + 4.0 * x4 + 5.0 * x5)
.finished();
}
};
/* ************************************ */
TEST(NonlinearFactor, NoiseModelFactor5) {
TestFactor5 tf;
Values tv;
tv.insert(X(1), double((1.0)));
tv.insert(X(2), double((2.0)));
tv.insert(X(3), double((3.0)));
tv.insert(X(4), double((4.0)));
tv.insert(X(5), double((5.0)));
EXPECT(assert_equal((Vector(1) << 55.0).finished(), tf.unwhitenedError(tv)));
DOUBLES_EQUAL(0.5 * 55.0 * 55.0 / 4.0, tf.error(tv), 1e-9);
JacobianFactor jf(*boost::dynamic_pointer_cast<JacobianFactor>(tf.linearize(tv)));
LONGS_EQUAL((long)X(1), (long)jf.keys()[0]);
LONGS_EQUAL((long)X(2), (long)jf.keys()[1]);
LONGS_EQUAL((long)X(3), (long)jf.keys()[2]);
LONGS_EQUAL((long)X(4), (long)jf.keys()[3]);
LONGS_EQUAL((long)X(5), (long)jf.keys()[4]);
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 0.5).finished(), jf.getA(jf.begin())));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 1.0).finished(), jf.getA(jf.begin()+1)));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 1.5).finished(), jf.getA(jf.begin()+2)));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 2.0).finished(), jf.getA(jf.begin()+3)));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 2.5).finished(), jf.getA(jf.begin()+4)));
EXPECT(assert_equal((Vector)(Vector(1) << 0.5 * -55.).finished(), jf.getb()));
}
/* ************************************************************************* */
class TestFactor6 : public NoiseModelFactor6<double, double, double, double, double, double> {
public:
typedef NoiseModelFactor6<double, double, double, double, double, double> Base;
TestFactor6() : Base(noiseModel::Diagonal::Sigmas((Vector(1) << 2.0).finished()), X(1), X(2), X(3), X(4), X(5), X(6)) {}
Vector
evaluateError(const X1& x1, const X2& x2, const X3& x3, const X4& x4, const X5& x5, const X6& x6,
boost::optional<Matrix&> H1 = boost::none,
boost::optional<Matrix&> H2 = boost::none,
boost::optional<Matrix&> H3 = boost::none,
boost::optional<Matrix&> H4 = boost::none,
boost::optional<Matrix&> H5 = boost::none,
boost::optional<Matrix&> H6 = boost::none) const override {
if(H1) {
*H1 = (Matrix(1, 1) << 1.0).finished();
*H2 = (Matrix(1, 1) << 2.0).finished();
*H3 = (Matrix(1, 1) << 3.0).finished();
*H4 = (Matrix(1, 1) << 4.0).finished();
*H5 = (Matrix(1, 1) << 5.0).finished();
*H6 = (Matrix(1, 1) << 6.0).finished();
}
return (Vector(1) << x1 + 2.0 * x2 + 3.0 * x3 + 4.0 * x4 + 5.0 * x5 +
6.0 * x6)
.finished();
}
};
/* ************************************ */
TEST(NonlinearFactor, NoiseModelFactor6) {
TestFactor6 tf;
Values tv;
tv.insert(X(1), double((1.0)));
tv.insert(X(2), double((2.0)));
tv.insert(X(3), double((3.0)));
tv.insert(X(4), double((4.0)));
tv.insert(X(5), double((5.0)));
tv.insert(X(6), double((6.0)));
EXPECT(assert_equal((Vector(1) << 91.0).finished(), tf.unwhitenedError(tv)));
DOUBLES_EQUAL(0.5 * 91.0 * 91.0 / 4.0, tf.error(tv), 1e-9);
JacobianFactor jf(*boost::dynamic_pointer_cast<JacobianFactor>(tf.linearize(tv)));
LONGS_EQUAL((long)X(1), (long)jf.keys()[0]);
LONGS_EQUAL((long)X(2), (long)jf.keys()[1]);
LONGS_EQUAL((long)X(3), (long)jf.keys()[2]);
LONGS_EQUAL((long)X(4), (long)jf.keys()[3]);
LONGS_EQUAL((long)X(5), (long)jf.keys()[4]);
LONGS_EQUAL((long)X(6), (long)jf.keys()[5]);
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 0.5).finished(), jf.getA(jf.begin())));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 1.0).finished(), jf.getA(jf.begin()+1)));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 1.5).finished(), jf.getA(jf.begin()+2)));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 2.0).finished(), jf.getA(jf.begin()+3)));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 2.5).finished(), jf.getA(jf.begin()+4)));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 3.0).finished(), jf.getA(jf.begin()+5)));
EXPECT(assert_equal((Vector)(Vector(1) << 0.5 * -91.).finished(), jf.getb()));
}
/* ************************************************************************* */
class TestFactorN : public NoiseModelFactorN<double, double, double, double> {
public:
typedef NoiseModelFactorN<double, double, double, double> Base;
using Type1 = ValueType<1>; // Test that we can use the ValueType<> template
TestFactorN() : Base(noiseModel::Diagonal::Sigmas((Vector(1) << 2.0).finished()), X(1), X(2), X(3), X(4)) {}
Vector
evaluateError(const double& x1, const double& x2, const double& x3, const double& x4,
boost::optional<Matrix&> H1 = boost::none,
boost::optional<Matrix&> H2 = boost::none,
boost::optional<Matrix&> H3 = boost::none,
boost::optional<Matrix&> H4 = boost::none) const override {
if (H1) *H1 = (Matrix(1, 1) << 1.0).finished();
if (H2) *H2 = (Matrix(1, 1) << 2.0).finished();
if (H3) *H3 = (Matrix(1, 1) << 3.0).finished();
if (H4) *H4 = (Matrix(1, 1) << 4.0).finished();
return (Vector(1) << x1 + 2.0 * x2 + 3.0 * x3 + 4.0 * x4).finished();
}
Key key1() const { return key<1>(); } // Test that we can use key<> template
};
/* ************************************ */
TEST(NonlinearFactor, NoiseModelFactorN) {
TestFactorN tf;
Values tv;
tv.insert(X(1), double((1.0)));
tv.insert(X(2), double((2.0)));
tv.insert(X(3), double((3.0)));
tv.insert(X(4), double((4.0)));
EXPECT(assert_equal((Vector(1) << 30.0).finished(), tf.unwhitenedError(tv)));
DOUBLES_EQUAL(0.5 * 30.0 * 30.0 / 4.0, tf.error(tv), 1e-9);
JacobianFactor jf(*boost::dynamic_pointer_cast<JacobianFactor>(tf.linearize(tv)));
LONGS_EQUAL((long)X(1), (long)jf.keys()[0]);
LONGS_EQUAL((long)X(2), (long)jf.keys()[1]);
LONGS_EQUAL((long)X(3), (long)jf.keys()[2]);
LONGS_EQUAL((long)X(4), (long)jf.keys()[3]);
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 0.5).finished(), jf.getA(jf.begin())));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 1.0).finished(), jf.getA(jf.begin()+1)));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 1.5).finished(), jf.getA(jf.begin()+2)));
EXPECT(assert_equal((Matrix)(Matrix(1, 1) << 2.0).finished(), jf.getA(jf.begin()+3)));
EXPECT(assert_equal((Vector)(Vector(1) << -0.5 * 30.).finished(), jf.getb()));
// Test all evaluateError argument overloads to ensure backward compatibility
Matrix H1_expected, H2_expected, H3_expected, H4_expected;
Vector e_expected = tf.evaluateError(9, 8, 7, 6, H1_expected, H2_expected,
H3_expected, H4_expected);
std::unique_ptr<NoiseModelFactorN<double, double, double, double>> base_ptr(
new TestFactorN(tf));
Matrix H1, H2, H3, H4;
EXPECT(assert_equal(e_expected, base_ptr->evaluateError(9, 8, 7, 6)));
EXPECT(assert_equal(e_expected, base_ptr->evaluateError(9, 8, 7, 6, H1)));
EXPECT(assert_equal(H1_expected, H1));
EXPECT(assert_equal(e_expected, //
base_ptr->evaluateError(9, 8, 7, 6, H1, H2)));
EXPECT(assert_equal(H1_expected, H1));
EXPECT(assert_equal(H2_expected, H2));
EXPECT(assert_equal(e_expected,
base_ptr->evaluateError(9, 8, 7, 6, H1, H2, H3)));
EXPECT(assert_equal(H1_expected, H1));
EXPECT(assert_equal(H2_expected, H2));
EXPECT(assert_equal(H3_expected, H3));
EXPECT(assert_equal(e_expected,
base_ptr->evaluateError(9, 8, 7, 6, H1, H2, H3, H4)));
EXPECT(assert_equal(H1_expected, H1));
EXPECT(assert_equal(H2_expected, H2));
EXPECT(assert_equal(H3_expected, H3));
EXPECT(assert_equal(H4_expected, H4));
// Test all functions/types for backwards compatibility
static_assert(std::is_same<TestFactor4::X1, double>::value,
"X1 type incorrect");
EXPECT(assert_equal(tf.key3(), X(3)));
// Test using `key<N>` and `ValueType<N>`
static_assert(std::is_same<TestFactorN::ValueType<1>, double>::value,
"ValueType<1> type incorrect");
static_assert(std::is_same<TestFactorN::ValueType<2>, double>::value,
"ValueType<2> type incorrect");
static_assert(std::is_same<TestFactorN::ValueType<3>, double>::value,
"ValueType<3> type incorrect");
static_assert(std::is_same<TestFactorN::ValueType<4>, double>::value,
"ValueType<4> type incorrect");
static_assert(std::is_same<TestFactorN::Type1, double>::value,
"TestFactorN::Type1 type incorrect");
EXPECT(assert_equal(tf.key<1>(), X(1)));
EXPECT(assert_equal(tf.key<2>(), X(2)));
EXPECT(assert_equal(tf.key<3>(), X(3)));
EXPECT(assert_equal(tf.key<4>(), X(4)));
EXPECT(assert_equal(tf.key1(), X(1)));
}
/* ************************************************************************* */
TEST( NonlinearFactor, clone_rekey )
{
shared_nlf init(new TestFactor4());
EXPECT_LONGS_EQUAL((long)X(1), (long)init->keys()[0]);
EXPECT_LONGS_EQUAL((long)X(2), (long)init->keys()[1]);
EXPECT_LONGS_EQUAL((long)X(3), (long)init->keys()[2]);
EXPECT_LONGS_EQUAL((long)X(4), (long)init->keys()[3]);
// Standard clone
shared_nlf actClone = init->clone();
EXPECT(actClone.get() != init.get()); // Ensure different pointers
EXPECT(assert_equal(*init, *actClone));
// Re-key factor - clones with different keys
KeyVector new_keys {X(5),X(6),X(7),X(8)};
shared_nlf actRekey = init->rekey(new_keys);
EXPECT(actRekey.get() != init.get()); // Ensure different pointers
// Ensure init is unchanged
EXPECT_LONGS_EQUAL((long)X(1), (long)init->keys()[0]);
EXPECT_LONGS_EQUAL((long)X(2), (long)init->keys()[1]);
EXPECT_LONGS_EQUAL((long)X(3), (long)init->keys()[2]);
EXPECT_LONGS_EQUAL((long)X(4), (long)init->keys()[3]);
// Check new keys
EXPECT_LONGS_EQUAL((long)X(5), (long)actRekey->keys()[0]);
EXPECT_LONGS_EQUAL((long)X(6), (long)actRekey->keys()[1]);
EXPECT_LONGS_EQUAL((long)X(7), (long)actRekey->keys()[2]);
EXPECT_LONGS_EQUAL((long)X(8), (long)actRekey->keys()[3]);
}
/* ************************************************************************* */
int main() { TestResult tr; return TestRegistry::runAllTests(tr);}
/* ************************************************************************* */
|