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:
parent
4220069d90
commit
da0514a779
@ -12,6 +12,12 @@
|
|||||||
#include "Util.h"
|
#include "Util.h"
|
||||||
|
|
||||||
|
|
||||||
|
bool FactorGraph::exportLd_ = false;
|
||||||
|
bool FactorGraph::exportUai_ = false;
|
||||||
|
bool FactorGraph::exportGv_ = false;
|
||||||
|
bool FactorGraph::printFg_ = false;
|
||||||
|
|
||||||
|
|
||||||
FactorGraph::FactorGraph (const FactorGraph& fg)
|
FactorGraph::FactorGraph (const FactorGraph& fg)
|
||||||
{
|
{
|
||||||
const VarNodes& varNodes = fg.varNodes();
|
const VarNodes& varNodes = fg.varNodes();
|
||||||
@ -288,41 +294,38 @@ FactorGraph::print (void) const
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
FactorGraph::exportToGraphViz (const char* fileName) const
|
FactorGraph::exportToLibDai (const char* fileName) const
|
||||||
{
|
{
|
||||||
ofstream out (fileName);
|
ofstream out (fileName);
|
||||||
if (!out.is_open()) {
|
if (!out.is_open()) {
|
||||||
cerr << "Error: couldn't open file '" << fileName << "'." ;
|
cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
out << "graph \"" << fileName << "\" {" << endl;
|
out << facNodes_.size() << endl << endl;
|
||||||
for (size_t i = 0; i < varNodes_.size(); i++) {
|
|
||||||
if (varNodes_[i]->hasEvidence()) {
|
|
||||||
out << '"' << varNodes_[i]->label() << '"' ;
|
|
||||||
out << " [style=filled, fillcolor=yellow]" << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (size_t i = 0; i < facNodes_.size(); i++) {
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
||||||
out << '"' << facNodes_[i]->getLabel() << '"' ;
|
Factor f (facNodes_[i]->factor());
|
||||||
out << " [label=\"" << facNodes_[i]->getLabel();
|
out << f.nrArguments() << endl;
|
||||||
out << "\"" << ", shape=box]" << endl;
|
out << Util::elementsToString (f.arguments()) << endl;
|
||||||
}
|
out << Util::elementsToString (f.ranges()) << endl;
|
||||||
for (size_t i = 0; i < facNodes_.size(); i++) {
|
VarIds args = f.arguments();
|
||||||
const VarNodes& myVars = facNodes_[i]->neighbors();
|
std::reverse (args.begin(), args.end());
|
||||||
for (size_t j = 0; j < myVars.size(); j++) {
|
f.reorderArguments (args);
|
||||||
out << '"' << facNodes_[i]->getLabel() << '"' ;
|
if (Globals::logDomain) {
|
||||||
out << " -- " ;
|
Util::exp (f.params());
|
||||||
out << '"' << myVars[j]->label() << '"' << endl;
|
|
||||||
}
|
}
|
||||||
|
out << f.size() << endl;
|
||||||
|
for (size_t j = 0; j < f.size(); j++) {
|
||||||
|
out << j << " " << f[j] << endl;
|
||||||
|
}
|
||||||
|
out << endl;
|
||||||
}
|
}
|
||||||
out << "}" << endl;
|
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
FactorGraph::exportToUaiFormat (const char* fileName) const
|
FactorGraph::exportToUai (const char* fileName) const
|
||||||
{
|
{
|
||||||
ofstream out (fileName);
|
ofstream out (fileName);
|
||||||
if (!out.is_open()) {
|
if (!out.is_open()) {
|
||||||
@ -366,31 +369,34 @@ FactorGraph::exportToUaiFormat (const char* fileName) const
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
FactorGraph::exportToLibDaiFormat (const char* fileName) const
|
FactorGraph::exportToGraphViz (const char* fileName) const
|
||||||
{
|
{
|
||||||
ofstream out (fileName);
|
ofstream out (fileName);
|
||||||
if (!out.is_open()) {
|
if (!out.is_open()) {
|
||||||
cerr << "Error: couldn't open file '" << fileName << "'." ;
|
cerr << "Error: couldn't open file '" << fileName << "'." ;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
out << facNodes_.size() << endl << endl;
|
out << "graph \"" << fileName << "\" {" << endl;
|
||||||
for (size_t i = 0; i < facNodes_.size(); i++) {
|
for (size_t i = 0; i < varNodes_.size(); i++) {
|
||||||
Factor f (facNodes_[i]->factor());
|
if (varNodes_[i]->hasEvidence()) {
|
||||||
out << f.nrArguments() << endl;
|
out << '"' << varNodes_[i]->label() << '"' ;
|
||||||
out << Util::elementsToString (f.arguments()) << endl;
|
out << " [style=filled, fillcolor=yellow]" << 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;
|
|
||||||
}
|
}
|
||||||
|
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();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,11 +106,35 @@ class FactorGraph
|
|||||||
|
|
||||||
void print (void) const;
|
void print (void) const;
|
||||||
|
|
||||||
|
void exportToLibDai (const char*) const;
|
||||||
|
|
||||||
|
void exportToUai (const char*) const;
|
||||||
|
|
||||||
void exportToGraphViz (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:
|
private:
|
||||||
void ignoreLines (std::ifstream&) const;
|
void ignoreLines (std::ifstream&) const;
|
||||||
@ -132,6 +156,11 @@ class FactorGraph
|
|||||||
typedef unordered_map<unsigned, VarNode*> VarMap;
|
typedef unordered_map<unsigned, VarNode*> VarMap;
|
||||||
VarMap varMap_;
|
VarMap varMap_;
|
||||||
|
|
||||||
|
static bool exportLd_;
|
||||||
|
static bool exportUai_;
|
||||||
|
static bool exportGv_;
|
||||||
|
static bool printFg_;
|
||||||
|
|
||||||
DISALLOW_ASSIGN (FactorGraph);
|
DISALLOW_ASSIGN (FactorGraph);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -32,6 +32,23 @@ main (int argc, const char* argv[])
|
|||||||
FactorGraph fg;
|
FactorGraph fg;
|
||||||
readFactorGraph (fg, argv[idx]);
|
readFactorGraph (fg, argv[idx]);
|
||||||
VarIds queryIds = readQueryAndEvidence (fg, argc, argv, idx + 1);
|
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);
|
runSolver (fg, queryIds);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -108,10 +108,21 @@ createGroundNetwork (void)
|
|||||||
evidenceList = YAP_TailOfTerm (evidenceList);
|
evidenceList = YAP_TailOfTerm (evidenceList);
|
||||||
nrObservedVars ++;
|
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) {
|
if (Globals::verbosity > 0) {
|
||||||
cout << "factor graph contains " ;
|
cout << "factor graph contains " ;
|
||||||
cout << fg->nrVarNodes() << " variables " ;
|
cout << fg->nrVarNodes() << " variables and " ;
|
||||||
cout << "(" << nrObservedVars << " observed) and " ;
|
|
||||||
cout << fg->nrFacNodes() << " factors " << endl;
|
cout << fg->nrFacNodes() << " factors " << endl;
|
||||||
}
|
}
|
||||||
YAP_Int p = (YAP_Int) (fg);
|
YAP_Int p = (YAP_Int) (fg);
|
||||||
|
@ -272,6 +272,47 @@ setHorusFlag (string key, string value)
|
|||||||
ss << value;
|
ss << value;
|
||||||
ss >> mi;
|
ss >> mi;
|
||||||
BeliefProp::setMaxIterations (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 {
|
} else {
|
||||||
cerr << "warning: invalid key `" << key << "'" << endl;
|
cerr << "warning: invalid key `" << key << "'" << endl;
|
||||||
returnVal = false;
|
returnVal = false;
|
||||||
|
@ -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.
|
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}
|
\begin{itemize}
|
||||||
\item \texttt{sequential}
|
\item \texttt{sequential}
|
||||||
\item \texttt{min\_neighbors}
|
\item \texttt{min\_neighbors}
|
||||||
@ -294,6 +294,8 @@ The key \texttt{bp\_msg\_schedule} controls the message sending order. Its possi
|
|||||||
\end{itemize}
|
\end{itemize}
|
||||||
It defaults to \texttt{seq\_fixed}.
|
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}
|
\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}
|
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}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user