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
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
namespace Horus {
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2013-02-20 23:34:03 +00:00
|
|
|
class BeliefProp : public GroundSolver {
|
2012-05-23 14:56:01 +01:00
|
|
|
private:
|
2013-02-20 23:34:03 +00:00
|
|
|
class SPNodeInfo;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
public:
|
2013-03-09 15:39:39 +00:00
|
|
|
enum class MsgSchedule {
|
2013-02-20 23:34:03 +00:00
|
|
|
seqFixedSch,
|
|
|
|
seqRandomSch,
|
|
|
|
parallelSch,
|
|
|
|
maxResidualSch
|
|
|
|
};
|
|
|
|
|
2012-06-12 16:29:57 +01:00
|
|
|
BeliefProp (const FactorGraph&);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
virtual ~BeliefProp();
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
Params solveQuery (VarIds);
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
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&);
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static double accuracy() { return accuracy_; }
|
2012-12-27 15:44:40 +00:00
|
|
|
|
|
|
|
static void setAccuracy (double acc) { accuracy_ = acc; }
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static unsigned maxIterations() { return maxIter_; }
|
2012-12-27 15:44:40 +00:00
|
|
|
|
|
|
|
static void setMaxIterations (unsigned mi) { maxIter_ = mi; }
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
static MsgSchedule msgSchedule() { return schedule_; }
|
2012-12-27 15:44:40 +00:00
|
|
|
|
|
|
|
static void setMsgSchedule (MsgSchedule sch) { schedule_ = sch; }
|
2012-12-27 15:00:30 +00:00
|
|
|
|
2012-06-13 12:47:41 +01:00
|
|
|
protected:
|
2013-02-20 23:59:03 +00:00
|
|
|
class BpLink {
|
|
|
|
public:
|
|
|
|
BpLink (FacNode* fn, VarNode* vn);
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
virtual ~BpLink() { };
|
2013-02-20 23:59:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
FacNode* facNode() const { return fac_; }
|
2013-02-20 23:59:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
VarNode* varNode() const { return var_; }
|
2013-02-20 23:59:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
const Params& message() const { return *currMsg_; }
|
2013-02-20 23:59:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
Params& nextMessage() { return *nextMsg_; }
|
2013-02-20 23:59:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
double residual() const { return residual_; }
|
2013-02-20 23:59:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
void clearResidual();
|
2013-02-20 23:59:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
void updateResidual();
|
2013-02-20 23:59:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
virtual void updateMessage();
|
2013-02-20 23:59:03 +00:00
|
|
|
|
2013-02-28 19:45:37 +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);
|
|
|
|
};
|
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
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-02-07 22:37:45 +00:00
|
|
|
|
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
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
void calculateAndUpdateMessage (BpLink* link, bool calcResidual = true);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
void calculateMessage (BpLink* link, bool calcResidual = true);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
void updateMessage (BpLink* link);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
void runSolver();
|
2012-12-27 15:05:40 +00:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
virtual void createLinks();
|
2012-12-27 15:05:40 +00:00
|
|
|
|
2013-02-28 19:45:37 +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_;
|
2013-02-07 22:37:45 +00:00
|
|
|
SortedOrder sortedOrder_;
|
|
|
|
BpLinkMap linkMap_;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
static double accuracy_;
|
2012-12-27 15:44:40 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
private:
|
2013-02-28 19:45:37 +00:00
|
|
|
void initializeSolver();
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
bool converged();
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
virtual void printLinkInformation() const;
|
2012-12-27 22:25:45 +00:00
|
|
|
|
2013-03-09 16:46:42 +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-02-06 00:24:02 +00:00
|
|
|
|
|
|
|
|
2013-03-09 16:41:53 +00:00
|
|
|
inline BeliefProp::BpLinks&
|
|
|
|
BeliefProp::getLinks (const VarNode* var)
|
2013-02-06 00:24:02 +00:00
|
|
|
{
|
2013-03-09 16:41:53 +00:00
|
|
|
return varsLinks_[var->getIndex()];
|
2013-02-06 00:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-09 16:41:53 +00:00
|
|
|
inline BeliefProp::BpLinks&
|
|
|
|
BeliefProp::getLinks (const FacNode* fac)
|
2013-02-06 00:24:02 +00:00
|
|
|
{
|
2013-03-09 16:41:53 +00:00
|
|
|
return facsLinks_[fac->getIndex()];
|
2013-02-06 00:24:02 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
} // 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
|
|
|
|