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);
55 graph.
add(Cloudy,
"0.5 0.5");
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++) {
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
89 cout <<
"\nMost Probable Explanation (MPE):" << endl;
96 graph.
add(Cloudy,
"1 0");
99 auto mpe_with_evidence = graph.
optimize();
101 cout <<
"\nMPE given C=0:" << endl;
102 print(mpe_with_evidence);
114 cout <<
"\n10 samples:" << endl;
115 for (
size_t i = 0;
i < 10;
i++) {
116 auto sample = chordal->sample();
int main(int argc, char **argv)
const ValueType at(Key j) const
Rot2 R(Rot2::fromAngle(0.1))
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
NonlinearFactorGraph graph
DiscreteValues optimize(OptionalOrderingType orderingType={}) const
Find the maximum probable explanation (MPE) by doing max-product.
void add(Args &&... args)
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
std::vector< float > Values
std::shared_ptr< This > shared_ptr
std::pair< Key, size_t > DiscreteKey
std::uint64_t Key
Integer nonlinear key type.
Marginals marginals(graph, result)