25 using namespace gtsam;
30 DiscreteKey Cathy(1, nrStates), Heather(2, nrStates), Mark(3, nrStates),
35 graph.
add(Cathy,
"1 3");
36 graph.
add(Heather,
"9 1");
37 graph.
add(Mark,
"1 3");
38 graph.
add(Allison,
"9 1");
41 graph.
add(Cathy & Heather,
"2 1 1 2");
42 graph.
add(Heather & Mark,
"2 1 1 2");
43 graph.
add(Mark & Allison,
"2 1 1 2");
46 DiscreteFactor::shared_ptr actualC =
marginals(Cathy.first);
49 values[Cathy.first] = 0;
62 const int nrNodes = 10;
63 const size_t nrStates = 7;
66 vector<DiscreteKey>
key;
67 for (
int i = 0;
i < nrNodes;
i++) {
76 graph.
add(key[0],
".3 .6 .1 0 0 0 0");
77 for (
int i = 1;
i < nrNodes;
i++)
78 graph.
add(key[
i],
"1 1 1 1 1 1 1");
80 const std::string edgePotential =
".08 .9 .01 0 0 0 .01 " 81 ".03 .95 .01 0 0 0 .01 " 82 ".06 .06 .75 .05 .05 .02 .01 " 83 "0 0 0 .3 .6 .09 .01 " 84 "0 0 0 .02 .95 .02 .01 " 85 "0 0 0 .01 .01 .97 .01 " 89 for (
int i = 0; i < nrNodes - 1; i++)
90 graph.
add(key[i] & key[i + 1], edgePotential);
93 DiscreteFactor::shared_ptr actualC =
marginals(key[2].first);
96 values[key[2].first] = 0;
103 const int nrNodes = 5;
104 const size_t nrStates = 2;
107 vector<DiscreteKey>
key;
108 for (
int i = 0;
i < nrNodes;
i++) {
110 key.push_back(key_i);
115 graph.
add(key[0] & key[2] & key[4],
"2 2 2 2 1 1 1 1");
116 graph.
add(key[1] & key[3] & key[4],
"1 1 1 1 2 2 2 2");
117 graph.
add(key[2] & key[3] & key[4],
"1 1 1 1 1 1 1 1");
122 Clique expected0(std::make_shared<DiscreteConditional>((key[0] | key[2], key[4]) =
"2/1 2/1 2/1 2/1"));
123 Clique::shared_ptr actual0 = (*bayesTree)[0];
126 Clique expected1(std::make_shared<DiscreteConditional>((key[1] | key[3], key[4]) =
"1/2 1/2 1/2 1/2"));
127 Clique::shared_ptr actual1 = (*bayesTree)[1];
135 DiscreteFactor::shared_ptr actualM0 =
marginals(0);
136 EXPECT(
assert_equal(expectedM0, *std::dynamic_pointer_cast<DecisionTreeFactor>(actualM0),1
e-5));
140 DiscreteFactor::shared_ptr actualM1 =
marginals(1);
141 EXPECT(
assert_equal(expectedM1, *std::dynamic_pointer_cast<DecisionTreeFactor>(actualM1),1
e-5));
147 const int nrNodes = 5;
148 const size_t nrStates = 2;
151 vector<DiscreteKey>
key;
152 for (
int i = 0;
i < nrNodes;
i++) {
154 key.push_back(key_i);
159 graph.
add(key[0] & key[2] & key[4],
"1 2 3 4 5 6 7 8");
160 graph.
add(key[1] & key[3] & key[4],
"1 2 3 4 5 6 7 8");
161 graph.
add(key[2] & key[3] & key[4],
"1 2 3 4 5 6 7 8");
164 auto allPosbValues = DiscreteValues::CartesianProduct(
165 key[0] & key[1] & key[2] & key[3] & key[4]);
167 for (
size_t i = 0;
i < allPosbValues.size(); ++
i) {
170 for (
size_t j = 0;
j < 5;
j++)
180 for (
size_t j = 0;
j < 5;
j++) {
181 double sum = T[
j] + F[
j];
186 const vector<double>
table{F[
j], T[
j]};
188 DiscreteFactor::shared_ptr actualM =
marginals(j);
190 expectedM, *std::dynamic_pointer_cast<DecisionTreeFactor>(actualM)));
const gtsam::Symbol key('X', 0)
TEST_UNSAFE(DiscreteMarginals, UGM_small)
static int runAllTests(TestResult &result)
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
NonlinearFactorGraph graph
void add(Args &&... args)
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
#define EXPECT(condition)
RealScalar RealScalar * px
Vector marginalProbabilities(const DiscreteKey &key) const
Array< double, 1, 3 > e(1./3., 0.5, 2.)
A class for computing marginals in a DiscreteFactorGraph.
std::pair< Key, size_t > DiscreteKey
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
std::shared_ptr< This > shared_ptr
Marginals marginals(graph, result)