30 using namespace gtsam;
31 static constexpr
bool debug =
false;
45 for (
int i = 0;
i < 15;
i++) {
47 keys.push_back(key_i);
51 bayesNet.
add(keys[14] %
"1/3");
53 bayesNet.
add(keys[13] | keys[14] =
"1/3 3/1");
54 bayesNet.
add(keys[12] | keys[14] =
"3/1 3/1");
56 bayesNet.
add((keys[11] | keys[13], keys[14]) =
"1/4 2/3 3/2 4/1");
57 bayesNet.
add((keys[10] | keys[13], keys[14]) =
"1/4 3/2 2/3 4/1");
58 bayesNet.
add((keys[9] | keys[12], keys[14]) =
"4/1 2/3 F 1/4");
59 bayesNet.
add((keys[8] | keys[12], keys[14]) =
"T 1/4 3/2 4/1");
61 bayesNet.
add((keys[7] | keys[11], keys[13]) =
"1/4 2/3 3/2 4/1");
62 bayesNet.
add((keys[6] | keys[11], keys[13]) =
"1/4 3/2 2/3 4/1");
63 bayesNet.
add((keys[5] | keys[10], keys[13]) =
"4/1 2/3 3/2 1/4");
64 bayesNet.
add((keys[4] | keys[10], keys[13]) =
"2/3 1/4 3/2 4/1");
66 bayesNet.
add((keys[3] | keys[9], keys[12]) =
"1/4 2/3 3/2 4/1");
67 bayesNet.
add((keys[2] | keys[9], keys[12]) =
"1/4 8/2 2/3 4/1");
68 bayesNet.
add((keys[1] | keys[8], keys[12]) =
"4/1 2/3 3/2 1/4");
69 bayesNet.
add((keys[0] | keys[8], keys[12]) =
"2/3 1/4 3/2 4/1");
79 const auto&
keys =
self.keys;
83 self.bayesNet.saveGraph(
"/tmp/discreteBayesNet.dot");
89 self.bayesTree->saveGraph(
"/tmp/discreteBayesTree.dot");
93 for (
size_t i : {13, 14, 9, 3, 2, 8, 1, 0, 10, 5, 4}) {
94 auto clique_i = (*
self.bayesTree)[
i];
98 auto R =
self.bayesTree->roots().front();
101 auto allPosbValues = DiscreteValues::CartesianProduct(
105 for (
size_t i = 0;
i < allPosbValues.size(); ++
i) {
107 double expected =
self.bayesNet.evaluate(x);
108 double actual =
self.bayesTree->evaluate(x);
114 double joint_12_14 = 0, joint_9_12_14 = 0, joint_8_12_14 = 0, joint_8_12 = 0,
115 joint82 = 0, joint12 = 0, joint24 = 0, joint45 = 0, joint46 = 0,
116 joint_4_11 = 0, joint_11_13 = 0, joint_11_13_14 = 0,
117 joint_11_12_13_14 = 0, joint_9_11_12_13 = 0, joint_8_11_12_13 = 0;
118 for (
size_t i = 0;
i < allPosbValues.size(); ++
i) {
120 double px =
self.bayesTree->evaluate(x);
121 for (
size_t i = 0;
i < 15;
i++)
122 if (x[
i]) marginals[
i] +=
px;
123 if (x[12] && x[14]) {
125 if (x[9]) joint_9_12_14 +=
px;
126 if (x[8]) joint_8_12_14 +=
px;
128 if (x[8] && x[12]) joint_8_12 +=
px;
130 if (x[8]) joint82 +=
px;
131 if (x[1]) joint12 +=
px;
134 if (x[2]) joint24 +=
px;
135 if (x[5]) joint45 +=
px;
136 if (x[6]) joint46 +=
px;
137 if (x[11]) joint_4_11 +=
px;
139 if (x[11] && x[13]) {
141 if (x[8] && x[12]) joint_8_11_12_13 +=
px;
142 if (x[9] && x[12]) joint_9_11_12_13 +=
px;
144 joint_11_13_14 +=
px;
146 joint_11_12_13_14 +=
px;
154 auto clique = (*
self.bayesTree)[0];
164 clique = (*
self.bayesTree)[9];
170 clique = (*
self.bayesTree)[11];
176 clique = (*
self.bayesTree)[9];
182 clique = (*
self.bayesTree)[8];
187 clique = (*
self.bayesTree)[2];
192 clique = (*
self.bayesTree)[0];
197 DiscreteBayesTree::Nodes cliques =
self.bayesTree->nodes();
198 for (
auto clique : cliques) {
201 clique.second->conditional_->printSignature();
202 shortcut.
print(
"shortcut:");
207 DiscreteFactor::shared_ptr marginalFactor;
208 for (
size_t i = 0;
i < 15;
i++) {
210 double actual = (*marginalFactor)(all1);
247 "0[label=\"13, 11, 6, 7\"];\n" 249 "1[label=\"14 : 11, 13\"];\n" 251 "2[label=\"9, 12 : 14\"];\n" 253 "3[label=\"3 : 9, 12\"];\n" 255 "4[label=\"2 : 9, 12\"];\n" 257 "5[label=\"8 : 12, 14\"];\n" 259 "6[label=\"1 : 8, 12\"];\n" 261 "7[label=\"0 : 8, 12\"];\n" 263 "8[label=\"10 : 13, 14\"];\n" 265 "9[label=\"5 : 10, 13\"];\n" 267 "10[label=\"4 : 10, 13\"];\n" std::pair< DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr > EliminateDiscrete(const DiscreteFactorGraph &factors, const Ordering &frontalKeys)
Main elimination function for DiscreteFactorGraph.
static int runAllTests(TestResult &result)
double evaluate(const DiscreteValues &values) const
A Bayes tree representing a Discrete distribution.
#define DOUBLES_EQUAL(expected, actual, threshold)
Rot2 R(Rot2::fromAngle(0.1))
static constexpr bool debug
DiscreteBayesNet bayesNet
#define EXPECT(condition)
RealScalar RealScalar * px
Array< double, 1, 3 > e(1./3., 0.5, 2.)
Discrete Bayes Tree, the result of eliminating a DiscreteJunctionTree.
#define LONGS_EQUAL(expected, actual)
TEST(DiscreteBayesTree, ThinTree)
typedef and functions to augment Eigen's VectorXd
#define EXPECT_LONGS_EQUAL(expected, actual)
vector< DiscreteKey > keys
std::shared_ptr< This > shared_ptr
void add(const DiscreteKey &key, const std::string &spec)
std::pair< Key, size_t > DiscreteKey
std::shared_ptr< DiscreteBayesTree > bayesTree
std::shared_ptr< BayesTreeType > eliminateMultifrontal(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Marginals marginals(graph, result)
void print(const std::string &s="BayesNet", const KeyFormatter &formatter=DefaultKeyFormatter) const override