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/horus/CbpSolver.h

184 lines
4.0 KiB
C
Raw Normal View History

#ifndef HORUS_CBPSOLVER_H
#define HORUS_CBPSOLVER_H
2012-05-23 14:56:01 +01:00
#include <unordered_map>
2012-05-23 14:56:01 +01:00
#include "Solver.h"
#include "FactorGraph.h"
#include "Util.h"
#include "Horus.h"
2012-05-23 14:56:01 +01:00
class VarCluster;
class FacCluster;
class VarSignHash;
class FacSignHash;
class WeightedBpSolver;
typedef long Color;
typedef vector<Color> Colors;
typedef vector<std::pair<Color,unsigned>> VarSignature;
typedef vector<Color> FacSignature;
typedef unordered_map<unsigned, Color> DistColorMap;
typedef unordered_map<unsigned, Colors> VarColorMap;
typedef unordered_map<VarSignature, VarNodes, VarSignHash> VarSignMap;
typedef unordered_map<FacSignature, FacNodes, FacSignHash> FacSignMap;
typedef vector<VarCluster*> VarClusters;
typedef vector<FacCluster*> FacClusters;
typedef unordered_map<VarId, VarCluster*> VarId2VarCluster;
struct VarSignHash
{
size_t operator() (const VarSignature &sig) const
{
size_t val = hash<size_t>()(sig.size());
for (size_t i = 0; i < sig.size(); i++) {
val ^= hash<size_t>()(sig[i].first);
val ^= hash<size_t>()(sig[i].second);
}
return val;
}
};
struct FacSignHash
{
size_t operator() (const FacSignature &sig) const
{
size_t val = hash<size_t>()(sig.size());
for (size_t i = 0; i < sig.size(); i++) {
val ^= hash<size_t>()(sig[i]);
}
return val;
}
};
class VarCluster
2012-05-23 14:56:01 +01:00
{
public:
VarCluster (const VarNodes& vs) : members_(vs) { }
2012-05-23 14:56:01 +01:00
const VarNode* first (void) const { return members_.front(); }
2012-05-23 14:56:01 +01:00
const VarNodes& members (void) const { return members_; }
2012-05-23 14:56:01 +01:00
VarNode* representative (void) const { return repr_; }
2012-05-23 14:56:01 +01:00
void setRepresentative (VarNode* vn) { repr_ = vn; }
private:
VarNodes members_;
VarNode* repr_;
};
class FacCluster
{
public:
FacCluster (const FacNodes& fcs, const VarClusters& vcs)
: members_(fcs), varClusters_(vcs) { }
const FacNode* first (void) const { return members_.front(); }
const FacNodes& members (void) const { return members_; }
VarClusters& varClusters (void) { return varClusters_; }
2012-05-23 14:56:01 +01:00
FacNode* representative (void) const { return repr_; }
void setRepresentative (FacNode* fn) { repr_ = fn; }
2012-05-23 14:56:01 +01:00
private:
FacNodes members_;
VarClusters varClusters_;
FacNode* repr_;
2012-05-23 14:56:01 +01:00
};
class CbpSolver : public Solver
2012-05-23 14:56:01 +01:00
{
public:
2012-05-23 14:56:01 +01:00
CbpSolver (const FactorGraph& fg);
~CbpSolver (void);
void printSolverFlags (void) const;
Params solveQuery (VarIds);
2012-05-31 23:06:53 +01:00
static bool checkForIdenticalFactors;
private:
Color getNewColor (void)
{
++ freeColor_;
return freeColor_ - 1;
}
Color getColor (const VarNode* vn) const
{
return varColors_[vn->getIndex()];
}
Color getColor (const FacNode* fn) const
{
return facColors_[fn->getIndex()];
}
2012-05-23 14:56:01 +01:00
void setColor (const VarNode* vn, Color c)
{
varColors_[vn->getIndex()] = c;
}
void setColor (const FacNode* fn, Color c)
{
facColors_[fn->getIndex()] = c;
}
void findIdenticalFactors (void);
void setInitialColors (void);
2012-05-23 14:56:01 +01:00
void createGroups (void);
void createClusters (const VarSignMap&, const FacSignMap&);
VarSignature getSignature (const VarNode*);
FacSignature getSignature (const FacNode*);
void printGroups (const VarSignMap&, const FacSignMap&) const;
VarId getRepresentative (VarId vid)
{
assert (Util::contains (vid2VarCluster_, vid));
VarCluster* vc = vid2VarCluster_.find (vid)->second;
return vc->representative()->varId();
}
2012-05-23 14:56:01 +01:00
FactorGraph* getCompressedFactorGraph (void);
2012-05-23 14:56:01 +01:00
vector<vector<unsigned>> getWeights (void) const;
2012-05-23 14:56:01 +01:00
unsigned getWeight (const FacCluster*,
const VarCluster*, size_t index) const;
2012-05-23 14:56:01 +01:00
Color freeColor_;
Colors varColors_;
Colors facColors_;
VarClusters varClusters_;
FacClusters facClusters_;
VarId2VarCluster vid2VarCluster_;
const FactorGraph* compressedFg_;
WeightedBpSolver* solver_;
2012-05-23 14:56:01 +01:00
};
#endif // HORUS_CBPSOLVER_H
2012-05-23 14:56:01 +01:00