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/clpbn/bp/CFactorGraph.h

246 lines
5.2 KiB
C
Raw Normal View History

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;
typedef vector<VarCluster*> VarClusterSet;
typedef vector<FacCluster*> FacClusterSet;
typedef unordered_map<Signature, FgVarSet, SignatureHash> VarSignMap;
typedef unordered_map<Signature, FgFacSet, 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:
VarCluster (const FgVarSet& vs)
{
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
}
const FacClusterSet& getFacClusters (void) const
{
2012-03-22 11:33:24 +00:00
return facClusters_;
2011-12-12 15:29:51 +00:00
}
FgVarNode* getRepresentativeVariable (void) const { return representVar_; }
void setRepresentativeVariable (FgVarNode* v) { representVar_ = v; }
const FgVarSet& getGroundFgVarNodes (void) const { return groundVars_; }
private:
2012-03-22 11:33:24 +00:00
FgVarSet groundVars_;
FacClusterSet facClusters_;
FgVarNode* representVar_;
2011-12-12 15:29:51 +00:00
};
class FacCluster
{
public:
FacCluster (const FgFacSet& groundFactors, const VarClusterSet& vcs)
{
groundFactors_ = groundFactors;
varClusters_ = vcs;
for (unsigned i = 0; i < varClusters_.size(); i++) {
varClusters_[i]->addFacCluster (this);
}
}
const VarClusterSet& getVarClusters (void) const
{
return varClusters_;
}
bool containsGround (const FgFacNode* fn)
{
for (unsigned i = 0; i < groundFactors_.size(); i++) {
if (groundFactors_[i] == fn) {
return true;
}
}
return false;
}
FgFacNode* getRepresentativeFactor (void) const
{
return representFactor_;
}
2012-03-31 23:27:37 +01:00
2011-12-12 15:29:51 +00:00
void setRepresentativeFactor (FgFacNode* fn)
{
representFactor_ = fn;
}
2012-03-31 23:27:37 +01:00
2011-12-12 15:29:51 +00:00
const FgFacSet& getGroundFactors (void) const
{
return groundFactors_;
}
private:
2012-03-22 11:33:24 +00:00
FgFacSet groundFactors_;
2011-12-12 15:29:51 +00:00
VarClusterSet varClusters_;
2012-03-22 11:33:24 +00:00
FgFacNode* 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-03-31 23:27:37 +01:00
const VarClusterSet& getVarClusters (void) { return varClusters_; }
const FacClusterSet& getFacClusters (void) { return facClusters_; }
2011-12-12 15:29:51 +00:00
FgVarNode* getEquivalentVariable (VarId vid)
{
VarCluster* vc = vid2VarCluster_.find (vid)->second;
return vc->getRepresentativeVariable();
}
2012-03-31 23:27:37 +01:00
FactorGraph* getCompressedFactorGraph (void);
unsigned getGroundEdgeCount (const FacCluster*, const VarCluster*) const;
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;
}
Color getColor (const FgVarNode* vn) const
{
return varColors_[vn->getIndex()];
}
Color getColor (const FgFacNode* fn) const {
return factorColors_[fn->getIndex()];
}
void setColor (const FgVarNode* vn, Color c)
{
varColors_[vn->getIndex()] = c;
}
void setColor (const FgFacNode* fn, Color c)
{
factorColors_[fn->getIndex()] = c;
}
VarCluster* getVariableCluster (VarId vid) const
{
return vid2VarCluster_.find (vid)->second;
}
2012-03-31 23:27:37 +01:00
void setInitialColors (void);
void createGroups (void);
void createClusters (const VarSignMap&, const FacSignMap&);
const Signature& getSignature (const FgVarNode*);
const Signature& getSignature (const FgFacNode*);
void printGroups (const VarSignMap&, const FacSignMap&) const;
2011-12-12 15:29:51 +00:00
Color freeColor_;
vector<Color> varColors_;
vector<Color> factorColors_;
vector<Signature> varSignatures_;
vector<Signature> factorSignatures_;
VarClusterSet varClusters_;
2012-03-31 23:27:37 +01:00
FacClusterSet facClusters_;
VarId2VarCluster vid2VarCluster_;
2011-12-12 15:29:51 +00:00
const FactorGraph* groundFg_;
};
#endif // HORUS_CFACTORGRAPH_H