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 }
LungCancer
static const DiscreteKey LungCancer(6, 2)
gtsam::EliminateableFactorGraph::eliminateSequential
std::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Definition: EliminateableFactorGraph-inst.h:29
Tuberculosis
static const DiscreteKey Tuberculosis(3, 2)
gtsam::DiscreteFactorGraph
Definition: DiscreteFactorGraph.h:98
XRay
static const DiscreteKey XRay(2, 2)
gtsam::BayesNet::print
void print(const std::string &s="BayesNet", const KeyFormatter &formatter=DefaultKeyFormatter) const override
Definition: BayesNet-inst.h:31
DiscreteFactorGraph.h
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
Bronchitis
static const DiscreteKey Bronchitis(7, 2)
gtsam::EliminateableFactorGraph::eliminateMultifrontal
std::shared_ptr< BayesTreeType > eliminateMultifrontal(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Definition: EliminateableFactorGraph-inst.h:89
gtsam::DiscreteBayesNet
Definition: DiscreteBayesNet.h:38
Smoking
static const DiscreteKey Smoking(4, 2)
test_DiscreteBayesNet.Asia
tuple Asia
Definition: test_DiscreteBayesNet.py:25
GTSAM_PRINT
#define GTSAM_PRINT(x)
Definition: Testable.h:43
Dyspnea
static const DiscreteKey Dyspnea(1, 2)
BayesNet.h
Bayes network.
DiscreteMarginals.h
A class for computing marginals in a DiscreteFactorGraph.
ordering
static enum @1096 ordering
key
const gtsam::Symbol key('X', 0)
Either
static const DiscreteKey Either(5, 2)
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::DiscreteFactorGraph::add
void add(Args &&... args)
Definition: DiscreteFactorGraph.h:138
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
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::Ordering
Definition: inference/Ordering.h:33
main
int main(int argc, char **argv)
Definition: DiscreteBayesNetExample.cpp:28
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9


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