2013-02-07 20:09:10 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <queue>
|
|
|
|
#include <iostream>
|
|
|
|
|
2012-11-09 23:52:35 +00:00
|
|
|
#include "LiftedOperations.h"
|
|
|
|
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
namespace Horus {
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2013-02-16 16:00:53 +00:00
|
|
|
namespace LiftedOperations {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
Parfactors absorve (ObservedFormula& obsFormula, Parfactor* g);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-11-09 23:52:35 +00:00
|
|
|
void
|
2013-02-16 16:00:53 +00:00
|
|
|
shatterAgainstQuery (ParfactorList& pfList, const Grounds& query)
|
2012-11-09 23:52:35 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < query.size(); i++) {
|
|
|
|
if (query[i].isAtom()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bool found = false;
|
|
|
|
Parfactors newPfs;
|
|
|
|
ParfactorList::iterator it = pfList.begin();
|
|
|
|
while (it != pfList.end()) {
|
|
|
|
if ((*it)->containsGround (query[i])) {
|
|
|
|
found = true;
|
|
|
|
std::pair<ConstraintTree*, ConstraintTree*> split;
|
|
|
|
LogVars queryLvs (
|
|
|
|
(*it)->constr()->logVars().begin(),
|
|
|
|
(*it)->constr()->logVars().begin() + query[i].arity());
|
|
|
|
split = (*it)->constr()->split (query[i].args());
|
|
|
|
ConstraintTree* commCt = split.first;
|
|
|
|
ConstraintTree* exclCt = split.second;
|
|
|
|
newPfs.push_back (new Parfactor (*it, commCt));
|
|
|
|
if (exclCt->empty() == false) {
|
|
|
|
newPfs.push_back (new Parfactor (*it, exclCt));
|
|
|
|
} else {
|
|
|
|
delete exclCt;
|
|
|
|
}
|
|
|
|
it = pfList.removeAndDelete (it);
|
|
|
|
} else {
|
|
|
|
++ it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found == false) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cerr << "Error: could not find a parfactor with ground " ;
|
|
|
|
std::cerr << "`" << query[i] << "'." << std::endl;
|
2012-12-20 17:37:59 +00:00
|
|
|
exit (EXIT_FAILURE);
|
2012-11-09 23:52:35 +00:00
|
|
|
}
|
|
|
|
pfList.add (newPfs);
|
|
|
|
}
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::verbosity > 2) {
|
|
|
|
Util::printAsteriskLine();
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << "SHATTERED AGAINST THE QUERY" << std::endl;
|
2012-11-09 23:52:35 +00:00
|
|
|
for (size_t i = 0; i < query.size(); i++) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << " -> " << query[i] << std::endl;
|
2012-11-09 23:52:35 +00:00
|
|
|
}
|
2013-02-08 21:01:53 +00:00
|
|
|
Util::printAsteriskLine();
|
2012-11-09 23:52:35 +00:00
|
|
|
pfList.print();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-10 00:18:20 +00:00
|
|
|
void
|
2013-02-16 16:00:53 +00:00
|
|
|
runWeakBayesBall (ParfactorList& pfList, const Grounds& query)
|
2012-11-10 00:18:20 +00:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::queue<PrvGroup> todo; // groups to process
|
|
|
|
std::set<PrvGroup> done; // processed or in queue
|
2012-11-10 00:18:20 +00:00
|
|
|
for (size_t i = 0; i < query.size(); i++) {
|
|
|
|
ParfactorList::iterator it = pfList.begin();
|
|
|
|
while (it != pfList.end()) {
|
|
|
|
PrvGroup group = (*it)->findGroup (query[i]);
|
2012-12-27 12:54:58 +00:00
|
|
|
if (group != std::numeric_limits<PrvGroup>::max()) {
|
2012-11-10 00:18:20 +00:00
|
|
|
todo.push (group);
|
|
|
|
done.insert (group);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++ it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
std::set<Parfactor*> requiredPfs;
|
2012-11-10 00:18:20 +00:00
|
|
|
while (todo.empty() == false) {
|
|
|
|
PrvGroup group = todo.front();
|
|
|
|
ParfactorList::iterator it = pfList.begin();
|
|
|
|
while (it != pfList.end()) {
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Util::contains (requiredPfs, *it) == false &&
|
2012-11-10 00:18:20 +00:00
|
|
|
(*it)->containsGroup (group)) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<PrvGroup> groups = (*it)->getAllGroups();
|
2012-11-10 00:18:20 +00:00
|
|
|
for (size_t i = 0; i < groups.size(); i++) {
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Util::contains (done, groups[i]) == false) {
|
2012-11-10 00:18:20 +00:00
|
|
|
todo.push (groups[i]);
|
|
|
|
done.insert (groups[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
requiredPfs.insert (*it);
|
|
|
|
}
|
|
|
|
++ it;
|
|
|
|
}
|
|
|
|
todo.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
ParfactorList::iterator it = pfList.begin();
|
|
|
|
bool foundNotRequired = false;
|
|
|
|
while (it != pfList.end()) {
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Util::contains (requiredPfs, *it) == false) {
|
|
|
|
if (Globals::verbosity > 2) {
|
2012-11-10 00:18:20 +00:00
|
|
|
if (foundNotRequired == false) {
|
2013-02-08 21:01:53 +00:00
|
|
|
Util::printHeader ("PARFACTORS TO DISCARD");
|
2012-11-10 00:18:20 +00:00
|
|
|
foundNotRequired = true;
|
|
|
|
}
|
|
|
|
(*it)->print();
|
|
|
|
}
|
|
|
|
it = pfList.removeAndDelete (it);
|
|
|
|
} else {
|
|
|
|
++ it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-09 23:52:35 +00:00
|
|
|
void
|
2013-02-16 16:00:53 +00:00
|
|
|
absorveEvidence (ParfactorList& pfList, ObservedFormulas& obsFormulas)
|
2012-11-09 23:52:35 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < obsFormulas.size(); i++) {
|
|
|
|
Parfactors newPfs;
|
|
|
|
ParfactorList::iterator it = pfList.begin();
|
|
|
|
while (it != pfList.end()) {
|
|
|
|
Parfactor* pf = *it;
|
|
|
|
it = pfList.remove (it);
|
|
|
|
Parfactors absorvedPfs = absorve (obsFormulas[i], pf);
|
|
|
|
if (absorvedPfs.empty() == false) {
|
2012-12-27 12:54:58 +00:00
|
|
|
if (absorvedPfs.size() == 1 && !absorvedPfs[0]) {
|
2012-11-09 23:52:35 +00:00
|
|
|
// just remove pf;
|
|
|
|
} else {
|
2013-02-08 21:01:53 +00:00
|
|
|
Util::addToVector (newPfs, absorvedPfs);
|
2012-11-09 23:52:35 +00:00
|
|
|
}
|
|
|
|
delete pf;
|
|
|
|
} else {
|
|
|
|
it = pfList.insertShattered (it, pf);
|
|
|
|
++ it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pfList.add (newPfs);
|
|
|
|
}
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Globals::verbosity > 2 && obsFormulas.empty() == false) {
|
|
|
|
Util::printAsteriskLine();
|
2013-02-16 15:30:44 +00:00
|
|
|
std::cout << "AFTER EVIDENCE ABSORVED" << std::endl;
|
2012-11-09 23:52:35 +00:00
|
|
|
for (size_t i = 0; i < obsFormulas.size(); i++) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cout << " -> " << obsFormulas[i] << std::endl;
|
2012-11-09 23:52:35 +00:00
|
|
|
}
|
2013-02-08 21:01:53 +00:00
|
|
|
Util::printAsteriskLine();
|
2012-11-09 23:52:35 +00:00
|
|
|
pfList.print();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parfactors
|
2013-02-16 16:00:53 +00:00
|
|
|
countNormalize (Parfactor* g, const LogVarSet& set)
|
2012-11-09 23:52:35 +00:00
|
|
|
{
|
|
|
|
Parfactors normPfs;
|
|
|
|
if (set.empty()) {
|
|
|
|
normPfs.push_back (new Parfactor (*g));
|
|
|
|
} else {
|
|
|
|
ConstraintTrees normCts = g->constr()->countNormalize (set);
|
|
|
|
for (size_t i = 0; i < normCts.size(); i++) {
|
|
|
|
normPfs.push_back (new Parfactor (g, normCts[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return normPfs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parfactor
|
2013-02-16 16:00:53 +00:00
|
|
|
calcGroundMultiplication (Parfactor pf)
|
2012-11-09 23:52:35 +00:00
|
|
|
{
|
|
|
|
LogVarSet lvs = pf.constr()->logVarSet();
|
|
|
|
lvs -= pf.constr()->singletons();
|
|
|
|
Parfactors newPfs = {new Parfactor (pf)};
|
|
|
|
for (size_t i = 0; i < lvs.size(); i++) {
|
|
|
|
Parfactors pfs = newPfs;
|
|
|
|
newPfs.clear();
|
|
|
|
for (size_t j = 0; j < pfs.size(); j++) {
|
|
|
|
bool countedLv = pfs[j]->countedLogVars().contains (lvs[i]);
|
|
|
|
if (countedLv) {
|
|
|
|
pfs[j]->fullExpand (lvs[i]);
|
|
|
|
newPfs.push_back (pfs[j]);
|
|
|
|
} else {
|
|
|
|
ConstraintTrees cts = pfs[j]->constr()->ground (lvs[i]);
|
|
|
|
for (size_t k = 0; k < cts.size(); k++) {
|
|
|
|
newPfs.push_back (new Parfactor (pfs[j], cts[k]));
|
|
|
|
}
|
|
|
|
delete pfs[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ParfactorList pfList (newPfs);
|
|
|
|
Parfactors groundShatteredPfs (pfList.begin(),pfList.end());
|
|
|
|
for (size_t i = 1; i < groundShatteredPfs.size(); i++) {
|
|
|
|
groundShatteredPfs[0]->multiply (*groundShatteredPfs[i]);
|
|
|
|
}
|
|
|
|
return Parfactor (*groundShatteredPfs[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-16 16:00:53 +00:00
|
|
|
namespace {
|
|
|
|
|
2012-11-09 23:52:35 +00:00
|
|
|
Parfactors
|
2013-02-16 16:00:53 +00:00
|
|
|
absorve (ObservedFormula& obsFormula, Parfactor* g)
|
2012-11-09 23:52:35 +00:00
|
|
|
{
|
|
|
|
Parfactors absorvedPfs;
|
|
|
|
const ProbFormulas& formulas = g->arguments();
|
|
|
|
for (size_t i = 0; i < formulas.size(); i++) {
|
|
|
|
if (obsFormula.functor() == formulas[i].functor() &&
|
|
|
|
obsFormula.arity() == formulas[i].arity()) {
|
|
|
|
|
|
|
|
if (obsFormula.isAtom()) {
|
|
|
|
if (formulas.size() > 1) {
|
|
|
|
g->absorveEvidence (formulas[i], obsFormula.evidence());
|
|
|
|
} else {
|
|
|
|
// hack to erase parfactor g
|
|
|
|
absorvedPfs.push_back (0);
|
|
|
|
}
|
|
|
|
break;
|
2012-12-20 23:19:10 +00:00
|
|
|
}
|
2012-11-09 23:52:35 +00:00
|
|
|
|
|
|
|
g->constr()->moveToTop (formulas[i].logVars());
|
|
|
|
std::pair<ConstraintTree*, ConstraintTree*> res;
|
|
|
|
res = g->constr()->split (
|
|
|
|
formulas[i].logVars(),
|
|
|
|
&(obsFormula.constr()),
|
|
|
|
obsFormula.constr().logVars());
|
|
|
|
ConstraintTree* commCt = res.first;
|
|
|
|
ConstraintTree* exclCt = res.second;
|
|
|
|
|
|
|
|
if (commCt->empty() == false) {
|
|
|
|
if (formulas.size() > 1) {
|
|
|
|
LogVarSet excl = g->exclusiveLogVars (i);
|
|
|
|
Parfactor tempPf (g, commCt);
|
|
|
|
Parfactors countNormPfs = LiftedOperations::countNormalize (
|
|
|
|
&tempPf, excl);
|
|
|
|
for (size_t j = 0; j < countNormPfs.size(); j++) {
|
|
|
|
countNormPfs[j]->absorveEvidence (
|
|
|
|
formulas[i], obsFormula.evidence());
|
|
|
|
absorvedPfs.push_back (countNormPfs[j]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delete commCt;
|
|
|
|
}
|
|
|
|
if (exclCt->empty() == false) {
|
|
|
|
absorvedPfs.push_back (new Parfactor (g, exclCt));
|
|
|
|
} else {
|
|
|
|
delete exclCt;
|
|
|
|
}
|
|
|
|
if (absorvedPfs.empty()) {
|
|
|
|
// hack to erase parfactor g
|
|
|
|
absorvedPfs.push_back (0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
delete commCt;
|
|
|
|
delete exclCt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return absorvedPfs;
|
|
|
|
}
|
|
|
|
|
2013-02-16 16:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace LiftedOperations
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
} // namespace Horus
|
2013-02-07 23:53:13 +00:00
|
|
|
|