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  // Create solver and eliminate
63  // This will create a DAG ordered with arrow of time reversed
65  factorGraph.eliminateSequential(ordering);
66  chordal->print("Eliminated");
67 
68  // solve
69  DiscreteFactor::sharedValues mpe = chordal->optimize();
70  GTSAM_PRINT(*mpe);
71 
72  // We can also sample from it
73  cout << "\n10 samples:" << endl;
74  for (size_t k = 0; k < 10; k++) {
75  DiscreteFactor::sharedValues 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 }
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
int main(int argc, char **argv)
Definition: HMMExample.cpp:29
Vector marginalProbabilities(const DiscreteKey &key) const
static enum @843 ordering
void add(const Signature &s)
Definition: Half.h:150
Bayes network.
Eigen::VectorXd Vector
Definition: Vector.h:38
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:34
A class for computing marginals in a DiscreteFactorGraph.
static std::stringstream ss
Definition: testBTree.cpp:33
traits
Definition: chartTesting.h:28
#define GTSAM_PRINT(x)
Definition: Testable.h:41
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
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 Sat May 8 2021 02:42:10