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
54  const Ordering ordering{0, 1, 2, 3, 4, 5, 6, 7};
55 
56  // solve
57  auto mpe = fg.optimize();
58  GTSAM_PRINT(mpe);
59 
60  // We can also build a Bayes tree (directed junction tree).
61  // The elimination order above will do fine:
62  auto bayesTree = fg.eliminateMultifrontal(ordering);
63  bayesTree->print("bayesTree", formatter);
64 
65  // add evidence, we were in Asia and we have dyspnea
66  fg.add(Asia, "0 1");
67  fg.add(Dyspnea, "0 1");
68 
69  // solve again, now with evidence
70  auto mpe2 = fg.optimize();
71  GTSAM_PRINT(mpe2);
72 
73  // We can also sample from it
75  cout << "\n10 samples:" << endl;
76  for (size_t i = 0; i < 10; i++) {
77  auto sample = chordal->sample();
78  GTSAM_PRINT(sample);
79  }
80  return 0;
81 }
const gtsam::Symbol key('X', 0)
Definition: BFloat16.h:88
Bayes network.
DiscreteValues optimize(OptionalOrderingType orderingType={}) const
Find the maximum probable explanation (MPE) by doing max-product.
static enum @1107 ordering
const KeyFormatter & formatter
static const DiscreteKey XRay(2, 2)
static const DiscreteKey Tuberculosis(3, 2)
std::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
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:43
std::shared_ptr< This > shared_ptr
static const DiscreteKey LungCancer(6, 2)
void add(const DiscreteKey &key, const std::string &spec)
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std::shared_ptr< BayesTreeType > eliminateMultifrontal(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
static const DiscreteKey Dyspnea(1, 2)
static const DiscreteKey Smoking(4, 2)
static const DiscreteKey Bronchitis(7, 2)
static const DiscreteKey Either(5, 2)
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
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:10