2013-02-07 20:09:10 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
#include "Parfactor.h"
|
|
|
|
#include "Histogram.h"
|
|
|
|
#include "Indexer.h"
|
|
|
|
#include "Util.h"
|
|
|
|
#include "Horus.h"
|
|
|
|
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
namespace Horus {
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
Parfactor::Parfactor (
|
|
|
|
const ProbFormulas& formulas,
|
2012-12-20 23:19:10 +00:00
|
|
|
const Params& params,
|
2012-05-23 14:56:01 +01:00
|
|
|
const Tuples& tuples,
|
|
|
|
unsigned distId)
|
|
|
|
{
|
|
|
|
args_ = formulas;
|
|
|
|
params_ = params;
|
|
|
|
distId_ = distId;
|
|
|
|
|
|
|
|
LogVars logVars;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
ranges_.push_back (args_[i].range());
|
|
|
|
const LogVars& lvs = args_[i].logVars();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t j = 0; j < lvs.size(); j++) {
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Util::contains (logVars, lvs[j]) == false) {
|
2012-05-23 14:56:01 +01:00
|
|
|
logVars.push_back (lvs[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-18 23:51:51 +00:00
|
|
|
LogVar newLv = logVars.size();
|
2012-05-23 14:56:01 +01:00
|
|
|
constr_ = new ConstraintTree (logVars, tuples);
|
2012-12-18 23:51:51 +00:00
|
|
|
// Change formulas like f(X,X), X in {(p1),(p2),...}
|
|
|
|
// to be like f(X,Y), (X,Y) in {(p1,p1),(p2,p2),...}.
|
|
|
|
// This will simplify shattering on the constraint tree.
|
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
|
|
|
LogVarSet lvSet;
|
|
|
|
LogVars& lvs = args_[i].logVars();
|
|
|
|
for (size_t j = 0; j < lvs.size(); j++) {
|
|
|
|
if (lvSet.contains (lvs[j]) == false) {
|
|
|
|
lvSet |= lvs[j];
|
|
|
|
} else {
|
|
|
|
constr_->cloneLogVar (lvs[j], newLv);
|
|
|
|
lvs[j] = newLv;
|
|
|
|
++ newLv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 21:01:53 +00:00
|
|
|
assert (params_.size() == Util::sizeExpected (ranges_));
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parfactor::Parfactor (const Parfactor* g, const Tuple& tuple)
|
|
|
|
{
|
|
|
|
args_ = g->arguments();
|
|
|
|
params_ = g->params();
|
|
|
|
ranges_ = g->ranges();
|
|
|
|
distId_ = g->distId();
|
|
|
|
constr_ = new ConstraintTree (g->logVars(), {tuple});
|
2013-02-08 21:01:53 +00:00
|
|
|
assert (params_.size() == Util::sizeExpected (ranges_));
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parfactor::Parfactor (const Parfactor* g, ConstraintTree* constr)
|
|
|
|
{
|
|
|
|
args_ = g->arguments();
|
|
|
|
params_ = g->params();
|
|
|
|
ranges_ = g->ranges();
|
|
|
|
distId_ = g->distId();
|
|
|
|
constr_ = constr;
|
2013-02-08 21:01:53 +00:00
|
|
|
assert (params_.size() == Util::sizeExpected (ranges_));
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parfactor::Parfactor (const Parfactor& g)
|
|
|
|
{
|
|
|
|
args_ = g.arguments();
|
|
|
|
params_ = g.params();
|
|
|
|
ranges_ = g.ranges();
|
|
|
|
distId_ = g.distId();
|
|
|
|
constr_ = new ConstraintTree (*g.constr());
|
2013-02-08 21:01:53 +00:00
|
|
|
assert (params_.size() == Util::sizeExpected (ranges_));
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
Parfactor::~Parfactor()
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
delete constr_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogVarSet
|
2013-02-28 19:45:37 +00:00
|
|
|
Parfactor::countedLogVars() const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
LogVarSet set;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (args_[i].isCounting()) {
|
|
|
|
set.insert (args_[i].countedLogVar());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogVarSet
|
2013-02-28 19:45:37 +00:00
|
|
|
Parfactor::uncountedLogVars() const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
return constr_->logVarSet() - countedLogVars();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogVarSet
|
2013-02-28 19:45:37 +00:00
|
|
|
Parfactor::elimLogVars() const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
LogVarSet requiredToElim = constr_->logVarSet();
|
|
|
|
requiredToElim -= constr_->singletons();
|
|
|
|
requiredToElim -= countedLogVars();
|
|
|
|
return requiredToElim;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogVarSet
|
2012-05-24 22:55:20 +01:00
|
|
|
Parfactor::exclusiveLogVars (size_t fIdx) const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
assert (fIdx < args_.size());
|
|
|
|
LogVarSet remaining;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (i != fIdx) {
|
|
|
|
remaining |= args_[i].logVarSet();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args_[fIdx].logVarSet() - remaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-05-25 20:15:05 +01:00
|
|
|
Parfactor::sumOutIndex (size_t fIdx)
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
assert (fIdx < args_.size());
|
|
|
|
assert (args_[fIdx].contains (elimLogVars()));
|
|
|
|
|
|
|
|
if (args_[fIdx].isCounting()) {
|
|
|
|
unsigned N = constr_->getConditionalCount (
|
|
|
|
args_[fIdx].countedLogVar());
|
|
|
|
unsigned R = args_[fIdx].range();
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<double> numAssigns = HistogramSet::getNumAssigns (N, R);
|
2012-05-25 21:16:08 +01:00
|
|
|
Indexer indexer (ranges_, fIdx);
|
2012-05-25 20:15:05 +01:00
|
|
|
while (indexer.valid()) {
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::logDomain) {
|
2012-05-25 20:15:05 +01:00
|
|
|
params_[indexer] += numAssigns[ indexer[fIdx] ];
|
2012-05-23 14:56:01 +01:00
|
|
|
} else {
|
2012-05-25 20:15:05 +01:00
|
|
|
params_[indexer] *= numAssigns[ indexer[fIdx] ];
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
++ indexer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LogVarSet excl = exclusiveLogVars (fIdx);
|
2012-05-25 20:15:05 +01:00
|
|
|
unsigned exp;
|
2012-05-23 14:56:01 +01:00
|
|
|
if (args_[fIdx].isCounting()) {
|
|
|
|
// counting log vars were already raised on counting conversion
|
2012-05-25 20:15:05 +01:00
|
|
|
exp = constr_->getConditionalCount (excl - args_[fIdx].countedLogVar());
|
2012-05-23 14:56:01 +01:00
|
|
|
} else {
|
2012-05-25 20:15:05 +01:00
|
|
|
exp = constr_->getConditionalCount (excl);
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
constr_->remove (excl);
|
|
|
|
|
2013-03-04 15:47:47 +00:00
|
|
|
GenericFactor<ProbFormula>::sumOutIndex (fIdx);
|
2013-02-08 21:01:53 +00:00
|
|
|
LogAware::pow (params_, exp);
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::multiply (Parfactor& g)
|
|
|
|
{
|
|
|
|
alignAndExponentiate (this, &g);
|
2013-03-04 15:47:47 +00:00
|
|
|
GenericFactor<ProbFormula>::multiply (g);
|
2012-05-23 14:56:01 +01:00
|
|
|
constr_->join (g.constr(), true);
|
|
|
|
simplifyGrounds();
|
|
|
|
assert (constr_->isCartesianProduct (countedLogVars()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
Parfactor::canCountConvert (LogVar X)
|
|
|
|
{
|
|
|
|
if (nrFormulas (X) != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-24 22:55:20 +01:00
|
|
|
size_t fIdx = indexOfLogVar (X);
|
2012-05-23 14:56:01 +01:00
|
|
|
if (args_[fIdx].isCounting()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (constr_->isCountNormalized (X) == false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (constr_->getConditionalCount (X) == 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (constr_->isCartesianProduct (countedLogVars() | X) == false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::countConvert (LogVar X)
|
|
|
|
{
|
2012-05-24 22:55:20 +01:00
|
|
|
size_t fIdx = indexOfLogVar (X);
|
2012-05-23 14:56:01 +01:00
|
|
|
assert (constr_->isCountNormalized (X));
|
|
|
|
assert (constr_->getConditionalCount (X) > 1);
|
|
|
|
assert (canCountConvert (X));
|
2012-12-20 23:19:10 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
unsigned N = constr_->getConditionalCount (X);
|
|
|
|
unsigned R = ranges_[fIdx];
|
|
|
|
unsigned H = HistogramSet::nrHistograms (N, R);
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<Histogram> histograms = HistogramSet::getHistograms (N, R);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2012-05-25 21:16:08 +01:00
|
|
|
Indexer indexer (ranges_);
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<Params> sumout (params_.size() / R);
|
2012-05-23 14:56:01 +01:00
|
|
|
unsigned count = 0;
|
|
|
|
while (indexer.valid()) {
|
|
|
|
sumout[count].reserve (R);
|
|
|
|
for (unsigned r = 0; r < R; r++) {
|
|
|
|
sumout[count].push_back (params_[indexer]);
|
2012-05-25 21:16:08 +01:00
|
|
|
indexer.incrementDimension (fIdx);
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
count ++;
|
2012-05-25 21:16:08 +01:00
|
|
|
indexer.resetDimension (fIdx);
|
|
|
|
indexer.incrementExceptDimension (fIdx);
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
params_.clear();
|
|
|
|
params_.reserve (sumout.size() * H);
|
|
|
|
|
|
|
|
ranges_[fIdx] = H;
|
2012-05-28 12:32:15 +01:00
|
|
|
MapIndexer mapIndexer (ranges_, fIdx);
|
|
|
|
while (mapIndexer.valid()) {
|
2013-02-08 21:01:53 +00:00
|
|
|
double prod = LogAware::multIdenty();
|
2012-05-28 12:32:15 +01:00
|
|
|
size_t i = mapIndexer;
|
|
|
|
unsigned h = mapIndexer[fIdx];
|
2012-05-23 14:56:01 +01:00
|
|
|
for (unsigned r = 0; r < R; r++) {
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::logDomain) {
|
|
|
|
prod += LogAware::pow (sumout[i][r], histograms[h][r]);
|
2012-05-23 14:56:01 +01:00
|
|
|
} else {
|
2013-02-08 21:01:53 +00:00
|
|
|
prod *= LogAware::pow (sumout[i][r], histograms[h][r]);
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
params_.push_back (prod);
|
2012-05-28 12:32:15 +01:00
|
|
|
++ mapIndexer;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
args_[fIdx].setCountedLogVar (X);
|
|
|
|
simplifyCountingFormulas (fIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::expand (LogVar X, LogVar X_new1, LogVar X_new2)
|
|
|
|
{
|
2012-05-24 22:55:20 +01:00
|
|
|
size_t fIdx = indexOfLogVar (X);
|
|
|
|
assert (fIdx != args_.size());
|
2012-05-23 14:56:01 +01:00
|
|
|
assert (args_[fIdx].isCounting());
|
|
|
|
|
|
|
|
unsigned N1 = constr_->getConditionalCount (X_new1);
|
|
|
|
unsigned N2 = constr_->getConditionalCount (X_new2);
|
|
|
|
unsigned N = N1 + N2;
|
|
|
|
unsigned R = args_[fIdx].range();
|
|
|
|
unsigned H1 = HistogramSet::nrHistograms (N1, R);
|
|
|
|
unsigned H2 = HistogramSet::nrHistograms (N2, R);
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<Histogram> histograms = HistogramSet::getHistograms (N, R);
|
|
|
|
std::vector<Histogram> histograms1 = HistogramSet::getHistograms (N1, R);
|
|
|
|
std::vector<Histogram> histograms2 = HistogramSet::getHistograms (N2, R);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<unsigned> sumIndexes;
|
2012-05-23 14:56:01 +01:00
|
|
|
sumIndexes.reserve (H1 * H2);
|
|
|
|
for (unsigned i = 0; i < H1; i++) {
|
|
|
|
for (unsigned j = 0; j < H2; j++) {
|
|
|
|
Histogram hist = histograms1[i];
|
2012-05-24 22:55:20 +01:00
|
|
|
hist += histograms2[j];
|
2012-05-23 14:56:01 +01:00
|
|
|
sumIndexes.push_back (HistogramSet::findIndex (hist, histograms));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expandPotential (fIdx, H1 * H2, sumIndexes);
|
|
|
|
|
|
|
|
args_.insert (args_.begin() + fIdx + 1, args_[fIdx]);
|
|
|
|
args_[fIdx].rename (X, X_new1);
|
|
|
|
args_[fIdx + 1].rename (X, X_new2);
|
|
|
|
if (H1 == 2) {
|
|
|
|
args_[fIdx].clearCountedLogVar();
|
|
|
|
}
|
|
|
|
if (H2 == 2) {
|
|
|
|
args_[fIdx + 1].clearCountedLogVar();
|
|
|
|
}
|
|
|
|
ranges_.insert (ranges_.begin() + fIdx + 1, H2);
|
|
|
|
ranges_[fIdx] = H1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::fullExpand (LogVar X)
|
|
|
|
{
|
2012-05-24 22:55:20 +01:00
|
|
|
size_t fIdx = indexOfLogVar (X);
|
|
|
|
assert (fIdx != args_.size());
|
2012-05-23 14:56:01 +01:00
|
|
|
assert (args_[fIdx].isCounting());
|
|
|
|
|
|
|
|
unsigned N = constr_->getConditionalCount (X);
|
|
|
|
unsigned R = args_[fIdx].range();
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<Histogram> originHists = HistogramSet::getHistograms (N, R);
|
|
|
|
std::vector<Histogram> expandHists = HistogramSet::getHistograms (1, R);
|
2012-05-23 14:56:01 +01:00
|
|
|
assert (ranges_[fIdx] == originHists.size());
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<unsigned> sumIndexes;
|
2012-05-23 14:56:01 +01:00
|
|
|
sumIndexes.reserve (N * R);
|
|
|
|
|
|
|
|
Ranges expandRanges (N, R);
|
2012-05-25 21:16:08 +01:00
|
|
|
Indexer indexer (expandRanges);
|
2012-05-23 14:56:01 +01:00
|
|
|
while (indexer.valid()) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<unsigned> hist (R, 0);
|
2012-05-23 14:56:01 +01:00
|
|
|
for (unsigned n = 0; n < N; n++) {
|
2012-05-24 22:55:20 +01:00
|
|
|
hist += expandHists[indexer[n]];
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
sumIndexes.push_back (HistogramSet::findIndex (hist, originHists));
|
|
|
|
++ indexer;
|
|
|
|
}
|
2012-12-20 23:19:10 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
expandPotential (fIdx, std::pow (R, N), sumIndexes);
|
|
|
|
|
|
|
|
ProbFormula f = args_[fIdx];
|
|
|
|
args_.erase (args_.begin() + fIdx);
|
|
|
|
ranges_.erase (ranges_.begin() + fIdx);
|
|
|
|
LogVars newLvs = constr_->expand (X);
|
|
|
|
assert (newLvs.size() == N);
|
|
|
|
for (unsigned i = 0 ; i < N; i++) {
|
|
|
|
ProbFormula newFormula (f.functor(), f.logVars(), f.range());
|
|
|
|
newFormula.rename (X, newLvs[i]);
|
|
|
|
args_.insert (args_.begin() + fIdx + i, newFormula);
|
|
|
|
ranges_.insert (ranges_.begin() + fIdx + i, R);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::reorderAccordingGrounds (const Grounds& grounds)
|
|
|
|
{
|
|
|
|
ProbFormulas newFormulas;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < grounds.size(); i++) {
|
|
|
|
for (size_t j = 0; j < args_.size(); j++) {
|
2012-12-20 23:19:10 +00:00
|
|
|
if (grounds[i].functor() == args_[j].functor() &&
|
2012-05-23 14:56:01 +01:00
|
|
|
grounds[i].arity() == args_[j].arity()) {
|
|
|
|
constr_->moveToTop (args_[j].logVars());
|
|
|
|
if (constr_->containsTuple (grounds[i].args())) {
|
|
|
|
newFormulas.push_back (args_[j]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert (newFormulas.size() == i + 1);
|
|
|
|
}
|
|
|
|
reorderArguments (newFormulas);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::absorveEvidence (const ProbFormula& formula, unsigned evidence)
|
|
|
|
{
|
2012-05-24 22:55:20 +01:00
|
|
|
size_t fIdx = indexOf (formula);
|
|
|
|
assert (fIdx != args_.size());
|
2012-05-23 14:56:01 +01:00
|
|
|
LogVarSet excl = exclusiveLogVars (fIdx);
|
|
|
|
assert (args_[fIdx].isCounting() == false);
|
|
|
|
assert (constr_->isCountNormalized (excl));
|
2013-02-08 21:01:53 +00:00
|
|
|
LogAware::pow (params_, constr_->getConditionalCount (excl));
|
2013-03-04 15:47:47 +00:00
|
|
|
GenericFactor<ProbFormula>::absorveEvidence (formula, evidence);
|
2012-05-23 14:56:01 +01:00
|
|
|
constr_->remove (excl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
Parfactor::setNewGroups()
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
args_[i].setGroup (ProbFormula::getNewGroup());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::applySubstitution (const Substitution& theta)
|
|
|
|
{
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
LogVars& lvs = args_[i].logVars();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t j = 0; j < lvs.size(); j++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
lvs[j] = theta.newNameFor (lvs[j]);
|
|
|
|
}
|
|
|
|
if (args_[i].isCounting()) {
|
|
|
|
LogVar clv = args_[i].countedLogVar();
|
|
|
|
args_[i].setCountedLogVar (theta.newNameFor (clv));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
constr_->applySubstitution (theta);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 11:57:00 +01:00
|
|
|
size_t
|
|
|
|
Parfactor::indexOfGround (const Ground& ground) const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2012-06-14 11:57:00 +01:00
|
|
|
size_t idx = args_.size();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2012-12-20 23:19:10 +00:00
|
|
|
if (args_[i].functor() == ground.functor() &&
|
2012-05-23 14:56:01 +01:00
|
|
|
args_[i].arity() == ground.arity()) {
|
|
|
|
constr_->moveToTop (args_[i].logVars());
|
|
|
|
if (constr_->containsTuple (ground.args())) {
|
2012-06-14 11:57:00 +01:00
|
|
|
idx = i;
|
2012-05-23 14:56:01 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-14 11:57:00 +01:00
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PrvGroup
|
|
|
|
Parfactor::findGroup (const Ground& ground) const
|
|
|
|
{
|
|
|
|
size_t idx = indexOfGround (ground);
|
|
|
|
return idx == args_.size()
|
2012-12-27 12:54:58 +00:00
|
|
|
? std::numeric_limits<PrvGroup>::max()
|
2012-06-14 11:57:00 +01:00
|
|
|
: args_[idx].group();
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
Parfactor::containsGround (const Ground& ground) const
|
|
|
|
{
|
2012-12-27 12:54:58 +00:00
|
|
|
return findGroup (ground) != std::numeric_limits<PrvGroup>::max();
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 11:57:00 +01:00
|
|
|
bool
|
|
|
|
Parfactor::containsGrounds (const Grounds& grounds) const
|
|
|
|
{
|
|
|
|
Tuple tuple;
|
|
|
|
LogVars tupleLvs;
|
|
|
|
for (size_t i = 0; i < grounds.size(); i++) {
|
|
|
|
size_t idx = indexOfGround (grounds[i]);
|
|
|
|
if (idx == args_.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LogVars lvs = args_[idx].logVars();
|
|
|
|
for (size_t j = 0; j < lvs.size(); j++) {
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Util::contains (tupleLvs, lvs[j]) == false) {
|
2012-06-14 11:57:00 +01:00
|
|
|
tuple.push_back (grounds[i].args()[j]);
|
|
|
|
tupleLvs.push_back (lvs[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
constr_->moveToTop (tupleLvs);
|
|
|
|
return constr_->containsTuple (tuple);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
bool
|
2012-05-24 23:38:44 +01:00
|
|
|
Parfactor::containsGroup (PrvGroup group) const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (args_[i].group() == group) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 11:57:00 +01:00
|
|
|
bool
|
2013-02-07 13:37:15 +00:00
|
|
|
Parfactor::containsGroups (std::vector<PrvGroup> groups) const
|
2012-06-14 11:57:00 +01:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < groups.size(); i++) {
|
|
|
|
if (containsGroup (groups[i]) == false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
unsigned
|
|
|
|
Parfactor::nrFormulas (LogVar X) const
|
|
|
|
{
|
|
|
|
unsigned count = 0;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (args_[i].contains (X)) {
|
|
|
|
count ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
Parfactor::indexOfLogVar (LogVar X) const
|
|
|
|
{
|
2012-05-24 22:55:20 +01:00
|
|
|
size_t idx = args_.size();
|
2012-05-23 14:56:01 +01:00
|
|
|
assert (nrFormulas (X) == 1);
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (args_[i].contains (X)) {
|
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2012-05-24 23:38:44 +01:00
|
|
|
Parfactor::indexOfGroup (PrvGroup group) const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2012-05-24 22:55:20 +01:00
|
|
|
size_t pos = args_.size();
|
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (args_[i].group() == group) {
|
|
|
|
pos = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned
|
2012-05-24 23:38:44 +01:00
|
|
|
Parfactor::nrFormulasWithGroup (PrvGroup group) const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
unsigned count = 0;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (args_[i].group() == group) {
|
|
|
|
count ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<PrvGroup>
|
2013-02-28 19:45:37 +00:00
|
|
|
Parfactor::getAllGroups() const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<PrvGroup> groups (args_.size());
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
groups[i] = args_[i].group();
|
|
|
|
}
|
|
|
|
return groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
std::string
|
2013-02-28 19:45:37 +00:00
|
|
|
Parfactor::getLabel() const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::stringstream ss;
|
2012-05-23 14:56:01 +01:00
|
|
|
ss << "phi(" ;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (i != 0) ss << "," ;
|
|
|
|
ss << args_[i];
|
|
|
|
}
|
|
|
|
ss << ")" ;
|
|
|
|
ConstraintTree copy (*constr_);
|
|
|
|
copy.moveToTop (copy.logVarSet().elements());
|
|
|
|
ss << "|" << copy.tupleSet();
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::print (bool printParams) const
|
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
cout << "Formulas: " ;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (i != 0) cout << ", " ;
|
|
|
|
cout << args_[i];
|
|
|
|
}
|
|
|
|
cout << endl;
|
2013-02-08 21:01:53 +00:00
|
|
|
if (args_[0].group() != Util::maxUnsigned()) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<std::string> groups;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2013-03-09 17:14:00 +00:00
|
|
|
groups.push_back (std::string ("g")
|
|
|
|
+ Util::toString (args_[i].group()));
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
cout << "Groups: " << groups << endl;
|
|
|
|
}
|
|
|
|
cout << "LogVars: " << constr_->logVarSet() << endl;
|
|
|
|
cout << "Ranges: " << ranges_ << endl;
|
|
|
|
if (printParams == false) {
|
|
|
|
cout << "Params: " ;
|
|
|
|
if (params_.size() <= 32) {
|
|
|
|
cout.precision(10);
|
|
|
|
cout << params_ << endl;
|
|
|
|
} else {
|
|
|
|
cout << "|" << params_.size() << "|" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ConstraintTree copy (*constr_);
|
|
|
|
copy.moveToTop (copy.logVarSet().elements());
|
|
|
|
cout << "Tuples: " << copy.tupleSet() << endl;
|
|
|
|
if (printParams) {
|
|
|
|
printParameters();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
Parfactor::printParameters() const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<std::string> jointStrings;
|
2012-05-25 21:16:08 +01:00
|
|
|
Indexer indexer (ranges_);
|
2012-05-23 14:56:01 +01:00
|
|
|
while (indexer.valid()) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::stringstream ss;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (i != 0) ss << ", " ;
|
|
|
|
if (args_[i].isCounting()) {
|
|
|
|
unsigned N = constr_->getConditionalCount (
|
|
|
|
args_[i].countedLogVar());
|
|
|
|
HistogramSet hs (N, args_[i].range());
|
|
|
|
unsigned c = 0;
|
|
|
|
while (c < indexer[i]) {
|
|
|
|
hs.nextHistogram();
|
|
|
|
c ++;
|
|
|
|
}
|
|
|
|
ss << hs;
|
|
|
|
} else {
|
|
|
|
ss << indexer[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jointStrings.push_back (ss.str());
|
|
|
|
++ indexer;
|
|
|
|
}
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < params_.size(); i++) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << "f(" << jointStrings[i] << ")" ;
|
|
|
|
std::cout << " = " << params_[i] << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
Parfactor::printProjections() const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
ConstraintTree copy (*constr_);
|
|
|
|
|
|
|
|
LogVarSet Xs = copy.logVarSet();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < Xs.size(); i++) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << "-> projection of " << Xs[i] << ": " ;
|
|
|
|
std::cout << copy.tupleSet ({Xs[i]}) << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::expandPotential (
|
2012-05-24 22:55:20 +01:00
|
|
|
size_t fIdx,
|
2012-05-23 14:56:01 +01:00
|
|
|
unsigned newRange,
|
2013-02-07 13:37:15 +00:00
|
|
|
const std::vector<unsigned>& sumIndexes)
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
ullong newSize = (params_.size() / ranges_[fIdx]) * newRange;
|
|
|
|
if (newSize > params_.max_size()) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cerr << "Error: an overflow occurred when performing expansion." ;
|
|
|
|
std::cerr << std::endl;
|
2012-12-20 17:37:59 +00:00
|
|
|
exit (EXIT_FAILURE);
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
2012-05-28 12:34:39 +01:00
|
|
|
Params backup = params_;
|
2012-05-23 14:56:01 +01:00
|
|
|
params_.clear();
|
|
|
|
params_.reserve (newSize);
|
|
|
|
|
2012-05-28 14:01:05 +01:00
|
|
|
size_t prod = 1;
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<size_t> offsets (ranges_.size());
|
2012-05-28 14:01:05 +01:00
|
|
|
for (size_t i = ranges_.size(); i-- > 0; ) {
|
|
|
|
offsets[i] = prod;
|
2012-05-23 14:56:01 +01:00
|
|
|
prod *= ranges_[i];
|
|
|
|
}
|
|
|
|
|
2012-05-24 22:55:20 +01:00
|
|
|
size_t index = 0;
|
2012-05-23 14:56:01 +01:00
|
|
|
ranges_[fIdx] = newRange;
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<unsigned> indices (ranges_.size(), 0);
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t k = 0; k < newSize; k++) {
|
2012-05-28 12:34:39 +01:00
|
|
|
assert (index < backup.size());
|
|
|
|
params_.push_back (backup[index]);
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = ranges_.size(); i-- > 0; ) {
|
2012-05-23 14:56:01 +01:00
|
|
|
indices[i] ++;
|
|
|
|
if (i == fIdx) {
|
|
|
|
if (indices[i] != ranges_[i]) {
|
|
|
|
int diff = sumIndexes[indices[i]] - sumIndexes[indices[i] - 1];
|
2012-05-28 14:01:05 +01:00
|
|
|
index += diff * offsets[i];
|
2012-05-23 14:56:01 +01:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// last index contains the old range minus 1
|
2012-05-28 14:01:05 +01:00
|
|
|
index -= sumIndexes.back() * offsets[i];
|
2012-05-23 14:56:01 +01:00
|
|
|
indices[i] = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (indices[i] != ranges_[i]) {
|
2012-05-28 14:01:05 +01:00
|
|
|
index += offsets[i];
|
2012-05-23 14:56:01 +01:00
|
|
|
break;
|
|
|
|
} else {
|
2012-05-28 14:01:05 +01:00
|
|
|
index -= (ranges_[i] - 1) * offsets[i];
|
2012-05-23 14:56:01 +01:00
|
|
|
indices[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-05-24 22:55:20 +01:00
|
|
|
Parfactor::simplifyCountingFormulas (size_t fIdx)
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
// check if we can simplify the parfactor
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_.size(); i++) {
|
|
|
|
if (i != fIdx &&
|
2012-05-23 14:56:01 +01:00
|
|
|
args_[i].isCounting() &&
|
|
|
|
args_[i].group() == args_[fIdx].group()) {
|
|
|
|
// if they only differ in the name of the counting log var
|
|
|
|
if ((args_[i].logVarSet() - args_[i].countedLogVar()) ==
|
|
|
|
(args_[fIdx].logVarSet()) - args_[fIdx].countedLogVar() &&
|
|
|
|
ranges_[i] == ranges_[fIdx]) {
|
|
|
|
simplifyParfactor (fIdx, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
Parfactor::simplifyGrounds()
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2012-05-24 23:38:44 +01:00
|
|
|
if (args_.size() == 1) {
|
|
|
|
return;
|
|
|
|
}
|
2012-05-23 14:56:01 +01:00
|
|
|
LogVarSet singletons = constr_->singletons();
|
2012-05-24 23:38:44 +01:00
|
|
|
for (long i = 0; i < (long)args_.size() - 1; i++) {
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t j = i + 1; j < args_.size(); j++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (args_[i].group() == args_[j].group() &&
|
|
|
|
singletons.contains (args_[i].logVarSet()) &&
|
|
|
|
singletons.contains (args_[j].logVarSet())) {
|
|
|
|
simplifyParfactor (i, j);
|
|
|
|
i --;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
Parfactor::canMultiply (Parfactor* g1, Parfactor* g2)
|
|
|
|
{
|
|
|
|
std::pair<LogVars, LogVars> res = getAlignLogVars (g1, g2);
|
|
|
|
LogVarSet Xs_1 (res.first);
|
|
|
|
LogVarSet Xs_2 (res.second);
|
|
|
|
LogVarSet Y_1 = g1->logVarSet() - Xs_1;
|
|
|
|
LogVarSet Y_2 = g2->logVarSet() - Xs_2;
|
|
|
|
Y_1 -= g1->countedLogVars();
|
|
|
|
Y_2 -= g2->countedLogVars();
|
|
|
|
return g1->constr()->isCountNormalized (Y_1) &&
|
|
|
|
g2->constr()->isCountNormalized (Y_2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-05-24 22:55:20 +01:00
|
|
|
Parfactor::simplifyParfactor (size_t fIdx1, size_t fIdx2)
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2012-05-28 12:34:39 +01:00
|
|
|
Params backup = params_;
|
2012-05-23 14:56:01 +01:00
|
|
|
params_.clear();
|
2012-05-25 21:16:08 +01:00
|
|
|
Indexer indexer (ranges_);
|
2012-05-23 14:56:01 +01:00
|
|
|
while (indexer.valid()) {
|
|
|
|
if (indexer[fIdx1] == indexer[fIdx2]) {
|
2012-05-28 12:34:39 +01:00
|
|
|
params_.push_back (backup[indexer]);
|
2012-12-20 23:19:10 +00:00
|
|
|
}
|
2012-05-23 14:56:01 +01:00
|
|
|
++ indexer;
|
|
|
|
}
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < args_[fIdx2].logVars().size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (nrFormulas (args_[fIdx2].logVars()[i]) == 1) {
|
|
|
|
constr_->remove ({ args_[fIdx2].logVars()[i] });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args_.erase (args_.begin() + fIdx2);
|
|
|
|
ranges_.erase (ranges_.begin() + fIdx2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::pair<LogVars, LogVars>
|
|
|
|
Parfactor::getAlignLogVars (Parfactor* g1, Parfactor* g2)
|
|
|
|
{
|
|
|
|
g1->simplifyGrounds();
|
|
|
|
g2->simplifyGrounds();
|
|
|
|
LogVars Xs_1, Xs_2;
|
2012-05-24 22:55:20 +01:00
|
|
|
TinySet<size_t> matchedI;
|
|
|
|
TinySet<size_t> matchedJ;
|
2012-05-23 14:56:01 +01:00
|
|
|
ProbFormulas& formulas1 = g1->arguments();
|
2012-12-20 23:19:10 +00:00
|
|
|
ProbFormulas& formulas2 = g2->arguments();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < formulas1.size(); i++) {
|
|
|
|
for (size_t j = 0; j < formulas2.size(); j++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (formulas1[i].group() == formulas2[j].group() &&
|
|
|
|
g1->range (i) == g2->range (j) &&
|
|
|
|
matchedI.contains (i) == false &&
|
|
|
|
matchedJ.contains (j) == false) {
|
2013-02-08 21:01:53 +00:00
|
|
|
Util::addToVector (Xs_1, formulas1[i].logVars());
|
|
|
|
Util::addToVector (Xs_2, formulas2[j].logVars());
|
2012-05-23 14:56:01 +01:00
|
|
|
matchedI.insert (i);
|
|
|
|
matchedJ.insert (j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return make_pair (Xs_1, Xs_2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::alignAndExponentiate (Parfactor* g1, Parfactor* g2)
|
|
|
|
{
|
|
|
|
alignLogicalVars (g1, g2);
|
|
|
|
LogVarSet comm = g1->logVarSet() & g2->logVarSet();
|
|
|
|
LogVarSet Y_1 = g1->logVarSet() - comm;
|
|
|
|
LogVarSet Y_2 = g2->logVarSet() - comm;
|
|
|
|
Y_1 -= g1->countedLogVars();
|
|
|
|
Y_2 -= g2->countedLogVars();
|
|
|
|
assert (g1->constr()->isCountNormalized (Y_1));
|
|
|
|
assert (g2->constr()->isCountNormalized (Y_2));
|
|
|
|
unsigned condCount1 = g1->constr()->getConditionalCount (Y_1);
|
|
|
|
unsigned condCount2 = g2->constr()->getConditionalCount (Y_2);
|
2013-02-08 21:01:53 +00:00
|
|
|
LogAware::pow (g1->params(), 1.0 / condCount2);
|
|
|
|
LogAware::pow (g2->params(), 1.0 / condCount1);
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::alignLogicalVars (Parfactor* g1, Parfactor* g2)
|
|
|
|
{
|
|
|
|
std::pair<LogVars, LogVars> res = getAlignLogVars (g1, g2);
|
|
|
|
const LogVars& alignLvs1 = res.first;
|
|
|
|
const LogVars& alignLvs2 = res.second;
|
2013-02-07 13:37:15 +00:00
|
|
|
// std::cout << "ALIGNING :::::::::::::::::" << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
// g1->print();
|
|
|
|
// cout << "AND" << endl;
|
|
|
|
// g2->print();
|
2013-02-07 13:37:15 +00:00
|
|
|
// std::cout << "-> align lvs1 = " << alignLvs1 << std::endl;
|
|
|
|
// std::cout << "-> align lvs2 = " << alignLvs2 << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
LogVar freeLogVar (0);
|
|
|
|
Substitution theta1, theta2;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < alignLvs1.size(); i++) {
|
2012-12-20 23:19:10 +00:00
|
|
|
bool b1 = theta1.containsReplacementFor (alignLvs1[i]);
|
2012-05-23 14:56:01 +01:00
|
|
|
bool b2 = theta2.containsReplacementFor (alignLvs2[i]);
|
|
|
|
if (b1 == false && b2 == false) {
|
|
|
|
theta1.add (alignLvs1[i], freeLogVar);
|
|
|
|
theta2.add (alignLvs2[i], freeLogVar);
|
|
|
|
++ freeLogVar;
|
|
|
|
} else if (b1 == false && b2) {
|
|
|
|
theta1.add (alignLvs1[i], theta2.newNameFor (alignLvs2[i]));
|
|
|
|
} else if (b1 && b2 == false) {
|
|
|
|
theta2.add (alignLvs2[i], theta1.newNameFor (alignLvs1[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const LogVarSet& allLvs1 = g1->logVarSet();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < allLvs1.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (theta1.containsReplacementFor (allLvs1[i]) == false) {
|
|
|
|
theta1.add (allLvs1[i], freeLogVar);
|
|
|
|
++ freeLogVar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const LogVarSet& allLvs2 = g2->logVarSet();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < allLvs2.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (theta2.containsReplacementFor (allLvs2[i]) == false) {
|
|
|
|
theta2.add (allLvs2[i], freeLogVar);
|
|
|
|
++ freeLogVar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle this type of situation:
|
2012-12-20 23:19:10 +00:00
|
|
|
// g1 = p(X), q(X) ; X in {(p1),(p2)}
|
2012-05-23 14:56:01 +01:00
|
|
|
// g2 = p(X), q(Y) ; (X,Y) in {(p1,p2),(p2,p1)}
|
|
|
|
LogVars discardedLvs1 = theta1.getDiscardedLogVars();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < discardedLvs1.size(); i++) {
|
2012-12-20 23:19:10 +00:00
|
|
|
if (g1->constr()->isSingleton (discardedLvs1[i]) &&
|
2012-05-23 14:56:01 +01:00
|
|
|
g1->nrFormulas (discardedLvs1[i]) == 1) {
|
|
|
|
g1->constr()->remove (discardedLvs1[i]);
|
|
|
|
} else {
|
|
|
|
LogVar X_new = ++ g1->constr()->logVarSet().back();
|
|
|
|
theta1.rename (discardedLvs1[i], X_new);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LogVars discardedLvs2 = theta2.getDiscardedLogVars();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < discardedLvs2.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (g2->constr()->isSingleton (discardedLvs2[i]) &&
|
|
|
|
g2->nrFormulas (discardedLvs2[i]) == 1) {
|
|
|
|
g2->constr()->remove (discardedLvs2[i]);
|
|
|
|
} else {
|
|
|
|
LogVar X_new = ++ g2->constr()->logVarSet().back();
|
|
|
|
theta2.rename (discardedLvs2[i], X_new);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
// std::cout << "theta1: " << theta1 << std::endl;
|
|
|
|
// std::cout << "theta2: " << theta2 << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
g1->applySubstitution (theta1);
|
|
|
|
g2->applySubstitution (theta2);
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
} // namespace Horus
|
2013-02-07 23:53:13 +00:00
|
|
|
|