KISS: just a map of pointers in BayesTree now

release/4.3a0
Frank Dellaert 2009-11-05 05:29:47 +00:00
parent 3d334401f5
commit 11fcd5a69d
3 changed files with 35 additions and 31 deletions

View File

@ -60,29 +60,29 @@ namespace gtsam {
void BayesTree<Conditional>::print(const string& s) const { void BayesTree<Conditional>::print(const string& s) const {
cout << s << ": size == " << nodes_.size() << endl; cout << s << ": size == " << nodes_.size() << endl;
if (nodes_.empty()) return; if (nodes_.empty()) return;
nodes_[0]->printTree(""); root_->printTree("");
} }
/* ************************************************************************* */ /* ************************************************************************* */
template<class Conditional> template<class Conditional>
bool BayesTree<Conditional>::equals(const BayesTree<Conditional>& other, bool BayesTree<Conditional>::equals(const BayesTree<Conditional>& other,
double tol) const { double tol) const {
return size()==other.size() && return size()==other.size();
equal(nodeMap_.begin(),nodeMap_.end(),other.nodeMap_.begin()) && //&& equal(nodes_.begin(),nodes_.end(),other.nodes_.begin(),equals_star<Node>(tol));
equal(nodes_.begin(),nodes_.end(),other.nodes_.begin(),equals_star<Node>(tol));
} }
/* ************************************************************************* */ /* ************************************************************************* */
template<class Conditional> template<class Conditional>
void BayesTree<Conditional>::addClique boost::shared_ptr<typename BayesTree<Conditional>::Node> BayesTree<Conditional>::addClique
(const boost::shared_ptr<Conditional>& conditional, node_ptr parent_clique) (const boost::shared_ptr<Conditional>& conditional, node_ptr parent_clique)
{ {
node_ptr new_clique(new Node(conditional)); node_ptr new_clique(new Node(conditional));
nodeMap_.insert(make_pair(conditional->key(), nodes_.size())); nodes_.insert(make_pair(conditional->key(), new_clique));
nodes_.push_back(new_clique); if (parent_clique!=NULL) {
if (parent_clique==NULL) return; new_clique->parent_ = parent_clique;
new_clique->parent_ = parent_clique; parent_clique->children_.push_back(new_clique);
parent_clique->children_.push_back(new_clique); }
return new_clique;
} }
/* ************************************************************************* */ /* ************************************************************************* */
@ -96,21 +96,20 @@ namespace gtsam {
// if no parents, start a new root clique // if no parents, start a new root clique
if (parents.empty()) { if (parents.empty()) {
addClique(conditional); root_ = addClique(conditional);
return; return;
} }
// otherwise, find the parent clique // otherwise, find the parent clique
string parent = parents.front(); string parent = parents.front();
NodeMap::const_iterator it = nodeMap_.find(parent); typename Nodes::const_iterator it = nodes_.find(parent);
if (it == nodeMap_.end()) throw(invalid_argument( if (it == nodes_.end()) throw(invalid_argument(
"BayesTree::insert('"+key+"'): parent '" + parent + "' not yet inserted")); "BayesTree::insert('"+key+"'): parent '" + parent + "' not yet inserted"));
int parent_index = it->second; node_ptr parent_clique = it->second;
node_ptr parent_clique = nodes_[parent_index];
// if the parents and parent clique have the same size, add to parent clique // if the parents and parent clique have the same size, add to parent clique
if (parent_clique->size() == parents.size()) { if (parent_clique->size() == parents.size()) {
nodeMap_.insert(make_pair(key, parent_index)); nodes_.insert(make_pair(key, parent_clique));
parent_clique->push_front(conditional); parent_clique->push_front(conditional);
return; return;
} }
@ -124,12 +123,18 @@ namespace gtsam {
boost::shared_ptr<Conditional> BayesTree<Conditional>::marginal(const string& key) const { boost::shared_ptr<Conditional> BayesTree<Conditional>::marginal(const string& key) const {
// find the clique to which key belongs // find the clique to which key belongs
NodeMap::const_iterator it = nodeMap_.find(key); typename Nodes::const_iterator it = nodes_.find(key);
if (it == nodeMap_.end()) throw(invalid_argument( if (it == nodes_.end()) throw(invalid_argument(
"BayesTree::marginal('"+key+"'): key not found")); "BayesTree::marginal('"+key+"'): key not found"));
// find all cliques on the path to the root // find all cliques on the path to the root and turn into factor graph
// FactorGraph // FactorGraph
node_ptr node = it->second;
int i=0;
while (node!=NULL) {
//node->print("node");
node = node->parent_;
}
boost::shared_ptr<Conditional> result(new Conditional); boost::shared_ptr<Conditional> result(new Conditional);
return result; return result;

View File

@ -56,17 +56,16 @@ namespace gtsam {
void printTree(const std::string& indent) const; void printTree(const std::string& indent) const;
}; };
/** vector of Nodes */ /** Map from keys to Node */
typedef boost::shared_ptr<Node> node_ptr; typedef boost::shared_ptr<Node> node_ptr;
typedef std::vector<node_ptr> Nodes; typedef std::map<std::string, node_ptr> Nodes;
Nodes nodes_; Nodes nodes_;
/** Map from keys to Node index */ /** Roor clique */
typedef std::map<std::string, int> NodeMap; node_ptr root_;
NodeMap nodeMap_;
/** add a clique */ /** add a clique */
void addClique(const conditional_ptr& conditional, node_ptr parent_clique=node_ptr()); node_ptr addClique(const conditional_ptr& conditional, node_ptr parent_clique=node_ptr());
public: public:
@ -92,7 +91,7 @@ namespace gtsam {
inline size_t size() const { return nodes_.size();} inline size_t size() const { return nodes_.size();}
/** return root clique */ /** return root clique */
const BayesNet<Conditional>& root() const {return *(nodes_[0]);} boost::shared_ptr<BayesNet<Conditional> > root() const {return root_;}
/** return marginal on any variable */ /** return marginal on any variable */
boost::shared_ptr<Conditional> marginal(const std::string& key) const; boost::shared_ptr<Conditional> marginal(const std::string& key) const;

View File

@ -50,15 +50,15 @@ TEST( BayesTree, constructor )
bayesTree.insert(X); bayesTree.insert(X);
// Check Size // Check Size
LONGS_EQUAL(4,bayesTree.size()); LONGS_EQUAL(6,bayesTree.size());
// Check root // Check root
BayesNet<SymbolicConditional> expected_root; BayesNet<SymbolicConditional> expected_root;
expected_root.push_back(E); expected_root.push_back(E);
expected_root.push_back(L); expected_root.push_back(L);
expected_root.push_back(B); expected_root.push_back(B);
BayesNet<SymbolicConditional> actual_root = bayesTree.root(); boost::shared_ptr<BayesNet<SymbolicConditional> > actual_root = bayesTree.root();
CHECK(assert_equal(expected_root,actual_root)); CHECK(assert_equal(expected_root,*actual_root));
// Create from symbolic Bayes chain in which we want to discover cliques // Create from symbolic Bayes chain in which we want to discover cliques
SymbolicBayesNet ASIA; SymbolicBayesNet ASIA;
@ -97,7 +97,7 @@ TEST( BayesTree, smoother )
// Create the Bayes tree // Create the Bayes tree
BayesTree<ConditionalGaussian> bayesTree(*chordalBayesNet); BayesTree<ConditionalGaussian> bayesTree(*chordalBayesNet);
LONGS_EQUAL(6,bayesTree.size()); LONGS_EQUAL(7,bayesTree.size());
} }
/* ************************************************************************* * /* ************************************************************************* *
@ -119,7 +119,7 @@ TEST( BayesTree, balanced_smoother_marginals )
// Create the Bayes tree // Create the Bayes tree
BayesTree<ConditionalGaussian> bayesTree(*chordalBayesNet); BayesTree<ConditionalGaussian> bayesTree(*chordalBayesNet);
LONGS_EQUAL(4,bayesTree.size()); LONGS_EQUAL(7,bayesTree.size());
// Check root clique // Check root clique
//BayesNet<ConditionalGaussian> expected_root; //BayesNet<ConditionalGaussian> expected_root;