2011-07-22 21:33:30 +01:00
|
|
|
#ifndef BP_SP_SOLVER_H
|
|
|
|
#define BP_SP_SOLVER_H
|
2011-05-17 12:00:33 +01:00
|
|
|
|
|
|
|
#include <vector>
|
2011-07-22 21:33:30 +01:00
|
|
|
#include <set>
|
2011-05-17 12:00:33 +01:00
|
|
|
|
|
|
|
#include "Solver.h"
|
|
|
|
#include "FgVarNode.h"
|
|
|
|
#include "Factor.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
class FactorGraph;
|
|
|
|
class SPSolver;
|
|
|
|
|
|
|
|
|
2011-07-22 21:33:30 +01:00
|
|
|
class Link
|
2011-05-17 12:00:33 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-07-22 21:33:30 +01:00
|
|
|
Link (Factor* f, FgVarNode* v)
|
|
|
|
{
|
|
|
|
factor_ = f;
|
|
|
|
var_ = v;
|
|
|
|
currMsg_.resize (v->getDomainSize(), 1);
|
|
|
|
nextMsg_.resize (v->getDomainSize(), 1);
|
|
|
|
msgSended_ = false;
|
|
|
|
residual_ = 0.0;
|
2011-05-17 12:00:33 +01:00
|
|
|
}
|
2011-07-22 21:33:30 +01:00
|
|
|
|
|
|
|
void setMessage (ParamSet msg)
|
2011-05-17 12:00:33 +01:00
|
|
|
{
|
2011-07-22 21:33:30 +01:00
|
|
|
Util::normalize (msg);
|
|
|
|
residual_ = Util::getMaxNorm (currMsg_, msg);
|
|
|
|
currMsg_ = msg;
|
2011-05-17 12:00:33 +01:00
|
|
|
}
|
|
|
|
|
2011-07-22 21:33:30 +01:00
|
|
|
void setNextMessage (CParamSet msg)
|
2011-05-17 12:00:33 +01:00
|
|
|
{
|
2011-07-22 21:33:30 +01:00
|
|
|
nextMsg_ = msg;
|
|
|
|
Util::normalize (nextMsg_);
|
|
|
|
residual_ = Util::getMaxNorm (currMsg_, nextMsg_);
|
2011-05-17 12:00:33 +01:00
|
|
|
}
|
|
|
|
|
2011-07-22 21:33:30 +01:00
|
|
|
void updateMessage (void)
|
2011-05-17 12:00:33 +01:00
|
|
|
{
|
2011-07-22 21:33:30 +01:00
|
|
|
currMsg_ = nextMsg_;
|
|
|
|
msgSended_ = true;
|
2011-05-17 12:00:33 +01:00
|
|
|
}
|
|
|
|
|
2011-07-22 21:33:30 +01:00
|
|
|
string toString (void) const
|
2011-05-17 12:00:33 +01:00
|
|
|
{
|
2011-07-22 21:33:30 +01:00
|
|
|
stringstream ss;
|
|
|
|
ss << factor_->getLabel();
|
|
|
|
ss << " -- " ;
|
|
|
|
ss << var_->getLabel();
|
|
|
|
return ss.str();
|
2011-05-17 12:00:33 +01:00
|
|
|
}
|
|
|
|
|
2011-07-22 21:33:30 +01:00
|
|
|
Factor* getFactor (void) const { return factor_; }
|
|
|
|
FgVarNode* getVariable (void) const { return var_; }
|
|
|
|
CParamSet getMessage (void) const { return currMsg_; }
|
|
|
|
bool messageWasSended (void) const { return msgSended_; }
|
|
|
|
double getResidual (void) const { return residual_; }
|
|
|
|
void clearResidual (void) { residual_ = 0.0; }
|
2011-05-17 12:00:33 +01:00
|
|
|
|
2011-07-22 21:33:30 +01:00
|
|
|
private:
|
|
|
|
Factor* factor_;
|
|
|
|
FgVarNode* var_;
|
|
|
|
ParamSet currMsg_;
|
|
|
|
ParamSet nextMsg_;
|
|
|
|
bool msgSended_;
|
|
|
|
double residual_;
|
2011-05-17 12:00:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-07-22 21:33:30 +01:00
|
|
|
class SPNodeInfo
|
2011-05-17 12:00:33 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-07-22 21:33:30 +01:00
|
|
|
void addLink (Link* link) { links_.push_back (link); }
|
|
|
|
CLinkSet getLinks (void) { return links_; }
|
2011-05-17 12:00:33 +01:00
|
|
|
|
|
|
|
private:
|
2011-07-22 21:33:30 +01:00
|
|
|
LinkSet links_;
|
2011-05-17 12:00:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-07-22 21:33:30 +01:00
|
|
|
class SPSolver : public Solver
|
2011-05-17 12:00:33 +01:00
|
|
|
{
|
2011-07-22 21:33:30 +01:00
|
|
|
public:
|
|
|
|
SPSolver (FactorGraph&);
|
|
|
|
virtual ~SPSolver (void);
|
2011-05-17 12:00:33 +01:00
|
|
|
|
2011-07-22 21:33:30 +01:00
|
|
|
void runSolver (void);
|
|
|
|
virtual ParamSet getPosterioriOf (Vid) const;
|
|
|
|
ParamSet getJointDistributionOf (CVidSet);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void initializeSolver (void);
|
|
|
|
void runTreeSolver (void);
|
|
|
|
bool readyToSendMessage (const Link*) const;
|
|
|
|
virtual void createLinks (void);
|
|
|
|
virtual void deleteJunction (Factor*, FgVarNode*);
|
|
|
|
bool converged (void);
|
|
|
|
virtual void maxResidualSchedule (void);
|
|
|
|
virtual ParamSet getFactor2VarMsg (const Link*) const;
|
|
|
|
virtual ParamSet getVar2FactorMsg (const Link*) const;
|
|
|
|
|
|
|
|
struct CompareResidual {
|
|
|
|
inline bool operator() (const Link* link1, const Link* link2)
|
|
|
|
{
|
|
|
|
return link1->getResidual() > link2->getResidual();
|
|
|
|
}
|
|
|
|
};
|
2011-05-17 12:00:33 +01:00
|
|
|
|
2011-07-22 21:33:30 +01:00
|
|
|
FactorGraph* fg_;
|
|
|
|
LinkSet links_;
|
|
|
|
vector<SPNodeInfo*> varsI_;
|
|
|
|
vector<SPNodeInfo*> factorsI_;
|
|
|
|
unsigned nIter_;
|
2011-05-17 12:00:33 +01:00
|
|
|
|
2011-07-22 21:33:30 +01:00
|
|
|
typedef multiset<Link*, CompareResidual> SortedOrder;
|
|
|
|
SortedOrder sortedOrder_;
|
2011-05-17 12:00:33 +01:00
|
|
|
|
2011-07-22 21:33:30 +01:00
|
|
|
typedef map<Link*, SortedOrder::iterator> LinkMap;
|
|
|
|
LinkMap linkMap_;
|
2011-05-17 12:00:33 +01:00
|
|
|
|
2011-07-22 21:33:30 +01:00
|
|
|
};
|
2011-05-17 12:00:33 +01:00
|
|
|
|
2011-07-22 21:33:30 +01:00
|
|
|
#endif // BP_SP_SOLVER_H
|
2011-05-17 12:00:33 +01:00
|
|
|
|