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 }
gtsam::EliminateableFactorGraph::eliminateSequential
std::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Definition: EliminateableFactorGraph-inst.h:29
Eigen::internal::print
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
Definition: NEON/PacketMath.h:3115
gtsam::DiscreteFactorGraph
Definition: DiscreteFactorGraph.h:98
gtsam::BayesNet::print
void print(const std::string &s="BayesNet", const KeyFormatter &formatter=DefaultKeyFormatter) const override
Definition: BayesNet-inst.h:31
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
DiscreteFactorGraph.h
main
int main(int argc, char **argv)
Definition: HMMExample.cpp:29
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
ss
static std::stringstream ss
Definition: testBTree.cpp:31
gtsam::DiscreteBayesNet
Definition: DiscreteBayesNet.h:38
gtsam::DiscreteMarginals
Definition: DiscreteMarginals.h:33
GTSAM_PRINT
#define GTSAM_PRINT(x)
Definition: Testable.h:43
BayesNet.h
Bayes network.
DiscreteMarginals.h
A class for computing marginals in a DiscreteFactorGraph.
ordering
static enum @1096 ordering
gtsam::DiscreteBayesNet::add
void add(const DiscreteKey &key, const std::string &spec)
Definition: DiscreteBayesNet.h:85
gtsam
traits
Definition: SFMdata.h:40
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std
Definition: BFloat16.h:88
gtsam::DiscreteBayesNet::shared_ptr
std::shared_ptr< This > shared_ptr
Definition: DiscreteBayesNet.h:43
gtsam::DiscreteFactorGraph::optimize
DiscreteValues optimize(OptionalOrderingType orderingType={}) const
Find the maximum probable explanation (MPE) by doing max-product.
Definition: DiscreteFactorGraph.cpp:189
marginals
Marginals marginals(graph, result)
gtsam::Ordering
Definition: inference/Ordering.h:33


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:24