Added more flexible print interface with optional key formatter

release/4.3a0
Alex Cunningham 2012-06-27 19:16:18 +00:00
parent b8c346b60d
commit b98f60ddb5
18 changed files with 81 additions and 47 deletions

View File

@ -63,9 +63,11 @@ namespace gtsam {
/// @{ /// @{
/** GTSAM-style print */ /** GTSAM-style print */
void print(const std::string& s = "Discrete Conditional: ") const { void print(const std::string& s = "Discrete Conditional: ",
const boost::function<std::string(Index)>& formatter
= &(boost::lexical_cast<std::string, Index>)) const {
std::cout << s << std::endl; std::cout << s << std::endl;
IndexConditional::print(s); IndexConditional::print(s, formatter);
Potentials::print(s); Potentials::print(s);
} }

View File

@ -82,8 +82,10 @@ namespace gtsam {
/// @{ /// @{
// print // print
virtual void print(const std::string& s = "DiscreteFactor\n") const { virtual void print(const std::string& s = "DiscreteFactor\n",
IndexFactor::print(s); const boost::function<std::string(Index)>& formatter
= &(boost::lexical_cast<std::string, Index>)) const {
IndexFactor::print(s,formatter);
} }
/// @} /// @}

View File

@ -63,6 +63,18 @@ namespace gtsam {
return product; return product;
} }
/* ************************************************************************* */
void DiscreteFactorGraph::print(const std::string& s,
const IndexFormatter& formatter) const {
std::cout << s << std::endl;
std::cout << "size: " << size() << std::endl;
for (size_t i = 0; i < factors_.size(); i++) {
std::stringstream ss;
ss << "factor " << i << ": ";
if (factors_[i] != NULL) factors_[i]->print(ss.str(), formatter);
}
}
/* ************************************************************************* */ /* ************************************************************************* */
std::pair<DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr> // std::pair<DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr> //
EliminateDiscrete(const FactorGraph<DiscreteFactor>& factors, size_t num) { EliminateDiscrete(const FactorGraph<DiscreteFactor>& factors, size_t num) {
@ -90,6 +102,7 @@ namespace gtsam {
return std::make_pair(cond, sum); return std::make_pair(cond, sum);
} }
/* ************************************************************************* */ /* ************************************************************************* */
} // namespace } // namespace

View File

@ -77,15 +77,8 @@ public:
double operator()(const DiscreteFactor::Values & values) const; double operator()(const DiscreteFactor::Values & values) const;
/// print /// print
void print(const std::string& s = "DiscreteFactorGraph") const { void print(const std::string& s = "DiscreteFactorGraph",
std::cout << s << std::endl; const IndexFormatter& formatter = &(boost::lexical_cast<std::string, Index>)) const;
std::cout << "size: " << size() << std::endl;
for (size_t i = 0; i < factors_.size(); i++) {
std::stringstream ss;
ss << "factor " << i << ": ";
if (factors_[i] != NULL) factors_[i]->print(ss.str());
}
}
}; };
// DiscreteFactorGraph // DiscreteFactorGraph

View File

