2012-05-23 14:56:01 +01:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include <iostream>
|
2012-12-27 12:54:58 +00:00
|
|
|
#include <fstream>
|
2013-02-07 20:09:10 +00:00
|
|
|
#include <sstream>
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2012-06-19 14:32:12 +01:00
|
|
|
#include "BayesBallGraph.h"
|
2012-05-23 14:56:01 +01:00
|
|
|
#include "Util.h"
|
|
|
|
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
namespace Horus {
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
void
|
2012-06-19 14:32:12 +01:00
|
|
|
BayesBallGraph::addNode (BBNode* n)
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2013-02-08 21:01:53 +00:00
|
|
|
assert (Util::contains (varMap_, n->varId()) == false);
|
2012-05-23 14:56:01 +01:00
|
|
|
nodes_.push_back (n);
|
|
|
|
varMap_[n->varId()] = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-06-19 14:32:12 +01:00
|
|
|
BayesBallGraph::addEdge (VarId vid1, VarId vid2)
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::unordered_map<VarId, BBNode*>::iterator it1;
|
|
|
|
std::unordered_map<VarId, BBNode*>::iterator it2;
|
2012-05-23 14:56:01 +01:00
|
|
|
it1 = varMap_.find (vid1);
|
|
|
|
it2 = varMap_.find (vid2);
|
|
|
|
assert (it1 != varMap_.end());
|
|
|
|
assert (it2 != varMap_.end());
|
|
|
|
it1->second->addChild (it2->second);
|
|
|
|
it2->second->addParent (it1->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-19 14:32:12 +01:00
|
|
|
const BBNode*
|
|
|
|
BayesBallGraph::getNode (VarId vid) const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::unordered_map<VarId, BBNode*>::const_iterator it;
|
2012-05-23 14:56:01 +01:00
|
|
|
it = varMap_.find (vid);
|
|
|
|
return it != varMap_.end() ? it->second : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-19 14:32:12 +01:00
|
|
|
BBNode*
|
|
|
|
BayesBallGraph::getNode (VarId vid)
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::unordered_map<VarId, BBNode*>::const_iterator it;
|
2012-05-23 14:56:01 +01:00
|
|
|
it = varMap_.find (vid);
|
|
|
|
return it != varMap_.end() ? it->second : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
BayesBallGraph::setIndexes()
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < nodes_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
nodes_[i]->setIndex (i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
BayesBallGraph::clear()
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < nodes_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
nodes_[i]->clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-06-19 14:32:12 +01:00
|
|
|
BayesBallGraph::exportToGraphViz (const char* fileName)
|
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 << "digraph {" << std::endl;
|
|
|
|
out << "ranksep=1" << std::endl;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < nodes_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
out << nodes_[i]->varId() ;
|
|
|
|
out << " [" ;
|
|
|
|
out << "label=\"" << nodes_[i]->label() << "\"" ;
|
|
|
|
if (nodes_[i]->hasEvidence()) {
|
|
|
|
out << ",style=filled, fillcolor=yellow" ;
|
|
|
|
}
|
2013-02-07 13:37:15 +00:00
|
|
|
out << "]" << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < nodes_.size(); i++) {
|
2013-02-07 13:37:15 +00:00
|
|
|
const std::vector<BBNode*>& childs = nodes_[i]->childs();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t j = 0; j < childs.size(); j++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
out << nodes_[i]->varId() << " -> " << childs[j]->varId();
|
2013-02-07 13:37:15 +00:00
|
|
|
out << " [style=bold]" << 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-02-08 21:12:46 +00:00
|
|
|
} // namespace Horus
|
2013-02-07 23:53:13 +00:00
|
|
|
|