Small formatting

release/4.3a0
Frank Dellaert 2012-09-15 11:49:42 +00:00
parent 4be00da291
commit 1df761d9a0
2 changed files with 29 additions and 28 deletions

View File

@ -22,7 +22,6 @@
#include <gtsam/discrete/DiscreteFactorGraph.h> #include <gtsam/discrete/DiscreteFactorGraph.h>
#include <gtsam/inference/GenericMultifrontalSolver.h> #include <gtsam/inference/GenericMultifrontalSolver.h>
#include <gtsam/linear/GaussianBayesTree.h>
namespace gtsam { namespace gtsam {

View File

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