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
12/**
13 * @file DiscreteBayesNet_FG.cpp
14 * @brief Discrete Bayes Net example using Factor Graphs
15 * @author Abhijit
16 * @date Jun 4, 2012
17 *
18 * We use the famous Rain/Cloudy/Sprinkler Example of [Russell & Norvig, 2009,
19 * p529] You may be familiar with other graphical model packages like BNT
20 * (available at http://bnt.googlecode.com/svn/trunk/docs/usage.html) where this
21 * is used as an example. The following demo is same as that in the above link,
22 * except that everything is using GTSAM.
23 */
24
25#include <gtsam/discrete/DiscreteFactorGraph.h>
26#include <gtsam/discrete/DiscreteMarginals.h>
27
28#include <iomanip>
29
30using namespace std;
31using namespace gtsam;
32
33int main(int argc, char **argv) {
34 // Define keys and a print function
35 Key C(1), S(2), R(3), W(4);
36 auto print = [=](const DiscreteFactor::Values& values) {
37 cout << boolalpha << "Cloudy = " << static_cast<bool>(values.at(k: C))
38 << " Sprinkler = " << static_cast<bool>(values.at(k: S))
39 << " Rain = " << boolalpha << static_cast<bool>(values.at(k: R))
40 << " WetGrass = " << static_cast<bool>(values.at(k: W)) << endl;
41 };
42
43 // We assume binary state variables
44 // we have 0 == "False" and 1 == "True"
45 const size_t nrStates = 2;
46
47 // define variables
48 DiscreteKey Cloudy(C, nrStates), Sprinkler(S, nrStates), Rain(R, nrStates),
49 WetGrass(W, nrStates);
50
51 // create Factor Graph of the bayes net
52 DiscreteFactorGraph graph;
53
54 // add factors
55 graph.add(args&: Cloudy, args: "0.5 0.5"); // P(Cloudy)
56 graph.add(args: Cloudy & Sprinkler, args: "0.5 0.5 0.9 0.1"); // P(Sprinkler | Cloudy)
57 graph.add(args: Cloudy & Rain, args: "0.8 0.2 0.2 0.8"); // P(Rain | Cloudy)
58 graph.add(args&: Sprinkler & Rain & WetGrass,
59 args: "1 0 0.1 0.9 0.1 0.9 0.001 0.99"); // P(WetGrass | Sprinkler, Rain)
60
61 // Alternatively we can also create a DiscreteBayesNet, add
62 // DiscreteConditional factors and create a FactorGraph from it. (See
63 // testDiscreteBayesNet.cpp)
64
65 // Since this is a relatively small distribution, we can as well print
66 // the whole distribution..
67 cout << "Distribution of Example: " << endl;
68 cout << setw(11) << "Cloudy(C)" << setw(14) << "Sprinkler(S)" << setw(10)
69 << "Rain(R)" << setw(14) << "WetGrass(W)" << setw(15) << "P(C,S,R,W)"
70 << endl;
71 for (size_t a = 0; a < nrStates; a++)
72 for (size_t m = 0; m < nrStates; m++)
73 for (size_t h = 0; h < nrStates; h++)
74 for (size_t c = 0; c < nrStates; c++) {
75 DiscreteFactor::Values values;
76 values[C] = c;
77 values[S] = h;
78 values[R] = m;
79 values[W] = a;
80 double prodPot = graph(values);
81 cout << setw(8) << static_cast<bool>(c) << setw(14)
82 << static_cast<bool>(h) << setw(12) << static_cast<bool>(m)
83 << setw(13) << static_cast<bool>(a) << setw(16) << prodPot
84 << endl;
85 }
86
87 // "Most Probable Explanation", i.e., configuration with largest value
88 auto mpe = graph.optimize();
89 cout << "\nMost Probable Explanation (MPE):" << endl;
90 print(mpe);
91
92 // "Inference" We show an inference query like: probability that the Sprinkler
93 // was on; given that the grass is wet i.e. P( S | C=0) = ?
94
95 // add evidence that it is not Cloudy
96 graph.add(args&: Cloudy, args: "1 0");
97
98 // solve again, now with evidence
99 auto mpe_with_evidence = graph.optimize();
100
101 cout << "\nMPE given C=0:" << endl;
102 print(mpe_with_evidence);
103
104 // we can also calculate arbitrary marginals:
105 DiscreteMarginals marginals(graph);
106 cout << "\nP(S=1|C=0):" << marginals.marginalProbabilities(key: Sprinkler)[1]
107 << endl;
108 cout << "\nP(R=0|C=0):" << marginals.marginalProbabilities(key: Rain)[0] << endl;
109 cout << "\nP(W=1|C=0):" << marginals.marginalProbabilities(key: WetGrass)[1]
110 << endl;
111
112 // We can also sample from the eliminated graph
113 DiscreteBayesNet::shared_ptr chordal = graph.eliminateSequential();
114 cout << "\n10 samples:" << endl;
115 for (size_t i = 0; i < 10; i++) {
116 auto sample = chordal->sample();
117 print(sample);
118 }
119 return 0;
120}
121