2013-02-07 20:09:10 +00:00
|
|
|
#include <cassert>
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-07 20:09:10 +00:00
|
|
|
#include <algorithm>
|
2012-05-23 14:56:01 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "FactorGraph.h"
|
|
|
|
#include "BayesBall.h"
|
|
|
|
#include "Util.h"
|
|
|
|
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
namespace Horus {
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2013-01-08 17:01:03 +00:00
|
|
|
bool FactorGraph::exportLd_ = false;
|
|
|
|
bool FactorGraph::exportUai_ = false;
|
|
|
|
bool FactorGraph::exportGv_ = false;
|
|
|
|
bool FactorGraph::printFg_ = false;
|
|
|
|
|
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
FactorGraph::FactorGraph (const FactorGraph& fg)
|
|
|
|
{
|
2013-03-14 16:57:34 +00:00
|
|
|
clone (fg);
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
FactorGraph::~FactorGraph()
|
2012-12-20 21:32:40 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < varNodes_.size(); i++) {
|
|
|
|
delete varNodes_[i];
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
|
|
|
delete facNodes_[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
void
|
|
|
|
FactorGraph::addFactor (const Factor& factor)
|
|
|
|
{
|
|
|
|
FacNode* fn = new FacNode (factor);
|
|
|
|
addFacNode (fn);
|
|
|
|
const VarIds& vids = fn->factor().arguments();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < vids.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
VarMap::const_iterator it = varMap_.find (vids[i]);
|
|
|
|
if (it != varMap_.end()) {
|
2012-12-20 23:19:10 +00:00
|
|
|
addEdge (it->second, fn);
|
2012-05-23 14:56:01 +01:00
|
|
|
} else {
|
|
|
|
VarNode* vn = new VarNode (vids[i], fn->factor().range (i));
|
|
|
|
addVarNode (vn);
|
|
|
|
addEdge (vn, fn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
FactorGraph::addVarNode (VarNode* vn)
|
|
|
|
{
|
|
|
|
varNodes_.push_back (vn);
|
|
|
|
vn->setIndex (varNodes_.size() - 1);
|
2013-02-07 13:37:15 +00:00
|
|
|
varMap_.insert (std::make_pair (vn->varId(), vn));
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
FactorGraph::addFacNode (FacNode* fn)
|
|
|
|
{
|
|
|
|
facNodes_.push_back (fn);
|
|
|
|
fn->setIndex (facNodes_.size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
FactorGraph::addEdge (VarNode* vn, FacNode* fn)
|
|
|
|
{
|
|
|
|
vn->addNeighbor (fn);
|
|
|
|
fn->addNeighbor (vn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
2013-02-28 19:45:37 +00:00
|
|
|
FactorGraph::isTree() const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
return !containsCycle();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-19 14:32:12 +01:00
|
|
|
BayesBallGraph&
|
2013-02-28 19:45:37 +00:00
|
|
|
FactorGraph::getStructure()
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2012-05-31 22:42:38 +01:00
|
|
|
assert (bayesFactors_);
|
2012-05-23 14:56:01 +01:00
|
|
|
if (structure_.empty()) {
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < varNodes_.size(); i++) {
|
2012-06-19 14:32:12 +01:00
|
|
|
structure_.addNode (new BBNode (varNodes_[i]));
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
const VarIds& vids = facNodes_[i]->factor().arguments();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t j = 1; j < vids.size(); j++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
structure_.addEdge (vids[j], vids[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return structure_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
FactorGraph::print() const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < varNodes_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
cout << "var id = " << varNodes_[i]->varId() << endl;
|
|
|
|
cout << "label = " << varNodes_[i]->label() << endl;
|
|
|
|
cout << "range = " << varNodes_[i]->range() << endl;
|
|
|
|
cout << "evidence = " << varNodes_[i]->getEvidence() << endl;
|
|
|
|
cout << "factors = " ;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t j = 0; j < varNodes_[i]->neighbors().size(); j++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
cout << varNodes_[i]->neighbors()[j]->getLabel() << " " ;
|
|
|
|
}
|
|
|
|
cout << endl << endl;
|
|
|
|
}
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
facNodes_[i]->factor().print();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-01-08 17:01:03 +00:00
|
|
|
FactorGraph::exportToLibDai (const char* fileName) const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::ofstream out (fileName);
|
2012-05-23 14:56:01 +01:00
|
|
|
if (!out.is_open()) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cerr << "Error: couldn't open file '" << fileName << "'." ;
|
2013-02-13 19:18:55 +00:00
|
|
|
std::cerr << std::endl;
|
2012-12-20 17:11:11 +00:00
|
|
|
return;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
2013-02-07 13:37:15 +00:00
|
|
|
out << facNodes_.size() << std::endl << std::endl;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
2013-01-08 17:01:03 +00:00
|
|
|
Factor f (facNodes_[i]->factor());
|
2013-02-07 13:37:15 +00:00
|
|
|
out << f.nrArguments() << std::endl;
|
2013-02-08 21:01:53 +00:00
|
|
|
out << Util::elementsToString (f.arguments()) << std::endl;
|
|
|
|
out << Util::elementsToString (f.ranges()) << std::endl;
|
2013-01-08 17:01:03 +00:00
|
|
|
VarIds args = f.arguments();
|
|
|
|
std::reverse (args.begin(), args.end());
|
|
|
|
f.reorderArguments (args);
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::logDomain) {
|
|
|
|
Util::exp (f.params());
|
2013-01-08 17:01:03 +00:00
|
|
|
}
|
2013-02-07 13:37:15 +00:00
|
|
|
out << f.size() << std::endl;
|
2013-01-08 17:01:03 +00:00
|
|
|
for (size_t j = 0; j < f.size(); j++) {
|
2013-02-07 13:37:15 +00:00
|
|
|
out << j << " " << f[j] << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
2013-02-07 13:37:15 +00:00
|
|
|
out << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-01-08 17:01:03 +00:00
|
|
|
FactorGraph::exportToUai (const char* fileName) const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::ofstream out (fileName);
|
2012-05-23 14:56:01 +01:00
|
|
|
if (!out.is_open()) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cerr << "Error: couldn't open file '" << fileName << "'." ;
|
2013-02-13 19:18:55 +00:00
|
|
|
std::cerr << std::endl;
|
2012-12-20 17:11:11 +00:00
|
|
|
return;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
2013-01-07 22:17:05 +00:00
|
|
|
out << (bayesFactors_ ? "BAYES" : "MARKOV") ;
|
2013-02-07 13:37:15 +00:00
|
|
|
out << std::endl << std::endl;
|
|
|
|
out << varNodes_.size() << std::endl;
|
2012-05-28 17:00:46 +01:00
|
|
|
VarNodes sortedVns = varNodes_;
|
|
|
|
std::sort (sortedVns.begin(), sortedVns.end(), sortByVarId());
|
|
|
|
for (size_t i = 0; i < sortedVns.size(); i++) {
|
|
|
|
out << ((i != 0) ? " " : "") << sortedVns[i]->range();
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
2013-02-07 13:37:15 +00:00
|
|
|
out << std::endl << facNodes_.size() << std::endl;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
2012-05-28 17:00:46 +01:00
|
|
|
VarIds args = facNodes_[i]->factor().arguments();
|
2013-01-07 22:17:05 +00:00
|
|
|
if (bayesFactors_) {
|
|
|
|
std::swap (args.front(), args.back());
|
|
|
|
}
|
2013-02-08 21:01:53 +00:00
|
|
|
out << args.size() << " " << Util::elementsToString (args);
|
2013-02-07 13:37:15 +00:00
|
|
|
out << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
2013-02-07 13:37:15 +00:00
|
|
|
out << std::endl;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
2013-01-07 22:17:05 +00:00
|
|
|
Factor f = facNodes_[i]->factor();
|
|
|
|
if (bayesFactors_) {
|
|
|
|
VarIds args = f.arguments();
|
|
|
|
std::swap (args.front(), args.back());
|
|
|
|
f.reorderArguments (args);
|
|
|
|
}
|
|
|
|
Params params = f.params();
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::logDomain) {
|
|
|
|
Util::exp (params);
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
2013-02-07 13:37:15 +00:00
|
|
|
out << params.size() << std::endl << " " ;
|
2013-02-08 21:01:53 +00:00
|
|
|
out << Util::elementsToString (params);
|
2013-02-07 13:37:15 +00:00
|
|
|
out << std::endl << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-01-08 17:01:03 +00:00
|
|
|
FactorGraph::exportToGraphViz (const char* fileName) const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::ofstream out (fileName);
|
2012-05-23 14:56:01 +01:00
|
|
|
if (!out.is_open()) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cerr << "Error: couldn't open file '" << fileName << "'." ;
|
2013-02-13 19:18:55 +00:00
|
|
|
std::cerr << std::endl;
|
2012-12-20 17:11:11 +00:00
|
|
|
return;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
2013-02-07 13:37:15 +00:00
|
|
|
out << "graph \"" << fileName << "\" {" << std::endl;
|
2013-01-08 17:01:03 +00:00
|
|
|
for (size_t i = 0; i < varNodes_.size(); i++) {
|
|
|
|
if (varNodes_[i]->hasEvidence()) {
|
|
|
|
out << '"' << varNodes_[i]->label() << '"' ;
|
2013-02-07 13:37:15 +00:00
|
|
|
out << " [style=filled, fillcolor=yellow]" << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
2013-01-08 17:01:03 +00:00
|
|
|
}
|
|
|
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
|
|
|
out << '"' << facNodes_[i]->getLabel() << '"' ;
|
|
|
|
out << " [label=\"" << facNodes_[i]->getLabel();
|
2013-02-07 13:37:15 +00:00
|
|
|
out << "\"" << ", shape=box]" << std::endl;
|
2013-01-08 17:01:03 +00:00
|
|
|
}
|
|
|
|
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 << " -- " ;
|
2013-02-07 13:37:15 +00:00
|
|
|
out << '"' << myVars[j]->label() << '"' << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-07 13:37:15 +00:00
|
|
|
out << "}" << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-14 16:57:34 +00:00
|
|
|
FactorGraph&
|
|
|
|
FactorGraph::operator= (const FactorGraph& fg)
|
|
|
|
{
|
|
|
|
if (this != &fg) {
|
|
|
|
for (size_t i = 0; i < varNodes_.size(); i++) {
|
|
|
|
delete varNodes_[i];
|
|
|
|
}
|
|
|
|
varNodes_.clear();
|
|
|
|
for (size_t i = 0; i < facNodes_.size(); i++) {
|
|
|
|
delete facNodes_[i];
|
|
|
|
}
|
|
|
|
facNodes_.clear();
|
|
|
|
varMap_.clear();
|
|
|
|
clone (fg);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FactorGraph
|
|
|
|
FactorGraph::readFromUaiFormat (const char* fileName)
|
|
|
|
{
|
|
|
|
std::ifstream is (fileName);
|
|
|
|
if (!is.is_open()) {
|
|
|
|
std::cerr << "Error: couldn't open file '" << fileName << "'." ;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
FactorGraph fg;
|
|
|
|
ignoreLines (is);
|
|
|
|
std::string line;
|
|
|
|
getline (is, line);
|
|
|
|
if (line == "BAYES") {
|
|
|
|
fg.bayesFactors_ = true;
|
|
|
|
} else if (line == "MARKOV") {
|
|
|
|
fg.bayesFactors_ = false;
|
|
|
|
} else {
|
|
|
|
std::cerr << "Error: the type of network is missing." << std::endl;
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
// read the number of vars
|
|
|
|
ignoreLines (is);
|
|
|
|
unsigned nrVars;
|
|
|
|
is >> nrVars;
|
|
|
|
// read the range of each var
|
|
|
|
ignoreLines (is);
|
|
|
|
Ranges ranges (nrVars);
|
|
|
|
for (unsigned i = 0; i < nrVars; i++) {
|
|
|
|
is >> ranges[i];
|
|
|
|
}
|
|
|
|
unsigned nrFactors;
|
|
|
|
unsigned nrArgs;
|
|
|
|
unsigned vid;
|
|
|
|
is >> nrFactors;
|
|
|
|
std::vector<VarIds> allVarIds;
|
|
|
|
std::vector<Ranges> allRanges;
|
|
|
|
for (unsigned i = 0; i < nrFactors; i++) {
|
|
|
|
ignoreLines (is);
|
|
|
|
is >> nrArgs;
|
|
|
|
allVarIds.push_back ({ });
|
|
|
|
allRanges.push_back ({ });
|
|
|
|
for (unsigned j = 0; j < nrArgs; j++) {
|
|
|
|
is >> vid;
|
|
|
|
if (vid >= ranges.size()) {
|
|
|
|
std::cerr << "Error: invalid variable identifier `" << vid << "'" ;
|
|
|
|
std::cerr << ". Identifiers must be between 0 and " ;
|
|
|
|
std::cerr << ranges.size() - 1 << "." << std::endl;
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
allVarIds.back().push_back (vid);
|
|
|
|
allRanges.back().push_back (ranges[vid]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// read the parameters
|
|
|
|
unsigned nrParams;
|
|
|
|
for (unsigned i = 0; i < nrFactors; i++) {
|
|
|
|
ignoreLines (is);
|
|
|
|
is >> nrParams;
|
|
|
|
if (nrParams != Util::sizeExpected (allRanges[i])) {
|
|
|
|
std::cerr << "Error: invalid number of parameters for factor nº " ;
|
|
|
|
std::cerr << i << ", " << Util::sizeExpected (allRanges[i]);
|
|
|
|
std::cerr << " expected, " << nrParams << " given." << std::endl;
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
Params params (nrParams);
|
|
|
|
for (unsigned j = 0; j < nrParams; j++) {
|
|
|
|
is >> params[j];
|
|
|
|
}
|
|
|
|
if (Globals::logDomain) {
|
|
|
|
Util::log (params);
|
|
|
|
}
|
|
|
|
Factor f (allVarIds[i], allRanges[i], params);
|
|
|
|
if (fg.bayesFactors_ && allVarIds[i].size() > 1) {
|
|
|
|
// In this format the child is the last variable,
|
|
|
|
// move it to be the first
|
|
|
|
std::swap (allVarIds[i].front(), allVarIds[i].back());
|
|
|
|
f.reorderArguments (allVarIds[i]);
|
|
|
|
}
|
|
|
|
fg.addFactor (f);
|
|
|
|
}
|
|
|
|
is.close();
|
|
|
|
return fg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FactorGraph
|
|
|
|
FactorGraph::readFromLibDaiFormat (const char* fileName)
|
|
|
|
{
|
|
|
|
std::ifstream is (fileName);
|
|
|
|
if (!is.is_open()) {
|
|
|
|
std::cerr << "Error: couldn't open file '" << fileName << "'." ;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
FactorGraph fg;
|
|
|
|
ignoreLines (is);
|
|
|
|
unsigned nrFactors;
|
|
|
|
unsigned nrArgs;
|
|
|
|
VarId vid;
|
|
|
|
is >> nrFactors;
|
|
|
|
for (unsigned i = 0; i < nrFactors; i++) {
|
|
|
|
ignoreLines (is);
|
|
|
|
// read the factor arguments
|
|
|
|
is >> nrArgs;
|
|
|
|
VarIds vids;
|
|
|
|
for (unsigned j = 0; j < nrArgs; j++) {
|
|
|
|
ignoreLines (is);
|
|
|
|
is >> vid;
|
|
|
|
vids.push_back (vid);
|
|
|
|
}
|
|
|
|
// read ranges
|
|
|
|
Ranges ranges (nrArgs);
|
|
|
|
for (unsigned j = 0; j < nrArgs; j++) {
|
|
|
|
ignoreLines (is);
|
|
|
|
is >> ranges[j];
|
|
|
|
VarNode* var = fg.getVarNode (vids[j]);
|
|
|
|
if (var && ranges[j] != var->range()) {
|
|
|
|
std::cerr << "Error: variable `" << vids[j] << "' appears" ;
|
|
|
|
std::cerr << " in two or more factors with a different range." ;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// read parameters
|
|
|
|
ignoreLines (is);
|
|
|
|
unsigned nNonzeros;
|
|
|
|
is >> nNonzeros;
|
|
|
|
Params params (Util::sizeExpected (ranges), 0);
|
|
|
|
for (unsigned j = 0; j < nNonzeros; j++) {
|
|
|
|
ignoreLines (is);
|
|
|
|
unsigned index;
|
|
|
|
is >> index;
|
|
|
|
ignoreLines (is);
|
|
|
|
double val;
|
|
|
|
is >> val;
|
|
|
|
params[index] = val;
|
|
|
|
}
|
|
|
|
if (Globals::logDomain) {
|
|
|
|
Util::log (params);
|
|
|
|
}
|
|
|
|
std::reverse (vids.begin(), vids.end());
|
|
|
|
std::reverse (ranges.begin(), ranges.end());
|
|
|
|
Factor f (vids, ranges, params);
|
|
|
|
std::reverse (vids.begin(), vids.end());
|
|
|
|
f.reorderArguments (vids);
|
|
|
|
fg.addFactor (f);
|
|
|
|
}
|
|
|
|
is.close();
|
|
|
|
return fg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
void
|
2013-03-14 16:57:34 +00:00
|
|
|
FactorGraph::clone (const FactorGraph& fg)
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2013-03-14 16:57:34 +00:00
|
|
|
const VarNodes& varNodes = fg.varNodes();
|
|
|
|
for (size_t i = 0; i < varNodes.size(); i++) {
|
|
|
|
addVarNode (new VarNode (varNodes[i]));
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
2013-03-14 16:57:34 +00:00
|
|
|
const FacNodes& facNodes = fg.facNodes();
|
|
|
|
for (size_t i = 0; i < facNodes.size(); i++) {
|
|
|
|
FacNode* facNode = new FacNode (facNodes[i]->factor());
|
|
|
|
addFacNode (facNode);
|
|
|
|
const VarNodes& neighs = facNodes[i]->neighbors();
|
|
|
|
for (size_t j = 0; j < neighs.size(); j++) {
|
|
|
|
addEdge (varNodes_[neighs[j]->getIndex()], facNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bayesFactors_ = fg.bayesianFactors();
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
2013-02-28 19:45:37 +00:00
|
|
|
FactorGraph::containsCycle() const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<bool> visitedVars (varNodes_.size(), false);
|
|
|
|
std::vector<bool> visitedFactors (facNodes_.size(), false);
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < varNodes_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
int v = varNodes_[i]->getIndex();
|
|
|
|
if (!visitedVars[v]) {
|
|
|
|
if (containsCycle (varNodes_[i], 0, visitedVars, visitedFactors)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
FactorGraph::containsCycle (
|
|
|
|
const VarNode* v,
|
|
|
|
const FacNode* p,
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<bool>& visitedVars,
|
|
|
|
std::vector<bool>& visitedFactors) const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
visitedVars[v->getIndex()] = true;
|
|
|
|
const FacNodes& adjacencies = v->neighbors();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < adjacencies.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
int w = adjacencies[i]->getIndex();
|
|
|
|
if (!visitedFactors[w]) {
|
|
|
|
if (containsCycle (adjacencies[i], v, visitedVars, visitedFactors)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (visitedFactors[w] && adjacencies[i] != p) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false; // no cycle detected in this component
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
FactorGraph::containsCycle (
|
|
|
|
const FacNode* v,
|
|
|
|
const VarNode* p,
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<bool>& visitedVars,
|
|
|
|
std::vector<bool>& visitedFactors) const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
visitedFactors[v->getIndex()] = true;
|
|
|
|
const VarNodes& adjacencies = v->neighbors();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < adjacencies.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
int w = adjacencies[i]->getIndex();
|
|
|
|
if (!visitedVars[w]) {
|
|
|
|
if (containsCycle (adjacencies[i], v, visitedVars, visitedFactors)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (visitedVars[w] && adjacencies[i] != p) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false; // no cycle detected in this component
|
|
|
|
}
|
|
|
|
|
2013-03-14 16:57:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
FactorGraph::ignoreLines (std::ifstream& is)
|
|
|
|
{
|
|
|
|
std::string ignoreStr;
|
|
|
|
while (is.peek() == '#' || is.peek() == '\n') {
|
|
|
|
getline (is, ignoreStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
} // namespace Horus
|
2013-02-07 23:53:13 +00:00
|
|
|
|