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  // Uses max-product.
72  auto optimalDecoding = graph.optimize();
73  optimalDecoding.print("\nMost Probable Explanation (optimalDecoding)\n");
74 
75  // "Inference" Computing marginals for each node
76  // Here we'll make use of DiscreteMarginals class, which makes use of
77  // bayes-tree based shortcut evaluation of marginals
79 
80  cout << "\nComputing Node Marginals ..(BayesTree based)" << endl;
81  gttic_(Multifrontal);
82  for (vector<DiscreteKey>::iterator it = nodes.begin(); it != nodes.end();
83  ++it) {
84  // Compute the marginal
85  Vector margProbs = marginals.marginalProbabilities(*it);
86 
87  // Print the marginals
88  cout << "Node#" << setw(4) << it->first << " : ";
89  print(margProbs);
90  }
91  gttoc_(Multifrontal);
92 
93  tictoc_print_();
94  return 0;
95 }
int main(int argc, char **argv)
Definition: UGM_chain.cpp:31
#define gttic_(label)
Definition: timing.h:245
KeyVector nodes
Definition: testMFAS.cpp:28
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.
size_t size() const
Definition: FactorGraph.h:334
Eigen::VectorXd Vector
Definition: Vector.h:38
Vector marginalProbabilities(const DiscreteKey &key) const
void tictoc_print_()
Definition: timing.h:268
A class for computing marginals in a DiscreteFactorGraph.
traits
Definition: chartTesting.h:28
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
#define gttoc_(label)
Definition: timing.h:250
Marginals marginals(graph, result)
Timing utilities.


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