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 }
timing.h
Timing utilities.
Eigen::internal::print
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
Definition: NEON/PacketMath.h:3115
gtsam::DiscreteFactorGraph
Definition: DiscreteFactorGraph.h:98
DiscreteFactorGraph.h
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
gttoc_
#define gttoc_(label)
Definition: timing.h:250
gttic_
#define gttic_(label)
Definition: timing.h:245
gtsam::DiscreteMarginals
Definition: DiscreteMarginals.h:33
gtsam::tictoc_print_
void tictoc_print_()
Definition: timing.h:268
DiscreteMarginals.h
A class for computing marginals in a DiscreteFactorGraph.
gtsam::FactorGraph::size
size_t size() const
Definition: FactorGraph.h:297
nodes
KeyVector nodes
Definition: testMFAS.cpp:28
gtsam
traits
Definition: SFMdata.h:40
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std
Definition: BFloat16.h:88
main
int main(int argc, char **argv)
Definition: UGM_chain.cpp:31
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::NonlinearFactorGraph::print
void print(const std::string &str="NonlinearFactorGraph: ", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Definition: NonlinearFactorGraph.cpp:55


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:43:02