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
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 }
Eigen::internal::print
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
Definition: NEON/PacketMath.h:3115
gtsam::DiscreteFactorGraph
Definition: DiscreteFactorGraph.h:98
DiscreteFactorGraph.h
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
main
int main(int argc, char **argv)
Definition: DiscreteBayesNet_FG.cpp:33
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
h
const double h
Definition: testSimpleHelicopter.cpp:19
gtsam::DiscreteMarginals
Definition: DiscreteMarginals.h:33
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
DiscreteMarginals.h
A class for computing marginals in a DiscreteFactorGraph.
gtsam::HybridValues::at
Vector & at(Key j)
Definition: HybridValues.cpp:129
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
C
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:50
gtsam
traits
Definition: SFMdata.h:40
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::symbol_shorthand::W
Key W(std::uint64_t j)
Definition: inference/Symbol.h:170
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std
Definition: BFloat16.h:88
gtsam::DiscreteBayesNet::shared_ptr
std::shared_ptr< This > shared_ptr
Definition: DiscreteBayesNet.h:43
gtsam::FactorGraph::add
IsDerived< DERIVEDFACTOR > add(std::shared_ptr< DERIVEDFACTOR > factor)
add is a synonym for push_back.
Definition: FactorGraph.h:171
graph
NonlinearFactorGraph graph
Definition: doc/Code/OdometryExample.cpp:2
marginals
Marginals marginals(graph, result)
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
R
Rot2 R(Rot2::fromAngle(0.1))
S
DiscreteKey S(1, 2)


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:32:26