Renamed BayesNet::insert -> push_back. BayesTree now uses Bayes nets as nodes.

release/4.3a0
Frank Dellaert 2009-11-02 05:17:44 +00:00
parent a8d267c4ca
commit eab038651e
13 changed files with 94 additions and 106 deletions

View File

@ -300,6 +300,7 @@
<buildTargets>
<target name="install" path="wrap" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>install</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -307,6 +308,7 @@
</target>
<target name="check" path="wrap" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>check</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -314,14 +316,15 @@
</target>
<target name="check" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildArguments>-k</buildArguments>
<buildTarget>check</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
<useDefaultCommand>false</useDefaultCommand>
<runAllBuilders>true</runAllBuilders>
</target>
<target name="testSimpleCamera.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testSimpleCamera.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -337,7 +340,6 @@
</target>
<target name="testVSLAMFactor.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testVSLAMFactor.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -345,6 +347,7 @@
</target>
<target name="testCalibratedCamera.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testCalibratedCamera.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -352,7 +355,6 @@
</target>
<target name="testConditionalGaussian.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testConditionalGaussian.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -360,6 +362,7 @@
</target>
<target name="testPose2.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testPose2.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -375,6 +378,7 @@
</target>
<target name="testRot3.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testRot3.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -382,7 +386,6 @@
</target>
<target name="testNonlinearOptimizer.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testNonlinearOptimizer.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -390,6 +393,7 @@
</target>
<target name="testLinearFactor.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testLinearFactor.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -397,6 +401,7 @@
</target>
<target name="testConstrainedNonlinearFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testConstrainedNonlinearFactorGraph.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -404,6 +409,7 @@
</target>
<target name="testLinearFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testLinearFactorGraph.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -411,7 +417,6 @@
</target>
<target name="testNonlinearFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testNonlinearFactorGraph.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -419,6 +424,7 @@
</target>
<target name="testPose3.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testPose3.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -426,7 +432,6 @@
</target>
<target name="testConstrainedLinearFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testConstrainedLinearFactorGraph.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -434,7 +439,6 @@
</target>
<target name="testVectorConfig.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testVectorConfig.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -442,7 +446,6 @@
</target>
<target name="testPoint2.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testPoint2.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -450,6 +453,7 @@
</target>
<target name="testNonlinearFactor.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testNonlinearFactor.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -457,6 +461,7 @@
</target>
<target name="timeLinearFactor.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>timeLinearFactor.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -464,6 +469,7 @@
</target>
<target name="timeLinearFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>timeLinearFactorGraph.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -471,6 +477,7 @@
</target>
<target name="testGaussianBayesNet.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testGaussianBayesNet.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -478,7 +485,6 @@
</target>
<target name="testBayesTree.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testBayesTree.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>false</useDefaultCommand>
@ -486,7 +492,6 @@
</target>
<target name="testSymbolicBayesChain.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testSymbolicBayesChain.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>false</useDefaultCommand>
@ -494,7 +499,6 @@
</target>
<target name="testSymbolicFactorGraph.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testSymbolicFactorGraph.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>false</useDefaultCommand>
@ -502,6 +506,7 @@
</target>
<target name="testVector.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testVector.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -509,6 +514,7 @@
</target>
<target name="testMatrix.run" path="cpp" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>testMatrix.run</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -516,7 +522,6 @@
</target>
<target name="install" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>install</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -524,7 +529,6 @@
</target>
<target name="clean" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>clean</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
@ -532,7 +536,6 @@
</target>
<target name="check" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>check</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>

View File

