2011-12-12 15:29:51 +00:00
|
|
|
#ifndef HORUS_CFACTORGRAPH_H
|
|
|
|
#define HORUS_CFACTORGRAPH_H
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include "FactorGraph.h"
|
|
|
|
#include "Factor.h"
|
2012-03-22 11:33:24 +00:00
|
|
|
#include "Horus.h"
|
2011-12-12 15:29:51 +00:00
|
|
|
|
|
|
|
class VarCluster;
|
|
|
|
class FacCluster;
|
|
|
|
class Distribution;
|
|
|
|
class Signature;
|
|
|
|
|
|
|
|
class SignatureHash;
|
|
|
|
|
|
|
|
|
2012-03-31 23:27:37 +01:00
|
|
|
typedef long Color;
|
|
|
|
|
|
|
|
typedef unordered_map<unsigned, vector<Color>> VarColorMap;
|
|
|
|
|
|
|
|
typedef unordered_map<unsigned, Color> DistColorMap;
|
|
|
|
typedef unordered_map<VarId, VarCluster*> VarId2VarCluster;
|
|
|
|
|
2012-04-10 20:43:08 +01:00
|
|
|
typedef vector<VarCluster*> VarClusters;
|
|
|
|
typedef vector<FacCluster*> FacClusters;
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-05 23:00:48 +01:00
|
|
|
typedef unordered_map<Signature, VarNodes, SignatureHash> VarSignMap;
|
2012-04-10 11:51:56 +01:00
|
|
|
typedef unordered_map<Signature, FacNodes, SignatureHash> FacSignMap;
|
2011-12-12 15:29:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-22 11:33:24 +00:00
|
|
|
struct Signature
|
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
Signature (unsigned size) : colors(size) { }
|
|
|
|
|
2011-12-12 15:29:51 +00:00
|
|
|
bool operator< (const Signature& sig) const
|
|
|
|
{
|
|
|
|
if (colors.size() < sig.colors.size()) {
|
|
|
|
return true;
|
|
|
|
} else if (colors.size() > sig.colors.size()) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 0; i < colors.size(); i++) {
|
|
|
|
if (colors[i] < sig.colors[i]) {
|
|
|
|
return true;
|
|
|
|
} else if (colors[i] > sig.colors[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2011-12-12 15:29:51 +00:00
|
|
|
bool operator== (const Signature& sig) const
|
|
|
|
{
|
|
|
|
if (colors.size() != sig.colors.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < colors.size(); i++) {
|
|
|
|
if (colors[i] != sig.colors[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2011-12-12 15:29:51 +00:00
|
|
|
vector<Color> colors;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-31 23:27:37 +01:00
|
|
|
struct SignatureHash
|
|
|
|
{
|
2011-12-12 15:29:51 +00:00
|
|
|
size_t operator() (const Signature &sig) const
|
|
|
|
{
|
|
|
|
size_t val = hash<size_t>()(sig.colors.size());
|
|
|
|
for (unsigned i = 0; i < sig.colors.size(); i++) {
|
|
|
|
val ^= hash<size_t>()(sig.colors[i]);
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VarCluster
|
|
|
|
{
|
|
|
|
public:
|
2012-04-05 23:00:48 +01:00
|
|
|
VarCluster (const VarNodes& vs)
|
2011-12-12 15:29:51 +00:00
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < vs.size(); i++) {
|
|
|
|
groundVars_.push_back (vs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void addFacCluster (FacCluster* fc)
|
|
|
|
{
|
2012-03-22 11:33:24 +00:00
|
|
|
facClusters_.push_back (fc);
|
2011-12-12 15:29:51 +00:00
|
|
|
}
|
|
|
|
|
2012-04-10 20:43:08 +01:00
|
|
|
const FacClusters& getFacClusters (void) const
|
2011-12-12 15:29:51 +00:00
|
|
|
{
|
2012-03-22 11:33:24 +00:00
|
|
|
return facClusters_;
|
2011-12-12 15:29:51 +00:00
|
|
|
}
|
|
|
|
|
2012-04-05 23:00:48 +01:00
|
|
|
VarNode* getRepresentativeVariable (void) const { return representVar_; }
|
2012-04-10 20:43:08 +01:00
|
|
|
|
|
|
|
void setRepresentativeVariable (VarNode* v) { representVar_ = v; }
|
|
|
|
|
|
|
|
const VarNodes& getGroundVarNodes (void) const { return groundVars_; }
|
2011-12-12 15:29:51 +00:00
|
|
|
|
|
|
|
private:
|
2012-04-05 23:00:48 +01:00
|
|
|
VarNodes groundVars_;
|
2012-04-10 20:43:08 +01:00
|
|
|
FacClusters facClusters_;
|
2012-04-05 23:00:48 +01:00
|
|
|
VarNode* representVar_;
|
2011-12-12 15:29:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class FacCluster
|
|
|
|
{
|
|
|
|
public:
|
2012-04-10 20:43:08 +01:00
|
|
|
FacCluster (const FacNodes& groundFactors, const VarClusters& vcs)
|
2011-12-12 15:29:51 +00:00
|
|
|
{
|
|
|
|
groundFactors_ = groundFactors;
|
|
|
|
varClusters_ = vcs;
|
|
|
|
for (unsigned i = 0; i < varClusters_.size(); i++) {
|
|
|
|
varClusters_[i]->addFacCluster (this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-10 20:43:08 +01:00
|
|
|
const VarClusters& getVarClusters (void) const
|
2011-12-12 15:29:51 +00:00
|
|
|
{
|
|
|
|
return varClusters_;
|
|
|
|
}
|
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
bool containsGround (const FacNode* fn)
|
2011-12-12 15:29:51 +00:00
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < groundFactors_.size(); i++) {
|
|
|
|
if (groundFactors_[i] == fn) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
FacNode* getRepresentativeFactor (void) const
|
2011-12-12 15:29:51 +00:00
|
|
|
{
|
|
|
|
return representFactor_;
|
|
|
|
}
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
void setRepresentativeFactor (FacNode* fn)
|
2011-12-12 15:29:51 +00:00
|
|
|
{
|
|
|
|
representFactor_ = fn;
|
|
|
|
}
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
const FacNodes& getGroundFactors (void) const
|
2011-12-12 15:29:51 +00:00
|
|
|
{
|
|
|
|
return groundFactors_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2012-04-10 11:51:56 +01:00
|
|
|
FacNodes groundFactors_;
|
2012-04-10 20:43:08 +01:00
|
|
|
VarClusters varClusters_;
|
2012-04-10 11:51:56 +01:00
|
|
|
FacNode* representFactor_;
|
2011-12-12 15:29:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class CFactorGraph
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CFactorGraph (const FactorGraph&);
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2011-12-12 15:29:51 +00:00
|
|
|
~CFactorGraph (void);
|
|
|
|
|
2012-04-10 20:43:08 +01:00
|
|
|
const VarClusters& getVarClusters (void) { return varClusters_; }
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-10 20:43:08 +01:00
|
|
|
const FacClusters& getFacClusters (void) { return facClusters_; }
|
2011-12-12 15:29:51 +00:00
|
|
|
|
2012-04-05 23:00:48 +01:00
|
|
|
VarNode* getEquivalentVariable (VarId vid)
|
2011-12-12 15:29:51 +00:00
|
|
|
{
|
|
|
|
VarCluster* vc = vid2VarCluster_.find (vid)->second;
|
|
|
|
return vc->getRepresentativeVariable();
|
|
|
|
}
|
|
|
|
|
2012-04-10 20:43:08 +01:00
|
|
|
FactorGraph* getGroundFactorGraph (void) const;
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-11 15:36:50 +01:00
|
|
|
unsigned getEdgeCount (const FacCluster*, const VarCluster*) const;
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-03-22 11:33:24 +00:00
|
|
|
static bool checkForIdenticalFactors;
|
2011-12-12 15:29:51 +00:00
|
|
|
|
|
|
|
private:
|
2012-03-31 23:27:37 +01:00
|
|
|
Color getFreeColor (void)
|
|
|
|
{
|
2011-12-12 15:29:51 +00:00
|
|
|
++ freeColor_;
|
|
|
|
return freeColor_ - 1;
|
|
|
|
}
|
|
|
|
|
2012-04-05 23:00:48 +01:00
|
|
|
Color getColor (const VarNode* vn) const
|
2011-12-12 15:29:51 +00:00
|
|
|
{
|
|
|
|
return varColors_[vn->getIndex()];
|
|
|
|
}
|
2012-04-10 11:51:56 +01:00
|
|
|
Color getColor (const FacNode* fn) const {
|
2012-04-10 20:43:08 +01:00
|
|
|
return facColors_[fn->getIndex()];
|
2011-12-12 15:29:51 +00:00
|
|
|
}
|
|
|
|
|
2012-04-05 23:00:48 +01:00
|
|
|
void setColor (const VarNode* vn, Color c)
|
2011-12-12 15:29:51 +00:00
|
|
|
{
|
|
|
|
varColors_[vn->getIndex()] = c;
|
|
|
|
}
|
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
void setColor (const FacNode* fn, Color c)
|
2011-12-12 15:29:51 +00:00
|
|
|
{
|
2012-04-10 20:43:08 +01:00
|
|
|
facColors_[fn->getIndex()] = c;
|
2011-12-12 15:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VarCluster* getVariableCluster (VarId vid) const
|
|
|
|
{
|
|
|
|
return vid2VarCluster_.find (vid)->second;
|
|
|
|
}
|
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
void setInitialColors (void);
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
void createGroups (void);
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
void createClusters (const VarSignMap&, const FacSignMap&);
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
const Signature& getSignature (const VarNode*);
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
const Signature& getSignature (const FacNode*);
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-10 11:51:56 +01:00
|
|
|
void printGroups (const VarSignMap&, const FacSignMap&) const;
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2011-12-12 15:29:51 +00:00
|
|
|
Color freeColor_;
|
|
|
|
vector<Color> varColors_;
|
2012-04-10 20:43:08 +01:00
|
|
|
vector<Color> facColors_;
|
2011-12-12 15:29:51 +00:00
|
|
|
vector<Signature> varSignatures_;
|
2012-04-10 20:43:08 +01:00
|
|
|
vector<Signature> facSignatures_;
|
2012-04-11 23:48:59 +01:00
|
|
|
VarClusters varClusters_;
|
|
|
|
FacClusters facClusters_;
|
2012-03-31 23:27:37 +01:00
|
|
|
VarId2VarCluster vid2VarCluster_;
|
2011-12-12 15:29:51 +00:00
|
|
|
const FactorGraph* groundFg_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // HORUS_CFACTORGRAPH_H
|
|
|
|
|