UGM_chain.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 
22 #include <gtsam/base/timing.h>
25 
26 #include <iomanip>
27 
28 using namespace std;
29 using namespace gtsam;
30 
31 int main(int argc, char** argv) {
32  // Set Number of Nodes in the Graph
33  const int nrNodes = 60;
34 
35  // Each node takes 1 of 7 possible states denoted by 0-6 in following order:
36  // ["VideoGames" "Industry" "GradSchool" "VideoGames(with PhD)"
37  // "Industry(with PhD)" "Academia" "Deceased"]
38  const size_t nrStates = 7;
39 
40  // define variables
41  vector<DiscreteKey> nodes;
42  for (int i = 0; i < nrNodes; i++) {
43  DiscreteKey dk(i, nrStates);
44  nodes.push_back(dk);
45  }
46 
47  // create graph
49 
50  // add node potentials
51  graph.add(nodes[0], ".3 .6 .1 0 0 0 0");
52  for (int i = 1; i < nrNodes; i++) graph.add(nodes[i], "1 1 1 1 1 1 1");
53 
54  const std::string edgePotential =
55  ".08 .9 .01 0 0 0 .01 "
56  ".03 .95 .01 0 0 0 .01 "
57  ".06 .06 .75 .05 .05 .02 .01 "
58  "0 0 0 .3 .6 .09 .01 "
59  "0 0 0 .02 .95 .02 .01 "
60  "0 0 0 .01 .01 .97 .01 "
61  "0 0 0 0 0 0 1";
62 
63  // add edge potentials
64  for (int i = 0; i < nrNodes - 1; i++)
65  graph.add(nodes[i] & nodes[i + 1], edgePotential);
66 
67  cout << "Created Factor Graph with " << nrNodes << " variable nodes and "
68  << graph.size() << " factors (Unary+Edge).";
69 
70  // "Decoding", i.e., configuration with largest value
71  // We use sequential variable elimination
73  DiscreteFactor::sharedValues optimalDecoding = chordal->optimize();
74  optimalDecoding->print("\nMost Probable Explanation (optimalDecoding)\n");
75 
76  // "Inference" Computing marginals for each node
77  // Here we'll make use of DiscreteMarginals class, which makes use of
78  // bayes-tree based shortcut evaluation of marginals
80 
81  cout << "\nComputing Node Marginals ..(BayesTree based)" << endl;
82  gttic_(Multifrontal);
83  for (vector<DiscreteKey>::iterator it = nodes.begin(); it != nodes.end();
84  ++it) {
85  // Compute the marginal
86  Vector margProbs = marginals.marginalProbabilities(*it);
87 
88  // Print the marginals
89  cout << "Node#" << setw(4) << it->first << " : ";
90  print(margProbs);
91  }
92  gttoc_(Multifrontal);
93 
94  tictoc_print_();
95  return 0;
96 }
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
size_t size() const
Definition: FactorGraph.h:306
int main(int argc, char **argv)
Definition: UGM_chain.cpp:31
void add(const DiscreteKey &j, SOURCE table)
#define gttic_(label)
Definition: timing.h:230
Vector marginalProbabilities(const DiscreteKey &key) const
KeyVector nodes
Definition: testMFAS.cpp:28
Definition: Half.h:150
NonlinearFactorGraph graph
Eigen::VectorXd Vector
Definition: Vector.h:38
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:34
void tictoc_print_()
Definition: timing.h:253
A class for computing marginals in a DiscreteFactorGraph.
traits
Definition: chartTesting.h:28
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
#define gttoc_(label)
Definition: timing.h:235
Marginals marginals(graph, result)
Timing utilities.


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