Add support for more infernce keys.

Add support for export_libdai, export_uai, export_gv and print_fg.
Document these keys.
This commit is contained in:
Tiago Gomes 2013-01-08 17:01:03 +00:00
parent 4220069d90
commit da0514a779
6 changed files with 148 additions and 42 deletions

View File

@ -12,6 +12,12 @@
#include "Util.h"
bool FactorGraph::exportLd_ = false;
bool FactorGraph::exportUai_ = false;
bool FactorGraph::exportGv_ = false;
bool FactorGraph::printFg_ = false;
FactorGraph::FactorGraph (const FactorGraph& fg)
{
const VarNodes& varNodes = fg.varNodes();
@ -288,41 +294,38 @@ FactorGraph::print (void) const
void
FactorGraph::exportToGraphViz (const char* fileName) const
FactorGraph::exportToLibDai (const char* fileName) const
{
ofstream out (fileName);
if (!out.is_open()) {
cerr << "Error: couldn't open file '" << fileName << "'." ;
return;
}
out << "graph \"" << fileName << "\" {" << endl;
for (size_t i = 0; i < varNodes_.size(); i++) {
if (varNodes_[i]->hasEvidence()) {
out << '"' << varNodes_[i]->label() << '"' ;
out << " [style=filled, fillcolor=yellow]" << endl;
}
}
out << facNodes_.size() << endl << endl;
for (size_t i = 0; i < facNodes_.size(); i++) {
out << '"' << facNodes_[i]->getLabel() << '"' ;
out << " [label=\"" << facNodes_[i]->getLabel();
out << "\"" << ", shape=box]" << endl;
}
for (size_t i = 0; i < facNodes_.size(); i++) {
const VarNodes& myVars = facNodes_[i]->neighbors();
for (size_t j = 0; j < myVars.size(); j++) {
out << '"' << facNodes_[i]->getLabel() << '"' ;
out << " -- " ;
out << '"' << myVars[j]->label() << '"' << endl;
Factor f (facNodes_[i]->factor());
out << f.nrArguments() << endl;
out << Util::elementsToString (f.arguments()) << endl;
out << Util::elementsToString (f.ranges()) << endl;
VarIds args = f.arguments();
std::reverse (args.begin(), args.end());
f.reorderArguments (args);
if (Globals::logDomain) {
Util::exp (f.params());
}
out << f.size() << endl;
for (size_t j = 0; j < f.size(); j++) {
out << j << " " << f[j] << endl;
}
out << endl;
}
out << "}" << endl;
out.close();
}
void
FactorGraph::exportToUaiFormat (const char* fileName) const
FactorGraph::exportToUai (const char* fileName) const
{
ofstream out (fileName);
if (!out.is_open()) {
@ -366,31 +369,34 @@ FactorGraph::exportToUaiFormat (const char* fileName) const
void
FactorGraph::exportToLibDaiFormat (const char* fileName) const
FactorGraph::exportToGraphViz (const char* fileName) const
{
ofstream out (fileName);
if (!out.is_open()) {
cerr << "Error: couldn't open file '" << fileName << "'." ;
return;
}
out << facNodes_.size() << endl << endl;
for (size_t i = 0; i < facNodes_.size(); i++) {
Factor f (facNodes_[i]->factor());
out << f.nrArguments() << endl;
out << Util::elementsToString (f.arguments()) << endl;
out << Util::elementsToString (f.ranges()) << endl;
VarIds args = f.arguments();
std::reverse (args.begin(), args.end());
f.reorderArguments (args);
if (Globals::logDomain) {
Util::exp (f.params());
out << "graph \"" << fileName << "\" {" << endl;
for (size_t i = 0; i < varNodes_.size(); i++) {
if (varNodes_[i]->hasEvidence()) {
out << '"' << varNodes_[i]->label() << '"' ;
out << " [style=filled, fillcolor=yellow]" << endl;
}
out << f.size() << endl;
for (size_t j = 0; j < f.size(); j++) {
out << j << " " << f[j] << endl;
}
out << endl;
}
for (size_t i = 0; i < facNodes_.size(); i++) {
out << '"' << facNodes_[i]->getLabel() << '"' ;
out << " [label=\"" << facNodes_[i]->getLabel();
out << "\"" << ", shape=box]" << endl;
}
for (size_t i = 0; i < facNodes_.size(); i++) {
const VarNodes& myVars = facNodes_[i]->neighbors();
for (size_t j = 0; j < myVars.size(); j++) {
out << '"' << facNodes_[i]->getLabel() << '"' ;
out << " -- " ;
out << '"' << myVars[j]->label() << '"' << endl;
}
}
out << "}" << endl;
out.close();
}

View File

@ -106,11 +106,35 @@ class FactorGraph
void print (void) const;
void exportToLibDai (const char*) const;
void exportToUai (const char*) const;
void exportToGraphViz (const char*) const;
void exportToUaiFormat (const char*) const;
static bool exportToLibDai (void) { return exportLd_; }
void exportToLibDaiFormat (const char*) const;
static bool exportToUai (void) { return exportUai_; }
static bool exportGraphViz (void) { return exportGv_; }
static bool printFactorGraph (void) { return printFg_; }
static void enableExportToLibDai (void) { exportLd_ = true; }
static void disableExportToLibDai (void) { exportLd_ = false; }
static void enableExportToUai (void) { exportUai_ = true; }
static void disableExportToUai (void) { exportUai_ = false; }
static void enableExportToGraphViz (void) { exportGv_ = true; }
static void disableExportToGraphViz (void) { exportGv_ = false; }
static void enablePrintFactorGraph (void) { printFg_ = true; }
static void disablePrintFactorGraph (void) { printFg_ = false; }
private:
void ignoreLines (std::ifstream&) const;
@ -132,6 +156,11 @@ class FactorGraph
typedef unordered_map<unsigned, VarNode*> VarMap;
VarMap varMap_;
static bool exportLd_;
static bool exportUai_;
static bool exportGv_;
static bool printFg_;
DISALLOW_ASSIGN (FactorGraph);
};

View File

@ -32,6 +32,23 @@ main (int argc, const char* argv[])
FactorGraph fg;
readFactorGraph (fg, argv[idx]);
VarIds queryIds = readQueryAndEvidence (fg, argc, argv, idx + 1);
if (FactorGraph::exportToLibDai()) {
fg.exportToLibDai ("model.fg");
}
if (FactorGraph::exportToUai()) {
fg.exportToUai ("model.uai");
}
if (FactorGraph::exportGraphViz()) {
fg.exportToGraphViz ("model.dot");
}
if (FactorGraph::printFactorGraph()) {
fg.print();
}
if (Globals::verbosity > 0) {
cout << "factor graph contains " ;
cout << fg.nrVarNodes() << " variables and " ;
cout << fg.nrFacNodes() << " factors " << endl;
}
runSolver (fg, queryIds);
return 0;
}

View File

@ -108,10 +108,21 @@ createGroundNetwork (void)
evidenceList = YAP_TailOfTerm (evidenceList);
nrObservedVars ++;
}
if (FactorGraph::exportToLibDai()) {
fg->exportToLibDai ("model.fg");
}
if (FactorGraph::exportToUai()) {
fg->exportToUai ("model.uai");
}
if (FactorGraph::exportGraphViz()) {
fg->exportToGraphViz ("model.dot");
}
if (FactorGraph::printFactorGraph()) {
fg->print();
}
if (Globals::verbosity > 0) {
cout << "factor graph contains " ;
cout << fg->nrVarNodes() << " variables " ;
cout << "(" << nrObservedVars << " observed) and " ;
cout << fg->nrVarNodes() << " variables and " ;
cout << fg->nrFacNodes() << " factors " << endl;
}
YAP_Int p = (YAP_Int) (fg);

View File

@ -272,6 +272,47 @@ setHorusFlag (string key, string value)
ss << value;
ss >> mi;
BeliefProp::setMaxIterations (mi);
} else if (key == "export_libdai") {
if ( value == "true") {
FactorGraph::enableExportToLibDai();
} else if (value == "false") {
FactorGraph::disableExportToLibDai();
Globals::logDomain = false;
} else {
cerr << "warning: invalid value `" << value << "' " ;
cerr << "for `" << key << "'" << endl;
returnVal = false;
}
} else if (key == "export_uai") {
if ( value == "true") {
FactorGraph::enableExportToUai();
} else if (value == "false") {
FactorGraph::disableExportToUai();
} else {
cerr << "warning: invalid value `" << value << "' " ;
cerr << "for `" << key << "'" << endl;
returnVal = false;
}
} else if (key == "export_graphviz") {
if ( value == "true") {
FactorGraph::enableExportToGraphViz();
} else if (value == "false") {
FactorGraph::disableExportToGraphViz();
} else {
cerr << "warning: invalid value `" << value << "' " ;
cerr << "for `" << key << "'" << endl;
returnVal = false;
}
} else if (key == "print_fg") {
if ( value == "true") {
FactorGraph::enablePrintFactorGraph();
} else if (value == "false") {
FactorGraph::disablePrintFactorGraph();
} else {
cerr << "warning: invalid value `" << value << "' " ;
cerr << "for `" << key << "'" << endl;
returnVal = false;
}
} else {
cerr << "warning: invalid key `" << key << "'" << endl;
returnVal = false;

View File

@ -265,7 +265,7 @@ This key defaults to 0 (no debugging information) and only \texttt{hve}, \texttt
The \texttt{use\_logarithms} key controls whether the calculations performed during inference should be done in a logarithm domain or not. Its values can be \texttt{true} or \texttt{false}. By default is \texttt{true} and only affects \texttt{hve}, \texttt{bp}, \texttt{cbp}, \texttt{lve}, \texttt{lkc} and \texttt{lbp} solvers. The remaining solvers always do their calculations in a logarithm domain.
There are keys specific only to some algorithm. The key \texttt{elim\_heuristic} key allows to chose which elimination heuristic will be used by the \texttt{hve} solver (but not \texttt{ve}). The following are supported:
There are keys specific only to some algorithms. The key \texttt{elim\_heuristic} key allows to chose which elimination heuristic will be used by the \texttt{hve} solver (but not \texttt{ve}). The following are supported:
\begin{itemize}
\item \texttt{sequential}
\item \texttt{min\_neighbors}
@ -294,6 +294,8 @@ The key \texttt{bp\_msg\_schedule} controls the message sending order. Its possi
\end{itemize}
It defaults to \texttt{seq\_fixed}.
The \texttt{export\_libdai} and \texttt{export\_uai} keys can be used to export the current model respectively to \href{http://cs.ru.nl/~jorism/libDAI/doc/fileformats.html}{libDAI}, and \href{http://graphmod.ics.uci.edu/uai08/FileFormat}{UAI08} formats. With the \texttt{export\_graphviz} key it is possible to save the factor graph into a format that can be read by \href{http://www.graphviz.org/}{Graphviz}. The \texttt{print\_fg} key allows to print all factors before perform inference. All these four keys accept \texttt{true} and \texttt{false} as their values and only produce effect in \texttt{hve}, \texttt{bp}, and \texttt{cbp} solvers.
\section{Horus Command Line}
This package also includes an utility to perform inference over probabilistic graphical models described in other formats, namely the \href{http://cs.ru.nl/~jorism/libDAI/doc/fileformats.html}{libDAI file format}, and the \href{http://graphmod.ics.uci.edu/uai08/FileFormat}{UAI08 file format}