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

177 lines
3.9 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-04-16 21:42:14 +01:00
#include "Util.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 VarSignatureHash;
class FacSignatureHash;
2011-12-12 15:29:51 +00:00
2012-03-31 23:27:37 +01:00
typedef long Color;
2012-04-13 15:56:37 +01:00
typedef vector<Color> Colors;
typedef vector<std::pair<Color,unsigned>> VarSignature;
typedef vector<Color> FacSignature;
2012-03-31 23:27:37 +01:00
2012-04-13 15:56:37 +01:00
typedef unordered_map<unsigned, Color> DistColorMap;
typedef unordered_map<unsigned, Colors> VarColorMap;
2012-03-31 23:27:37 +01:00
typedef unordered_map<VarSignature, VarNodes, VarSignatureHash> VarSignMap;
typedef unordered_map<FacSignature, FacNodes, FacSignatureHash> FacSignMap;
2012-03-31 23:27:37 +01:00
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-13 15:56:37 +01:00
typedef unordered_map<VarId, VarCluster*> VarId2VarCluster;
2011-12-12 15:29:51 +00:00
struct VarSignatureHash
2012-03-22 11:33:24 +00:00
{
size_t operator() (const VarSignature &sig) const
2011-12-12 15:29:51 +00:00
{
size_t val = hash<size_t>()(sig.size());
for (unsigned i = 0; i < sig.size(); i++) {
val ^= hash<size_t>()(sig[i].first);
val ^= hash<size_t>()(sig[i].second);
2011-12-12 15:29:51 +00:00
}
return val;
2011-12-12 15:29:51 +00:00
}
};
struct FacSignatureHash
2012-03-31 23:27:37 +01:00
{
size_t operator() (const FacSignature &sig) const
2011-12-12 15:29:51 +00:00
{
size_t val = hash<size_t>()(sig.size());
for (unsigned i = 0; i < sig.size(); i++) {
val ^= hash<size_t>()(sig[i]);
2011-12-12 15:29:51 +00:00
}
return val;
}
};
class VarCluster
{
public:
2012-04-16 21:42:14 +01:00
VarCluster (const VarNodes& vs) : members_(vs) { }
2011-12-12 15:29:51 +00:00
const VarNode* first (void) const { return members_.front(); }
2011-12-12 15:29:51 +00:00
const VarNodes& members (void) const { return members_; }
2012-04-10 20:43:08 +01:00
VarNode* representative (void) const { return repr_; }
2012-04-10 20:43:08 +01:00
2012-04-16 21:42:14 +01:00
void setRepresentative (VarNode* vn) { repr_ = vn; }
2011-12-12 15:29:51 +00:00
private:
2012-04-16 21:42:14 +01:00
VarNodes members_;
VarNode* repr_;
2011-12-12 15:29:51 +00:00
};
class FacCluster
{
public:
2012-04-16 21:42:14 +01:00
FacCluster (const FacNodes& fcs, const VarClusters& vcs)
: members_(fcs), varClusters_(vcs) { }
const FacNode* first (void) const { return members_.front(); }
2012-04-16 21:42:14 +01:00
const FacNodes& members (void) const { return members_; }
2011-12-12 15:29:51 +00:00
VarClusters& varClusters (void) { return varClusters_; }
2011-12-12 15:29:51 +00:00
FacNode* representative (void) const { return repr_; }
2011-12-12 15:29:51 +00:00
2012-04-16 21:42:14 +01:00
void setRepresentative (FacNode* fn) { repr_ = fn; }
2011-12-12 15:29:51 +00:00
private:
2012-04-16 21:42:14 +01:00
FacNodes members_;
VarClusters varClusters_;
FacNode* repr_;
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-16 21:42:14 +01:00
const VarClusters& varClusters (void) { return varClusters_; }
2012-03-31 23:27:37 +01:00
2012-04-16 21:42:14 +01:00
const FacClusters& facClusters (void) { return facClusters_; }
2011-12-12 15:29:51 +00:00
VarNode* getEquivalent (VarId vid)
2011-12-12 15:29:51 +00:00
{
VarCluster* vc = vid2VarCluster_.find (vid)->second;
return vc->representative();
2011-12-12 15:29:51 +00:00
}
FactorGraph* getGroundFactorGraph (void);
2012-03-31 23:27:37 +01:00
unsigned getEdgeCount (const FacCluster*,
const VarCluster*, unsigned index) 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
}
2012-04-16 21:42:14 +01:00
void findIdenticalFactors (void);
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
VarSignature getSignature (const VarNode*);
2012-03-31 23:27:37 +01:00
FacSignature 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
Color freeColor_;
Colors varColors_;
Colors facColors_;
VarClusters varClusters_;
FacClusters facClusters_;
VarId2VarCluster vid2VarCluster_;
const FactorGraph* groundFg_;
2011-12-12 15:29:51 +00:00
};
#endif // HORUS_CFACTORGRAPH_H