Small formatting
parent
4be00da291
commit
1df761d9a0
|
|
@ -22,7 +22,6 @@
|
|||
|
||||
#include <gtsam/discrete/DiscreteFactorGraph.h>
|
||||
#include <gtsam/inference/GenericMultifrontalSolver.h>
|
||||
#include <gtsam/linear/GaussianBayesTree.h>
|
||||
|
||||
namespace gtsam {
|
||||
|
||||
|
|
|
|||
|
|
@ -19,8 +19,10 @@
|
|||
|
||||
#include <gtsam/discrete/DiscreteMarginals.h>
|
||||
#include <gtsam/discrete/DiscreteSequentialSolver.h>
|
||||
|
||||
#include <boost/assign/std/vector.hpp>
|
||||
using namespace boost::assign;
|
||||
|
||||
#include <CppUnitLite/TestHarness.h>
|
||||
|
||||
using namespace std;
|
||||
|
|
@ -65,19 +67,19 @@ TEST_UNSAFE( DiscreteMarginals, UGM_chain ) {
|
|||
const size_t nrStates = 7;
|
||||
|
||||
// define variables
|
||||
vector<DiscreteKey> nodes;
|
||||
vector<DiscreteKey> key;
|
||||
for (int i = 0; i < nrNodes; i++) {
|
||||
DiscreteKey dk(i, nrStates);
|
||||
nodes.push_back(dk);
|
||||
DiscreteKey key_i(i, nrStates);
|
||||
key.push_back(key_i);
|
||||
}
|
||||
|
||||
// create graph
|
||||
DiscreteFactorGraph graph;
|
||||
|
||||
// add node potentials
|
||||
graph.add(nodes[0], ".3 .6 .1 0 0 0 0");
|
||||
graph.add(key[0], ".3 .6 .1 0 0 0 0");
|
||||
for (int i = 1; i < nrNodes; i++)
|
||||
graph.add(nodes[i], "1 1 1 1 1 1 1");
|
||||
graph.add(key[i], "1 1 1 1 1 1 1");
|
||||
|
||||
const std::string edgePotential = ".08 .9 .01 0 0 0 .01 "
|
||||
".03 .95 .01 0 0 0 .01 "
|
||||
|
|
@ -89,13 +91,13 @@ TEST_UNSAFE( DiscreteMarginals, UGM_chain ) {
|
|||
|
||||
// add edge potentials
|
||||
for (int i = 0; i < nrNodes - 1; i++)
|
||||
graph.add(nodes[i] & nodes[i + 1], edgePotential);
|
||||
graph.add(key[i] & key[i + 1], edgePotential);
|
||||
|
||||
DiscreteMarginals marginals(graph);
|
||||
DiscreteFactor::shared_ptr actualC = marginals(nodes[2].first);
|
||||
DiscreteFactor::shared_ptr actualC = marginals(key[2].first);
|
||||
DiscreteFactor::Values values;
|
||||
|
||||
values[nodes[2].first] = 0;
|
||||
values[key[2].first] = 0;
|
||||
EXPECT_DOUBLES_EQUAL( 0.03426, (*actualC)(values), 1e-4);
|
||||
}
|
||||
|
||||
|
|
@ -106,28 +108,28 @@ TEST_UNSAFE( DiscreteMarginals, truss ) {
|
|||
const size_t nrStates = 2;
|
||||
|
||||
// define variables
|
||||
vector<DiscreteKey> nodes;
|
||||
vector<DiscreteKey> key;
|
||||
for (int i = 0; i < nrNodes; i++) {
|
||||
DiscreteKey dk(i, nrStates);
|
||||
nodes.push_back(dk);
|
||||
DiscreteKey key_i(i, nrStates);
|
||||
key.push_back(key_i);
|
||||
}
|
||||
|
||||
// create graph and add three truss potentials
|
||||
DiscreteFactorGraph graph;
|
||||
graph.add(nodes[0] & nodes[2] & nodes[4],"2 2 2 2 1 1 1 1");
|
||||
graph.add(nodes[1] & nodes[3] & nodes[4],"1 1 1 1 2 2 2 2");
|
||||
graph.add(nodes[2] & nodes[3] & nodes[4],"1 1 1 1 1 1 1 1");
|
||||
graph.add(key[0] & key[2] & key[4],"2 2 2 2 1 1 1 1");
|
||||
graph.add(key[1] & key[3] & key[4],"1 1 1 1 2 2 2 2");
|
||||
graph.add(key[2] & key[3] & key[4],"1 1 1 1 1 1 1 1");
|
||||
typedef JunctionTree<DiscreteFactorGraph> JT;
|
||||
GenericMultifrontalSolver<DiscreteFactor, JT> solver(graph);
|
||||
BayesTree<DiscreteConditional>::shared_ptr bayesTree = solver.eliminate(&EliminateDiscrete);
|
||||
// bayesTree->print("Bayes Tree");
|
||||
typedef BayesTreeClique<DiscreteConditional> Clique;
|
||||
|
||||
Clique expected0(boost::make_shared<DiscreteConditional>((nodes[0] | nodes[2], nodes[4]) = "2/1 2/1 2/1 2/1"));
|
||||
Clique expected0(boost::make_shared<DiscreteConditional>((key[0] | key[2], key[4]) = "2/1 2/1 2/1 2/1"));
|
||||
Clique::shared_ptr actual0 = (*bayesTree)[0];
|
||||
// EXPECT(assert_equal(expected0, *actual0)); // TODO, correct but fails
|
||||
|
||||
Clique expected1(boost::make_shared<DiscreteConditional>((nodes[1] | nodes[3], nodes[4]) = "1/2 1/2 1/2 1/2"));
|
||||
Clique expected1(boost::make_shared<DiscreteConditional>((key[1] | key[3], key[4]) = "1/2 1/2 1/2 1/2"));
|
||||
Clique::shared_ptr actual1 = (*bayesTree)[1];
|
||||
// EXPECT(assert_equal(expected1, *actual1)); // TODO, correct but fails
|
||||
|
||||
|
|
@ -135,12 +137,12 @@ TEST_UNSAFE( DiscreteMarginals, truss ) {
|
|||
DiscreteMarginals marginals(graph);
|
||||
|
||||
// test 0
|
||||
DecisionTreeFactor expectedM0(nodes[0],"0.666667 0.333333");
|
||||
DecisionTreeFactor expectedM0(key[0],"0.666667 0.333333");
|
||||
DiscreteFactor::shared_ptr actualM0 = marginals(0);
|
||||
EXPECT(assert_equal(expectedM0, *boost::dynamic_pointer_cast<DecisionTreeFactor>(actualM0),1e-5));
|
||||
|
||||
// test 1
|
||||
DecisionTreeFactor expectedM1(nodes[1],"0.333333 0.666667");
|
||||
DecisionTreeFactor expectedM1(key[1],"0.333333 0.666667");
|
||||
DiscreteFactor::shared_ptr actualM1 = marginals(1);
|
||||
EXPECT(assert_equal(expectedM1, *boost::dynamic_pointer_cast<DecisionTreeFactor>(actualM1),1e-5));
|
||||
}
|
||||
|
|
@ -153,21 +155,21 @@ TEST_UNSAFE( DiscreteMarginals, truss2 ) {
|
|||
const size_t nrStates = 2;
|
||||
|
||||
// define variables
|
||||
vector<DiscreteKey> nodes;
|
||||
vector<DiscreteKey> key;
|
||||
for (int i = 0; i < nrNodes; i++) {
|
||||
DiscreteKey dk(i, nrStates);
|
||||
nodes.push_back(dk);
|
||||
DiscreteKey key_i(i, nrStates);
|
||||
key.push_back(key_i);
|
||||
}
|
||||
|
||||
// create graph and add three truss potentials
|
||||
DiscreteFactorGraph graph;
|
||||
graph.add(nodes[0] & nodes[2] & nodes[4],"1 2 3 4 5 6 7 8");
|
||||
graph.add(nodes[1] & nodes[3] & nodes[4],"1 2 3 4 5 6 7 8");
|
||||
graph.add(nodes[2] & nodes[3] & nodes[4],"1 2 3 4 5 6 7 8");
|
||||
graph.add(key[0] & key[2] & key[4],"1 2 3 4 5 6 7 8");
|
||||
graph.add(key[1] & key[3] & key[4],"1 2 3 4 5 6 7 8");
|
||||
graph.add(key[2] & key[3] & key[4],"1 2 3 4 5 6 7 8");
|
||||
|
||||
// Calculate the marginals by brute force
|
||||
vector<DiscreteFactor::Values> allPosbValues = cartesianProduct(
|
||||
nodes[0] & nodes[1] & nodes[2] & nodes[3] & nodes[4]);
|
||||
key[0] & key[1] & key[2] & key[3] & key[4]);
|
||||
Vector T = zero(5), F = zero(5);
|
||||
for (size_t i = 0; i < allPosbValues.size(); ++i) {
|
||||
DiscreteFactor::Values x = allPosbValues[i];
|
||||
|
|
@ -186,13 +188,13 @@ TEST_UNSAFE( DiscreteMarginals, truss2 ) {
|
|||
F[j]/=sum;
|
||||
|
||||
// solver
|
||||
Vector actualV = solver.marginalProbabilities(nodes[j]);
|
||||
Vector actualV = solver.marginalProbabilities(key[j]);
|
||||
EXPECT(assert_equal(Vector_(2,F[j],T[j]), actualV));
|
||||
|
||||
// Marginals
|
||||
vector<double> table;
|
||||
table += F[j],T[j];
|
||||
DecisionTreeFactor expectedM(nodes[j],table);
|
||||
DecisionTreeFactor expectedM(key[j],table);
|
||||
DiscreteFactor::shared_ptr actualM = marginals(j);
|
||||
EXPECT(assert_equal(expectedM, *boost::dynamic_pointer_cast<DecisionTreeFactor>(actualM)));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue