Go to the documentation of this file.
31 using namespace gtsam;
33 int main(
int argc,
char **argv) {
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;
45 const size_t nrStates = 2;
48 DiscreteKey Cloudy(
C, nrStates), Sprinkler(
S, nrStates), Rain(
R, nrStates),
49 WetGrass(
W, nrStates);
56 graph.
add(Cloudy & Sprinkler,
"0.5 0.5 0.9 0.1");
57 graph.
add(Cloudy & Rain,
"0.8 0.2 0.2 0.8");
58 graph.
add(Sprinkler & Rain & WetGrass,
59 "1 0 0.1 0.9 0.1 0.9 0.001 0.99");
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)"
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++) {
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
88 auto mpe =
graph.optimize();
89 cout <<
"\nMost Probable Explanation (MPE):" << endl;
99 auto mpe_with_evidence =
graph.optimize();
101 cout <<
"\nMPE given C=0:" << endl;
102 print(mpe_with_evidence);
106 cout <<
"\nP(S=1|C=0):" <<
marginals.marginalProbabilities(Sprinkler)[1]
108 cout <<
"\nP(R=0|C=0):" <<
marginals.marginalProbabilities(Rain)[0] << endl;
109 cout <<
"\nP(W=1|C=0):" <<
marginals.marginalProbabilities(WetGrass)[1]
114 cout <<
"\n10 samples:" << endl;
115 for (
size_t i = 0;
i < 10;
i++) {
116 auto sample = chordal->sample();
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
int main(int argc, char **argv)
A class for computing marginals in a DiscreteFactorGraph.
Matrix< Scalar, Dynamic, Dynamic > C
std::pair< Key, size_t > DiscreteKey
std::shared_ptr< This > shared_ptr
IsDerived< DERIVEDFACTOR > add(std::shared_ptr< DERIVEDFACTOR > factor)
add is a synonym for push_back.
NonlinearFactorGraph graph
Marginals marginals(graph, result)
std::uint64_t Key
Integer nonlinear key type.
Rot2 R(Rot2::fromAngle(0.1))
gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:13