"outputMetisFormat" outputs index in hmetis hypergraph format

release/4.3a0
Frank Dellaert 2012-02-08 15:23:11 +00:00
parent c323a052d5
commit 282ac6484d
2 changed files with 30 additions and 10 deletions

View File

@ -21,6 +21,8 @@
namespace gtsam { namespace gtsam {
using namespace std;
/* ************************************************************************* */ /* ************************************************************************* */
void VariableIndex::permute(const Permutation& permutation) { void VariableIndex::permute(const Permutation& permutation) {
#ifndef NDEBUG #ifndef NDEBUG
@ -36,7 +38,7 @@ void VariableIndex::permute(const Permutation& permutation) {
/* ************************************************************************* */ /* ************************************************************************* */
bool VariableIndex::equals(const VariableIndex& other, double tol) const { bool VariableIndex::equals(const VariableIndex& other, double tol) const {
if(this->nEntries_ == other.nEntries_ && this->nFactors_ == other.nFactors_) { if(this->nEntries_ == other.nEntries_ && this->nFactors_ == other.nFactors_) {
for(size_t var=0; var < std::max(this->index_.size(), other.index_.size()); ++var) for(size_t var=0; var < max(this->index_.size(), other.index_.size()); ++var)
if(var >= this->index_.size() || var >= other.index_.size()) { if(var >= this->index_.size() || var >= other.index_.size()) {
if(!((var >= this->index_.size() && other.index_[var].empty()) || if(!((var >= this->index_.size() && other.index_[var].empty()) ||
(var >= other.index_.size() && this->index_[var].empty()))) (var >= other.index_.size() && this->index_[var].empty())))
@ -49,21 +51,33 @@ bool VariableIndex::equals(const VariableIndex& other, double tol) const {
} }
/* ************************************************************************* */ /* ************************************************************************* */
void VariableIndex::print(const std::string& str) const { void VariableIndex::print(const string& str) const {
std::cout << str << "\n"; cout << str << "\n";
std::cout << "nEntries = " << this->nEntries_ << ", nFactors = " << this->nFactors_ << "\n"; cout << "nEntries = " << nEntries() << ", nFactors = " << nFactors() << "\n";
Index var = 0; Index var = 0;
BOOST_FOREACH(const Factors& variable, index_.container()) { BOOST_FOREACH(const Factors& variable, index_.container()) {
Permutation::const_iterator rvar = find(index_.permutation().begin(), index_.permutation().end(), var); Permutation::const_iterator rvar = find(index_.permutation().begin(), index_.permutation().end(), var);
assert(rvar != index_.permutation().end()); assert(rvar != index_.permutation().end());
std::cout << "var " << (rvar-index_.permutation().begin()) << ":"; cout << "var " << (rvar-index_.permutation().begin()) << ":";
BOOST_FOREACH(const size_t factor, variable) { BOOST_FOREACH(const size_t factor, variable)
std::cout << " " << factor; cout << " " << factor;
} cout << "\n";
std::cout << "\n";
++ var; ++ var;
} }
std::cout << std::flush; cout << flush;
}
/* ************************************************************************* */
void VariableIndex::outputMetisFormat(ostream& os) const {
os << size() << " " << nFactors() << "\n";
// run over variables, which will be hyper-edges.
BOOST_FOREACH(const Factors& variable, index_.container()) {
// every variable is a hyper-edge covering its factors
BOOST_FOREACH(const size_t factor, variable)
os << (factor+1) << " "; // base 1
os << "\n";
}
os << flush;
} }
} }

View File

@ -101,6 +101,12 @@ public:
/** Print the variable index (for unit tests and debugging). */ /** Print the variable index (for unit tests and debugging). */
void print(const std::string& str = "VariableIndex: ") const; void print(const std::string& str = "VariableIndex: ") const;
/**
* Output dual hypergraph to Metis file format for use with hmetis
* In the dual graph, variables are hyperedges, factors are nodes.
*/
void outputMetisFormat(std::ostream& os) const;
/// @} /// @}
/// @name Advanced Interface /// @name Advanced Interface