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/BayesBall.h

85 lines
1.6 KiB
C
Raw Normal View History

2012-05-23 14:56:01 +01:00
#ifndef HORUS_BAYESBALL_H
#define HORUS_BAYESBALL_H
#include <vector>
#include <queue>
#include <list>
#include "FactorGraph.h"
2012-06-19 14:32:12 +01:00
#include "BayesBallGraph.h"
2012-05-23 14:56:01 +01:00
#include "Horus.h"
using namespace std;
struct ScheduleInfo
{
2012-12-20 23:19:10 +00:00
ScheduleInfo (BBNode* n, bool vfp, bool vfc)
: node(n), visitedFromParent(vfp), visitedFromChild(vfc) { }
2012-05-23 14:56:01 +01:00
2012-06-19 14:32:12 +01:00
BBNode* node;
bool visitedFromParent;
bool visitedFromChild;
2012-05-23 14:56:01 +01:00
};
typedef queue<ScheduleInfo, list<ScheduleInfo>> Scheduling;
class BayesBall
{
public:
2012-12-20 23:19:10 +00:00
BayesBall (FactorGraph& fg)
2012-06-19 15:30:55 +01:00
: 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);
}
2012-05-23 14:56:01 +01:00
private:
void constructGraph (FactorGraph* fg) const;
2012-06-19 14:32:12 +01:00
void scheduleParents (const BBNode* n, Scheduling& sch) const;
2012-05-23 14:56:01 +01:00
2012-06-19 14:32:12 +01:00
void scheduleChilds (const BBNode* n, Scheduling& sch) const;
2012-05-23 14:56:01 +01:00
FactorGraph& fg_;
2012-06-19 14:32:12 +01:00
BayesBallGraph& dag_;
2012-05-23 14:56:01 +01:00
};
inline void
2012-06-19 14:32:12 +01:00
BayesBall::scheduleParents (const BBNode* n, Scheduling& sch) const
2012-05-23 14:56:01 +01:00
{
2012-06-19 14:32:12 +01:00
const vector<BBNode*>& ps = n->parents();
2012-12-20 23:19:10 +00:00
for (vector<BBNode*>::const_iterator it = ps.begin();
2012-05-28 14:12:18 +01:00
it != ps.end(); ++it) {
2012-05-23 14:56:01 +01:00
sch.push (ScheduleInfo (*it, false, true));
}
}
inline void
2012-06-19 14:32:12 +01:00
BayesBall::scheduleChilds (const BBNode* n, Scheduling& sch) const
2012-05-23 14:56:01 +01:00
{
2012-06-19 14:32:12 +01:00
const vector<BBNode*>& cs = n->childs();
for (vector<BBNode*>::const_iterator it = cs.begin();
2012-05-28 14:12:18 +01:00
it != cs.end(); ++it) {
2012-05-23 14:56:01 +01:00
sch.push (ScheduleInfo (*it, true, false));
}
}
#endif // HORUS_BAYESBALL_H