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

165 lines
3.6 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 class MsgSchedule {
seqFixedSch,
seqRandomSch,
parallelSch,
maxResidualSch
};
BeliefProp (const FactorGraph&);
2012-05-23 14:56:01 +01:00
virtual ~BeliefProp();
2012-05-23 14:56:01 +01:00
Params solveQuery (VarIds);
virtual void printSolverFlags() const;
2012-05-23 14:56:01 +01:00
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() { return accuracy_; }
static void setAccuracy (double acc) { accuracy_ = acc; }
static unsigned maxIterations() { return maxIter_; }
static void setMaxIterations (unsigned mi) { maxIter_ = mi; }
static MsgSchedule msgSchedule() { 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() { };
2013-02-20 23:59:03 +00:00
FacNode* facNode() const { return fac_; }
2013-02-20 23:59:03 +00:00
VarNode* varNode() const { return var_; }
2013-02-20 23:59:03 +00:00
const Params& message() const { return *currMsg_; }
2013-02-20 23:59:03 +00:00
Params& nextMessage() { return *nextMsg_; }
2013-02-20 23:59:03 +00:00
double residual() const { return residual_; }
2013-02-20 23:59:03 +00:00
void clearResidual();
2013-02-20 23:59:03 +00:00
void updateResidual();
2013-02-20 23:59:03 +00:00
virtual void updateMessage();
2013-02-20 23:59:03 +00:00
std::string toString() const;
2013-02-20 23:59:03 +00:00
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;
2013-03-09 16:41:53 +00:00
BpLinks& getLinks (const VarNode* var);
2012-05-23 14:56:01 +01:00
2013-03-09 16:41:53 +00:00
BpLinks& getLinks (const FacNode* fac);
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
void runSolver();
2012-12-27 15:05:40 +00:00
virtual void createLinks();
2012-12-27 15:05:40 +00:00
virtual void maxResidualSchedule();
2012-12-27 15:05:40 +00:00
virtual void calcFactorToVarMsg (BpLink*);
2013-03-09 16:41:53 +00:00
virtual Params getVarToFactorMsg (const BpLink*);
2012-12-27 15:05:40 +00:00
virtual Params getJointByConditioning (const VarIds&) const;
2013-02-07 13:37:15 +00:00
BpLinks links_;
unsigned nIters_;
bool runned_;
SortedOrder sortedOrder_;
BpLinkMap linkMap_;
2012-05-23 14:56:01 +01:00
static double accuracy_;
2012-05-23 14:56:01 +01:00
private:
void initializeSolver();
2012-05-23 14:56:01 +01:00
bool converged();
2012-05-23 14:56:01 +01:00
virtual void printLinkInformation() const;
2012-12-27 22:25:45 +00:00
std::vector<BpLinks> varsLinks_;
std::vector<BpLinks> facsLinks_;
static unsigned maxIter_;
static MsgSchedule schedule_;
2012-12-27 22:25:45 +00:00
DISALLOW_COPY_AND_ASSIGN (BeliefProp);
2012-05-23 14:56:01 +01:00
};
2013-03-09 16:41:53 +00:00
inline BeliefProp::BpLinks&
BeliefProp::getLinks (const VarNode* var)
{
2013-03-09 16:41:53 +00:00
return varsLinks_[var->getIndex()];
}
2013-03-09 16:41:53 +00:00
inline BeliefProp::BpLinks&
BeliefProp::getLinks (const FacNode* fac)
{
2013-03-09 16:41:53 +00:00
return facsLinks_[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