This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/CLPBN/horus/BayesBallGraph.cpp

110 lines
2.1 KiB
C++
Raw Normal View History

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-07 23:53:13 +00:00
namespace horus {
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 00:15:41 +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
2012-06-19 14:32:12 +01:00
BayesBallGraph::setIndexes (void)
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
2012-06-19 14:32:12 +01:00
BayesBallGraph::clear (void)
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 << "'." ;
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-07 23:53:13 +00:00
} // namespace horus