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/ElimGraph.h

176 lines
3.3 KiB
C
Raw Normal View History

2013-02-07 17:50:02 +00:00
#ifndef YAP_PACKAGES_CLPBN_HORUS_ELIMGRAPH_H_
#define YAP_PACKAGES_CLPBN_HORUS_ELIMGRAPH_H_
2012-05-23 14:56:01 +01:00
2013-02-07 20:09:10 +00:00
#include <cassert>
#include <vector>
#include <unordered_map>
2012-05-23 14:56:01 +01:00
#include "FactorGraph.h"
#include "TinySet.h"
#include "Horus.h"
2013-02-07 13:37:15 +00:00
class EgNode;
typedef TinySet<EgNode*> EGNeighs;
2012-05-23 14:56:01 +01:00
2012-12-17 18:39:42 +00:00
enum ElimHeuristic
2012-05-23 14:56:01 +01:00
{
2012-05-29 17:12:57 +01:00
SEQUENTIAL,
2012-05-23 14:56:01 +01:00
MIN_NEIGHBORS,
MIN_WEIGHT,
MIN_FILL,
WEIGHTED_MIN_FILL
};
class EgNode : public Var
{
public:
EgNode (VarId vid, unsigned range) : Var (vid, range) { }
void addNeighbor (EgNode* n) { neighs_.insert (n); }
void removeNeighbor (EgNode* n) { neighs_.remove (n); }
bool isNeighbor (EgNode* n) const { return neighs_.contains (n); }
const EGNeighs& neighbors (void) const { return neighs_; }
private:
EGNeighs neighs_;
};
class ElimGraph
{
public:
2012-05-29 17:12:57 +01:00
ElimGraph (const Factors&);
2012-05-23 14:56:01 +01:00
~ElimGraph (void);
2012-12-17 18:39:42 +00:00
2012-05-23 14:56:01 +01:00
VarIds getEliminatingOrder (const VarIds&);
void print (void) const;
void exportToGraphViz (const char*, bool = true,
const VarIds& = VarIds()) const;
2012-05-29 17:12:57 +01:00
static VarIds getEliminationOrder (const Factors&, VarIds);
2012-05-23 14:56:01 +01:00
static ElimHeuristic elimHeuristic (void) { return elimHeuristic_; }
static void setElimHeuristic (ElimHeuristic eh) { elimHeuristic_ = eh; }
2012-05-23 14:56:01 +01:00
private:
void addEdge (EgNode* n1, EgNode* n2);
2012-05-23 14:56:01 +01:00
unsigned getNeighborsCost (const EgNode* n) const;
2012-05-23 14:56:01 +01:00
unsigned getWeightCost (const EgNode* n) const;
2012-05-23 14:56:01 +01:00
unsigned getFillCost (const EgNode* n) const;
2012-05-23 14:56:01 +01:00
unsigned getWeightedFillCost (const EgNode* n) const;
bool neighbors (EgNode* n1, EgNode* n2) const;
2012-05-23 14:56:01 +01:00
void addNode (EgNode*);
EgNode* getEgNode (VarId) const;
EgNode* getLowestCostNode (void) const;
void connectAllNeighbors (const EgNode*);
2013-02-07 13:37:15 +00:00
std::vector<EgNode*> nodes_;
TinySet<EgNode*> unmarked_;
std::unordered_map<VarId, EgNode*> varMap_;
static ElimHeuristic elimHeuristic_;
2012-12-27 22:25:45 +00:00
DISALLOW_COPY_AND_ASSIGN (ElimGraph);
2012-05-23 14:56:01 +01:00
};
inline void
ElimGraph::addEdge (EgNode* n1, EgNode* n2)
{
assert (n1 != n2);
n1->addNeighbor (n2);
n2->addNeighbor (n1);
}
inline unsigned
ElimGraph::getNeighborsCost (const EgNode* n) const
{
return n->neighbors().size();
}
inline unsigned
ElimGraph::getWeightCost (const EgNode* n) const
{
unsigned cost = 1;
const EGNeighs& neighs = n->neighbors();
for (size_t i = 0; i < neighs.size(); i++) {
cost *= neighs[i]->range();
}
return cost;
}
inline unsigned
ElimGraph::getFillCost (const EgNode* n) const
{
unsigned cost = 0;
const EGNeighs& neighs = n->neighbors();
if (neighs.size() > 0) {
for (size_t i = 0; i < neighs.size() - 1; i++) {
for (size_t j = i + 1; j < neighs.size(); j++) {
if ( ! neighbors (neighs[i], neighs[j])) {
cost ++;
}
}
}
}
return cost;
}
inline unsigned
ElimGraph::getWeightedFillCost (const EgNode* n) const
{
unsigned cost = 0;
const EGNeighs& neighs = n->neighbors();
if (neighs.size() > 0) {
for (size_t i = 0; i < neighs.size() - 1; i++) {
for (size_t j = i + 1; j < neighs.size(); j++) {
if ( ! neighbors (neighs[i], neighs[j])) {
cost += neighs[i]->range() * neighs[j]->range();
}
}
}
}
return cost;
}
inline bool
ElimGraph::neighbors (EgNode* n1, EgNode* n2) const
{
return n1->isNeighbor (n2);
}
2013-02-07 17:50:02 +00:00
#endif // YAP_PACKAGES_CLPBN_HORUS_ELIMGRAPH_H_
2012-05-23 14:56:01 +01:00