DiscreteBayesNet_FG.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 
27 
28 #include <iomanip>
29 
30 using namespace std;
31 using namespace gtsam;
32 
33 int 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(C))
38  << " Sprinkler = " << static_cast<bool>(values.at(S))
39  << " Rain = " << boolalpha << static_cast<bool>(values.at(R))
40  << " WetGrass = " << static_cast<bool>(values.at(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
53 
54  // add factors
55  graph.add(Cloudy, "0.5 0.5"); // P(Cloudy)
56  graph.add(Cloudy & Sprinkler, "0.5 0.5 0.9 0.1"); // P(Sprinkler | Cloudy)
57  graph.add(Cloudy & Rain, "0.8 0.2 0.2 0.8"); // P(Rain | Cloudy)
58  graph.add(Sprinkler & Rain & WetGrass,
59  "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++) {
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(Cloudy, "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:
106  cout << "\nP(S=1|C=0):" << marginals.marginalProbabilities(Sprinkler)[1]
107  << endl;
108  cout << "\nP(R=0|C=0):" << marginals.marginalProbabilities(Rain)[0] << endl;
109  cout << "\nP(W=1|C=0):" << marginals.marginalProbabilities(WetGrass)[1]
110  << endl;
111 
112  // We can also sample from the eliminated graph
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 }
Matrix3f m
int main(int argc, char **argv)
const ValueType at(Key j) const
Definition: Values-inl.h:204
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
Rot2 R(Rot2::fromAngle(0.1))
leaf::MyValues values
Definition: BFloat16.h:88
Key W(std::uint64_t j)
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
DiscreteKey S(1, 2)
NonlinearFactorGraph graph
DiscreteValues optimize(OptionalOrderingType orderingType={}) const
Find the maximum probable explanation (MPE) by doing max-product.
Vector marginalProbabilities(const DiscreteKey &key) const
std::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
A class for computing marginals in a DiscreteFactorGraph.
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:50
traits
Definition: chartTesting.h:28
const double h
std::vector< float > Values
std::shared_ptr< This > shared_ptr
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
Marginals marginals(graph, result)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:10