HMMExample.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010-2020, 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 
23 #include <iomanip>
24 #include <sstream>
25 
26 using namespace std;
27 using namespace gtsam;
28 
29 int main(int argc, char **argv) {
30  const int nrNodes = 4;
31  const size_t nrStates = 3;
32 
33  // Define variables as well as ordering
35  vector<DiscreteKey> keys;
36  for (int k = 0; k < nrNodes; k++) {
37  DiscreteKey key_i(k, nrStates);
38  keys.push_back(key_i);
39  ordering.emplace_back(k);
40  }
41 
42  // Create HMM as a DiscreteBayesNet
43  DiscreteBayesNet hmm;
44 
45  // Define backbone
46  const string transition = "8/1/1 1/8/1 1/1/8";
47  for (int k = 1; k < nrNodes; k++) {
48  hmm.add(keys[k] | keys[k - 1] = transition);
49  }
50 
51  // Add some measurements, not needed for all time steps!
52  hmm.add(keys[0] % "7/2/1");
53  hmm.add(keys[1] % "1/9/0");
54  hmm.add(keys.back() % "5/4/1");
55 
56  // print
57  hmm.print("HMM");
58 
59  // Convert to factor graph
60  DiscreteFactorGraph factorGraph(hmm);
61 
62  // Do max-prodcut
63  auto mpe = factorGraph.optimize();
64  GTSAM_PRINT(mpe);
65 
66  // Create solver and eliminate
67  // This will create a DAG ordered with arrow of time reversed
69  factorGraph.eliminateSequential(ordering);
70  chordal->print("Eliminated");
71 
72  // We can also sample from it
73  cout << "\n10 samples:" << endl;
74  for (size_t k = 0; k < 10; k++) {
75  auto sample = chordal->sample();
76  GTSAM_PRINT(sample);
77  }
78 
79  // Or compute the marginals. This re-eliminates the FG into a Bayes tree
80  cout << "\nComputing Node Marginals .." << endl;
81  DiscreteMarginals marginals(factorGraph);
82  for (int k = 0; k < nrNodes; k++) {
83  Vector margProbs = marginals.marginalProbabilities(keys[k]);
84  stringstream ss;
85  ss << "marginal " << k;
86  print(margProbs, ss.str());
87  }
88 
89  // TODO(frank): put in the glue to have DiscreteMarginals produce *arbitrary*
90  // joints efficiently, by the Bayes tree shortcut magic. All the code is there
91  // but it's not yet connected.
92 
93  return 0;
94 }
int main(int argc, char **argv)
Definition: HMMExample.cpp:29
Definition: BFloat16.h:88
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
Bayes network.
DiscreteValues optimize(OptionalOrderingType orderingType={}) const
Find the maximum probable explanation (MPE) by doing max-product.
static enum @1107 ordering
Eigen::VectorXd Vector
Definition: Vector.h:38
Vector marginalProbabilities(const DiscreteKey &key) const
std::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
A class for computing marginals in a DiscreteFactorGraph.
static std::stringstream ss
Definition: testBTree.cpp:31
traits
Definition: chartTesting.h:28
#define GTSAM_PRINT(x)
Definition: Testable.h:43
std::shared_ptr< This > shared_ptr
void add(const DiscreteKey &key, const std::string &spec)
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
const KeyVector keys
Marginals marginals(graph, result)
void print(const std::string &s="BayesNet", const KeyFormatter &formatter=DefaultKeyFormatter) const override
Definition: BayesNet-inst.h:31


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:19