2012-03-22 11:33:24 +00:00
|
|
|
|
|
|
|
#include "Parfactor.h"
|
|
|
|
#include "Histogram.h"
|
|
|
|
#include "Indexer.h"
|
2012-03-31 23:27:37 +01:00
|
|
|
#include "Util.h"
|
2012-03-22 11:33:24 +00:00
|
|
|
#include "Horus.h"
|
|
|
|
|
|
|
|
|
|
|
|
Parfactor::Parfactor (
|
|
|
|
const ProbFormulas& formulas,
|
|
|
|
const Params& params,
|
|
|
|
const Tuples& tuples,
|
|
|
|
unsigned distId)
|
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
args_ = formulas;
|
|
|
|
params_ = params;
|
|
|
|
distId_ = distId;
|
2012-03-22 11:33:24 +00:00
|
|
|
|
|
|
|
LogVars logVars;
|
2012-03-31 23:27:37 +01:00
|
|
|
for (unsigned i = 0; i < args_.size(); i++) {
|
|
|
|
ranges_.push_back (args_[i].range());
|
|
|
|
const LogVars& lvs = args_[i].logVars();
|
2012-03-22 11:33:24 +00:00
|
|
|
for (unsigned j = 0; j < lvs.size(); j++) {
|
2012-03-31 23:27:37 +01:00
|
|
|
if (Util::contains (logVars, lvs[j]) == false) {
|
2012-03-22 11:33:24 +00:00
|
|
|
logVars.push_back (lvs[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
constr_ = new ConstraintTree (logVars, tuples);
|
2012-03-31 23:27:37 +01:00
|
|
|
assert (params_.size() == Util::expectedSize (ranges_));
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parfactor::Parfactor (const Parfactor* g, const Tuple& tuple)
|
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
args_ = g->arguments();
|
|
|
|
params_ = g->params();
|
|
|
|
ranges_ = g->ranges();
|
|
|
|
distId_ = g->distId();
|
|
|
|
constr_ = new ConstraintTree (g->logVars(), {tuple});
|
|
|
|
assert (params_.size() == Util::expectedSize (ranges_));
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parfactor::Parfactor (const Parfactor* g, ConstraintTree* constr)
|
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
args_ = g->arguments();
|
|
|
|
params_ = g->params();
|
|
|
|
ranges_ = g->ranges();
|
|
|
|
distId_ = g->distId();
|
|
|
|
constr_ = constr;
|
|
|
|
assert (params_.size() == Util::expectedSize (ranges_));
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parfactor::Parfactor (const Parfactor& g)
|
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
args_ = g.arguments();
|
|
|
|
params_ = g.params();
|
|
|
|
ranges_ = g.ranges();
|
|
|
|
distId_ = g.distId();
|
|
|
|
constr_ = new ConstraintTree (*g.constr());
|
|
|
|
assert (params_.size() == Util::expectedSize (ranges_));
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parfactor::~Parfactor (void)
|
|
|
|
{
|
|
|
|
delete constr_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogVarSet
|
|
|
|
Parfactor::countedLogVars (void) const
|
|
|
|
{
|
|
|
|
LogVarSet set;
|
2012-03-31 23:27:37 +01:00
|
|
|
for (unsigned i = 0; i < args_.size(); i++) {
|
|
|
|
if (args_[i].isCounting()) {
|
|
|
|
set.insert (args_[i].countedLogVar());
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogVarSet
|
|
|
|
Parfactor::uncountedLogVars (void) const
|
|
|
|
{
|
|
|
|
return constr_->logVarSet() - countedLogVars();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogVarSet
|
|
|
|
Parfactor::elimLogVars (void) const
|
|
|
|
{
|
|
|
|
LogVarSet requiredToElim = constr_->logVarSet();
|
|
|
|
requiredToElim -= constr_->singletons();
|
|
|
|
requiredToElim -= countedLogVars();
|
|
|
|
return requiredToElim;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogVarSet
|
|
|
|
Parfactor::exclusiveLogVars (unsigned fIdx) const
|
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
assert (fIdx < args_.size());
|
2012-03-22 11:33:24 +00:00
|
|
|
LogVarSet remaining;
|
2012-03-31 23:27:37 +01:00
|
|
|
for (unsigned i = 0; i < args_.size(); i++) {
|
2012-03-22 11:33:24 +00:00
|
|
|
if (i != fIdx) {
|
2012-03-31 23:27:37 +01:00
|
|
|
remaining |= args_[i].logVarSet();
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-31 23:27:37 +01:00
|
|
|
return args_[fIdx].logVarSet() - remaining;
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::setConstraintTree (ConstraintTree* newTree)
|
|
|
|
{
|
|
|
|
delete constr_;
|
|
|
|
constr_ = newTree;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::sumOut (unsigned fIdx)
|
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
assert (fIdx < args_.size());
|
|
|
|
assert (args_[fIdx].contains (elimLogVars()));
|
2012-03-22 11:33:24 +00:00
|
|
|
|
2012-03-31 23:27:37 +01:00
|
|
|
if (args_[fIdx].isCounting()) {
|
2012-03-22 11:33:24 +00:00
|
|
|
unsigned N = constr_->getConditionalCount (
|
2012-03-31 23:27:37 +01:00
|
|
|
args_[fIdx].countedLogVar());
|
|
|
|
unsigned R = args_[fIdx].range();
|
|
|
|
vector<double> numAssigns = HistogramSet::getNumAssigns (N, R);
|
|
|
|
StatesIndexer sindexer (ranges_, fIdx);
|
|
|
|
while (sindexer.valid()) {
|
|
|
|
unsigned h = sindexer[fIdx];
|
|
|
|
if (Globals::logDomain) {
|
|
|
|
params_[sindexer] += numAssigns[h];
|
|
|
|
} else {
|
|
|
|
params_[sindexer] *= numAssigns[h];
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
2012-03-31 23:27:37 +01:00
|
|
|
++ sindexer;
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Params copy = params_;
|
|
|
|
params_.clear();
|
2012-03-31 23:27:37 +01:00
|
|
|
params_.resize (copy.size() / ranges_[fIdx], LogAware::addIdenty());
|
2012-03-22 11:33:24 +00:00
|
|
|
MapIndexer indexer (ranges_, fIdx);
|
2012-03-31 23:27:37 +01:00
|
|
|
if (Globals::logDomain) {
|
|
|
|
for (unsigned i = 0; i < copy.size(); i++) {
|
|
|
|
params_[indexer] = Util::logSum (params_[indexer], copy[i]);
|
|
|
|
++ indexer;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 0; i < copy.size(); i++) {
|
|
|
|
params_[indexer] += copy[i];
|
|
|
|
++ indexer;
|
|
|
|
}
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
2012-03-31 23:27:37 +01:00
|
|
|
|
2012-04-27 14:47:30 +01:00
|
|
|
LogVarSet excl = exclusiveLogVars (fIdx);
|
|
|
|
if (args_[fIdx].isCounting()) {
|
2012-04-28 01:10:04 +01:00
|
|
|
// counting log vars were already raised on counting conversion
|
2012-04-27 14:47:30 +01:00
|
|
|
LogAware::pow (params_, constr_->getConditionalCount (
|
|
|
|
excl - args_[fIdx].countedLogVar()));
|
|
|
|
} else {
|
|
|
|
LogAware::pow (params_, constr_->getConditionalCount (excl));
|
|
|
|
}
|
|
|
|
constr_->remove (excl);
|
|
|
|
|
2012-03-31 23:27:37 +01:00
|
|
|
args_.erase (args_.begin() + fIdx);
|
2012-03-22 11:33:24 +00:00
|
|
|
ranges_.erase (ranges_.begin() + fIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::multiply (Parfactor& g)
|
|
|
|
{
|
|
|
|
alignAndExponentiate (this, &g);
|
2012-03-31 23:27:37 +01:00
|
|
|
TFactor<ProbFormula>::multiply (g);
|
2012-03-22 11:33:24 +00:00
|
|
|
constr_->join (g.constr(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::countConvert (LogVar X)
|
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
int fIdx = indexOfLogVar (X);
|
2012-03-22 11:33:24 +00:00
|
|
|
assert (fIdx != -1);
|
|
|
|
assert (constr_->isCountNormalized (X));
|
|
|
|
assert (constr_->getConditionalCount (X) > 1);
|
|
|
|
assert (constr_->isCarteesianProduct (countedLogVars() | X));
|
|
|
|
|
|
|
|
unsigned N = constr_->getConditionalCount (X);
|
|
|
|
unsigned R = ranges_[fIdx];
|
|
|
|
unsigned H = HistogramSet::nrHistograms (N, R);
|
|
|
|
vector<Histogram> histograms = HistogramSet::getHistograms (N, R);
|
|
|
|
|
|
|
|
StatesIndexer indexer (ranges_);
|
2012-03-31 23:27:37 +01:00
|
|
|
vector<Params> sumout (params_.size() / R);
|
2012-03-22 11:33:24 +00:00
|
|
|
unsigned count = 0;
|
|
|
|
while (indexer.valid()) {
|
2012-03-31 23:27:37 +01:00
|
|
|
sumout[count].reserve (R);
|
2012-03-22 11:33:24 +00:00
|
|
|
for (unsigned r = 0; r < R; r++) {
|
2012-03-31 23:27:37 +01:00
|
|
|
sumout[count].push_back (params_[indexer]);
|
2012-03-22 11:33:24 +00:00
|
|
|
indexer.increment (fIdx);
|
|
|
|
}
|
|
|
|
count ++;
|
|
|
|
indexer.reset (fIdx);
|
|
|
|
indexer.incrementExcluding (fIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
params_.clear();
|
2012-03-31 23:27:37 +01:00
|
|
|
params_.reserve (sumout.size() * H);
|
2012-03-22 11:33:24 +00:00
|
|
|
|
|
|
|
ranges_[fIdx] = H;
|
2012-03-31 23:27:37 +01:00
|
|
|
MapIndexer mapIndexer (ranges_, fIdx);
|
2012-03-22 11:33:24 +00:00
|
|
|
while (mapIndexer.valid()) {
|
2012-03-31 23:27:37 +01:00
|
|
|
double prod = LogAware::multIdenty();
|
2012-03-22 11:33:24 +00:00
|
|
|
unsigned i = mapIndexer.mappedIndex();
|
|
|
|
unsigned h = mapIndexer[fIdx];
|
|
|
|
for (unsigned r = 0; r < R; r++) {
|
2012-03-31 23:27:37 +01:00
|
|
|
if (Globals::logDomain) {
|
|
|
|
prod += LogAware::pow (sumout[i][r], histograms[h][r]);
|
|
|
|
} else {
|
|
|
|
prod *= LogAware::pow (sumout[i][r], histograms[h][r]);
|
|
|
|
}
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
params_.push_back (prod);
|
|
|
|
++ mapIndexer;
|
|
|
|
}
|
2012-03-31 23:27:37 +01:00
|
|
|
args_[fIdx].setCountedLogVar (X);
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-03-31 23:27:37 +01:00
|
|
|
Parfactor::expand (LogVar X, LogVar X_new1, LogVar X_new2)
|
2012-03-22 11:33:24 +00:00
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
int fIdx = indexOfLogVar (X);
|
2012-03-22 11:33:24 +00:00
|
|
|
assert (fIdx != -1);
|
2012-03-31 23:27:37 +01:00
|
|
|
assert (args_[fIdx].isCounting());
|
2012-03-22 11:33:24 +00:00
|
|
|
|
|
|
|
unsigned N1 = constr_->getConditionalCount (X_new1);
|
|
|
|
unsigned N2 = constr_->getConditionalCount (X_new2);
|
|
|
|
unsigned N = N1 + N2;
|
2012-03-31 23:27:37 +01:00
|
|
|
unsigned R = args_[fIdx].range();
|
2012-03-22 11:33:24 +00:00
|
|
|
unsigned H1 = HistogramSet::nrHistograms (N1, R);
|
|
|
|
unsigned H2 = HistogramSet::nrHistograms (N2, R);
|
|
|
|
|
|
|
|
vector<Histogram> histograms = HistogramSet::getHistograms (N, R);
|
|
|
|
vector<Histogram> histograms1 = HistogramSet::getHistograms (N1, R);
|
|
|
|
vector<Histogram> histograms2 = HistogramSet::getHistograms (N2, R);
|
|
|
|
|
|
|
|
vector<unsigned> sumIndexes;
|
|
|
|
sumIndexes.reserve (H1 * H2);
|
|
|
|
for (unsigned i = 0; i < H1; i++) {
|
|
|
|
for (unsigned j = 0; j < H2; j++) {
|
|
|
|
Histogram hist = histograms1[i];
|
|
|
|
std::transform (
|
|
|
|
hist.begin(), hist.end(),
|
|
|
|
histograms2[j].begin(),
|
|
|
|
hist.begin(),
|
|
|
|
plus<int>());
|
|
|
|
sumIndexes.push_back (HistogramSet::findIndex (hist, histograms));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-31 23:27:37 +01:00
|
|
|
expandPotential (fIdx, H1 * H2, sumIndexes);
|
2012-03-22 11:33:24 +00:00
|
|
|
|
2012-03-31 23:27:37 +01:00
|
|
|
args_.insert (args_.begin() + fIdx + 1, args_[fIdx]);
|
|
|
|
args_[fIdx].rename (X, X_new1);
|
|
|
|
args_[fIdx + 1].rename (X, X_new2);
|
2012-04-28 01:01:23 +01:00
|
|
|
if (H1 == 2) {
|
|
|
|
args_[fIdx].clearCountedLogVar();
|
|
|
|
}
|
|
|
|
if (H2 == 2) {
|
|
|
|
args_[fIdx + 1].clearCountedLogVar();
|
|
|
|
}
|
2012-03-22 11:33:24 +00:00
|
|
|
ranges_.insert (ranges_.begin() + fIdx + 1, H2);
|
|
|
|
ranges_[fIdx] = H1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::fullExpand (LogVar X)
|
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
int fIdx = indexOfLogVar (X);
|
2012-03-22 11:33:24 +00:00
|
|
|
assert (fIdx != -1);
|
2012-03-31 23:27:37 +01:00
|
|
|
assert (args_[fIdx].isCounting());
|
2012-03-22 11:33:24 +00:00
|
|
|
|
|
|
|
unsigned N = constr_->getConditionalCount (X);
|
2012-03-31 23:27:37 +01:00
|
|
|
unsigned R = args_[fIdx].range();
|
2012-03-22 11:33:24 +00:00
|
|
|
|
|
|
|
vector<Histogram> originHists = HistogramSet::getHistograms (N, R);
|
|
|
|
vector<Histogram> expandHists = HistogramSet::getHistograms (1, R);
|
|
|
|
|
|
|
|
vector<unsigned> sumIndexes;
|
|
|
|
sumIndexes.reserve (N * R);
|
|
|
|
|
|
|
|
Ranges expandRanges (N, R);
|
|
|
|
StatesIndexer indexer (expandRanges);
|
|
|
|
while (indexer.valid()) {
|
|
|
|
vector<unsigned> hist (R, 0);
|
|
|
|
for (unsigned n = 0; n < N; n++) {
|
|
|
|
std::transform (
|
|
|
|
hist.begin(), hist.end(),
|
|
|
|
expandHists[indexer[n]].begin(),
|
|
|
|
hist.begin(),
|
|
|
|
plus<int>());
|
|
|
|
}
|
|
|
|
sumIndexes.push_back (HistogramSet::findIndex (hist, originHists));
|
|
|
|
++ indexer;
|
|
|
|
}
|
|
|
|
|
2012-03-31 23:27:37 +01:00
|
|
|
expandPotential (fIdx, std::pow (R, N), sumIndexes);
|
2012-03-22 11:33:24 +00:00
|
|
|
|
2012-03-31 23:27:37 +01:00
|
|
|
ProbFormula f = args_[fIdx];
|
|
|
|
args_.erase (args_.begin() + fIdx);
|
2012-03-22 11:33:24 +00:00
|
|
|
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]);
|
2012-03-31 23:27:37 +01:00
|
|
|
args_.insert (args_.begin() + fIdx + i, newFormula);
|
2012-03-22 11:33:24 +00:00
|
|
|
ranges_.insert (ranges_.begin() + fIdx + i, R);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::reorderAccordingGrounds (const Grounds& grounds)
|
|
|
|
{
|
|
|
|
ProbFormulas newFormulas;
|
|
|
|
for (unsigned i = 0; i < grounds.size(); i++) {
|
2012-03-31 23:27:37 +01:00
|
|
|
for (unsigned j = 0; j < args_.size(); j++) {
|
|
|
|
if (grounds[i].functor() == args_[j].functor() &&
|
|
|
|
grounds[i].arity() == args_[j].arity()) {
|
|
|
|
constr_->moveToTop (args_[j].logVars());
|
2012-03-22 11:33:24 +00:00
|
|
|
if (constr_->containsTuple (grounds[i].args())) {
|
2012-03-31 23:27:37 +01:00
|
|
|
newFormulas.push_back (args_[j]);
|
2012-03-22 11:33:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert (newFormulas.size() == i + 1);
|
|
|
|
}
|
2012-03-31 23:27:37 +01:00
|
|
|
reorderArguments (newFormulas);
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-03-31 23:27:37 +01:00
|
|
|
Parfactor::absorveEvidence (const ProbFormula& formula, unsigned evidence)
|
2012-03-22 11:33:24 +00:00
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
int fIdx = indexOf (formula);
|
|
|
|
assert (fIdx != -1);
|
2012-03-22 11:33:24 +00:00
|
|
|
LogVarSet excl = exclusiveLogVars (fIdx);
|
2012-03-31 23:27:37 +01:00
|
|
|
assert (args_[fIdx].isCounting() == false);
|
2012-03-22 11:33:24 +00:00
|
|
|
assert (constr_->isCountNormalized (excl));
|
2012-03-31 23:27:37 +01:00
|
|
|
LogAware::pow (params_, constr_->getConditionalCount (excl));
|
|
|
|
TFactor<ProbFormula>::absorveEvidence (formula, evidence);
|
2012-03-22 11:33:24 +00:00
|
|
|
constr_->remove (excl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::setNewGroups (void)
|
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
for (unsigned i = 0; i < args_.size(); i++) {
|
|
|
|
args_[i].setGroup (ProbFormula::getNewGroup());
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::applySubstitution (const Substitution& theta)
|
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
for (unsigned i = 0; i < args_.size(); i++) {
|
|
|
|
LogVars& lvs = args_[i].logVars();
|
2012-03-22 11:33:24 +00:00
|
|
|
for (unsigned j = 0; j < lvs.size(); j++) {
|
|
|
|
lvs[j] = theta.newNameFor (lvs[j]);
|
|
|
|
}
|
2012-03-31 23:27:37 +01:00
|
|
|
if (args_[i].isCounting()) {
|
|
|
|
LogVar clv = args_[i].countedLogVar();
|
|
|
|
args_[i].setCountedLogVar (theta.newNameFor (clv));
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
constr_->applySubstitution (theta);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-31 23:27:37 +01:00
|
|
|
int
|
2012-04-03 11:58:21 +01:00
|
|
|
Parfactor::findGroup (const Ground& ground) const
|
2012-03-22 11:33:24 +00:00
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
int group = -1;
|
|
|
|
for (unsigned i = 0; i < args_.size(); i++) {
|
|
|
|
if (args_[i].functor() == ground.functor() &&
|
|
|
|
args_[i].arity() == ground.arity()) {
|
|
|
|
constr_->moveToTop (args_[i].logVars());
|
2012-03-22 11:33:24 +00:00
|
|
|
if (constr_->containsTuple (ground.args())) {
|
2012-03-31 23:27:37 +01:00
|
|
|
group = args_[i].group();
|
|
|
|
break;
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-31 23:27:37 +01:00
|
|
|
return group;
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
2012-03-31 23:27:37 +01:00
|
|
|
Parfactor::containsGround (const Ground& ground) const
|
2012-03-22 11:33:24 +00:00
|
|
|
{
|
2012-04-03 11:58:21 +01:00
|
|
|
return findGroup (ground) != -1;
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-31 23:27:37 +01:00
|
|
|
bool
|
|
|
|
Parfactor::containsGroup (unsigned group) const
|
2012-03-22 11:33:24 +00:00
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
for (unsigned i = 0; i < args_.size(); i++) {
|
|
|
|
if (args_[i].group() == group) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
Parfactor::nrFormulas (LogVar X) const
|
|
|
|
{
|
|
|
|
unsigned count = 0;
|
2012-03-31 23:27:37 +01:00
|
|
|
for (unsigned i = 0; i < args_.size(); i++) {
|
|
|
|
if (args_[i].contains (X)) {
|
2012-03-22 11:33:24 +00:00
|
|
|
count ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2012-03-31 23:27:37 +01:00
|
|
|
Parfactor::indexOfLogVar (LogVar X) const
|
2012-03-22 11:33:24 +00:00
|
|
|
{
|
|
|
|
int idx = -1;
|
|
|
|
assert (nrFormulas (X) == 1);
|
2012-03-31 23:27:37 +01:00
|
|
|
for (unsigned i = 0; i < args_.size(); i++) {
|
|
|
|
if (args_[i].contains (X)) {
|
2012-03-22 11:33:24 +00:00
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2012-03-31 23:27:37 +01:00
|
|
|
Parfactor::indexOfGroup (unsigned group) const
|
2012-03-22 11:33:24 +00:00
|
|
|
{
|
|
|
|
int pos = -1;
|
2012-03-31 23:27:37 +01:00
|
|
|
for (unsigned i = 0; i < args_.size(); i++) {
|
|
|
|
if (args_[i].group() == group) {
|
2012-03-22 11:33:24 +00:00
|
|
|
pos = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vector<unsigned>
|
|
|
|
Parfactor::getAllGroups (void) const
|
|
|
|
{
|
2012-03-31 23:27:37 +01:00
|
|
|
vector<unsigned> groups (args_.size());
|
|
|
|
for (unsigned i = 0; i < args_.size(); i++) {
|
|
|
|
groups[i] = args_[i].group();
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
return groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string
|
2012-03-31 23:27:37 +01:00
|
|
|
Parfactor::getLabel (void) const
|
2012-03-22 11:33:24 +00:00
|
|
|
{
|
|
|
|
stringstream ss;
|
|
|
|
ss << "phi(" ;
|
2012-03-31 23:27:37 +01:00
|
|
|
for (unsigned i = 0; i < args_.size(); i++) {
|
2012-03-22 11:33:24 +00:00
|
|
|
if (i != 0) ss << "," ;
|
2012-03-31 23:27:37 +01:00
|
|
|
ss << args_[i];
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
ss << ")" ;
|
|
|
|
ConstraintTree copy (*constr_);
|
|
|
|
copy.moveToTop (copy.logVarSet().elements());
|
|
|
|
ss << "|" << copy.tupleSet();
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::print (bool printParams) const
|
|
|
|
{
|
|
|
|
cout << "Formulas: " ;
|
2012-03-31 23:27:37 +01:00
|
|
|
for (unsigned i = 0; i < args_.size(); i++) {
|
2012-03-22 11:33:24 +00:00
|
|
|
if (i != 0) cout << ", " ;
|
2012-03-31 23:27:37 +01:00
|
|
|
cout << args_[i];
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
cout << endl;
|
2012-04-11 15:36:50 +01:00
|
|
|
if (args_[0].group() != Util::maxUnsigned()) {
|
|
|
|
vector<string> groups;
|
|
|
|
for (unsigned i = 0; i < args_.size(); i++) {
|
|
|
|
groups.push_back (string ("g") + Util::toString (args_[i].group()));
|
|
|
|
}
|
|
|
|
cout << "Groups: " << groups << endl;
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
2012-03-31 23:27:37 +01:00
|
|
|
cout << "LogVars: " << constr_->logVarSet() << endl;
|
2012-03-22 11:33:24 +00:00
|
|
|
cout << "Ranges: " << ranges_ << endl;
|
|
|
|
if (printParams == false) {
|
2012-04-27 01:19:28 +01:00
|
|
|
cout << "Params: " ;
|
|
|
|
if (params_.size() <= 32) {
|
|
|
|
cout << params_ << endl;
|
|
|
|
} else {
|
|
|
|
cout << "|" << params_.size() << "|" << endl;
|
|
|
|
}
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
2012-03-31 23:27:37 +01:00
|
|
|
ConstraintTree copy (*constr_);
|
|
|
|
copy.moveToTop (copy.logVarSet().elements());
|
|
|
|
cout << "Tuples: " << copy.tupleSet() << endl;
|
2012-03-22 11:33:24 +00:00
|
|
|
if (printParams) {
|
|
|
|
vector<string> jointStrings;
|
|
|
|
StatesIndexer indexer (ranges_);
|
|
|
|
while (indexer.valid()) {
|
|
|
|
stringstream ss;
|
2012-03-31 23:27:37 +01:00
|
|
|
for (unsigned i = 0; i < args_.size(); i++) {
|
2012-03-22 11:33:24 +00:00
|
|
|
if (i != 0) ss << ", " ;
|
2012-03-31 23:27:37 +01:00
|
|
|
if (args_[i].isCounting()) {
|
|
|
|
unsigned N = constr_->getConditionalCount (
|
|
|
|
args_[i].countedLogVar());
|
|
|
|
HistogramSet hs (N, args_[i].range());
|
2012-03-22 11:33:24 +00:00
|
|
|
unsigned c = 0;
|
|
|
|
while (c < indexer[i]) {
|
|
|
|
hs.nextHistogram();
|
|
|
|
c ++;
|
|
|
|
}
|
|
|
|
ss << hs;
|
|
|
|
} else {
|
|
|
|
ss << indexer[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jointStrings.push_back (ss.str());
|
|
|
|
++ indexer;
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < params_.size(); i++) {
|
|
|
|
cout << "f(" << jointStrings[i] << ")" ;
|
|
|
|
cout << " = " << params_[i] << endl;
|
|
|
|
}
|
|
|
|
}
|
2012-04-03 19:53:27 +01:00
|
|
|
cout << endl;
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-03-31 23:27:37 +01:00
|
|
|
Parfactor::expandPotential (
|
|
|
|
int fIdx,
|
|
|
|
unsigned newRange,
|
|
|
|
const vector<unsigned>& sumIndexes)
|
2012-03-22 11:33:24 +00:00
|
|
|
{
|
2012-04-20 17:16:10 +01:00
|
|
|
Params::size_type newSize = (params_.size() / ranges_[fIdx]) * newRange;
|
|
|
|
if (newSize > Util::maxUnsigned()) {
|
|
|
|
// factor will become to bigger, is not worth to continue
|
|
|
|
cerr << "error: an overflow occurred when performing expansion" ;
|
|
|
|
cerr << endl;
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2012-03-22 11:33:24 +00:00
|
|
|
Params copy = params_;
|
|
|
|
params_.clear();
|
2012-04-20 14:50:14 +01:00
|
|
|
params_.reserve (newSize);
|
2012-03-31 23:27:37 +01:00
|
|
|
|
|
|
|
unsigned prod = 1;
|
|
|
|
vector<unsigned> offsets_ (ranges_.size());
|
|
|
|
for (int i = ranges_.size() - 1; i >= 0; i--) {
|
|
|
|
offsets_[i] = prod;
|
|
|
|
prod *= ranges_[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned index = 0;
|
|
|
|
ranges_[fIdx] = newRange;
|
|
|
|
vector<unsigned> indices (ranges_.size(), 0);
|
2012-04-20 14:50:14 +01:00
|
|
|
for (unsigned k = 0; k < newSize; k++) {
|
2012-04-20 17:16:10 +01:00
|
|
|
if (index >= copy.size()) {
|
|
|
|
abort();
|
|
|
|
}
|
2012-04-20 14:50:14 +01:00
|
|
|
assert (index < copy.size());
|
2012-03-31 23:27:37 +01:00
|
|
|
params_.push_back (copy[index]);
|
|
|
|
for (int i = ranges_.size() - 1; i >= 0; i--) {
|
|
|
|
indices[i] ++;
|
|
|
|
if (i == fIdx) {
|
2012-04-20 14:50:14 +01:00
|
|
|
if (indices[i] != ranges_[i]) {
|
|
|
|
int diff = sumIndexes[indices[i]] - sumIndexes[indices[i] - 1];
|
2012-03-31 23:27:37 +01:00
|
|
|
index += diff * offsets_[i];
|
2012-04-20 14:50:14 +01:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// last index contains the old range minus 1
|
|
|
|
index -= sumIndexes.back() * offsets_[i];
|
|
|
|
indices[i] = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (indices[i] != ranges_[i]) {
|
|
|
|
index += offsets_[i];
|
|
|
|
break;
|
2012-03-31 23:27:37 +01:00
|
|
|
} else {
|
2012-04-20 14:50:14 +01:00
|
|
|
index -= (ranges_[i] - 1) * offsets_[i];
|
|
|
|
indices[i] = 0;
|
2012-03-31 23:27:37 +01:00
|
|
|
}
|
|
|
|
}
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::alignAndExponentiate (Parfactor* g1, Parfactor* g2)
|
|
|
|
{
|
|
|
|
LogVars X_1, X_2;
|
2012-03-31 23:27:37 +01:00
|
|
|
const ProbFormulas& formulas1 = g1->arguments();
|
|
|
|
const ProbFormulas& formulas2 = g2->arguments();
|
2012-03-22 11:33:24 +00:00
|
|
|
for (unsigned i = 0; i < formulas1.size(); i++) {
|
|
|
|
for (unsigned j = 0; j < formulas2.size(); j++) {
|
|
|
|
if (formulas1[i].group() == formulas2[j].group()) {
|
2012-03-31 23:27:37 +01:00
|
|
|
Util::addToVector (X_1, formulas1[i].logVars());
|
|
|
|
Util::addToVector (X_2, formulas2[j].logVars());
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LogVarSet Y_1 = g1->logVarSet() - LogVarSet (X_1);
|
|
|
|
LogVarSet Y_2 = g2->logVarSet() - LogVarSet (X_2);
|
2012-04-28 01:10:04 +01:00
|
|
|
// counting log vars were already raised on counting conversion
|
|
|
|
Y_1 -= g1->countedLogVars();
|
|
|
|
Y_2 -= g2->countedLogVars();
|
2012-03-22 11:33:24 +00:00
|
|
|
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);
|
2012-03-31 23:27:37 +01:00
|
|
|
LogAware::pow (g1->params(), 1.0 / condCount2);
|
|
|
|
LogAware::pow (g2->params(), 1.0 / condCount1);
|
2012-04-26 18:00:06 +01:00
|
|
|
// the alignment should be done in the end or else X_1 and X_2
|
|
|
|
// will refer to the old log var names on the code above
|
2012-03-31 23:27:37 +01:00
|
|
|
align (g1, X_1, g2, X_2);
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Parfactor::align (
|
|
|
|
Parfactor* g1, const LogVars& alignLvs1,
|
|
|
|
Parfactor* g2, const LogVars& alignLvs2)
|
|
|
|
{
|
|
|
|
LogVar freeLogVar = 0;
|
2012-04-26 18:00:06 +01:00
|
|
|
Substitution theta1, theta2;
|
|
|
|
for (unsigned i = 0; i < alignLvs1.size(); i++) {
|
|
|
|
bool b1 = theta1.containsReplacementFor (alignLvs1[i]);
|
|
|
|
bool b2 = theta2.containsReplacementFor (alignLvs2[i]);
|
|
|
|
// handle this type of situation:
|
|
|
|
// g1 = p(X), q(X) ; X in {x1}
|
|
|
|
// g2 = p(X), q(Y) ; X in {x1}, Y in {x1}
|
|
|
|
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]));
|
|
|
|
}
|
|
|
|
}
|
2012-03-22 11:33:24 +00:00
|
|
|
const LogVarSet& allLvs1 = g1->logVarSet();
|
|
|
|
for (unsigned i = 0; i < allLvs1.size(); i++) {
|
2012-04-26 18:00:06 +01:00
|
|
|
if (theta1.containsReplacementFor (allLvs1[i]) == false) {
|
|
|
|
theta1.add (allLvs1[i], freeLogVar);
|
|
|
|
++ freeLogVar;
|
|
|
|
}
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
|
|
|
const LogVarSet& allLvs2 = g2->logVarSet();
|
|
|
|
for (unsigned i = 0; i < allLvs2.size(); i++) {
|
2012-04-26 18:00:06 +01:00
|
|
|
if (theta2.containsReplacementFor (allLvs2[i]) == false) {
|
|
|
|
theta2.add (allLvs2[i], freeLogVar);
|
|
|
|
++ freeLogVar;
|
|
|
|
}
|
2012-03-22 11:33:24 +00:00
|
|
|
}
|
2012-04-26 18:00:06 +01:00
|
|
|
// cout << "theta1: " << theta1 << endl;
|
|
|
|
// cout << "theta2: " << theta2 << endl;
|
2012-03-22 11:33:24 +00:00
|
|
|
g1->applySubstitution (theta1);
|
|
|
|
g2->applySubstitution (theta2);
|
|
|
|
}
|
|
|
|
|