test_DiscreteBayesTree.py
Go to the documentation of this file.
1 """
2 GTSAM Copyright 2010-2021, 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 Discrete Bayes trees.
9 Author: Frank Dellaert
10 """
11 
12 # pylint: disable=no-name-in-module, invalid-name
13 
14 import unittest
15 
16 from gtsam import (DiscreteBayesNet, DiscreteBayesTreeClique,
17  DiscreteConditional, DiscreteFactorGraph, Ordering)
18 from gtsam.utils.test_case import GtsamTestCase
19 
20 
22  """Tests for Discrete Bayes Nets."""
23 
24  def test_elimination(self):
25  """Test Multifrontal elimination."""
26 
27  # Define DiscreteKey pairs.
28  keys = [(j, 2) for j in range(15)]
29 
30  # Create thin-tree Bayesnet.
31  bayesNet = DiscreteBayesNet()
32 
33  bayesNet.add(keys[0], [keys[8], keys[12]], "2/3 1/4 3/2 4/1")
34  bayesNet.add(keys[1], [keys[8], keys[12]], "4/1 2/3 3/2 1/4")
35  bayesNet.add(keys[2], [keys[9], keys[12]], "1/4 8/2 2/3 4/1")
36  bayesNet.add(keys[3], [keys[9], keys[12]], "1/4 2/3 3/2 4/1")
37 
38  bayesNet.add(keys[4], [keys[10], keys[13]], "2/3 1/4 3/2 4/1")
39  bayesNet.add(keys[5], [keys[10], keys[13]], "4/1 2/3 3/2 1/4")
40  bayesNet.add(keys[6], [keys[11], keys[13]], "1/4 3/2 2/3 4/1")
41  bayesNet.add(keys[7], [keys[11], keys[13]], "1/4 2/3 3/2 4/1")
42 
43  bayesNet.add(keys[8], [keys[12], keys[14]], "T 1/4 3/2 4/1")
44  bayesNet.add(keys[9], [keys[12], keys[14]], "4/1 2/3 F 1/4")
45  bayesNet.add(keys[10], [keys[13], keys[14]], "1/4 3/2 2/3 4/1")
46  bayesNet.add(keys[11], [keys[13], keys[14]], "1/4 2/3 3/2 4/1")
47 
48  bayesNet.add(keys[12], [keys[14]], "3/1 3/1")
49  bayesNet.add(keys[13], [keys[14]], "1/3 3/1")
50 
51  bayesNet.add(keys[14], "1/3")
52 
53  # Create a factor graph out of the Bayes net.
54  factorGraph = DiscreteFactorGraph(bayesNet)
55 
56  # Create a BayesTree out of the factor graph.
57  ordering = Ordering()
58  for j in range(15):
59  ordering.push_back(j)
60  bayesTree = factorGraph.eliminateMultifrontal(ordering)
61 
62  # Uncomment these for visualization:
63  # print(bayesTree)
64  # for key in range(15):
65  # bayesTree[key].printSignature()
66  # bayesTree.saveGraph("test_DiscreteBayesTree.dot")
67 
68  self.assertFalse(bayesTree.empty())
69  self.assertEqual(12, bayesTree.size())
70 
71  # The root is P( 8 12 14), we can retrieve it by key:
72  root = bayesTree[8]
73  self.assertIsInstance(root, DiscreteBayesTreeClique)
74  self.assertTrue(root.isRoot())
75  self.assertIsInstance(root.conditional(), DiscreteConditional)
76 
77 
78 if __name__ == "__main__":
79  unittest.main()
Double_ range(const Point2_ &p, const Point2_ &q)


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