@ -34,10 +34,11 @@ namespace gtsam {
/* ************************************************************************* */ /* ************************************************************************* */
template<class CONDITIONAL> template<class CONDITIONAL>
void BayesNet<CONDITIONAL>::print(const std::string& s) const { void BayesNet<CONDITIONAL>::print(const std::string& s,
const boost::function<std::string(KeyType)>& formatter) const {
std::cout << s; std::cout << s;
BOOST_REVERSE_FOREACH(sharedConditional conditional, conditionals_) BOOST_REVERSE_FOREACH(sharedConditional conditional, conditionals_)
conditional->print(); conditional->print("Conditional", formatter);
} }
/* ************************************************************************* */ /* ************************************************************************* */

View File

@ -23,6 +23,7 @@
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/serialization/nvp.hpp> #include <boost/serialization/nvp.hpp>
#include <boost/assign/list_inserter.hpp> #include <boost/assign/list_inserter.hpp>
#include <boost/lexical_cast.hpp>
#include <gtsam/base/types.h> #include <gtsam/base/types.h>
#include <gtsam/base/FastList.h> #include <gtsam/base/FastList.h>
@ -48,6 +49,7 @@ public:
typedef typename boost::shared_ptr<BayesNet<CONDITIONAL> > shared_ptr; typedef typename boost::shared_ptr<BayesNet<CONDITIONAL> > shared_ptr;
/** We store shared pointers to Conditional densities */ /** We store shared pointers to Conditional densities */
typedef typename CONDITIONAL::KeyType KeyType;
typedef typename boost::shared_ptr<CONDITIONAL> sharedConditional; typedef typename boost::shared_ptr<CONDITIONAL> sharedConditional;
typedef typename boost::shared_ptr<const CONDITIONAL> const_sharedConditional; typedef typename boost::shared_ptr<const CONDITIONAL> const_sharedConditional;
typedef typename std::list<sharedConditional> Conditionals; typedef typename std::list<sharedConditional> Conditionals;
@ -88,7 +90,8 @@ public:
/// @{ /// @{
/** print */ /** print */
void print(const std::string& s = "") const; void print(const std::string& s = "",
const boost::function<std::string(KeyType)>& formatter = &(boost::lexical_cast<std::string, KeyType>)) const;
/** print statistics */ /** print statistics */
void printStats(const std::string& s = "") const; void printStats(const std::string& s = "") const;

View File

@ -71,10 +71,11 @@ namespace gtsam {
/* ************************************************************************* */ /* ************************************************************************* */
template<class FG> template<class FG>
void ClusterTree<FG>::Cluster::print(const std::string& indent) const { void ClusterTree<FG>::Cluster::print(const std::string& indent,
const boost::function<std::string(KeyType)>& formatter) const {
std::cout << indent; std::cout << indent;
BOOST_FOREACH(const Index key, frontal) BOOST_FOREACH(const Index key, frontal)
std::cout << key << " "; std::cout << formatter(key) << " ";
std::cout << ": "; std::cout << ": ";
BOOST_FOREACH(const Index key, separator) BOOST_FOREACH(const Index key, separator)
std::cout << key << " "; std::cout << key << " ";
@ -83,19 +84,21 @@ namespace gtsam {
/* ************************************************************************* */ /* ************************************************************************* */
template<class FG> template<class FG>
void ClusterTree<FG>::Cluster::printTree(const std::string& indent) const { void ClusterTree<FG>::Cluster::printTree(const std::string& indent,
print(indent); const boost::function<std::string(KeyType)>& formatter) const {
print(indent, formatter);
BOOST_FOREACH(const shared_ptr& child, children_) BOOST_FOREACH(const shared_ptr& child, children_)
child->printTree(indent + " "); child->printTree(indent + " ", formatter);
} }
/* ************************************************************************* * /* ************************************************************************* *
* ClusterTree * ClusterTree
* ************************************************************************* */ * ************************************************************************* */
template<class FG> template<class FG>
void ClusterTree<FG>::print(const std::string& str) const { void ClusterTree<FG>::print(const std::string& str,
const boost::function<std::string(KeyType)>& formatter) const {
std::cout << str << std::endl; std::cout << str << std::endl;
if (root_) root_->printTree(""); if (root_) root_->printTree("", formatter);
} }
/* ************************************************************************* */ /* ************************************************************************* */

View File

@ -25,6 +25,7 @@
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp> #include <boost/weak_ptr.hpp>
#include <boost/lexical_cast.hpp>
#include <gtsam/base/types.h> #include <gtsam/base/types.h>
@ -38,6 +39,9 @@ namespace gtsam {
*/ */
template <class FG> template <class FG>
class ClusterTree { class ClusterTree {
public:
// Access to factor types
typedef typename FG::KeyType KeyType;
protected: protected:
@ -74,10 +78,10 @@ namespace gtsam {
Cluster(FRONTALIT firstFrontal, FRONTALIT lastFrontal, SEPARATORIT firstSeparator, SEPARATORIT lastSeparator); Cluster(FRONTALIT firstFrontal, FRONTALIT lastFrontal, SEPARATORIT firstSeparator, SEPARATORIT lastSeparator);
/// print /// print
void print(const std::string& indent) const; void print(const std::string& indent, const boost::function<std::string(KeyType)>& formatter = &(boost::lexical_cast<std::string, KeyType>)) const;
/// print the enire tree /// print the enire tree
void printTree(const std::string& indent) const; void printTree(const std::string& indent, const boost::function<std::string(KeyType)>& formatter = &(boost::lexical_cast<std::string, KeyType>)) const;
/// check equality /// check equality
bool equals(const Cluster& other) const; bool equals(const Cluster& other) const;
@ -123,7 +127,7 @@ namespace gtsam {
/// @{ /// @{
/// print the object /// print the object
void print(const std::string& str="") const; void print(const std::string& str="", const boost::function<std::string(KeyType)>& formatter = &(boost::lexical_cast<std::string, KeyType>)) const;
/** check equality */ /** check equality */
bool equals(const ClusterTree<FG>& other, double tol = 1e-9) const; bool equals(const ClusterTree<FG>& other, double tol = 1e-9) const;

View File

@ -166,12 +166,13 @@ EliminationTree<FACTOR>::Create(const FactorGraph<DERIVEDFACTOR>& factorGraph) {
/* ************************************************************************* */ /* ************************************************************************* */
template<class FACTORGRAPH> template<class FACTORGRAPH>
void EliminationTree<FACTORGRAPH>::print(const std::string& name) const { void EliminationTree<FACTORGRAPH>::print(const std::string& name,
std::cout << name << " (" << key_ << ")" << std::endl; const boost::function<std::string(KeyType)>& formatter) const {
std::cout << name << " (" << formatter(key_) << ")" << std::endl;
BOOST_FOREACH(const sharedFactor& factor, factors_) { BOOST_FOREACH(const sharedFactor& factor, factors_) {
factor->print(name + " "); } factor->print(name + " ", formatter); }
BOOST_FOREACH(const shared_ptr& child, subTrees_) { BOOST_FOREACH(const shared_ptr& child, subTrees_) {
child->print(name + " "); } child->print(name + " ", formatter); }
} }
/* ************************************************************************* */ /* ************************************************************************* */

View File

@ -54,6 +54,8 @@ public:
typedef boost::shared_ptr<This> shared_ptr; ///< Shared pointer to this class typedef boost::shared_ptr<This> shared_ptr; ///< Shared pointer to this class
typedef typename boost::shared_ptr<FACTOR> sharedFactor; ///< Shared pointer to a factor typedef typename boost::shared_ptr<FACTOR> sharedFactor; ///< Shared pointer to a factor
typedef gtsam::BayesNet<typename FACTOR::ConditionalType> BayesNet; ///< The BayesNet corresponding to FACTOR typedef gtsam::BayesNet<typename FACTOR::ConditionalType> BayesNet; ///< The BayesNet corresponding to FACTOR
typedef FACTOR Factor;
typedef typename FACTOR::KeyType KeyType;
/** Typedef for an eliminate subroutine */ /** Typedef for an eliminate subroutine */
typedef typename FactorGraph<FACTOR>::Eliminate Eliminate; typedef typename FactorGraph<FACTOR>::Eliminate Eliminate;
@ -67,7 +69,7 @@ private:
typedef FastList<shared_ptr> SubTrees; typedef FastList<shared_ptr> SubTrees;
typedef std::vector<typename FACTOR::ConditionalType::shared_ptr> Conditionals; typedef std::vector<typename FACTOR::ConditionalType::shared_ptr> Conditionals;
Index key_; ///< index associated with root Index key_; ///< index associated with root // FIXME: doesn't this require that "Index" is the type of keys in the generic factor?
Factors factors_; ///< factors associated with root Factors factors_; ///< factors associated with root
SubTrees subTrees_; ///< sub-trees SubTrees subTrees_; ///< sub-trees
@ -141,7 +143,8 @@ public:
/// @{ /// @{
/** Print the tree to cout */ /** Print the tree to cout */
void print(const std::string& name = "EliminationTree: ") const; void print(const std::string& name = "EliminationTree: ",
const boost::function<std::string(KeyType)>& formatter = &(boost::lexical_cast<std::string, KeyType>)) const;
/** Test whether the tree is equal to another */ /** Test whether the tree is equal to another */
bool equals(const EliminationTree& other, double tol = 1e-9) const; bool equals(const EliminationTree& other, double tol = 1e-9) const;

View File

@ -56,9 +56,10 @@ namespace gtsam {
/* ************************************************************************* */ /* ************************************************************************* */
template<typename KEY> template<typename KEY>
void Factor<KEY>::print(const std::string& s) const { void Factor<KEY>::print(const std::string& s,
const boost::function<std::string(KeyType)>& formatter) const {
std::cout << s << " "; std::cout << s << " ";
BOOST_FOREACH(KEY key, keys_) std::cout << " " << key; BOOST_FOREACH(KEY key, keys_) std::cout << " " << formatter(key);
std::cout << std::endl; std::cout << std::endl;
} }

View File

@ -25,6 +25,8 @@
#include <vector> #include <vector>
#include <boost/serialization/nvp.hpp> #include <boost/serialization/nvp.hpp>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <boost/function/function1.hpp>
#include <boost/lexical_cast.hpp>
#include <gtsam/base/FastMap.h> #include <gtsam/base/FastMap.h>
namespace gtsam { namespace gtsam {
@ -190,7 +192,8 @@ public:
/// @{ /// @{
/// print /// print
void print(const std::string& s = "Factor") const; void print(const std::string& s = "Factor",
const boost::function<std::string(KeyType)>& formatter = &(boost::lexical_cast<std::string, KeyType>)) const;
/// check equality /// check equality
bool equals(const This& other, double tol = 1e-9) const; bool equals(const This& other, double tol = 1e-9) const;

View File

@ -48,13 +48,14 @@ namespace gtsam {
/* ************************************************************************* */ /* ************************************************************************* */
template<class FACTOR> template<class FACTOR>
void FactorGraph<FACTOR>::print(const std::string& s) const { void FactorGraph<FACTOR>::print(const std::string& s,
const boost::function<std::string(KeyType)>& formatter) const {
std::cout << s << std::endl; std::cout << s << std::endl;
std::cout << "size: " << size() << std::endl; std::cout << "size: " << size() << std::endl;
for (size_t i = 0; i < factors_.size(); i++) { for (size_t i = 0; i < factors_.size(); i++) {
std::stringstream ss; std::stringstream ss;
ss << "factor " << i << ": "; ss << "factor " << i << ": ";
if (factors_[i] != NULL) factors_[i]->print(ss.str()); if (factors_[i] != NULL) factors_[i]->print(ss.str(), formatter);
} }
} }

View File

@ -138,7 +138,8 @@ template<class CONDITIONAL, class CLIQUE> class BayesTree;
/// @{ /// @{
/** print out graph */ /** print out graph */
void print(const std::string& s = "FactorGraph") const; void print(const std::string& s = "FactorGraph",
const boost::function<std::string(KeyType)>& formatter = &(boost::lexical_cast<std::string, KeyType>)) const;
/** Check equality */ /** Check equality */
bool equals(const This& fg, double tol = 1e-9) const; bool equals(const This& fg, double tol = 1e-9) const;

View File

@ -23,11 +23,11 @@ using namespace std;
namespace gtsam { namespace gtsam {
/* ************************************************************************* */ /* ************************************************************************* */
void GaussianDensity::print(const string &s) const void GaussianDensity::print(const string &s, const IndexFormatter& formatter) const
{ {
cout << s << ": density on "; cout << s << ": density on ";
for(const_iterator it = beginFrontals(); it != endFrontals(); ++it) for(const_iterator it = beginFrontals(); it != endFrontals(); ++it)
cout << (boost::format("[%1%]")%(*it)).str() << " "; cout << (boost::format("[%1%]")%(formatter(*it))).str() << " ";
cout << endl; cout << endl;
gtsam::print(Matrix(get_R()),"R"); gtsam::print(Matrix(get_R()),"R");
gtsam::print(Vector(get_d()),"d"); gtsam::print(Vector(get_d()),"d");

View File

@ -54,7 +54,8 @@ namespace gtsam {
} }
/// print /// print
void print(const std::string& = "GaussianDensity") const; void print(const std::string& = "GaussianDensity",
const IndexFormatter& formatter = &(boost::lexical_cast<std::string, Index>)) const;
/// Mean \f$ \mu = R^{-1} d \f$ /// Mean \f$ \mu = R^{-1} d \f$
Vector mean() const; Vector mean() const;

View File

@ -77,10 +77,10 @@ void VectorValues::insert(Index j, const Vector& value) {
} }
/* ************************************************************************* */ /* ************************************************************************* */
void VectorValues::print(const std::string& str) const { void VectorValues::print(const std::string& str, const IndexFormatter& formatter) const {
std::cout << str << ": " << size() << " elements\n"; std::cout << str << ": " << size() << " elements\n";
for (Index var = 0; var < size(); ++var) for (Index var = 0; var < size(); ++var)
std::cout << " " << var << ": \n" << operator[](var) << "\n"; std::cout << " " << formatter(var) << ": \n" << operator[](var) << "\n";
std::cout.flush(); std::cout.flush();
} }

View File

@ -21,6 +21,7 @@
#include <gtsam/base/types.h> #include <gtsam/base/types.h>
#include <gtsam/inference/Permutation.h> #include <gtsam/inference/Permutation.h>
#include <boost/lexical_cast.hpp>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <numeric> #include <numeric>
@ -159,15 +160,16 @@ namespace gtsam {
iterator begin() { chk(); return maps_.begin(); } ///< Iterator over variables iterator begin() { chk(); return maps_.begin(); } ///< Iterator over variables
const_iterator begin() const { chk(); return maps_.begin(); } ///< Iterator over variables const_iterator begin() const { chk(); return maps_.begin(); } ///< Iterator over variables
iterator end() { chk(); return maps_.end(); } ///< Iterator over variables iterator end() { chk(); return maps_.end(); } ///< Iterator over variables
const_iterator end() const { chk(); return maps_.end(); } ///< Iterator over variables const_iterator end() const { chk(); return maps_.end(); } ///< Iterator over variables
reverse_iterator rbegin() { chk(); return maps_.rbegin(); } ///< Reverse iterator over variables reverse_iterator rbegin() { chk(); return maps_.rbegin(); } ///< Reverse iterator over variables
const_reverse_iterator rbegin() const { chk(); return maps_.rbegin(); } ///< Reverse iterator over variables const_reverse_iterator rbegin() const { chk(); return maps_.rbegin(); } ///< Reverse iterator over variables
reverse_iterator rend() { chk(); return maps_.rend(); } ///< Reverse iterator over variables reverse_iterator rend() { chk(); return maps_.rend(); } ///< Reverse iterator over variables
const_reverse_iterator rend() const { chk(); return maps_.rend(); } ///< Reverse iterator over variables const_reverse_iterator rend() const { chk(); return maps_.rend(); } ///< Reverse iterator over variables
/** print required by Testable for unit testing */ /** print required by Testable for unit testing */
void print(const std::string& str = "VectorValues: ") const; void print(const std::string& str = "VectorValues: ",
const IndexFormatter& formatter = &(boost::lexical_cast<std::string, Index>)) const;
/** equals required by Testable for unit testing */ /** equals required by Testable for unit testing */
bool equals(const VectorValues& x, double tol = 1e-9) const; bool equals(const VectorValues& x, double tol = 1e-9) const;