UGM_small.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 #include <gtsam/base/Vector.h>
23 
24 using namespace std;
25 using namespace gtsam;
26 
27 int main(int argc, char** argv) {
28 
29  // We will assume 2-state variables, where, to conform to the "small" example
30  // we have 0 == "right answer" and 1 == "wrong answer"
31  size_t nrStates = 2;
32 
33  // define variables
34  DiscreteKey Cathy(1, nrStates), Heather(2, nrStates), Mark(3, nrStates),
35  Allison(4, nrStates);
36 
37  // create graph
39 
40  // add node potentials
41  graph.add(Cathy, "1 3");
42  graph.add(Heather, "9 1");
43  graph.add(Mark, "1 3");
44  graph.add(Allison, "9 1");
45 
46  // add edge potentials
47  graph.add(Cathy & Heather, "2 1 1 2");
48  graph.add(Heather & Mark, "2 1 1 2");
49  graph.add(Mark & Allison, "2 1 1 2");
50 
51  // Print the UGM distribution
52  cout << "\nUGM distribution:" << endl;
53  auto allPosbValues =
54  DiscreteValues::CartesianProduct(Cathy & Heather & Mark & Allison);
55  for (size_t i = 0; i < allPosbValues.size(); ++i) {
56  DiscreteFactor::Values values = allPosbValues[i];
57  double prodPot = graph(values);
58  cout << values[Cathy.first] << " " << values[Heather.first] << " "
59  << values[Mark.first] << " " << values[Allison.first] << " :\t"
60  << prodPot << "\t" << prodPot / 3790 << endl;
61  }
62 
63  // "Decoding", i.e., configuration with largest value (MPE)
64  // Uses max-product
65  auto optimalDecoding = graph.optimize();
66  GTSAM_PRINT(optimalDecoding);
67 
68  // "Inference" Computing marginals
69  cout << "\nComputing Node Marginals .." << endl;
71 
72  Vector margProbs = marginals.marginalProbabilities(Cathy);
73  print(margProbs, "Cathy's Node Marginal:");
74 
75  margProbs = marginals.marginalProbabilities(Heather);
76  print(margProbs, "Heather's Node Marginal");
77 
78  margProbs = marginals.marginalProbabilities(Mark);
79  print(margProbs, "Mark's Node Marginal");
80 
81  margProbs = marginals.marginalProbabilities(Allison);
82  print(margProbs, "Allison's Node Marginal");
83 
84  return 0;
85 }
86 
leaf::MyValues values
Definition: BFloat16.h:88
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
NonlinearFactorGraph graph
DiscreteValues optimize(OptionalOrderingType orderingType={}) const
Find the maximum probable explanation (MPE) by doing max-product.
Eigen::VectorXd Vector
Definition: Vector.h:38
int main(int argc, char **argv)
Definition: UGM_small.cpp:27
Vector marginalProbabilities(const DiscreteKey &key) const
A class for computing marginals in a DiscreteFactorGraph.
traits
Definition: chartTesting.h:28
typedef and functions to augment Eigen&#39;s VectorXd
std::vector< float > Values
#define GTSAM_PRINT(x)
Definition: Testable.h:43
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
Marginals marginals(graph, result)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:40:39