22 #include <boost/assign/std/vector.hpp> 28 using namespace gtsam;
33 DiscreteKey Cathy(1, nrStates), Heather(2, nrStates), Mark(3, nrStates),
38 graph.
add(Cathy,
"1 3");
39 graph.
add(Heather,
"9 1");
40 graph.
add(Mark,
"1 3");
41 graph.
add(Allison,
"9 1");
44 graph.
add(Cathy & Heather,
"2 1 1 2");
45 graph.
add(Heather & Mark,
"2 1 1 2");
46 graph.
add(Mark & Allison,
"2 1 1 2");
52 values[Cathy.first] = 0;
65 const int nrNodes = 10;
66 const size_t nrStates = 7;
69 vector<DiscreteKey>
key;
70 for (
int i = 0;
i < nrNodes;
i++) {
79 graph.
add(key[0],
".3 .6 .1 0 0 0 0");
80 for (
int i = 1;
i < nrNodes;
i++)
81 graph.
add(key[
i],
"1 1 1 1 1 1 1");
83 const std::string edgePotential =
".08 .9 .01 0 0 0 .01 " 84 ".03 .95 .01 0 0 0 .01 " 85 ".06 .06 .75 .05 .05 .02 .01 " 86 "0 0 0 .3 .6 .09 .01 " 87 "0 0 0 .02 .95 .02 .01 " 88 "0 0 0 .01 .01 .97 .01 " 92 for (
int i = 0; i < nrNodes - 1; i++)
93 graph.
add(key[i] & key[i + 1], edgePotential);
99 values[key[2].first] = 0;
106 const int nrNodes = 5;
107 const size_t nrStates = 2;
110 vector<DiscreteKey>
key;
111 for (
int i = 0;
i < nrNodes;
i++) {
113 key.push_back(key_i);
118 graph.
add(key[0] & key[2] & key[4],
"2 2 2 2 1 1 1 1");
119 graph.
add(key[1] & key[3] & key[4],
"1 1 1 1 2 2 2 2");
120 graph.
add(key[2] & key[3] & key[4],
"1 1 1 1 1 1 1 1");
125 Clique expected0(boost::make_shared<DiscreteConditional>((key[0] | key[2], key[4]) =
"2/1 2/1 2/1 2/1"));
126 Clique::shared_ptr actual0 = (*bayesTree)[0];
129 Clique expected1(boost::make_shared<DiscreteConditional>((key[1] | key[3], key[4]) =
"1/2 1/2 1/2 1/2"));
130 Clique::shared_ptr actual1 = (*bayesTree)[1];
139 EXPECT(
assert_equal(expectedM0, *boost::dynamic_pointer_cast<DecisionTreeFactor>(actualM0),1
e-5));
144 EXPECT(
assert_equal(expectedM1, *boost::dynamic_pointer_cast<DecisionTreeFactor>(actualM1),1
e-5));
150 const int nrNodes = 5;
151 const size_t nrStates = 2;
154 vector<DiscreteKey>
key;
155 for (
int i = 0;
i < nrNodes;
i++) {
157 key.push_back(key_i);
162 graph.
add(key[0] & key[2] & key[4],
"1 2 3 4 5 6 7 8");
163 graph.
add(key[1] & key[3] & key[4],
"1 2 3 4 5 6 7 8");
164 graph.
add(key[2] & key[3] & key[4],
"1 2 3 4 5 6 7 8");
167 vector<DiscreteFactor::Values> allPosbValues =
170 for (
size_t i = 0;
i < allPosbValues.size(); ++
i) {
173 for (
size_t j = 0;
j < 5;
j++)
183 for (
size_t j = 0;
j < 5;
j++) {
184 double sum = T[
j] + F[
j];
189 vector<double>
table;
194 expectedM, *boost::dynamic_pointer_cast<DecisionTreeFactor>(actualM)));
std::vector< Assignment< L > > cartesianProduct(const std::vector< std::pair< L, size_t > > &keys)
Get Cartesian product consisting all possible configurations.
void add(const DiscreteKey &j, SOURCE table)
TEST_UNSAFE(DiscreteMarginals, UGM_small)
static int runAllTests(TestResult &result)
Vector marginalProbabilities(const DiscreteKey &key) const
boost::shared_ptr< BayesTreeType > eliminateMultifrontal(OptionalOrderingType orderingType=boost::none, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex=boost::none) const
NonlinearFactorGraph graph
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
constexpr int first(int i)
Implementation details for constexpr functions.
std::pair< Key, size_t > DiscreteKey
#define EXPECT(condition)
RealScalar RealScalar * px
Array< double, 1, 3 > e(1./3., 0.5, 2.)
const mpreal sum(const mpreal tab[], const unsigned long int n, int &status, mp_rnd_t mode=mpreal::get_default_rnd())
A class for computing marginals in a DiscreteFactorGraph.
boost::shared_ptr< DiscreteFactor > shared_ptr
shared_ptr to this class
boost::shared_ptr< This > shared_ptr
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
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)