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

205 lines
4.4 KiB
C
Raw Normal View History

2012-03-22 11:33:24 +00:00
#ifndef HORUS_CONSTRAINTTREE_H
#define HORUS_CONSTRAINTTREE_H
#include <cassert>
#include <algorithm>
#include <iostream>
#include <sstream>
#include "TinySet.h"
#include "LiftedUtils.h"
using namespace std;
class CTNode;
typedef vector<CTNode*> CTNodes;
class ConstraintTree;
typedef vector<ConstraintTree*> ConstraintTrees;
class CTNode
{
public:
CTNode (const CTNode& n) : symbol_(n.symbol()), level_(n.level()) { }
CTNode (Symbol s, unsigned l) : symbol_(s) , level_(l) { }
unsigned level (void) const { return level_; }
void setLevel (unsigned level) { level_ = level; }
Symbol symbol (void) const { return symbol_; }
void setSymbol (const Symbol s) { symbol_ = s; }
CTNodes& childs (void) { return childs_; }
const CTNodes& childs (void) const { return childs_; }
unsigned nrChilds (void) const { return childs_.size(); }
bool isRoot (void) const { return level_ == 0; }
bool isLeaf (void) const { return childs_.empty(); }
2012-03-31 23:27:37 +01:00
void addChild (CTNode*, bool = true);
2012-03-22 11:33:24 +00:00
2012-03-31 23:27:37 +01:00
void removeChild (CTNode*);
2012-03-22 11:33:24 +00:00
void removeChilds (void);
2012-03-31 23:27:37 +01:00
void removeAndDeleteChild (CTNode*);
void removeAndDeleteAllChilds (void);
SymbolSet childSymbols (void) const;
static CTNode* copySubtree (const CTNode*);
2012-03-22 11:33:24 +00:00
2012-03-31 23:27:37 +01:00
static void deleteSubtree (CTNode*);
2012-03-22 11:33:24 +00:00
2012-03-31 23:27:37 +01:00
private:
void updateChildLevels (CTNode*, unsigned);
Symbol symbol_;
CTNodes childs_;
unsigned level_;
};
2012-03-22 11:33:24 +00:00
ostream& operator<< (ostream &out, const CTNode&);
class ConstraintTree
{
public:
2012-03-31 23:27:37 +01:00
ConstraintTree (unsigned);
2012-03-22 11:33:24 +00:00
ConstraintTree (const LogVars&);
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
ConstraintTree (const LogVars&, const Tuples&);
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
ConstraintTree (const ConstraintTree&);
2012-03-31 23:27:37 +01:00
2012-03-22 11:33:24 +00:00
~ConstraintTree (void);
CTNode* root (void) const { return root_; }
bool empty (void) const { return root_->childs().empty(); }
const LogVars& logVars (void) const
{
assert (LogVarSet (logVars_) == logVarSet_);
return logVars_;
}
const LogVarSet& logVarSet (void) const
{
assert (LogVarSet (logVars_) == logVarSet_);
return logVarSet_;
}
unsigned nrLogVars (void) const
{
return logVars_.size();
assert (LogVarSet (logVars_) == logVarSet_);
}
2012-03-31 23:27:37 +01:00
void addTuple (const Tuple&);
bool containsTuple (const Tuple&);
void moveToTop (const LogVars&);
void moveToBottom (const LogVars&);
void join (ConstraintTree*, bool = false);
unsigned getLevel (LogVar) const;
void rename (LogVar, LogVar);
void applySubstitution (const Substitution&);
void project (const LogVarSet&);
void remove (const LogVarSet&);
bool isSingleton (LogVar);
LogVarSet singletons (void);
TupleSet tupleSet (unsigned = 0) const;
TupleSet tupleSet (const LogVars&);
unsigned size (void) const;
unsigned nrSymbols (LogVar);
void exportToGraphViz (const char*, bool = false) const;
bool isCountNormalized (const LogVarSet&);
unsigned getConditionalCount (const LogVarSet&);
TinySet<unsigned> getConditionalCounts (const LogVarSet&);
bool isCarteesianProduct (const LogVarSet&) const;
2012-03-22 11:33:24 +00:00
std::pair<ConstraintTree*, ConstraintTree*> split (
2012-03-31 23:27:37 +01:00
const Tuple&, unsigned);
2012-03-22 11:33:24 +00:00
std::pair<ConstraintTree*, ConstraintTree*> split (
2012-03-31 23:27:37 +01:00
const ConstraintTree*, unsigned) const;
2012-03-22 11:33:24 +00:00
2012-03-31 23:27:37 +01:00
ConstraintTrees countNormalize (const LogVarSet&);
2012-03-22 11:33:24 +00:00
ConstraintTrees jointCountNormalize (
2012-03-31 23:27:37 +01:00
ConstraintTree*, ConstraintTree*, LogVar, LogVar, LogVar);
static bool identical (
const ConstraintTree*, const ConstraintTree*, unsigned);
static bool overlap (
const ConstraintTree*, const ConstraintTree*, unsigned);
LogVars expand (LogVar);
ConstraintTrees ground (LogVar);
2012-03-22 11:33:24 +00:00
private:
2012-03-31 23:27:37 +01:00
unsigned countTuples (const CTNode*) const;
CTNodes getNodesBelow (CTNode*) const;
CTNodes getNodesAtLevel (unsigned) const;
void swapLogVar (LogVar);
bool join (CTNode*, const Tuple&, unsigned, CTNode*);
bool indenticalSubtrees (
const CTNode*, const CTNode*, bool) const;
void getTuples (CTNode*, Tuples, unsigned, Tuples&, CTNodes&) const;
2012-03-22 11:33:24 +00:00
vector<std::pair<CTNode*, unsigned>> countNormalize (
2012-03-31 23:27:37 +01:00
const CTNode*, unsigned);
static void split (
CTNode*, CTNode*, CTNodes&, unsigned);
static bool overlap (const CTNode*, const CTNode*, unsigned);
CTNode* root_;
LogVars logVars_;
LogVarSet logVarSet_;
2012-03-22 11:33:24 +00:00
};
#endif // HORUS_CONSTRAINTTREE_H