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

108 lines
2.1 KiB
C++
Raw Normal View History

2012-05-23 14:56:01 +01:00
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <fstream>
#include <sstream>
2012-06-19 14:32:12 +01:00
#include "BayesBallGraph.h"
2012-05-23 14:56:01 +01:00
#include "Util.h"
void
2012-06-19 14:32:12 +01:00
BayesBallGraph::addNode (BBNode* n)
2012-05-23 14:56:01 +01:00
{
assert (Util::contains (varMap_, n->varId()) == false);
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
{
2012-06-19 14:32:12 +01:00
unordered_map<VarId, BBNode*>::iterator it1;
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
{
2012-06-19 14:32:12 +01:00
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
{
2012-06-19 14:32:12 +01:00
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
{
ofstream out (fileName);
if (!out.is_open()) {
cerr << "error: cannot open file to write at " ;
2012-06-19 14:32:12 +01:00
cerr << "BayesBallGraph::exportToDotFile()" << endl;
2012-05-23 14:56:01 +01:00
abort();
}
out << "digraph {" << endl;
out << "ranksep=1" << 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" ;
}
out << "]" << endl;
}
2012-05-24 22:55:20 +01:00
for (size_t i = 0; i < nodes_.size(); i++) {
2012-06-19 14:32:12 +01:00
const 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();
out << " [style=bold]" << endl ;
}
}
out << "}" << endl;
out.close();
}