fix the graph related functions in FactorGraph as well as its unit tests

release/4.3a0
Kai Ni 2010-01-13 23:59:46 +00:00
parent 93465945e9
commit 3c66861790
6 changed files with 36 additions and 24 deletions

View File

@ -269,20 +269,24 @@ void FactorGraph<Factor>::associateFactor(int index, sharedFactor factor) {
}
}
/* ************************************************************************* *
template<class Factor>
map<string, string> FactorGraph<Factor>::findMinimumSpanningTree() const {
/* ************************************************************************* */
template<class Factor> template <class Key>
map<Key,Key> FactorGraph<Factor>::findMinimumSpanningTree() const {
SDGraph g = gtsam::toBoostGraph<FactorGraph<Factor>, sharedFactor>(*this);
typedef typename boost::graph_traits<SDGraph<Key> >::vertex_descriptor BoostVertex;
typedef typename boost::graph_traits<SDGraph<Key> >::vertex_iterator BoostVertexIterator;
SDGraph<Key> g = gtsam::toBoostGraph<FactorGraph<Factor>, sharedFactor, Key>(*this);
// find minimum spanning tree
vector<BoostVertex> p_map(boost::num_vertices(g));
prim_minimum_spanning_tree(g, &p_map[0]);
// convert edge to string pairs
map<string, string> tree;
map<Key, Key> tree;
BoostVertexIterator itVertex = boost::vertices(g).first;
for (vector<BoostVertex>::iterator vi = p_map.begin(); vi!=p_map.end(); itVertex++, vi++) {
typename vector<BoostVertex>::iterator vi;
for (vi = p_map.begin(); vi!=p_map.end(); itVertex++, vi++) {
string key = boost::get(boost::vertex_name, g, *itVertex);
string parent = boost::get(boost::vertex_name, g, *vi);
// printf("%s parent: %s\n", key.c_str(), parent.c_str());
@ -292,8 +296,8 @@ map<string, string> FactorGraph<Factor>::findMinimumSpanningTree() const {
return tree;
}
template<class Factor>
void FactorGraph<Factor>::split(map<string, string> tree, FactorGraph<Factor>& Ab1, FactorGraph<Factor>& Ab2) const {
template<class Factor> template <class Key>
void FactorGraph<Factor>::split(map<Key, Key> tree, FactorGraph<Factor>& Ab1, FactorGraph<Factor>& Ab2) const {
BOOST_FOREACH(sharedFactor factor, factors_){
if (factor->keys().size() > 2)

View File

@ -120,14 +120,14 @@ namespace gtsam {
/**
* find the minimum spanning tree.
*/
// std::map<std::string, std::string> findMinimumSpanningTree() const;
template<class Key> std::map<Key, Key> findMinimumSpanningTree() const;
/**
* Split the graph into two parts: one corresponds to the given spanning tre,
* and the other corresponds to the rest of the factors
*/
// void split(std::map<std::string, std::string> tree,
// FactorGraph<Factor>& Ab1, FactorGraph<Factor>& Ab2) const;
template<class Key> void split(std::map<Key, Key> tree,
FactorGraph<Factor>& Ab1, FactorGraph<Factor>& Ab2) const;
private:
/** Associate factor index with the variables connected to the factor */

View File

@ -171,6 +171,22 @@ public:
*/
std::list<std::string> keys() const;
/**
* return the first key
* @return The set of all variable keys
*/
std::string key1() const { return As_.begin()->first; }
/**
* return the first key
* @return The set of all variable keys
*/
std::string key2() const {
if (As_.size() < 2) throw std::invalid_argument("GaussianFactor: less than 2 keys!");
return (++(As_.begin()))->first;
}
/**
* Find all variables and their dimensions
* @return The set of all variable/dimension pairs

View File

@ -20,7 +20,6 @@ using namespace boost::assign;
#define FOREACH_PAIR( KEY, VAL, COL) BOOST_FOREACH (boost::tie(KEY,VAL),COL)
/*
class ordering_key_visitor : public boost::default_bfs_visitor {
public:
ordering_key_visitor(Ordering& ordering_in) : ordering_(ordering_in) {}
@ -31,13 +30,6 @@ public:
Ordering& ordering_;
};
class ordering_edge_action {
public:
void act (string& child, string& parent, SVertex& v1, SVertex& v2, SGraph& g){
//boost::add_edge(v1, v2, g);
}
};
*/
/* ************************************************************************* *
Ordering::Ordering(const map<string, string>& p_map) {

View File

@ -30,7 +30,7 @@ namespace gtsam {
template<class G, class F, class Key>
SDGraph<Key> toBoostGraph(const G& graph) {
// convert the factor graph to boost graph
SDGraph<Key> g(0);
SDGraph<Key> g;
typedef typename boost::graph_traits<SDGraph<Key> >::vertex_descriptor BoostVertex;
map<Key, BoostVertex> key2vertex;
BoostVertex v1, v2;

View File

@ -730,7 +730,7 @@ TEST( GaussianFactorGraph, constrained_multi2 )
CHECK(assert_equal(expected, actual));
}
/* ************************************************************************* *
/* ************************************************************************* */
TEST( GaussianFactorGraph, findMinimumSpanningTree )
{
GaussianFactorGraph g;
@ -743,14 +743,14 @@ TEST( GaussianFactorGraph, findMinimumSpanningTree )
g.add("x2", I, "x4", I, b, 0);
g.add("x3", I, "x4", I, b, 0);
map<string, string> tree = g.findMinimumSpanningTree();
map<string, string> tree = g.findMinimumSpanningTree<string>();
CHECK(tree["x1"].compare("x1")==0);
CHECK(tree["x2"].compare("x1")==0);
CHECK(tree["x3"].compare("x1")==0);
CHECK(tree["x4"].compare("x1")==0);
}
/* ************************************************************************* *
/* ************************************************************************* */
TEST( GaussianFactorGraph, split )
{
GaussianFactorGraph g;
@ -769,7 +769,7 @@ TEST( GaussianFactorGraph, split )
tree["x4"] = "x1";
GaussianFactorGraph Ab1, Ab2;
g.split(tree, Ab1, Ab2);
g.split<string>(tree, Ab1, Ab2);
LONGS_EQUAL(3, Ab1.size());
LONGS_EQUAL(2, Ab2.size());
}