2012-04-05 18:38:56 +01:00
|
|
|
#ifndef HORUS_BAYESBALL_H
|
|
|
|
#define HORUS_BAYESBALL_H
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <queue>
|
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "FactorGraph.h"
|
|
|
|
#include "BayesNet.h"
|
2012-04-05 23:00:48 +01:00
|
|
|
#include "Horus.h"
|
2012-04-05 18:38:56 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
struct ScheduleInfo
|
|
|
|
{
|
|
|
|
ScheduleInfo (DAGraphNode* n, bool vfp, bool vfc) :
|
|
|
|
node(n), visitedFromParent(vfp), visitedFromChild(vfc) { }
|
2012-04-05 20:08:54 +01:00
|
|
|
|
2012-04-05 18:38:56 +01:00
|
|
|
DAGraphNode* node;
|
2012-04-05 20:08:54 +01:00
|
|
|
bool visitedFromParent;
|
|
|
|
bool visitedFromChild;
|
2012-04-05 18:38:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef queue<ScheduleInfo, list<ScheduleInfo>> Scheduling;
|
|
|
|
|
|
|
|
|
|
|
|
class BayesBall
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BayesBall (FactorGraph& fg)
|
|
|
|
: fg_(fg) , dag_(fg.getStructure())
|
|
|
|
{
|
|
|
|
dag_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
FactorGraph* getMinimalFactorGraph (const VarIds&);
|
|
|
|
|
|
|
|
static FactorGraph* getMinimalFactorGraph (FactorGraph& fg, VarIds vids)
|
|
|
|
{
|
|
|
|
BayesBall bb (fg);
|
|
|
|
return bb.getMinimalFactorGraph (vids);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void constructGraph (FactorGraph* fg) const;
|
|
|
|
|
|
|
|
void scheduleParents (const DAGraphNode* n, Scheduling& sch) const;
|
|
|
|
|
|
|
|
void scheduleChilds (const DAGraphNode* n, Scheduling& sch) const;
|
|
|
|
|
|
|
|
FactorGraph& fg_;
|
|
|
|
|
|
|
|
DAGraph& dag_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
|
|
|
BayesBall::scheduleParents (const DAGraphNode* n, Scheduling& sch) const
|
|
|
|
{
|
|
|
|
const vector<DAGraphNode*>& ps = n->parents();
|
|
|
|
for (vector<DAGraphNode*>::const_iterator it = ps.begin();
|
|
|
|
it != ps.end(); it++) {
|
|
|
|
sch.push (ScheduleInfo (*it, false, true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
|
|
|
BayesBall::scheduleChilds (const DAGraphNode* n, Scheduling& sch) const
|
|
|
|
{
|
|
|
|
const vector<DAGraphNode*>& cs = n->childs();
|
|
|
|
for (vector<DAGraphNode*>::const_iterator it = cs.begin();
|
|
|
|
it != cs.end(); it++) {
|
|
|
|
sch.push (ScheduleInfo (*it, true, false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HORUS_BAYESBALL_H
|
|
|
|
|