@ -37,7 +37,7 @@ namespace gtsam {
/* ************************************************************************* */
template<class Conditional>
void BayesNet<Conditional>::insert
void BayesNet<Conditional>::push_back
(const boost::shared_ptr<Conditional>& conditional) {
indices_.insert(make_pair(conditional->key(),conditionals_.size()));
conditionals_.push_back(conditional);

View File

@ -67,8 +67,8 @@ namespace gtsam {
/** check equality */
bool equals(const BayesNet& other, double tol = 1e-9) const;
/** insert: use reverse topological sort (i.e. parents last / elimination order) */
void insert(const boost::shared_ptr<Conditional>& conditional);
/** push_back: use reverse topological sort (i.e. parents last / elimination order) */
void push_back(const boost::shared_ptr<Conditional>& conditional);
/** size is the number of nodes */
inline size_t size() const {

View File

@ -13,36 +13,32 @@ namespace gtsam {
/* ************************************************************************* */
template<class Conditional>
Front<Conditional>::Front(const conditional_ptr& conditional) {
add(conditional);
separator_ = conditional->parents();
}
/* ************************************************************************* */
template<class Conditional>
void Front<Conditional>::print(const string& s) const {
cout << s;
BOOST_FOREACH(const conditional_ptr& conditional, conditionals_)
cout << " " << conditional->key();
if (!separator_.empty()) {
cout << " :";
BOOST_FOREACH(string key, separator_)
cout << " " << key;
BayesTree<Conditional>::Node::Node(const boost::shared_ptr<Conditional>& conditional) {
separator_ = conditional->parents();
this->push_back(conditional);
}
cout << endl;
}
/* ************************************************************************* */
template<class Conditional>
bool Front<Conditional>::equals(const Front<Conditional>& other, double tol) const {
return equal(conditionals_.begin(),conditionals_.end(),other.conditionals_.begin(),equals_star<Conditional>);
}
void BayesTree<Conditional>::Node::print(const string& s) const {
cout << s;
BOOST_REVERSE_FOREACH(const conditional_ptr& conditional, this->conditionals_)
cout << " " << conditional->key();
if (!separator_.empty()) {
cout << " :";
BOOST_FOREACH(string key, separator_)
cout << " " << key;
}
cout << endl;
}
/* ************************************************************************* */
template<class Conditional>
void Front<Conditional>::add(const conditional_ptr& conditional) {
conditionals_.push_front(conditional);
}
void BayesTree<Conditional>::Node::printTree(const string& indent) const {
print(indent);
BOOST_FOREACH(shared_ptr child, children_)
child->printTree(indent+" ");
}
/* ************************************************************************* */
template<class Conditional>
@ -104,12 +100,13 @@ namespace gtsam {
"BayesTree::insert('"+key+"'): parent '" + parent + "' was not yet inserted"));
int index = it->second;
node_ptr parent_clique = nodes_[index];
if (verbose) cout << "Parent clique " << index << " of size " << parent_clique->size() << endl;
// if the parents and parent clique have the same size, add to parent clique
if (parent_clique->size() == parents.size()) {
if (verbose) cout << "Adding to clique " << index << endl;
nodeMap_.insert(make_pair(key, index));
parent_clique->add(conditional);
parent_clique->push_back(conditional);
return;
}

View File

@ -13,36 +13,12 @@
#include <vector>
#include <boost/serialization/map.hpp>
#include <boost/serialization/list.hpp>
#include "Testable.h"
#include "BayesNet.h"
namespace gtsam {
/** A clique in a Bayes tree consisting of frontal nodes and conditionals */
template<class Conditional>
class Front: Testable<Front<Conditional> > {
private:
typedef boost::shared_ptr<Conditional> conditional_ptr;
std::list<conditional_ptr> conditionals_; /** conditionals */
std::list<std::string> separator_; /** separator keys */
public:
/** constructor */
Front(const conditional_ptr& conditional);
/** print */
void print(const std::string& s = "") const;
/** check equality */
bool equals(const Front<Conditional>& other, double tol = 1e-9) const;
/** add a frontal node */
void add(const conditional_ptr& conditional);
/** return size of the clique */
inline size_t size() const {return conditionals_.size() + separator_.size();}
};
/**
* Bayes tree
* Templated on the Conditional class, the type of node in the underlying Bayes chain.
@ -58,20 +34,27 @@ namespace gtsam {
private:
/** A Node in the tree is a Front with tree connectivity */
struct Node : public Front<Conditional> {
/** A Node in the tree is an incomplete Bayes net: the variables
* in the Bayes net are the frontal nodes, and the variables conditioned
* on is the separator. We also have pointers up and down the tree.
*/
struct Node : public BayesNet<Conditional> {
typedef boost::shared_ptr<Node> shared_ptr;
shared_ptr parent_;
std::list<std::string> separator_; /** separator keys */
std::list<shared_ptr> children_;
Node(const boost::shared_ptr<Conditional>& conditional):Front<Conditional>(conditional) {}
//* Constructor */
Node(const boost::shared_ptr<Conditional>& conditional);
/** The size *includes* the separator */
size_t size() const { return this->conditionals_.size() + separator_.size(); }
/** print this node */
void print(const std::string& s="Bayes tree node") const;
/** print this node and entire subtree below it*/
void printTree(const std::string& indent) const {
print(indent);
BOOST_FOREACH(shared_ptr child, children_)
child->printTree(indent+" ");
}
void printTree(const std::string& indent) const;
};
/** vector of Nodes */
@ -107,7 +90,7 @@ namespace gtsam {
inline size_t size() const { return nodes_.size();}
/** return root clique */
const Front<Conditional>& root() const {return *(nodes_[0]);}
const BayesNet<Conditional>& root() const {return *(nodes_[0]);}
}; // BayesTree

View File

@ -79,12 +79,12 @@ GaussianBayesNet::shared_ptr ConstrainedLinearFactorGraph::eliminate(const Order
if (is_constrained(key))
{
ConditionalGaussian::shared_ptr ccg = eliminate_constraint(key);
cbn->insert(ccg);
cbn->push_back(ccg);
}
else
{
ConditionalGaussian::shared_ptr cg = eliminateOne<ConditionalGaussian>(key);
cbn->insert(cg);
cbn->push_back(cg);
}
}

View File

@ -63,7 +63,7 @@ LinearFactorGraph::eliminate_partially(const Ordering& ordering)
BOOST_FOREACH(string key, ordering) {
ConditionalGaussian::shared_ptr cg = eliminateOne<ConditionalGaussian>(key);
chordalBayesNet->insert(cg);
chordalBayesNet->push_back(cg);
}
return chordalBayesNet;

View File

@ -26,7 +26,7 @@ namespace gtsam {
BOOST_FOREACH(string key, ordering) {
SymbolicConditional::shared_ptr conditional = eliminateOne<SymbolicConditional>(key);
bayesNet->insert(conditional);
bayesNet->push_back(conditional);
}
return bayesNet;

View File

@ -203,8 +203,8 @@ GaussianBayesNet createSmallGaussianBayesNet()
Px_y(new ConditionalGaussian("x",d1,R11,"y",S12)),
Py(new ConditionalGaussian("y",d2,R22));
GaussianBayesNet cbn;
cbn.insert(Px_y);
cbn.insert(Py);
cbn.push_back(Px_y);
cbn.push_back(Py);
return cbn;
}

View File

@ -27,10 +27,12 @@ SymbolicConditional::shared_ptr B(new SymbolicConditional("B")), L(
/* ************************************************************************* */
TEST( BayesTree, Front )
{
Front<SymbolicConditional> f1(B);
f1.add(L);
Front<SymbolicConditional> f2(L);
f2.add(B);
BayesNet<SymbolicConditional> f1;
f1.push_back(B);
f1.push_back(L);
BayesNet<SymbolicConditional> f2;
f2.push_back(L);
f2.push_back(B);
CHECK(f1.equals(f1));
CHECK(!f1.equals(f2));
}
@ -51,21 +53,24 @@ TEST( BayesTree, constructor )
LONGS_EQUAL(4,bayesTree.size());
// Check root
Front<SymbolicConditional> expected_root(B);
expected_root.add(L);
expected_root.add(E);
Front<SymbolicConditional> actual_root = bayesTree.root();
BayesNet<SymbolicConditional> expected_root;
expected_root.push_back(B);
expected_root.push_back(L);
expected_root.push_back(E);
BayesNet<SymbolicConditional> actual_root = bayesTree.root();
CHECK(assert_equal(expected_root,actual_root));
// Create from symbolic Bayes chain in which we want to discover cliques
SymbolicBayesNet ASIA;
ASIA.insert(X);
ASIA.insert(T);
ASIA.insert(S);
ASIA.insert(E);
ASIA.insert(L);
ASIA.insert(B);
BayesTree<SymbolicConditional> bayesTree2(ASIA);
ASIA.push_back(X);
ASIA.push_back(T);
ASIA.push_back(S);
ASIA.push_back(E);
ASIA.push_back(L);
ASIA.push_back(B);
bool verbose = true;
BayesTree<SymbolicConditional> bayesTree2(ASIA,verbose);
if (verbose) bayesTree2.print("bayesTree2");
// Check whether the same
CHECK(assert_equal(bayesTree,bayesTree2));

View File

@ -276,9 +276,9 @@ TEST( LinearFactorGraph, eliminateAll )
ConditionalGaussian::shared_ptr cg3(new ConditionalGaussian("x2",d3, R3, "l1", A21, "x1", A22));
GaussianBayesNet expected;
expected.insert(cg3);
expected.insert(cg2);
expected.insert(cg1);
expected.push_back(cg3);
expected.push_back(cg2);
expected.push_back(cg1);
// Check one ordering
LinearFactorGraph fg1 = createLinearFactorGraph();

View File

@ -27,9 +27,9 @@ TEST( SymbolicBayesNet, constructor )
l1(new SymbolicConditional("l1","x1")),
x1(new SymbolicConditional("x1"));
SymbolicBayesNet expected;
expected.insert(x2);
expected.insert(l1);
expected.insert(x1);
expected.push_back(x2);
expected.push_back(l1);
expected.push_back(x1);
// Create from a factor graph
LinearFactorGraph factorGraph = createLinearFactorGraph();

View File

@ -129,9 +129,9 @@ TEST( LinearFactorGraph, eliminate )
SymbolicConditional::shared_ptr x1(new SymbolicConditional("x1"));
SymbolicBayesNet expected;
expected.insert(x2);
expected.insert(l1);
expected.insert(x1);
expected.push_back(x2);
expected.push_back(l1);
expected.push_back(x1);
// create a test graph
LinearFactorGraph factorGraph = createLinearFactorGraph();