DiscreteBayesNetExample.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 
23 #include <iomanip>
24 
25 using namespace std;
26 using namespace gtsam;
27 
28 int main(int argc, char **argv) {
29  DiscreteBayesNet asia;
30  DiscreteKey Asia(0, 2), Smoking(4, 2), Tuberculosis(3, 2), LungCancer(6, 2),
31  Bronchitis(7, 2), Either(5, 2), XRay(2, 2), Dyspnea(1, 2);
32  asia.add(Asia % "99/1");
33  asia.add(Smoking % "50/50");
34 
35  asia.add(Tuberculosis | Asia = "99/1 95/5");
36  asia.add(LungCancer | Smoking = "99/1 90/10");
37  asia.add(Bronchitis | Smoking = "70/30 40/60");
38 
39  asia.add((Either | Tuberculosis, LungCancer) = "F T T T");
40 
41  asia.add(XRay | Either = "95/5 2/98");
42  asia.add((Dyspnea | Either, Bronchitis) = "9/1 2/8 3/7 1/9");
43 
44  // print
45  vector<string> pretty = {"Asia", "Dyspnea", "XRay", "Tuberculosis",
46  "Smoking", "Either", "LungCancer", "Bronchitis"};
47  auto formatter = [pretty](Key key) { return pretty[key]; };
48  asia.print("Asia", formatter);
49 
50  // Convert to factor graph
51  DiscreteFactorGraph fg(asia);
52 
53  // Create solver and eliminate
55  ordering += Key(0), Key(1), Key(2), Key(3), Key(4), Key(5), Key(6), Key(7);
57 
58  // solve
59  DiscreteFactor::sharedValues mpe = chordal->optimize();
60  GTSAM_PRINT(*mpe);
61 
62  // We can also build a Bayes tree (directed junction tree).
63  // The elimination order above will do fine:
64  auto bayesTree = fg.eliminateMultifrontal(ordering);
65  bayesTree->print("bayesTree", formatter);
66 
67  // add evidence, we were in Asia and we have dyspnea
68  fg.add(Asia, "0 1");
69  fg.add(Dyspnea, "0 1");
70 
71  // solve again, now with evidence
72  DiscreteBayesNet::shared_ptr chordal2 = fg.eliminateSequential(ordering);
73  DiscreteFactor::sharedValues mpe2 = chordal2->optimize();
74  GTSAM_PRINT(*mpe2);
75 
76  // We can also sample from it
77  cout << "\n10 samples:" << endl;
78  for (size_t i = 0; i < 10; i++) {
79  DiscreteFactor::sharedValues sample = chordal2->sample();
80  GTSAM_PRINT(*sample);
81  }
82  return 0;
83 }
void add(const DiscreteKey &j, SOURCE table)
static enum @843 ordering
boost::shared_ptr< BayesTreeType > eliminateMultifrontal(OptionalOrderingType orderingType=boost::none, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none) const
void add(const Signature &s)
Definition: Half.h:150
Bayes network.
const KeyFormatter & formatter
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:34
A class for computing marginals in a DiscreteFactorGraph.
traits
Definition: chartTesting.h:28
int main(int argc, char **argv)
#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
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
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:41:59