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 
Eigen::internal::print
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
Definition: NEON/PacketMath.h:3115
gtsam::DiscreteFactorGraph
Definition: DiscreteFactorGraph.h:98
Vector.h
typedef and functions to augment Eigen's VectorXd
DiscreteFactorGraph.h
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
main
int main(int argc, char **argv)
Definition: UGM_small.cpp:27
gtsam::DiscreteMarginals
Definition: DiscreteMarginals.h:33
GTSAM_PRINT
#define GTSAM_PRINT(x)
Definition: Testable.h:43
DiscreteMarginals.h
A class for computing marginals in a DiscreteFactorGraph.
gtsam
traits
Definition: chartTesting.h:28
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
leaf::values
leaf::MyValues values
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std
Definition: BFloat16.h:88
gtsam::FactorGraph::add
IsDerived< DERIVEDFACTOR > add(std::shared_ptr< DERIVEDFACTOR > factor)
add is a synonym for push_back.
Definition: FactorGraph.h:171
graph
NonlinearFactorGraph graph
Definition: doc/Code/OdometryExample.cpp:2
marginals
Marginals marginals(graph, result)
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:07:52