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

177 lines
4.0 KiB
C
Raw Normal View History

2013-02-07 17:50:02 +00:00
#ifndef YAP_PACKAGES_CLPBN_HORUS_BELIEFPROP_H_
#define YAP_PACKAGES_CLPBN_HORUS_BELIEFPROP_H_
2012-05-23 14:56:01 +01:00
#include <vector>
2013-02-07 20:09:10 +00:00
#include <set>
#include <string>
2012-05-23 14:56:01 +01:00
2012-11-14 21:55:51 +00:00
#include "GroundSolver.h"
2012-05-23 14:56:01 +01:00
#include "FactorGraph.h"
2012-12-27 12:54:58 +00:00
2012-05-23 14:56:01 +01:00
namespace Horus {
2013-02-07 23:53:13 +00:00
class BeliefProp : public GroundSolver {
2012-05-23 14:56:01 +01:00
private:
class SPNodeInfo;
2012-05-23 14:56:01 +01:00
public:
enum MsgSchedule {
seqFixedSch,
seqRandomSch,
parallelSch,
maxResidualSch
};
BeliefProp (const FactorGraph&);
2012-05-23 14:56:01 +01:00
virtual ~BeliefProp (void);
2012-05-23 14:56:01 +01:00
Params solveQuery (VarIds);
virtual void printSolverFlags (void) const;
virtual Params getPosterioriOf (VarId);
virtual Params getJointDistributionOf (const VarIds&);
2012-12-17 18:39:42 +00:00
2012-12-27 15:05:40 +00:00
Params getFactorJoint (FacNode* fn, const VarIds&);
static double accuracy (void) { return accuracy_; }
static void setAccuracy (double acc) { accuracy_ = acc; }
static unsigned maxIterations (void) { return maxIter_; }
static void setMaxIterations (unsigned mi) { maxIter_ = mi; }
static MsgSchedule msgSchedule (void) { return schedule_; }
static void setMsgSchedule (MsgSchedule sch) { schedule_ = sch; }
protected:
2013-02-20 23:59:03 +00:00
class BpLink {
public:
BpLink (FacNode* fn, VarNode* vn);
virtual ~BpLink (void) { };
FacNode* facNode (void) const { return fac_; }
VarNode* varNode (void) const { return var_; }
const Params& message (void) const { return *currMsg_; }
Params& nextMessage (void) { return *nextMsg_; }
double residual (void) const { return residual_; }
void clearResidual (void);
void updateResidual (void);
virtual void updateMessage (void);
std::string toString (void) const;
protected:
FacNode* fac_;
VarNode* var_;
Params v1_;
Params v2_;
Params* currMsg_;
Params* nextMsg_;
double residual_;
private:
DISALLOW_COPY_AND_ASSIGN (BpLink);
};
struct CmpResidual {
bool operator() (const BpLink* l1, const BpLink* l2) {
return l1->residual() > l2->residual();
}};
2013-02-20 23:59:03 +00:00
typedef std::vector<BeliefProp::BpLink*> BpLinks;
typedef std::multiset<BpLink*, CmpResidual> SortedOrder;
typedef std::unordered_map<BpLink*, SortedOrder::iterator> BpLinkMap;
SPNodeInfo* ninf (const VarNode* var) const;
2012-05-23 14:56:01 +01:00
SPNodeInfo* ninf (const FacNode* fac) const;
2012-05-23 14:56:01 +01:00
void calculateAndUpdateMessage (BpLink* link, bool calcResidual = true);
2012-05-23 14:56:01 +01:00
void calculateMessage (BpLink* link, bool calcResidual = true);
2012-05-23 14:56:01 +01:00
void updateMessage (BpLink* link);
2012-05-23 14:56:01 +01:00
2012-12-27 15:05:40 +00:00
void runSolver (void);
virtual void createLinks (void);
virtual void maxResidualSchedule (void);
virtual void calcFactorToVarMsg (BpLink*);
virtual Params getVarToFactorMsg (const BpLink*) const;
virtual Params getJointByConditioning (const VarIds&) const;
2013-02-07 13:37:15 +00:00
BpLinks links_;
unsigned nIters_;
std::vector<SPNodeInfo*> varsI_;
std::vector<SPNodeInfo*> facsI_;
bool runned_;
SortedOrder sortedOrder_;
BpLinkMap linkMap_;
2012-05-23 14:56:01 +01:00
static double accuracy_;
static unsigned maxIter_;
static MsgSchedule schedule_;
2012-05-23 14:56:01 +01:00
private:
class SPNodeInfo {
public:
SPNodeInfo (void) { }
2013-02-20 23:59:03 +00:00
void addBpLink (BeliefProp::BpLink* link) { links_.push_back (link); }
const BpLinks& getLinks (void) { return links_; }
private:
BpLinks links_;
DISALLOW_COPY_AND_ASSIGN (SPNodeInfo);
};
2012-05-23 14:56:01 +01:00
void initializeSolver (void);
bool converged (void);
virtual void printLinkInformation (void) const;
2012-12-27 22:25:45 +00:00
DISALLOW_COPY_AND_ASSIGN (BeliefProp);
2012-05-23 14:56:01 +01:00
};
inline BeliefProp::SPNodeInfo*
BeliefProp::ninf (const VarNode* var) const
{
return varsI_[var->getIndex()];
}
inline BeliefProp::SPNodeInfo*
BeliefProp::ninf (const FacNode* fac) const
{
return facsI_[fac->getIndex()];
}
} // namespace Horus
2013-02-07 23:53:13 +00:00
2013-02-08 00:20:01 +00:00
#endif // YAP_PACKAGES_CLPBN_HORUS_BELIEFPROP_H_
2012-05-23 14:56:01 +01:00