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

248 lines
5.3 KiB
C
Raw Normal View History

2013-02-07 17:50:02 +00:00
#ifndef YAP_PACKAGES_CLPBN_HORUS_CONSTRAINTTREE_H_
#define YAP_PACKAGES_CLPBN_HORUS_CONSTRAINTTREE_H_
2012-05-23 14:56:01 +01:00
#include <cassert>
2013-02-07 20:09:10 +00:00
#include <vector>
#include <algorithm>
#include <string>
#include <ostream>
2012-05-23 14:56:01 +01:00
#include "TinySet.h"
#include "LiftedUtils.h"
namespace Horus {
2013-02-07 23:53:13 +00:00
2012-05-23 14:56:01 +01:00
class CTNode;
class ConstraintTree;
2013-02-08 01:11:18 +00:00
typedef std::vector<CTNode*> CTNodes;
typedef std::vector<ConstraintTree*> ConstraintTrees;
2012-05-23 14:56:01 +01:00
class CTNode {
2012-05-23 14:56:01 +01:00
private:
struct CmpSymbol {
bool operator() (const CTNode* n1, const CTNode* n2) const {
return n1->symbol() < n2->symbol();
}};
2012-05-23 14:56:01 +01:00
public:
typedef TinySet<CTNode*, CmpSymbol> CTChilds;
CTNode (const CTNode& n, const CTChilds& chs = CTChilds())
2012-05-23 14:56:01 +01:00
: symbol_(n.symbol()), childs_(chs), level_(n.level()) { }
CTNode (Symbol s, unsigned l, const CTChilds& chs = CTChilds())
2012-05-23 14:56:01 +01:00
: symbol_(s), childs_(chs), level_(l) { }
unsigned level (void) const { return level_; }
void setLevel (unsigned level) { level_ = level; }
Symbol symbol (void) const { return symbol_; }
2013-02-13 23:47:00 +00:00
void setSymbol (Symbol s) { symbol_ = s; }
2012-05-23 14:56:01 +01:00
CTChilds& childs (void) { return childs_; }
2012-05-23 14:56:01 +01:00
const CTChilds& childs (void) const { return childs_; }
2012-12-17 18:39:42 +00:00
2012-05-24 22:55:20 +01:00
size_t nrChilds (void) const { return childs_.size(); }
2012-05-23 14:56:01 +01:00
bool isRoot (void) const { return level_ == 0; }
bool isLeaf (void) const { return childs_.empty(); }
CTChilds::iterator findSymbol (Symbol symb);
2012-05-23 14:56:01 +01:00
void mergeSubtree (CTNode*, bool = true);
void removeChild (CTNode*);
void removeChilds (void);
void removeAndDeleteChild (CTNode*);
void removeAndDeleteAllChilds (void);
SymbolSet childSymbols (void) const;
static CTNode* copySubtree (const CTNode*);
static void deleteSubtree (CTNode*);
private:
void updateChildLevels (CTNode*, unsigned);
2012-12-27 22:25:45 +00:00
Symbol symbol_;
2013-02-16 16:17:14 +00:00
CTChilds childs_;
2012-12-27 22:25:45 +00:00
unsigned level_;
DISALLOW_ASSIGN (CTNode);
2012-05-23 14:56:01 +01:00
};
typedef CTNode::CTChilds CTChilds;
2012-05-23 14:56:01 +01:00
inline CTChilds::iterator
CTNode::findSymbol (Symbol symb)
{
CTNode tmp (symb, 0);
return childs_.find (&tmp);
}
std::ostream& operator<< (std::ostream&, const CTNode&);
class ConstraintTree {
2012-05-23 14:56:01 +01:00
public:
ConstraintTree (unsigned);
ConstraintTree (const LogVars&);
ConstraintTree (const LogVars&, const Tuples&);
2012-12-17 18:39:42 +00:00
2013-02-07 13:37:15 +00:00
ConstraintTree (std::vector<std::vector<std::string>> names);
2012-05-23 14:56:01 +01:00
ConstraintTree (const ConstraintTree&);
ConstraintTree (const CTChilds& rootChilds, const LogVars& logVars);
2012-05-23 14:56:01 +01:00
~ConstraintTree (void);
CTNode* root (void) const { return root_; }
2012-12-17 18:39:42 +00:00
2012-05-23 14:56:01 +01:00
bool empty (void) const { return root_->childs().empty(); }
const LogVars& logVars (void) const;
2012-05-23 14:56:01 +01:00
const LogVarSet& logVarSet (void) const;
2012-12-17 18:39:42 +00:00
size_t nrLogVars (void) const;
2012-12-17 18:39:42 +00:00
2012-05-23 14:56:01 +01:00
void addTuple (const Tuple&);
2012-12-17 18:39:42 +00:00
2012-05-23 14:56:01 +01:00
bool containsTuple (const Tuple&);
2012-12-17 18:39:42 +00:00
2012-05-23 14:56:01 +01:00
void moveToTop (const LogVars&);
void moveToBottom (const LogVars&);
void join (ConstraintTree*, bool oneTwoOne = false);
unsigned getLevel (LogVar) const;
void rename (LogVar, LogVar);
void applySubstitution (const Substitution&);
void project (const LogVarSet&);
2012-12-17 18:39:42 +00:00
2012-10-29 21:37:58 +00:00
ConstraintTree projectedCopy (const LogVarSet&);
2012-05-23 14:56:01 +01:00
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 isCartesianProduct (const LogVarSet&);
std::pair<ConstraintTree*, ConstraintTree*> split (const Tuple&);
std::pair<ConstraintTree*, ConstraintTree*> split (
const LogVars&, ConstraintTree*, const LogVars&);
ConstraintTrees countNormalize (const LogVarSet&);
ConstraintTrees jointCountNormalize (
ConstraintTree*, ConstraintTree*, LogVar, LogVar, LogVar);
LogVars expand (LogVar);
ConstraintTrees ground (LogVar);
2012-12-18 23:51:51 +00:00
void cloneLogVar (LogVar, LogVar);
2012-12-17 18:39:42 +00:00
ConstraintTree& operator= (const ConstraintTree& ct);
2012-12-17 18:39:42 +00:00
2012-05-23 14:56:01 +01:00
private:
unsigned countTuples (const CTNode*) const;
CTNodes getNodesBelow (CTNode*) const;
CTNodes getNodesAtLevel (unsigned) const;
unsigned nrNodes (const CTNode* n) const;
void appendOnBottom (CTNode* n1, const CTChilds&);
void swapLogVar (LogVar);
2012-05-24 22:55:20 +01:00
bool join (CTNode*, const Tuple&, size_t, CTNode*);
2012-05-23 14:56:01 +01:00
void getTuples (CTNode*, Tuples, unsigned, Tuples&, CTNodes&) const;
2013-02-07 13:37:15 +00:00
std::vector<std::pair<CTNode*, unsigned>> countNormalize (
2012-05-23 14:56:01 +01:00
const CTNode*, unsigned);
static void split (CTNode*, CTNode*, CTChilds&, CTChilds&, unsigned);
2012-05-23 14:56:01 +01:00
CTNode* root_;
LogVars logVars_;
LogVarSet logVarSet_;
};
inline const LogVars&
ConstraintTree::logVars (void) const
{
assert (LogVarSet (logVars_) == logVarSet_);
return logVars_;
}
inline const LogVarSet&
ConstraintTree::logVarSet (void) const
{
assert (LogVarSet (logVars_) == logVarSet_);
return logVarSet_;
}
inline size_t
ConstraintTree::nrLogVars (void) const
{
assert (LogVarSet (logVars_) == logVarSet_);
2013-02-13 23:47:00 +00:00
return logVars_.size();
}
} // namespace Horus
2013-02-07 23:53:13 +00:00
2013-02-08 00:20:01 +00:00
#endif // YAP_PACKAGES_CLPBN_HORUS_CONSTRAINTTREE_H_
2012-05-23 14:56:01 +01:00