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);
37  cout << boolalpha << "Cloudy = " << static_cast<bool>((*values)[C])
38  << " Sprinkler = " << static_cast<bool>((*values)[S])
39  << " Rain = " << boolalpha << static_cast<bool>((*values)[R])
40  << " WetGrass = " << static_cast<bool>((*values)[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  DiscreteFactor::sharedValues mpe = graph.eliminateSequential()->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
100  DiscreteFactor::sharedValues mpe_with_evidence = chordal->optimize();
101 
102  cout << "\nMPE given C=0:" << endl;
103  print(mpe_with_evidence);
104 
105  // we can also calculate arbitrary marginals:
107  cout << "\nP(S=1|C=0):" << marginals.marginalProbabilities(Sprinkler)[1]
108  << endl;
109  cout << "\nP(R=0|C=0):" << marginals.marginalProbabilities(Rain)[0] << endl;
110  cout << "\nP(W=1|C=0):" << marginals.marginalProbabilities(WetGrass)[1]
111  << endl;
112 
113  // We can also sample from it
114  cout << "\n10 samples:" << endl;
115  for (size_t i = 0; i < 10; i++) {
116  DiscreteFactor::sharedValues sample = chordal->sample();
117  print(sample);
118  }
119  return 0;
120 }
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
Matrix3f m
void add(const DiscreteKey &j, SOURCE table)
int main(int argc, char **argv)
Vector marginalProbabilities(const DiscreteKey &key) const
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
Rot2 R(Rot2::fromAngle(0.1))
leaf::MyValues values
Definition: Half.h:150
Key W(std::uint64_t j)
NonlinearFactorGraph graph
Array33i a
Key S(std::uint64_t j)
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:34
A class for computing marginals in a DiscreteFactorGraph.
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:37
traits
Definition: chartTesting.h:28
const double h
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
Marginals marginals(graph, result)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:59