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  vector<DiscreteFactor::Values> allPosbValues = cartesianProduct(
54  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  // We use sequential variable elimination
66  DiscreteFactor::sharedValues optimalDecoding = chordal->optimize();
67  optimalDecoding->print("\noptimalDecoding");
68 
69  // "Inference" Computing marginals
70  cout << "\nComputing Node Marginals .." << endl;
72 
73  Vector margProbs = marginals.marginalProbabilities(Cathy);
74  print(margProbs, "Cathy's Node Marginal:");
75 
76  margProbs = marginals.marginalProbabilities(Heather);
77  print(margProbs, "Heather's Node Marginal");
78 
79  margProbs = marginals.marginalProbabilities(Mark);
80  print(margProbs, "Mark's Node Marginal");
81 
82  margProbs = marginals.marginalProbabilities(Allison);
83  print(margProbs, "Allison's Node Marginal");
84 
85  return 0;
86 }
87 
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
std::vector< Assignment< L > > cartesianProduct(const std::vector< std::pair< L, size_t > > &keys)
Get Cartesian product consisting all possible configurations.
Definition: Assignment.h:62
void add(const DiscreteKey &j, SOURCE table)
Vector marginalProbabilities(const DiscreteKey &key) const
leaf::MyValues values
Definition: Half.h:150
NonlinearFactorGraph graph
Eigen::VectorXd Vector
Definition: Vector.h:38
int main(int argc, char **argv)
Definition: UGM_small.cpp:27
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:34
A class for computing marginals in a DiscreteFactorGraph.
traits
Definition: chartTesting.h:28
typedef and functions to augment Eigen&#39;s VectorXd
boost::shared_ptr< Values > sharedValues
boost::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType=boost::none, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none) const
boost::shared_ptr< This > shared_ptr
Marginals marginals(graph, result)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:51:19