test_ShonanAveraging.py
Go to the documentation of this file.
1 """
2 GTSAM Copyright 2010-2019, Georgia Tech Research Corporation,
3 Atlanta, Georgia 30332-0415
4 All Rights Reserved
5 
6 See LICENSE for the license information
7 
8 Unit tests for Shonan Rotation Averaging.
9 Author: Frank Dellaert
10 """
11 # pylint: disable=invalid-name, no-name-in-module, no-member
12 
13 import unittest
14 
15 import gtsam
16 from gtsam import ShonanAveraging3, ShonanAveragingParameters3
17 from gtsam.utils.test_case import GtsamTestCase
18 
19 DEFAULT_PARAMS = ShonanAveragingParameters3(
21 
22 
23 def fromExampleName(name: str, parameters=DEFAULT_PARAMS):
24  g2oFile = gtsam.findExampleDataFile(name)
25  return ShonanAveraging3(g2oFile, parameters)
26 
27 
29  """Tests for Shonan Rotation Averaging."""
30 
31  def setUp(self):
32  """Set up common variables."""
33  self.shonan = fromExampleName("toyExample.g2o")
34 
36  self.assertEqual(5, self.shonan.nrUnknowns())
37 
38  D = self.shonan.denseD()
39  self.assertEqual((15, 15), D.shape)
40 
41  Q = self.shonan.denseQ()
42  self.assertEqual((15, 15), Q.shape)
43 
44  L = self.shonan.denseL()
45  self.assertEqual((15, 15), L.shape)
46 
47  def test_buildGraphAt(self):
48  graph = self.shonan.buildGraphAt(5)
49  self.assertEqual(7, graph.size())
50 
52  random = self.shonan.initializeRandomlyAt(4)
53  lambdaMin = self.shonan.computeMinEigenValue(random)
54  self.assertAlmostEqual(-414.87376657555996,
55  lambdaMin, places=3) # Regression test
56  self.assertFalse(self.shonan.checkOptimality(random))
57 
59  initial = self.shonan.initializeRandomlyAt(3)
60  self.assertFalse(self.shonan.checkOptimality(initial))
61  result = self.shonan.tryOptimizingAt(3, initial)
62  self.assertTrue(self.shonan.checkOptimality(result))
63  lambdaMin = self.shonan.computeMinEigenValue(result)
64  self.assertAlmostEqual(-5.427688831332745e-07,
65  lambdaMin, places=3) # Regression test
66  self.assertAlmostEqual(0, self.shonan.costAt(3, result), places=3)
67  SO3Values = self.shonan.roundSolution(result)
68  self.assertAlmostEqual(0, self.shonan.cost(SO3Values), places=3)
69 
71  random = self.shonan.initializeRandomlyAt(4)
72  result = self.shonan.tryOptimizingAt(4, random)
73  self.assertTrue(self.shonan.checkOptimality(result))
74  self.assertAlmostEqual(0, self.shonan.costAt(4, result), places=2)
75  lambdaMin = self.shonan.computeMinEigenValue(result)
76  self.assertAlmostEqual(-5.427688831332745e-07,
77  lambdaMin, places=3) # Regression test
78  SO3Values = self.shonan.roundSolution(result)
79  self.assertAlmostEqual(0, self.shonan.cost(SO3Values), places=3)
80 
82  random = self.shonan.initializeRandomlyAt(3)
83  Qstar3 = self.shonan.tryOptimizingAt(3, random)
84  lambdaMin, minEigenVector = self.shonan.computeMinEigenVector(Qstar3)
85  initialQ4 = self.shonan.initializeWithDescent(
86  4, Qstar3, minEigenVector, lambdaMin)
87  self.assertAlmostEqual(5, initialQ4.size())
88 
89  def test_run(self):
90  initial = self.shonan.initializeRandomly()
91  result, lambdaMin = self.shonan.run(initial, 5, 10)
92  self.assertAlmostEqual(0, self.shonan.cost(result), places=2)
93  self.assertAlmostEqual(-5.427688831332745e-07,
94  lambdaMin, places=3) # Regression test
95 
97  # Load 2D toy example
99  # lmParams.setVerbosityLM("SUMMARY")
100  g2oFile = gtsam.findExampleDataFile("noisyToyGraph.txt")
101  parameters = gtsam.ShonanAveragingParameters2(lmParams)
102  shonan = gtsam.ShonanAveraging2(g2oFile, parameters)
103  self.assertAlmostEqual(4, shonan.nrUnknowns())
104 
105  # Check graph building
106  graph = shonan.buildGraphAt(2)
107  self.assertAlmostEqual(6, graph.size())
108  initial = shonan.initializeRandomly()
109  result, lambdaMin = shonan.run(initial, 2, 10)
110  self.assertAlmostEqual(0.0008211, shonan.cost(result), places=5)
111  self.assertAlmostEqual(0, lambdaMin, places=9) # certificate!
112 
113  # Test alpha/beta/gamma prior weighting.
114  def test_PriorWeights(self):
116  params = ShonanAveragingParameters3(lmParams)
117  self.assertAlmostEqual(0, params.getAnchorWeight(), 1e-9)
118  self.assertAlmostEqual(1, params.getKarcherWeight(), 1e-9)
119  self.assertAlmostEqual(0, params.getGaugesWeight(), 1e-9)
120  alpha, beta, gamma = 100.0, 200.0, 300.0
121  params.setAnchorWeight(alpha)
122  params.setKarcherWeight(beta)
123  params.setGaugesWeight(gamma)
124  self.assertAlmostEqual(alpha, params.getAnchorWeight(), 1e-9)
125  self.assertAlmostEqual(beta, params.getKarcherWeight(), 1e-9)
126  self.assertAlmostEqual(gamma, params.getGaugesWeight(), 1e-9)
127  params.setKarcherWeight(0)
128  shonan = fromExampleName("Klaus3.g2o", params)
129 
130  initial = gtsam.Values()
131  for i in range(3):
132  initial.insert(i, gtsam.Rot3())
133  self.assertAlmostEqual(3.0756, shonan.cost(initial), places=3)
134  result, _lambdaMin = shonan.run(initial, 3, 3)
135  self.assertAlmostEqual(0.0015, shonan.cost(result), places=3)
136 
137 
138 if __name__ == '__main__':
139  unittest.main()
static LevenbergMarquardtParams CeresDefaults()
Parameters governing optimization etc.
ShonanAveragingParameters< 3 > ShonanAveragingParameters3
GTSAM_EXPORT std::string findExampleDataFile(const std::string &name)
Definition: dataset.cpp:65


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