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.cpp

1171 lines
28 KiB
C++
Raw Normal View History

2012-03-22 11:33:24 +00:00
#include <queue>
2012-03-31 23:27:37 +01:00
#include <fstream>
2012-03-22 11:33:24 +00:00
#include "ConstraintTree.h"
#include "Util.h"
void
CTNode::mergeSubtree (CTNode* n, bool updateLevels)
2012-03-22 11:33:24 +00:00
{
if (updateLevels) {
updateChildLevels (n, level_ + 1);
}
CTChilds::iterator chIt = findChild (n);
if (chIt != childs_.end()) {
assert ((*chIt)->symbol() == n->symbol());
const CTChilds& childsToAdd = n->childs();
for (CTChilds::const_iterator it = childsToAdd.begin();
it != childsToAdd.end(); ++ it) {
(*chIt)->mergeSubtree (*it, false);
2012-03-22 11:33:24 +00:00
}
delete n;
2012-03-22 11:33:24 +00:00
} else {
childs_.insert (n);
2012-03-22 11:33:24 +00:00
}
}
void
CTNode::removeChild (CTNode* child)
{
CTChilds::iterator it;
it = findChild (child);
2012-03-22 11:33:24 +00:00
assert (it != childs_.end());
childs_.erase (it);
}
void
CTNode::removeChilds (void)
{
childs_.clear();
}
2012-03-31 23:27:37 +01:00
void
CTNode::removeAndDeleteChild (CTNode* child)
{
removeChild (child);
CTNode::deleteSubtree (child);
}
void
CTNode::removeAndDeleteAllChilds (void)
{
for (CTChilds::const_iterator chIt = childs_.begin();
2012-04-15 03:19:15 +01:00
chIt != childs_.end(); ++ chIt) {
deleteSubtree (*chIt);
2012-03-31 23:27:37 +01:00
}
childs_.clear();
}
2012-03-22 11:33:24 +00:00
SymbolSet
CTNode::childSymbols (void) const
{
SymbolSet symbols;
for (CTChilds::const_iterator chIt = childs_.begin();
2012-04-15 03:19:15 +01:00
chIt != childs_.end(); ++ chIt) {
symbols.insert ((*chIt)->symbol());
2012-03-22 11:33:24 +00:00
}
return symbols;
}
void
CTNode::updateChildLevels (CTNode* n, unsigned level)
{
/*
2012-03-22 11:33:24 +00:00
n->setLevel (level);
const CTChilds& childs = n->childs();
for (CTChilds::const_iterator chIt = childs.begin();
2012-04-15 03:19:15 +01:00
chIt != childs.end(); ++ chIt) {
updateChildLevels (*chIt, level + 1);
2012-03-22 11:33:24 +00:00
}
*/
CTNodes stack;
stack.push_back (n);
n->setLevel (level);
while (stack.empty() == false) {
CTNode* node = stack.back();
stack.pop_back();
for (CTChilds::const_iterator chIt = node->childs().begin();
chIt != node->childs().end(); ++ chIt) {
(*chIt)->setLevel (node->level() + 1);
}
stack.insert (stack.end(), node->childs().begin(),
node->childs().end());
}
2012-03-22 11:33:24 +00:00
}
2012-03-31 23:27:37 +01:00
CTNode*
CTNode::copySubtree (const CTNode* root1)
2012-03-31 23:27:37 +01:00
{
if (root1->childs().empty()) {
return new CTNode (*root1);
}
CTNode* root2 = new CTNode (*root1);
typedef pair<const CTNode*, CTNode*> StackPair;
vector<StackPair> stack = { StackPair (root1, root2) };
while (stack.empty() == false) {
const CTNode* n1 = stack.back().first;
CTNode* n2 = stack.back().second;
stack.pop_back();
n2->childs().reserve (n1->nrChilds());
stack.reserve (n1->nrChilds());
for (CTChilds::const_iterator chIt = n1->childs().begin();
chIt != n1->childs().end(); ++ chIt) {
CTNode* chCopy = new CTNode (**chIt);
n2->childs().push_back (chCopy);
if ((*chIt)->nrChilds() != 0) {
stack.push_back (StackPair (*chIt, chCopy));
}
}
}
return root2;
/*
CTNode* newNode = new CTNode (*root1);
const CTChilds& childs = root1->childs();
newNode->childs().reserve (childs.size());
for (CTChilds::const_iterator chIt = childs.begin();
chIt != childs.end(); ++ chIt) {
newNode->childs().push_back ((copySubtree (*chIt)));
2012-03-31 23:27:37 +01:00
}
return newNode;
*/
2012-03-31 23:27:37 +01:00
}
void
CTNode::deleteSubtree (CTNode* n)
{
assert (n);
const CTChilds& childs = n->childs();
for (CTChilds::const_iterator chIt = childs.begin();
2012-04-15 03:19:15 +01:00
chIt != childs.end(); ++ chIt) {
deleteSubtree (*chIt);
2012-03-31 23:27:37 +01:00
}
delete n;
}
2012-03-22 11:33:24 +00:00
ostream& operator<< (ostream &out, const CTNode& n)
{
out << "(" << n.level() << ") " ;
2012-03-22 11:33:24 +00:00
out << n.symbol();
return out;
}
2012-03-31 23:27:37 +01:00
ConstraintTree::ConstraintTree (unsigned nrLvs)
{
for (unsigned i = 0; i < nrLvs; i++) {
logVars_.push_back (LogVar (i));
}
root_ = new CTNode (0, 0);
logVarSet_ = LogVarSet (logVars_);
}
2012-03-22 11:33:24 +00:00
ConstraintTree::ConstraintTree (const LogVars& logVars)
{
root_ = new CTNode (0, 0);
logVars_ = logVars;
logVarSet_ = LogVarSet (logVars);
}
ConstraintTree::ConstraintTree (
const LogVars& logVars,
const Tuples& tuples)
2012-03-22 11:33:24 +00:00
{
root_ = new CTNode (0, 0);
logVars_ = logVars;
logVarSet_ = LogVarSet (logVars);
for (unsigned i = 0; i < tuples.size(); i++) {
addTuple (tuples[i]);
}
}
ConstraintTree::ConstraintTree (const ConstraintTree& ct)
{
2012-03-31 23:27:37 +01:00
root_ = CTNode::copySubtree (ct.root_);
2012-03-22 11:33:24 +00:00
logVars_ = ct.logVars_;
logVarSet_ = ct.logVarSet_;
}
ConstraintTree::~ConstraintTree (void)
{
2012-03-31 23:27:37 +01:00
CTNode::deleteSubtree (root_);
2012-03-22 11:33:24 +00:00
}
void
ConstraintTree::addTuple (const Tuple& tuple)
{
CTNode* prevNode = root_;
for (unsigned i = 0; i < tuple.size(); i++) {
CTChilds::const_iterator it = prevNode->findSymbol (tuple[i]);
if (it == prevNode->childs().end()) {
CTNode* newNode = new CTNode (tuple[i], i + 1);
prevNode->mergeSubtree (newNode, false);
prevNode = newNode;
} else {
prevNode = *it;
}
}
2012-03-22 11:33:24 +00:00
}
bool
ConstraintTree::containsTuple (const Tuple& tuple)
{
CTNode* prevNode = root_;
for (unsigned i = 0; i < tuple.size(); i++) {
CTChilds::const_iterator it = prevNode->findSymbol (tuple[i]);
if (it == prevNode->childs().end()) {
return false;
} else {
prevNode = *it;
2012-03-22 11:33:24 +00:00
}
}
return true;
2012-03-22 11:33:24 +00:00
}
void
ConstraintTree::moveToTop (const LogVars& lvs)
{
for (unsigned i = 0; i < lvs.size(); i++) {
LogVars::iterator it =
std::find (logVars_.begin(), logVars_.end(), lvs[i]);
assert (it != logVars_.end());
unsigned pos = std::distance (logVars_.begin(), it);
for (unsigned j = pos; j > i; j--) {
swapLogVar (logVars_[j-1]);
}
}
}
void
ConstraintTree::moveToBottom (const LogVars& lvs)
{
for (int i = lvs.size() - 1; i >= 0; i--) {
LogVars::iterator it =
std::find (logVars_.begin(), logVars_.end(), lvs[i]);
assert (it != logVars_.end());
unsigned pos = std::distance (logVars_.begin(), it);
unsigned stop = logVars_.size() - (lvs.size() - i - 1);
for (unsigned j = pos; j < stop - 1; j++) {
swapLogVar (logVars_[j]);
}
}
}
void
ConstraintTree::join (ConstraintTree* ct, bool oneTwoOne)
{
2012-03-31 23:27:37 +01:00
if (logVarSet_.empty()) {
CTNode::deleteSubtree (root_);
2012-03-31 23:27:37 +01:00
root_ = CTNode::copySubtree (ct->root());
logVars_ = ct->logVars();
logVarSet_ = ct->logVarSet();
return;
}
if (oneTwoOne) {
if (logVarSet_.contains (ct->logVarSet())) {
return;
}
if (ct->logVarSet().contains (logVarSet_)) {
CTNode::deleteSubtree (root_);
root_ = CTNode::copySubtree (ct->root());
logVars_ = ct->logVars();
logVarSet_ = ct->logVarSet();
return;
}
}
2012-03-22 11:33:24 +00:00
LogVarSet intersect = logVarSet_ & ct->logVarSet_;
if (intersect.empty()) {
// carteesian product
appendOnBottom (root_, ct->root()->childs());
2012-03-31 23:27:37 +01:00
Util::addToVector (logVars_, ct->logVars_);
2012-03-22 11:33:24 +00:00
logVarSet_ |= ct->logVarSet_;
} else {
moveToTop (intersect.elements());
2012-03-22 11:33:24 +00:00
ct->moveToTop (intersect.elements());
Tuples tuples;
CTNodes appendNodes;
getTuples (ct->root(), Tuples(), intersect.size(),
tuples, appendNodes);
CTNodes::const_iterator appendIt = appendNodes.begin();
for (unsigned i = 0; i < tuples.size(); ++ i, ++ appendIt) {
bool tupleFounded = join (root_, tuples[i], 0, *appendIt);
if (oneTwoOne) {
2012-03-22 11:33:24 +00:00
assert (tupleFounded);
tupleFounded = true; // hack to avoid gcc warning
2012-03-22 11:33:24 +00:00
}
}
2012-03-31 23:27:37 +01:00
LogVars newLvs (ct->logVars().begin() + intersect.size(),
ct->logVars().end());
Util::addToVector (logVars_, newLvs);
logVarSet_ |= LogVarSet (newLvs);
2012-03-22 11:33:24 +00:00
}
}
unsigned
ConstraintTree::getLevel (LogVar X) const
{
LogVars::const_iterator it =
std::find (logVars_.begin(), logVars_.end(), X);
assert (it != logVars_.end());
unsigned level = std::distance (logVars_.begin(), it);
level += 1; // root is in level 0, first logVar is in level 1
return level;
}
void
ConstraintTree::rename (LogVar X_old, LogVar X_new)
{
assert (logVarSet_.contains (X_old));
assert (logVarSet_.contains (X_new) == false);
logVarSet_ -= X_old;
logVarSet_ |= X_new;
for (unsigned i = 0; i < logVars_.size(); i++) {
if (logVars_[i] == X_old) {
logVars_[i] = X_new;
return;
}
}
assert (false);
}
void
ConstraintTree::applySubstitution (const Substitution& theta)
{
2012-04-03 11:58:21 +01:00
LogVars discardedLvs = theta.getDiscardedLogVars();
for (unsigned i = 0; i < discardedLvs.size(); i++) {
remove(discardedLvs[i]);
}
2012-03-22 11:33:24 +00:00
for (unsigned i = 0; i < logVars_.size(); i++) {
logVars_[i] = theta.newNameFor (logVars_[i]);
}
logVarSet_ = LogVarSet (logVars_);
}
void
ConstraintTree::project (const LogVarSet& X)
{
assert (logVarSet_.contains (X));
remove ((logVarSet_ - X));
}
void
ConstraintTree::remove (const LogVarSet& X)
{
assert (logVarSet_.contains (X));
if (X.empty()) {
return;
}
moveToBottom (X.elements());
unsigned level = getLevel (X.front()) - 1;
CTNodes nodes = getNodesAtLevel (level);
2012-04-15 03:19:15 +01:00
for (CTNodes::const_iterator it = nodes.begin();
it != nodes.end(); ++ it) {
(*it)->removeAndDeleteAllChilds();
2012-03-22 11:33:24 +00:00
}
logVars_.resize (logVars_.size() - X.size());
logVarSet_ -= X;
}
bool
ConstraintTree::ConstraintTree::isSingleton (LogVar X)
{
/*
const CTNodes& nodes = getNodesAtLevel (getLevel (X));
Symbol symb = nodes.front()->symbol();
2012-04-15 03:19:15 +01:00
for (CTNodes::const_iterator it = nodes.begin();
it != nodes.end(); ++ it) {
if ((*it)->symbol() != symb) {
return false;
}
2012-03-22 11:33:24 +00:00
}
return true;
*/
Symbol symb;
unsigned level = getLevel (X);
CTNodes stack;
stack.push_back (root_);
while (stack.empty() == false) {
CTNode* node = stack.back();
stack.pop_back();
if (node->level() == level) {
if (symb.valid()) {
if (node->symbol() != symb) {
return false;
}
} else {
symb = node->symbol();
}
} else {
stack.insert (stack.end(), node->childs().begin(),
node->childs().end());
}
}
return true;
2012-03-22 11:33:24 +00:00
}
LogVarSet
ConstraintTree::singletons (void)
{
LogVarSet singletons;
for (unsigned i = 0; i < logVars_.size(); i++) {
if (isSingleton (logVars_[i])) {
singletons.insert (logVars_[i]);
}
}
return singletons;
}
TupleSet
ConstraintTree::tupleSet (unsigned stopLevel) const
{
assert (root_->isRoot());
Tuples tuples;
if (stopLevel == 0) {
stopLevel = logVars_.size();
}
getTuples (root_, Tuples(), stopLevel, tuples, CTNodes() = {});
return TupleSet (tuples);
}
TupleSet
ConstraintTree::tupleSet (const LogVars& lvs)
{
Tuples tuples;
moveToTop (lvs);
unsigned stopLevel = lvs.size();
getTuples (root_, Tuples(), stopLevel, tuples, CTNodes() = {});
return TupleSet (tuples);
}
void
ConstraintTree::exportToGraphViz (
const char* fileName,
bool showLogVars) const
{
ofstream out (fileName);
if (!out.is_open()) {
cerr << "error: cannot open file to write at " ;
cerr << "ConstraintTree::exportToDotFile()" << endl;
abort();
}
out << "digraph {" << endl;
ConstraintTree copy (*this);
// copy.moveToTop (copy.logVarSet_.elements());
CTNodes nodes = getNodesBelow (copy.root_);
out << "\"" << copy.root_ << "\"" << " [label=\"R\"]" << endl;
for (CTNodes::const_iterator it = ++ nodes.begin();
2012-04-15 03:19:15 +01:00
it != nodes.end(); ++ it) {
out << "\"" << *it << "\"";
out << " [label=\"" << **it << "\"]" ;
2012-03-22 11:33:24 +00:00
out << endl;
}
2012-04-15 03:19:15 +01:00
for (CTNodes::const_iterator it = nodes.begin();
it != nodes.end(); ++ it) {
const CTChilds& childs = (*it)->childs();
for (CTChilds::const_iterator chIt = childs.begin();
2012-04-15 03:19:15 +01:00
chIt != childs.end(); ++ chIt) {
out << "\"" << *it << "\"" ;
2012-03-22 11:33:24 +00:00
out << " -> " ;
2012-04-15 03:19:15 +01:00
out << "\"" << *chIt << "\"" << endl ;
2012-03-22 11:33:24 +00:00
}
}
if (showLogVars) {
out << "Root [label=\"\", shape=plaintext]" << endl;
for (unsigned i = 0; i < copy.logVars_.size(); i++) {
out << copy.logVars_[i] << " [label=" ;
out << copy.logVars_[i] << ", " ;
out << "shape=plaintext, fontsize=14]" << endl;
}
out << "Root -> " << copy.logVars_[0];
out << " [style=invis]" << endl;
for (unsigned i = 0; i < copy.logVars_.size() - 1; i++) {
out << copy.logVars_[i] << " -> " << copy.logVars_[i + 1];
out << " [style=invis]" << endl;
}
}
out << "}" << endl;
out.close();
}
bool
ConstraintTree::isCountNormalized (const LogVarSet& Ys)
{
assert (logVarSet_.contains (Ys));
if (Ys.empty()) {
return true;
}
if (Ys.size() == logVars_.size()) {
assert (LogVarSet (logVars_) == LogVarSet (Ys));
return true;
}
LogVarSet Zs = logVarSet_ - LogVarSet (Ys);
moveToTop (Zs.elements());
CTNodes nodes = getNodesAtLevel (Zs.size());
2012-04-15 03:19:15 +01:00
unsigned count = countTuples (*nodes.begin());
for (CTNodes::const_iterator it = nodes.begin();
it != nodes.end(); ++ it) {
if (countTuples (*it) != count) {
2012-03-22 11:33:24 +00:00
return false;
}
}
return true;
}
unsigned
ConstraintTree::getConditionalCount (const LogVarSet& Ys)
{
assert (isCountNormalized (Ys));
if (Ys.empty()) {
return 1;
}
if (Ys.size() == logVars_.size()) {
assert (LogVarSet (Ys) == LogVarSet (logVars_));
return countTuples (root_);
}
LogVarSet Zs = logVarSet_ - Ys;
moveToTop (Zs.elements());
CTNode* n = root_;
unsigned l = 0;
while (l != Zs.size()) {
2012-04-15 03:19:15 +01:00
n = *(n->childs().begin());
2012-03-22 11:33:24 +00:00
l ++;
}
return countTuples (n);
}
TinySet<unsigned>
ConstraintTree::getConditionalCounts (const LogVarSet& Ys)
{
TinySet<unsigned> counts;
assert (logVarSet_.contains (Ys));
if (Ys.empty()) {
counts.insert (1);
} else if (Ys.size() == logVars_.size()) {
assert (LogVarSet (logVars_) == LogVarSet (Ys));
counts.insert (countTuples (root_));
} else {
LogVarSet Zs = logVarSet_ - LogVarSet (Ys);
moveToTop (Zs.elements());
CTNodes nodes = getNodesAtLevel (Zs.size());
2012-04-15 03:19:15 +01:00
for (CTNodes::const_iterator it = nodes.begin();
it != nodes.end(); ++ it) {
counts.insert (countTuples (*it));
2012-03-22 11:33:24 +00:00
}
}
return counts;
}
bool
ConstraintTree::isCarteesianProduct (const LogVarSet& Xs) const
{
assert (logVarSet_.contains (Xs));
if (Xs.size() <= 1) {
return true;
}
for (unsigned i = 1; i < Xs.size(); i++) {
CTNodes nodes = getNodesAtLevel (i);
2012-03-22 11:33:24 +00:00
for (unsigned j = 1; j < nodes.size(); j++) {
if (nodes[j-1]->nrChilds() != nodes[ j ]->nrChilds()) {
return false;
}
if (nodes[j-1]->childSymbols() != nodes[ j ]->childSymbols()) {
return false;
}
}
}
return true;
}
std::pair<ConstraintTree*,ConstraintTree*>
ConstraintTree::split (
const Tuple& tuple,
unsigned stopLevel)
{
ConstraintTree tempCt (logVars_, {tuple});
return split (&tempCt, stopLevel);
}
std::pair<ConstraintTree*, ConstraintTree*>
ConstraintTree::split (
const ConstraintTree* ct,
unsigned stopLevel) const
{
assert (stopLevel <= logVars_.size());
assert (stopLevel <= ct->logVars_.size());
CTChilds commChilds, exclChilds;
split (root_, ct->root(), commChilds, exclChilds, stopLevel);
ConstraintTree* commCt = new ConstraintTree (commChilds, logVars_);
ConstraintTree* exclCt = new ConstraintTree (exclChilds, logVars_);
2012-03-31 23:27:37 +01:00
// cout << commCt->tupleSet() << " + " ;
// cout << exclCt->tupleSet() << " = " ;
// cout << tupleSet() << endl;
assert ((commCt->tupleSet() | exclCt->tupleSet()) == tupleSet());
assert ((exclCt->tupleSet (stopLevel) & ct->tupleSet (stopLevel)).empty());
2012-03-22 11:33:24 +00:00
return {commCt, exclCt};
}
ConstraintTrees
ConstraintTree::countNormalize (const LogVarSet& Ys)
{
assert (logVarSet_.contains (Ys));
LogVarSet Zs = logVarSet_ - Ys;
if (Ys.empty() || Zs.empty()) {
return { new ConstraintTree (*this) };
}
moveToTop (Zs.elements());
ConstraintTrees cts;
unordered_map<unsigned, ConstraintTree*> countMap;
unsigned stopLevel = getLevel (Zs.back());
const CTChilds& childs = root_->childs();
2012-03-22 11:33:24 +00:00
for (CTChilds::const_iterator chIt = childs.begin();
2012-04-15 03:19:15 +01:00
chIt != childs.end(); ++ chIt) {
const vector<pair<CTNode*, unsigned>>& res =
countNormalize (*chIt, stopLevel);
2012-03-22 11:33:24 +00:00
for (unsigned j = 0; j < res.size(); j++) {
unordered_map<unsigned, ConstraintTree*>::iterator it
= countMap.find (res[j].second);
if (it == countMap.end()) {
ConstraintTree* newCt = new ConstraintTree (logVars_);
it = countMap.insert (make_pair (res[j].second, newCt)).first;
cts.push_back (newCt);
}
it->second->root_->mergeSubtree (res[j].first);
2012-03-22 11:33:24 +00:00
}
}
return cts;
}
ConstraintTrees
ConstraintTree::jointCountNormalize (
ConstraintTree* commCt,
ConstraintTree* exclCt,
LogVar X,
LogVar X_new1,
LogVar X_new2)
{
unsigned N = getConditionalCount (X);
2012-03-31 23:27:37 +01:00
// cout << "My tuples: " << tupleSet() << endl;
// cout << "CommCt tuples: " << commCt->tupleSet() << endl;
// cout << "ExclCt tuples: " << exclCt->tupleSet() << endl;
// cout << "Counted Lv: " << X << endl;
// cout << "X_new1: " << X_new1 << endl;
// cout << "X_new2: " << X_new2 << endl;
// cout << "Original N: " << N << endl;
// cout << endl;
2012-03-22 11:33:24 +00:00
ConstraintTrees normCts1 = commCt->countNormalize (X);
vector<unsigned> counts1 (normCts1.size());
for (unsigned i = 0; i < normCts1.size(); i++) {
counts1[i] = normCts1[i]->getConditionalCount (X);
2012-03-31 23:27:37 +01:00
// cout << "normCts1[" << i << "] #" << counts1[i] ;
// cout << " " << normCts1[i]->tupleSet() << endl;
2012-03-22 11:33:24 +00:00
}
ConstraintTrees normCts2 = exclCt->countNormalize (X);
vector<unsigned> counts2 (normCts2.size());
for (unsigned i = 0; i < normCts2.size(); i++) {
counts2[i] = normCts2[i]->getConditionalCount (X);
2012-03-31 23:27:37 +01:00
// cout << "normCts2[" << i << "] #" << counts2[i] ;
// cout << " " << normCts2[i]->tupleSet() << endl;
2012-03-22 11:33:24 +00:00
}
2012-03-31 23:27:37 +01:00
// cout << endl;
2012-03-22 11:33:24 +00:00
ConstraintTree* excl1 = 0;
for (unsigned i = 0; i < normCts1.size(); i++) {
if (counts1[i] == N) {
excl1 = normCts1[i];
normCts1.erase (normCts1.begin() + i);
counts1.erase (counts1.begin() + i);
2012-03-31 23:27:37 +01:00
// cout << "joint-count(" << N << ",0)" << endl;
2012-03-22 11:33:24 +00:00
break;
}
}
ConstraintTree* excl2 = 0;
for (unsigned i = 0; i < normCts2.size(); i++) {
if (counts2[i] == N) {
excl2 = normCts2[i];
normCts2.erase (normCts2.begin() + i);
counts2.erase (counts2.begin() + i);
2012-03-31 23:27:37 +01:00
// cout << "joint-count(0," << N << ")" << endl;
2012-03-22 11:33:24 +00:00
break;
}
}
for (unsigned i = 0; i < normCts1.size(); i++) {
unsigned j;
for (j = 0; counts1[i] + counts2[j] != N; j++) ;
2012-03-31 23:27:37 +01:00
// cout << "joint-count(" << counts1[i] ;
// cout << "," << counts2[j] << ")" << endl;
const CTChilds& childs = normCts2[j]->root_->childs();
for (CTChilds::const_iterator chIt = childs.begin();
2012-04-15 03:19:15 +01:00
chIt != childs.end(); ++ chIt) {
normCts1[i]->root_->mergeSubtree (CTNode::copySubtree (*chIt));
2012-03-22 11:33:24 +00:00
}
2012-03-31 23:27:37 +01:00
delete normCts2[j];
2012-03-22 11:33:24 +00:00
}
ConstraintTrees cts = normCts1;
commCt->rename (X, X_new1);
exclCt->rename (X, X_new2);
for (unsigned i = 0; i < cts.size(); i++) {
cts[i]->remove (X);
cts[i]->join (commCt);
cts[i]->join (exclCt);
}
if (excl1 != 0) {
cts.push_back (excl1);
}
if (excl2 != 0) {
cts.push_back (excl2);
}
return cts;
}
bool
ConstraintTree::identical (
const ConstraintTree* ct1,
const ConstraintTree* ct2,
unsigned stopLevel)
{
TupleSet ts1 = ct1->tupleSet (stopLevel);
TupleSet ts2 = ct2->tupleSet (stopLevel);
return ts1 == ts2;
}
bool
ConstraintTree::disjoint (
2012-03-22 11:33:24 +00:00
const ConstraintTree* ct1,
const ConstraintTree* ct2,
unsigned stopLevel)
{
TupleSet ts1 = ct1->tupleSet (stopLevel);
TupleSet ts2 = ct2->tupleSet (stopLevel);
return (ts1 & ts2).empty();
2012-03-22 11:33:24 +00:00
}
LogVars
ConstraintTree::expand (LogVar X)
{
moveToBottom ({X});
assert (isCountNormalized (X));
CTNodes nodes = getNodesAtLevel (logVars_.size() - 1);
unsigned nrSymbols = getConditionalCount (X);
2012-04-15 03:19:15 +01:00
for (CTNodes::const_iterator it = nodes.begin();
it != nodes.end(); ++ it) {
2012-03-22 11:33:24 +00:00
Symbols symbols;
const CTChilds& childs = (*it)->childs();
for (CTChilds::const_iterator chIt = childs.begin();
2012-04-15 03:19:15 +01:00
chIt != childs.end(); ++ chIt) {
symbols.push_back ((*chIt)->symbol());
2012-03-22 11:33:24 +00:00
}
2012-04-15 03:19:15 +01:00
(*it)->removeAndDeleteAllChilds();
CTNode* prev = *it;
2012-03-22 11:33:24 +00:00
assert (symbols.size() == nrSymbols);
for (unsigned j = 0; j < nrSymbols; j++) {
2012-04-15 03:19:15 +01:00
CTNode* newNode = new CTNode (symbols[j], (*it)->level() + j);
prev->mergeSubtree (newNode);
2012-03-22 11:33:24 +00:00
prev = newNode;
}
}
LogVars newLvs;
logVars_.pop_back();
for (unsigned i = 0; i < nrSymbols; i++) {
logVars_.push_back (LogVar (logVarSet_.back() + 1));
newLvs.push_back (LogVar (logVarSet_.back() + 1));
logVarSet_.insert (LogVar (logVarSet_.back() + 1));
}
logVarSet_ -= X;
return newLvs;
}
ConstraintTrees
ConstraintTree::ground (LogVar X)
{
moveToTop ({X});
ConstraintTrees cts;
const CTChilds& nodes = root_->childs();
for (CTChilds::const_iterator it = nodes.begin();
2012-04-15 03:19:15 +01:00
it != nodes.end(); ++ it) {
CTNode* copy = CTNode::copySubtree (*it);
copy->setSymbol ((*it)->symbol());
2012-03-22 11:33:24 +00:00
ConstraintTree* newCt = new ConstraintTree (logVars_);
newCt->root()->mergeSubtree (copy);
2012-03-22 11:33:24 +00:00
cts.push_back (newCt);
}
return cts;
}
unsigned
ConstraintTree::countTuples (const CTNode* n) const
{
if (n->isLeaf()) {
return 1;
}
unsigned sum = 0;
const CTChilds& childs = n->childs();
for (CTChilds::const_iterator chIt = childs.begin();
2012-04-15 03:19:15 +01:00
chIt != childs.end(); ++ chIt) {
sum += countTuples (*chIt);
2012-03-22 11:33:24 +00:00
}
return sum;
}
CTNodes
ConstraintTree::getNodesBelow (CTNode* fromHere) const
{
CTNodes nodes;
queue<CTNode*> queue;
queue.push (fromHere);
while (queue.empty() == false) {
CTNode* node = queue.front();
nodes.push_back (node);
for (CTChilds::const_iterator chIt = node->childs().begin();
chIt != node->childs().end(); ++ chIt) {
queue.push (*chIt);
}
queue.pop();
}
return nodes;
}
CTNodes
ConstraintTree::getNodesAtLevel (unsigned level) const
2012-03-22 11:33:24 +00:00
{
assert (level <= logVars_.size());
if (level == 0) {
return { root_ };
}
/*
2012-03-22 11:33:24 +00:00
CTNodes nodes;
queue<CTNode*> queue;
queue.push (root_);
while (queue.empty() == false) {
CTNode* node = queue.front();
if (node->level() == level) {
nodes.push_back (node);
} else {
for (CTChilds::const_iterator chIt = node->childs().begin();
2012-04-15 03:19:15 +01:00
chIt != node->childs().end(); ++ chIt) {
queue.push (*chIt);
2012-03-22 11:33:24 +00:00
}
}
queue.pop();
}
*/
CTNodes stack;
CTNodes nodes;
stack.push_back (root_);
while (stack.empty() == false) {
CTNode* node = stack.back();
stack.pop_back();
if (node->level() + 1 == level) {
nodes.insert (nodes.end(), node->childs().begin(),
node->childs().end());
} else {
stack.insert (stack.end(), node->childs().begin(),
node->childs().end());
}
}
2012-03-22 11:33:24 +00:00
return nodes;
}
unsigned
ConstraintTree::nrNodes (const CTNode* n) const
{
unsigned nr = 0;
if (n->isLeaf() == false) {
for (CTChilds::const_iterator chIt = n->childs().begin();
chIt != n->childs().end(); ++ chIt) {
nr += nrNodes (*chIt);
}
}
return nr;
}
void
ConstraintTree::appendOnBottom (CTNode* n, const CTChilds& childs)
{
if (childs.empty()) {
return;
}
CTNodes stack { n };
while (stack.empty() == false) {
CTNode* node = stack.back();
stack.pop_back();
if (node->isLeaf()) {
for (CTChilds::const_iterator chIt = childs.begin();
chIt != childs.end(); ++ chIt) {
node->mergeSubtree (CTNode::copySubtree (*chIt));
}
} else {
stack.insert (stack.end(), node->childs().begin(),
node->childs().end());
}
}
}
2012-03-22 11:33:24 +00:00
void
ConstraintTree::swapLogVar (LogVar X)
{
2012-04-15 03:19:15 +01:00
LogVars::iterator it;
it = std::find (logVars_.begin(),logVars_.end(), X);
2012-03-22 11:33:24 +00:00
assert (it != logVars_.end());
unsigned pos = std::distance (logVars_.begin(), it);
const CTNodes& nodes = getNodesAtLevel (pos);
2012-04-15 03:19:15 +01:00
for (CTNodes::const_iterator nodeIt = nodes.begin();
nodeIt != nodes.end(); ++ nodeIt) {
CTChilds childsCopy = (*nodeIt)->childs();
2012-04-15 03:19:15 +01:00
(*nodeIt)->removeChilds();
for (CTChilds::const_iterator ccIt = childsCopy.begin();
2012-04-15 03:19:15 +01:00
ccIt != childsCopy.end(); ++ ccIt) {
const CTChilds& grandsons = (*ccIt)->childs();
for (CTChilds::const_iterator gsIt = grandsons.begin();
2012-04-15 03:19:15 +01:00
gsIt != grandsons.end(); ++ gsIt) {
CTNode* childCopy = new CTNode (
(*ccIt)->symbol(), (*ccIt)->level() + 1, (*gsIt)->childs());
2012-04-15 03:19:15 +01:00
(*gsIt)->removeChilds();
(*gsIt)->childs().push_back (childCopy);
2012-04-15 03:19:15 +01:00
(*gsIt)->setLevel ((*gsIt)->level() - 1);
(*nodeIt)->mergeSubtree ((*gsIt), false);
2012-03-22 11:33:24 +00:00
}
2012-04-15 03:19:15 +01:00
delete (*ccIt);
2012-03-22 11:33:24 +00:00
}
}
std::swap (logVars_[pos], logVars_[pos + 1]);
}
bool
ConstraintTree::join (
CTNode* currNode,
2012-03-22 11:33:24 +00:00
const Tuple& tuple,
unsigned currIdx,
CTNode* appendNode)
{
bool tupleFounded = false;
CTChilds::const_iterator it = currNode->findSymbol (tuple[currIdx]);
if (it != currNode->childs().end()) {
2012-03-22 11:33:24 +00:00
if (currIdx == tuple.size() - 1) {
2012-04-18 18:14:15 +01:00
appendOnBottom (*it, appendNode->childs());
2012-03-22 11:33:24 +00:00
return true;
} else {
tupleFounded = join (*it, tuple, currIdx + 1, appendNode);
2012-03-22 11:33:24 +00:00
}
}
2012-03-22 11:33:24 +00:00
return tupleFounded;
}
void
ConstraintTree::getTuples (
CTNode* n,
Tuples currTuples,
unsigned stopLevel,
Tuples& tuplesCollected,
CTNodes& continuationNodes) const
{
if (n->isRoot() == false) {
if (currTuples.size() == 0) {
currTuples.push_back ({ n->symbol()});
} else {
for (unsigned i = 0; i < currTuples.size(); i++) {
currTuples[i].push_back (n->symbol());
}
}
if (n->level() == stopLevel) {
for (unsigned i = 0; i < currTuples.size(); i++) {
tuplesCollected.push_back (currTuples[i]);
continuationNodes.push_back (n);
}
return;
}
}
const CTChilds& childs = n->childs();
for (CTChilds::const_iterator chIt = childs.begin();
2012-04-15 03:19:15 +01:00
chIt != childs.end(); ++ chIt) {
getTuples (*chIt, currTuples, stopLevel, tuplesCollected,
continuationNodes);
2012-03-22 11:33:24 +00:00
}
}
unsigned
ConstraintTree::size (void) const
{
return countTuples (root_);
}
unsigned
ConstraintTree::nrSymbols (LogVar X)
{
moveToTop ({X});
return root_->childs().size();
}
vector<pair<CTNode*, unsigned>>
ConstraintTree::countNormalize (
const CTNode* n,
unsigned stopLevel)
{
if (n->level() == stopLevel) {
return vector<pair<CTNode*, unsigned>>() = {
2012-03-31 23:27:37 +01:00
make_pair (CTNode::copySubtree (n), countTuples (n))
2012-03-22 11:33:24 +00:00
};
}
vector<pair<CTNode*, unsigned>> res;
const CTChilds& childs = n->childs();
for (CTChilds::const_iterator chIt = childs.begin();
2012-04-15 03:19:15 +01:00
chIt != childs.end(); ++ chIt) {
2012-03-22 11:33:24 +00:00
const vector<pair<CTNode*, unsigned>>& lowerRes =
2012-04-15 03:19:15 +01:00
countNormalize (*chIt, stopLevel);
2012-03-22 11:33:24 +00:00
for (unsigned j = 0; j < lowerRes.size(); j++) {
CTNode* newNode = new CTNode (*n);
newNode->mergeSubtree (lowerRes[j].first);
2012-03-22 11:33:24 +00:00
res.push_back (make_pair (newNode, lowerRes[j].second));
}
}
return res;
}
void
ConstraintTree::split (
CTNode* n1,
CTNode* n2,
CTChilds& commChilds,
CTChilds& exclChilds,
2012-03-22 11:33:24 +00:00
unsigned stopLevel)
{
CTChilds& childs1 = n1->childs();
for (CTChilds::const_iterator chIt1 = childs1.begin();
2012-04-15 03:19:15 +01:00
chIt1 != childs1.end(); ++ chIt1) {
CTChilds::iterator chIt2 = n2->findSymbol ((*chIt1)->symbol());
if (chIt2 == n2->childs().end()) {
exclChilds.push_back (CTNode::copySubtree (*chIt1));
2012-03-22 11:33:24 +00:00
} else {
if ((*chIt1)->level() == stopLevel) {
commChilds.push_back (CTNode::copySubtree (*chIt1));
} else {
CTChilds lowerCommChilds, lowerExclChilds;
split (*chIt1, *chIt2, lowerCommChilds, lowerExclChilds, stopLevel);
if (lowerCommChilds.empty() == false) {
commChilds.push_back (new CTNode (**chIt1, lowerCommChilds));
}
if (lowerExclChilds.empty() == false) {
exclChilds.push_back (new CTNode (**chIt1, lowerExclChilds));
2012-03-22 11:33:24 +00:00
}
}
}
}
}