size_t invasion

This commit is contained in:
Tiago Gomes
2012-05-24 22:55:20 +01:00
parent 5124098b6c
commit af6601837c
36 changed files with 513 additions and 546 deletions

View File

@@ -42,7 +42,7 @@ LiftedOperator::printValidOps (
{
vector<LiftedOperator*> validOps;
validOps = LiftedOperator::getValidOps (pfList, query);
for (unsigned i = 0; i < validOps.size(); i++) {
for (size_t i = 0; i < validOps.size(); i++) {
cout << "-> " << validOps[i]->toString();
delete validOps[i];
}
@@ -148,13 +148,13 @@ ProductOperator::validOp (Parfactor* g1, Parfactor* g2)
TinySet<unsigned> g2_gs (g2->getAllGroups());
if (g1_gs.contains (g2_gs) || g2_gs.contains (g1_gs)) {
TinySet<unsigned> intersect = g1_gs & g2_gs;
for (unsigned i = 0; i < intersect.size(); i++) {
for (size_t i = 0; i < intersect.size(); i++) {
if (g1->nrFormulasWithGroup (intersect[i]) != 1 ||
g2->nrFormulasWithGroup (intersect[i]) != 1) {
return false;
}
int idx1 = g1->indexOfGroup (intersect[i]);
int idx2 = g2->indexOfGroup (intersect[i]);
size_t idx1 = g1->indexOfGroup (intersect[i]);
size_t idx2 = g2->indexOfGroup (intersect[i]);
if (g1->range (idx1) != g2->range (idx2)) {
return false;
}
@@ -185,11 +185,11 @@ SumOutOperator::getLogCost (void)
return std::log (0.0);
}
double cost = 1.0;
for (unsigned i = 0; i < groupSet.size(); i++) {
for (size_t i = 0; i < groupSet.size(); i++) {
pfIter = pfList_.begin();
while (pfIter != pfList_.end()) {
if ((*pfIter)->containsGroup (groupSet[i])) {
int idx = (*pfIter)->indexOfGroup (groupSet[i]);
size_t idx = (*pfIter)->indexOfGroup (groupSet[i]);
cost *= (*pfIter)->range (idx);
break;
}
@@ -208,7 +208,7 @@ SumOutOperator::apply (void)
iters = getParfactorsWithGroup (pfList_, group_);
Parfactor* product = *(iters[0]);
pfList_.remove (iters[0]);
for (unsigned i = 1; i < iters.size(); i++) {
for (size_t i = 1; i < iters.size(); i++) {
product->multiply (**(iters[i]));
pfList_.removeAndDelete (iters[i]);
}
@@ -216,14 +216,14 @@ SumOutOperator::apply (void)
delete product;
return;
}
int fIdx = product->indexOfGroup (group_);
size_t fIdx = product->indexOfGroup (group_);
LogVarSet excl = product->exclusiveLogVars (fIdx);
if (product->constr()->isCountNormalized (excl)) {
product->sumOut (fIdx);
pfList_.addShattered (product);
} else {
Parfactors pfs = FoveSolver::countNormalize (product, excl);
for (unsigned i = 0; i < pfs.size(); i++) {
for (size_t i = 0; i < pfs.size(); i++) {
pfs[i]->sumOut (fIdx);
pfList_.add (pfs[i]);
}
@@ -243,7 +243,7 @@ SumOutOperator::getValidOps (
ParfactorList::const_iterator it = pfList.begin();
while (it != pfList.end()) {
const ProbFormulas& formulas = (*it)->arguments();
for (unsigned i = 0; i < formulas.size(); i++) {
for (size_t i = 0; i < formulas.size(); i++) {
allGroups.insert (formulas[i].group());
}
++ it;
@@ -266,7 +266,7 @@ SumOutOperator::toString (void)
stringstream ss;
vector<ParfactorList::iterator> pfIters;
pfIters = getParfactorsWithGroup (pfList_, group_);
int idx = (*pfIters[0])->indexOfGroup (group_);
size_t idx = (*pfIters[0])->indexOfGroup (group_);
ProbFormula f = (*pfIters[0])->argument (idx);
TupleSet tupleSet = (*pfIters[0])->constr()->tupleSet (f.logVars());
ss << "sum out " << f.functor() << "/" << f.arity();
@@ -289,11 +289,11 @@ SumOutOperator::validOp (
return false;
}
int range = -1;
for (unsigned i = 0; i < pfIters.size(); i++) {
for (size_t i = 0; i < pfIters.size(); i++) {
if ((*pfIters[i])->nrFormulasWithGroup (group) > 1) {
return false;
}
int fIdx = (*pfIters[i])->indexOfGroup (group);
size_t fIdx = (*pfIters[i])->indexOfGroup (group);
if ((*pfIters[i])->argument (fIdx).contains (
(*pfIters[i])->elimLogVars()) == false) {
return false;
@@ -315,10 +315,10 @@ SumOutOperator::isToEliminate (
unsigned group,
const Grounds& query)
{
int fIdx = g->indexOfGroup (group);
size_t fIdx = g->indexOfGroup (group);
const ProbFormula& formula = g->argument (fIdx);
bool toElim = true;
for (unsigned i = 0; i < query.size(); i++) {
for (size_t i = 0; i < query.size(); i++) {
if (formula.functor() == query[i].functor() &&
formula.arity() == query[i].arity()) {
g->constr()->moveToTop (formula.logVars());
@@ -337,23 +337,23 @@ double
CountingOperator::getLogCost (void)
{
double cost = 0.0;
int fIdx = (*pfIter_)->indexOfLogVar (X_);
size_t fIdx = (*pfIter_)->indexOfLogVar (X_);
unsigned range = (*pfIter_)->range (fIdx);
unsigned size = (*pfIter_)->size() / range;
TinySet<unsigned> counts;
counts = (*pfIter_)->constr()->getConditionalCounts (X_);
for (unsigned i = 0; i < counts.size(); i++) {
for (size_t i = 0; i < counts.size(); i++) {
cost += size * HistogramSet::nrHistograms (counts[i], range);
}
unsigned group = (*pfIter_)->argument (fIdx).group();
int lvIndex = Util::indexOf (
size_t lvIndex = Util::indexOf (
(*pfIter_)->argument (fIdx).logVars(), X_);
assert (lvIndex != -1);
assert (lvIndex != (*pfIter_)->argument (fIdx).logVars().size());
ParfactorList::iterator pfIter = pfList_.begin();
while (pfIter != pfList_.end()) {
if (pfIter != pfIter_) {
int fIdx2 = (*pfIter)->indexOfGroup (group);
if (fIdx2 != -1) {
size_t fIdx2 = (*pfIter)->indexOfGroup (group);
if (fIdx2 != (*pfIter)->nrArguments()) {
LogVar Y = ((*pfIter)->argument (fIdx2).logVars()[lvIndex]);
if ((*pfIter)->canCountConvert (Y) == false) {
// the real cost should be the cost of grounding Y
@@ -377,7 +377,7 @@ CountingOperator::apply (void)
Parfactor* pf = *pfIter_;
pfList_.remove (pfIter_);
Parfactors pfs = FoveSolver::countNormalize (pf, X_);
for (unsigned i = 0; i < pfs.size(); i++) {
for (size_t i = 0; i < pfs.size(); i++) {
unsigned condCount = pfs[i]->constr()->getConditionalCount (X_);
bool cartProduct = pfs[i]->constr()->isCartesianProduct (
pfs[i]->countedLogVars() | X_);
@@ -399,7 +399,7 @@ CountingOperator::getValidOps (ParfactorList& pfList)
ParfactorList::iterator it = pfList.begin();
while (it != pfList.end()) {
LogVarSet candidates = (*it)->uncountedLogVars();
for (unsigned i = 0; i < candidates.size(); i++) {
for (size_t i = 0; i < candidates.size(); i++) {
if (validOp (*it, candidates[i])) {
validOps.push_back (new CountingOperator (
it, candidates[i], pfList));
@@ -422,11 +422,11 @@ CountingOperator::toString (void)
ss << " [cost=" << std::exp (getLogCost()) << "]" << endl;
Parfactors pfs = FoveSolver::countNormalize (*pfIter_, X_);
if ((*pfIter_)->constr()->isCountNormalized (X_) == false) {
for (unsigned i = 0; i < pfs.size(); i++) {
for (size_t i = 0; i < pfs.size(); i++) {
ss << " º " << pfs[i]->getLabel() << endl;
}
}
for (unsigned i = 0; i < pfs.size(); i++) {
for (size_t i = 0; i < pfs.size(); i++) {
delete pfs[i];
}
return ss.str();
@@ -440,7 +440,7 @@ CountingOperator::validOp (Parfactor* g, LogVar X)
if (g->nrFormulas (X) != 1) {
return false;
}
int fIdx = g->indexOfLogVar (X);
size_t fIdx = g->indexOfLogVar (X);
if (g->argument (fIdx).isCounting()) {
return false;
}
@@ -459,7 +459,7 @@ GroundOperator::getLogCost (void)
vector<pair<unsigned, unsigned>> affectedFormulas;
affectedFormulas = getAffectedFormulas();
// cout << "affected formulas: " ;
// for (unsigned i = 0; i < affectedFormulas.size(); i++) {
// for (size_t i = 0; i < affectedFormulas.size(); i++) {
// cout << affectedFormulas[i].first << ":" ;
// cout << affectedFormulas[i].second << " " ;
// }
@@ -472,9 +472,9 @@ GroundOperator::getLogCost (void)
double pfSize = std::log (pf->size());
bool willBeAffected = false;
LogVarSet lvsToGround;
for (unsigned i = 0; i < affectedFormulas.size(); i++) {
int fIdx = pf->indexOfGroup (affectedFormulas[i].first);
if (fIdx != -1) {
for (size_t i = 0; i < affectedFormulas.size(); i++) {
size_t fIdx = pf->indexOfGroup (affectedFormulas[i].first);
if (fIdx != pf->nrArguments()) {
ProbFormula f = pf->argument (fIdx);
LogVar X = f.logVars()[affectedFormulas[i].second];
bool isCountingLv = pf->countedLogVars().contains (X);
@@ -514,7 +514,7 @@ GroundOperator::apply (void)
ParfactorList::iterator pfIter;
pfIter = getParfactorsWithGroup (pfList_, group_).front();
Parfactor* pf = *pfIter;
int idx = pf->indexOfGroup (group_);
size_t idx = pf->indexOfGroup (group_);
ProbFormula f = pf->argument (idx);
LogVar X = f.logVars()[lvIndex_];
bool countedLv = pf->countedLogVars().contains (X);
@@ -524,7 +524,7 @@ GroundOperator::apply (void)
pfList_.add (pf);
} else {
ConstraintTrees cts = pf->constr()->ground (X);
for (unsigned i = 0; i < cts.size(); i++) {
for (size_t i = 0; i < cts.size(); i++) {
pfList_.add (new Parfactor (pf, cts[i]));
}
delete pf;
@@ -546,10 +546,10 @@ GroundOperator::getValidOps (ParfactorList& pfList)
ParfactorList::const_iterator it = pfList.begin();
while (it != pfList.end()) {
const ProbFormulas& formulas = (*it)->arguments();
for (unsigned i = 0; i < formulas.size(); i++) {
for (size_t i = 0; i < formulas.size(); i++) {
if (Util::contains (allGroups, formulas[i].group()) == false) {
const LogVars& lvs = formulas[i].logVars();
for (unsigned j = 0; j < lvs.size(); j++) {
for (size_t j = 0; j < lvs.size(); j++) {
if ((*it)->constr()->isSingleton (lvs[j]) == false) {
validOps.push_back (new GroundOperator (
formulas[i].group(), j, pfList));
@@ -572,7 +572,7 @@ GroundOperator::toString (void)
vector<ParfactorList::iterator> pfIters;
pfIters = getParfactorsWithGroup (pfList_, group_);
Parfactor* pf = *(getParfactorsWithGroup (pfList_, group_).front());
int idx = pf->indexOfGroup (group_);
size_t idx = pf->indexOfGroup (group_);
ProbFormula f = pf->argument (idx);
LogVar lv = f.logVars()[lvIndex_];
TupleSet tupleSet = pf->constr()->tupleSet ({lv});
@@ -604,12 +604,12 @@ GroundOperator::getAffectedFormulas (void)
pair<unsigned, unsigned> front = q.front();
ParfactorList::iterator pflIt = pfList_.begin();
while (pflIt != pfList_.end()) {
int idx = (*pflIt)->indexOfGroup (front.first);
if (idx != -1) {
size_t idx = (*pflIt)->indexOfGroup (front.first);
if (idx != (*pflIt)->nrArguments()) {
ProbFormula f = (*pflIt)->argument (idx);
LogVar X = f.logVars()[front.second];
const ProbFormulas& fs = (*pflIt)->arguments();
for (unsigned i = 0; i < fs.size(); i++) {
for (size_t i = 0; i < fs.size(); i++) {
if ((int)i != idx && fs[i].contains (X)) {
pair<unsigned, unsigned> pair = make_pair (
fs[i].group(), fs[i].indexOf (X));
@@ -668,7 +668,7 @@ FoveSolver::absorveEvidence (
ParfactorList& pfList,
ObservedFormulas& obsFormulas)
{
for (unsigned i = 0; i < obsFormulas.size(); i++) {
for (size_t i = 0; i < obsFormulas.size(); i++) {
Parfactors newPfs;
ParfactorList::iterator it = pfList.begin();
while (it != pfList.end()) {
@@ -692,7 +692,7 @@ FoveSolver::absorveEvidence (
if (Globals::verbosity > 2 && obsFormulas.empty() == false) {
Util::printAsteriskLine();
cout << "AFTER EVIDENCE ABSORVED" << endl;
for (unsigned i = 0; i < obsFormulas.size(); i++) {
for (size_t i = 0; i < obsFormulas.size(); i++) {
cout << " -> " << obsFormulas[i] << endl;
}
Util::printAsteriskLine();
@@ -712,7 +712,7 @@ FoveSolver::countNormalize (
normPfs.push_back (new Parfactor (*g));
} else {
ConstraintTrees normCts = g->constr()->countNormalize (set);
for (unsigned i = 0; i < normCts.size(); i++) {
for (size_t i = 0; i < normCts.size(); i++) {
normPfs.push_back (new Parfactor (g, normCts[i]));
}
}
@@ -727,17 +727,17 @@ FoveSolver::calcGroundMultiplication (Parfactor pf)
LogVarSet lvs = pf.constr()->logVarSet();
lvs -= pf.constr()->singletons();
Parfactors newPfs = {new Parfactor (pf)};
for (unsigned i = 0; i < lvs.size(); i++) {
for (size_t i = 0; i < lvs.size(); i++) {
Parfactors pfs = newPfs;
newPfs.clear();
for (unsigned j = 0; j < pfs.size(); j++) {
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 (unsigned k = 0; k < cts.size(); k++) {
for (size_t k = 0; k < cts.size(); k++) {
newPfs.push_back (new Parfactor (pfs[j], cts[k]));
}
delete pfs[j];
@@ -746,7 +746,7 @@ FoveSolver::calcGroundMultiplication (Parfactor pf)
}
ParfactorList pfList (newPfs);
Parfactors groundShatteredPfs (pfList.begin(),pfList.end());
for (unsigned i = 1; i < groundShatteredPfs.size(); i++) {
for (size_t i = 1; i < groundShatteredPfs.size(); i++) {
groundShatteredPfs[0]->multiply (*groundShatteredPfs[i]);
}
return Parfactor (*groundShatteredPfs[0]);
@@ -807,7 +807,7 @@ FoveSolver::getBestOperation (const Grounds& query)
LiftedOperator* bestOp = 0;
vector<LiftedOperator*> validOps;
validOps = LiftedOperator::getValidOps (pfList_, query);
for (unsigned i = 0; i < validOps.size(); i++) {
for (size_t i = 0; i < validOps.size(); i++) {
double cost = validOps[i]->getLogCost();
if ((bestOp == 0) || (cost < bestCost)) {
bestOp = validOps[i];
@@ -817,7 +817,7 @@ FoveSolver::getBestOperation (const Grounds& query)
if (bestCost > largestCost_) {
largestCost_ = bestCost;
}
for (unsigned i = 0; i < validOps.size(); i++) {
for (size_t i = 0; i < validOps.size(); i++) {
if (validOps[i] != bestOp) {
delete validOps[i];
}
@@ -832,7 +832,7 @@ FoveSolver::runWeakBayesBall (const Grounds& query)
{
queue<unsigned> todo; // groups to process
set<unsigned> done; // processed or in queue
for (unsigned i = 0; i < query.size(); i++) {
for (size_t i = 0; i < query.size(); i++) {
ParfactorList::iterator it = pfList_.begin();
while (it != pfList_.end()) {
int group = (*it)->findGroup (query[i]);
@@ -853,7 +853,7 @@ FoveSolver::runWeakBayesBall (const Grounds& query)
if (Util::contains (requiredPfs, *it) == false &&
(*it)->containsGroup (group)) {
vector<unsigned> groups = (*it)->getAllGroups();
for (unsigned i = 0; i < groups.size(); i++) {
for (size_t i = 0; i < groups.size(); i++) {
if (Util::contains (done, groups[i]) == false) {
todo.push (groups[i]);
done.insert (groups[i]);
@@ -889,7 +889,7 @@ FoveSolver::runWeakBayesBall (const Grounds& query)
void
FoveSolver::shatterAgainstQuery (const Grounds& query)
{
for (unsigned i = 0; i < query.size(); i++) {
for (size_t i = 0; i < query.size(); i++) {
if (query[i].isAtom()) {
continue;
}
@@ -927,7 +927,7 @@ FoveSolver::shatterAgainstQuery (const Grounds& query)
if (Globals::verbosity > 2) {
Util::printAsteriskLine();
cout << "SHATTERED AGAINST THE QUERY" << endl;
for (unsigned i = 0; i < query.size(); i++) {
for (size_t i = 0; i < query.size(); i++) {
cout << " -> " << query[i] << endl;
}
Util::printAsteriskLine();
@@ -944,7 +944,7 @@ FoveSolver::absorve (
{
Parfactors absorvedPfs;
const ProbFormulas& formulas = g->arguments();
for (unsigned i = 0; i < formulas.size(); i++) {
for (size_t i = 0; i < formulas.size(); i++) {
if (obsFormula.functor() == formulas[i].functor() &&
obsFormula.arity() == formulas[i].arity()) {
@@ -971,7 +971,7 @@ FoveSolver::absorve (
if (formulas.size() > 1) {
LogVarSet excl = g->exclusiveLogVars (i);
Parfactors countNormPfs = countNormalize (g, excl);
for (unsigned j = 0; j < countNormPfs.size(); j++) {
for (size_t j = 0; j < countNormPfs.size(); j++) {
countNormPfs[j]->absorveEvidence (
formulas[i], obsFormula.evidence());
absorvedPfs.push_back (countNormPfs[j]);