2012-04-10 20:43:08 +01:00
|
|
|
#ifndef HORUS_BPSOLVER_H
|
|
|
|
#define HORUS_BPSOLVER_H
|
2012-04-05 23:04:27 +01:00
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "Solver.h"
|
|
|
|
#include "Factor.h"
|
|
|
|
#include "FactorGraph.h"
|
|
|
|
#include "Util.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
class SpLink
|
|
|
|
{
|
|
|
|
public:
|
2012-04-10 11:51:56 +01:00
|
|
|
SpLink (FacNode* fn, VarNode* vn)
|
2012-04-05 23:04:27 +01:00
|
|
|
{
|
|
|
|
fac_ = fn;
|
|
|
|
var_ = vn;
|
|
|
|
v1_.resize (vn->range(), LogAware::tl (1.0 / vn->range()));
|
|
|
|
v2_.resize (vn->range(), LogAware::tl (1.0 / vn->range()));
|
|
|
|
currMsg_ = &v1_;
|
|
|
|
nextMsg_ = &v2_;
|
|
|
|
msgSended_ = false;
|
|
|
|
residual_ = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~SpLink (void) { };
|
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
FacNode* getFactor (void) const { return fac_; }
|
2012-04-05 23:04:27 +01:00
|
|
|
|
|
|
|
VarNode* getVariable (void) const { return var_; }
|
|
|
|
|
|
|
|
const Params& getMessage (void) const { return *currMsg_; }
|
|
|
|
|
|
|
|
Params& getNextMessage (void) { return *nextMsg_; }
|
|
|
|
|
|
|
|
bool messageWasSended (void) const { return msgSended_; }
|
|
|
|
|
|
|
|
double getResidual (void) const { return residual_; }
|
|
|
|
|
|
|
|
void clearResidual (void) { residual_ = 0.0; }
|
|
|
|
|
|
|
|
void updateResidual (void)
|
|
|
|
{
|
|
|
|
residual_ = LogAware::getMaxNorm (v1_,v2_);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void updateMessage (void)
|
|
|
|
{
|
|
|
|
swap (currMsg_, nextMsg_);
|
|
|
|
msgSended_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
string toString (void) const
|
|
|
|
{
|
|
|
|
stringstream ss;
|
|
|
|
ss << fac_->getLabel();
|
|
|
|
ss << " -- " ;
|
|
|
|
ss << var_->label();
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2012-04-10 11:51:56 +01:00
|
|
|
FacNode* fac_;
|
|
|
|
VarNode* var_;
|
|
|
|
Params v1_;
|
|
|
|
Params v2_;
|
|
|
|
Params* currMsg_;
|
|
|
|
Params* nextMsg_;
|
|
|
|
bool msgSended_;
|
|
|
|
double residual_;
|
2012-04-05 23:04:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef vector<SpLink*> SpLinkSet;
|
|
|
|
|
|
|
|
|
|
|
|
class SPNodeInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void addSpLink (SpLink* link) { links_.push_back (link); }
|
|
|
|
const SpLinkSet& getLinks (void) { return links_; }
|
|
|
|
private:
|
|
|
|
SpLinkSet links_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class BpSolver : public Solver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BpSolver (const FactorGraph&);
|
|
|
|
|
|
|
|
virtual ~BpSolver (void);
|
|
|
|
|
2012-04-10 15:00:18 +01:00
|
|
|
Params solveQuery (VarIds);
|
2012-04-05 23:04:27 +01:00
|
|
|
|
|
|
|
virtual Params getPosterioriOf (VarId);
|
|
|
|
|
|
|
|
virtual Params getJointDistributionOf (const VarIds&);
|
|
|
|
|
|
|
|
protected:
|
2012-04-10 20:43:08 +01:00
|
|
|
void runSolver (void);
|
2012-04-05 23:04:27 +01:00
|
|
|
|
|
|
|
virtual void createLinks (void);
|
|
|
|
|
|
|
|
virtual void maxResidualSchedule (void);
|
|
|
|
|
2012-04-09 15:40:51 +01:00
|
|
|
virtual void calculateFactor2VariableMsg (SpLink*);
|
2012-04-05 23:04:27 +01:00
|
|
|
|
|
|
|
virtual Params getVar2FactorMsg (const SpLink*) const;
|
|
|
|
|
|
|
|
virtual Params getJointByConditioning (const VarIds&) const;
|
|
|
|
|
|
|
|
SPNodeInfo* ninf (const VarNode* var) const
|
|
|
|
{
|
|
|
|
return varsI_[var->getIndex()];
|
|
|
|
}
|
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
SPNodeInfo* ninf (const FacNode* fac) const
|
2012-04-05 23:04:27 +01:00
|
|
|
{
|
|
|
|
return facsI_[fac->getIndex()];
|
|
|
|
}
|
|
|
|
|
|
|
|
void calculateAndUpdateMessage (SpLink* link, bool calcResidual = true)
|
|
|
|
{
|
|
|
|
if (Constants::DEBUG >= 3) {
|
|
|
|
cout << "calculating & updating " << link->toString() << endl;
|
|
|
|
}
|
|
|
|
calculateFactor2VariableMsg (link);
|
|
|
|
if (calcResidual) {
|
|
|
|
link->updateResidual();
|
|
|
|
}
|
|
|
|
link->updateMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void calculateMessage (SpLink* link, bool calcResidual = true)
|
|
|
|
{
|
|
|
|
if (Constants::DEBUG >= 3) {
|
|
|
|
cout << "calculating " << link->toString() << endl;
|
|
|
|
}
|
|
|
|
calculateFactor2VariableMsg (link);
|
|
|
|
if (calcResidual) {
|
|
|
|
link->updateResidual();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateMessage (SpLink* link)
|
|
|
|
{
|
|
|
|
link->updateMessage();
|
|
|
|
if (Constants::DEBUG >= 3) {
|
|
|
|
cout << "updating " << link->toString() << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CompareResidual
|
|
|
|
{
|
|
|
|
inline bool operator() (const SpLink* link1, const SpLink* link2)
|
|
|
|
{
|
|
|
|
return link1->getResidual() > link2->getResidual();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SpLinkSet links_;
|
|
|
|
unsigned nIters_;
|
|
|
|
vector<SPNodeInfo*> varsI_;
|
|
|
|
vector<SPNodeInfo*> facsI_;
|
2012-04-10 15:00:18 +01:00
|
|
|
bool runned_;
|
2012-04-10 20:43:08 +01:00
|
|
|
const FactorGraph* fg_;
|
2012-04-05 23:04:27 +01:00
|
|
|
|
|
|
|
typedef multiset<SpLink*, CompareResidual> SortedOrder;
|
|
|
|
SortedOrder sortedOrder_;
|
|
|
|
|
|
|
|
typedef unordered_map<SpLink*, SortedOrder::iterator> SpLinkMap;
|
|
|
|
SpLinkMap linkMap_;
|
|
|
|
|
|
|
|
private:
|
2012-04-10 20:43:08 +01:00
|
|
|
void initializeSolver (void);
|
|
|
|
|
2012-04-05 23:04:27 +01:00
|
|
|
bool converged (void);
|
2012-04-10 20:43:08 +01:00
|
|
|
|
|
|
|
void printLinkInformation (void) const;
|
2012-04-05 23:04:27 +01:00
|
|
|
};
|
|
|
|
|
2012-04-10 20:43:08 +01:00
|
|
|
#endif // HORUS_BPSOLVER_H
|
2012-04-05 23:04:27 +01:00
|
|
|
|