00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 #include "clause.h"
00068 #include "domain.h"
00069 #include "database.h"
00070 #include "mln.h"
00071 #include "truefalsegroundingsstore.h"
00072
00073 const char* Domain::PROPOSITIONAL_TYPE = "AlchemyPropositionalType";
00074 const char* Domain::PROPOSITIONAL_CONSTANT = "AlchemyPropositionalConstant";
00075
00076 Domain::~Domain()
00077 {
00078 if (typeDualMap_) delete typeDualMap_;
00079 if (constDualMap_) delete constDualMap_;
00080
00081 if (strToPredTemplateMap_)
00082 {
00083
00084
00085 StrToPredTemplateMap::iterator it = strToPredTemplateMap_->begin();
00086 for (; it != strToPredTemplateMap_->end(); it++)
00087 delete (*it).second;
00088 delete strToPredTemplateMap_;
00089 }
00090 if (predDualMap_) delete predDualMap_;
00091
00092 if (strToFuncTemplateMap_)
00093 {
00094
00095
00096 StrToFuncTemplateMap::iterator it2 = strToFuncTemplateMap_->begin();
00097 for (; it2 != strToFuncTemplateMap_->end(); it2++)
00098 delete (*it2).second;
00099 delete strToFuncTemplateMap_;
00100 }
00101 if (funcDualMap_) delete funcDualMap_;
00102
00103 if (equalPredTemplate_) delete equalPredTemplate_;
00104
00105 if (emptyPredTemplate_) delete emptyPredTemplate_;
00106
00107 if (emptyFuncUnaryTemplate_) delete emptyFuncUnaryTemplate_;
00108
00109 if (emptyFuncBinaryTemplate_) delete emptyFuncBinaryTemplate_;
00110
00111 if (constantsByType_)
00112 {
00113 for (int i = 0; i < constantsByType_->size(); i++)
00114 delete (*constantsByType_)[i];
00115 delete constantsByType_;
00116 }
00117
00118 if (externalConstantsByType_)
00119 {
00120 for (int i = 0; i < externalConstantsByType_->size(); i++)
00121 delete (*externalConstantsByType_)[i];
00122 delete externalConstantsByType_;
00123 }
00124
00125 if (predBlocks_)
00126 {
00127 for (int i = 0; i < predBlocks_->size(); i++)
00128 delete (*predBlocks_)[i];
00129 delete predBlocks_;
00130 }
00131
00132 if (truePredsInBlock_)
00133 {
00134 for (int i = 0; i < truePredsInBlock_->size(); i++)
00135 delete (*truePredsInBlock_)[i];
00136 delete truePredsInBlock_;
00137 }
00138
00139 if (blockSizes_) delete blockSizes_;
00140
00141 if (blockEvidence_) delete blockEvidence_;
00142
00143 if (db_) delete db_;
00144
00145 if (trueFalseGroundingsStore_) delete trueFalseGroundingsStore_;
00146
00147 if (funcSet_)
00148 {
00149 FunctionSet::iterator fit;
00150 while (!funcSet_->empty())
00151 {
00152 fit = funcSet_->begin();
00153 funcSet_->erase(fit);
00154 delete *fit;
00155 }
00156 delete funcSet_;
00157 }
00158
00159
00160
00161 if (numNonEvidAtomsPerPred_) delete numNonEvidAtomsPerPred_;
00162 if (numTrueNonEvidGndingsPerClause_) delete numTrueNonEvidGndingsPerClause_;
00163 if (numFalseNonEvidGndingsPerClause_) delete numFalseNonEvidGndingsPerClause_;
00164 }
00165
00166
00167 void Domain::deleteDB() { if (db_) delete db_; db_ = NULL; }
00168
00169
00170 void Domain::newTrueFalseGroundingsStore()
00171 { trueFalseGroundingsStore_ = new TrueFalseGroundingsStore(this); }
00172
00173
00174 void Domain::compress()
00175 {
00176 typeDualMap_->compress();
00177 constDualMap_->compress();
00178 predDualMap_->compress();
00179 funcDualMap_->compress();
00180 constantsByType_->compress();
00181 for (int i = 0; i < constantsByType_->size(); i++)
00182 (*constantsByType_)[i]->compress();
00183 externalConstantsByType_->compress();
00184 for (int i = 0; i < externalConstantsByType_->size(); i++)
00185 (*externalConstantsByType_)[i]->compress();
00186 db_->compress();
00187 }
00188
00194 void Domain::updatePerOldToNewIds(MLN* const & mln,
00195 hash_map<int,int> & oldToNewConstIds)
00196 {
00197
00198 const ClauseHashArray* clauses = mln->getClauses();
00199 for (int i = 0; i < clauses->size(); i++)
00200 {
00201 Clause* c = (*clauses)[i];
00202 for (int i = 0; i < c->getNumPredicates(); i++)
00203 changePredTermsToNewIds(c->getPredicate(i), oldToNewConstIds);
00204 }
00205
00206
00207 for (int i = 0; i < predBlocks_->size(); i++)
00208 {
00209 changePredTermsToNewIds((*predBlocks_)[i], oldToNewConstIds);
00210 }
00211 for (int i = 0; i < truePredsInBlock_->size(); i++)
00212 {
00213 changePredTermsToNewIds((*truePredsInBlock_)[i], oldToNewConstIds);
00214 }
00215
00216
00217 if (db_)
00218 {
00219 db_->changeConstantsToNewIds(oldToNewConstIds);
00220 }
00221 }
00222
00223
00224 void Domain::reorderConstants(MLN* const & mln,
00225 hash_map<int, PredicateHashArray*>& predIdToPredsMap)
00226 {
00227 hash_map<int,int> oldToNewConstIds;
00228
00229 ConstDualMap* newConstDualMap = new ConstDualMap;
00230 Array<Array<int>*>* newConstantsByType = new Array<Array<int>*>;
00231 Array<Array<int>*>* newExternalConstantsByType = new Array<Array<int>*>;
00232
00233 int prevNewConstId = -1;
00234 bool constChanged = false;
00235 for (int i = 0; i < constantsByType_->size(); i++)
00236 {
00237 newConstantsByType->append(new Array<int>);
00238 Array<int>* constIds = (*constantsByType_)[i];
00239 for (int j = 0; j < constIds->size(); j++)
00240 {
00241 int constId = (*constIds)[j];
00242 const char* constName = getConstantName(constId);
00243 int newConstId = newConstDualMap->insert(constName, i);
00244 assert(prevNewConstId + 1 == newConstId);
00245 prevNewConstId = newConstId;
00246 (*newConstantsByType)[i]->append(newConstId);
00247 oldToNewConstIds[constId] = newConstId;
00248 if (constId != newConstId) constChanged = true;
00249 }
00250 }
00251 for (int i = 0; i < externalConstantsByType_->size(); i++)
00252 {
00253 newExternalConstantsByType->append(new Array<int>);
00254 Array<int>* constIds = (*externalConstantsByType_)[i];
00255 for (int j = 0; j < constIds->size(); j++)
00256 {
00257 int constId = (*constIds)[j];
00258 const char* constName = getConstantName(constId);
00259 int newConstId = newConstDualMap->insert(constName, i);
00260 assert(prevNewConstId + 1 == newConstId);
00261 prevNewConstId = newConstId;
00262 (*newExternalConstantsByType)[i]->append(newConstId);
00263 oldToNewConstIds[constId] = newConstId;
00264 if (constId != newConstId) constChanged = true;
00265 }
00266 }
00267
00268 if (!constChanged)
00269 {
00270 delete newConstDualMap;
00271 for (int i = 0; i < newConstantsByType->size(); i++)
00272 delete (*newConstantsByType)[i];
00273 delete newConstantsByType;
00274 for (int i = 0; i < newExternalConstantsByType->size(); i++)
00275 delete (*newExternalConstantsByType)[i];
00276 delete newExternalConstantsByType;
00277 return;
00278 }
00279
00280 delete constDualMap_;
00281 for (int i = 0; i < constantsByType_->size(); i++)
00282 delete (*constantsByType_)[i];
00283 delete constantsByType_;
00284 for (int i = 0; i < externalConstantsByType_->size(); i++)
00285 delete (*externalConstantsByType_)[i];
00286 delete externalConstantsByType_;
00287
00288 constDualMap_ = newConstDualMap;
00289 constantsByType_ = newConstantsByType;
00290 externalConstantsByType_ = newExternalConstantsByType;
00291
00292 constantsByType_->compress();
00293 for (int i = 0; i < constantsByType_->size(); i++)
00294 (*constantsByType_)[i]->compress();
00295 externalConstantsByType_->compress();
00296 for (int i = 0; i < externalConstantsByType_->size(); i++)
00297 (*externalConstantsByType_)[i]->compress();
00298 constDualMap_->compress();
00299
00300
00301 updatePerOldToNewIds(mln, oldToNewConstIds);
00302
00303
00304 hash_map<int, PredicateHashArray*>::iterator mit = predIdToPredsMap.begin();
00305 for (; mit != predIdToPredsMap.end(); mit++)
00306 {
00307 PredicateHashArray* pha = (*mit).second;
00308 for (int i = 0; i < pha->size(); i++ )
00309 changePredTermsToNewIds((*pha)[i], oldToNewConstIds);
00310 }
00311
00312
00313
00314 mln->rehashClauses();
00315 }
00316
00317
00318 void Domain::reorderConstants(MLN* const & mln)
00319 {
00320 hash_map<int,int> oldToNewConstIds;
00321
00322 ConstDualMap* newConstDualMap = new ConstDualMap;
00323 Array<Array<int>*>* newConstantsByType = new Array<Array<int>*>;
00324 Array<Array<int>*>* newExternalConstantsByType = new Array<Array<int>*>;
00325
00326 int prevNewConstId = -1;
00327 bool constChanged = false;
00328 assert(constantsByType_->size() == externalConstantsByType_->size());
00329 for (int i = 0; i < constantsByType_->size(); i++)
00330 {
00331 newConstantsByType->append(new Array<int>);
00332 Array<int>* constIds = (*constantsByType_)[i];
00333 for (int j = 0; j < constIds->size(); j++)
00334 {
00335 int constId = (*constIds)[j];
00336 const char* constName = getConstantName(constId);
00337 int newConstId = newConstDualMap->insert(constName, i);
00338 assert(prevNewConstId + 1 == newConstId);
00339 prevNewConstId = newConstId;
00340 (*newConstantsByType)[i]->append(newConstId);
00341 oldToNewConstIds[constId] = newConstId;
00342 if (constId != newConstId) constChanged = true;
00343 }
00344 newExternalConstantsByType->append(new Array<int>);
00345 Array<int>* extConstIds = (*externalConstantsByType_)[i];
00346 for (int j = 0; j < extConstIds->size(); j++)
00347 {
00348 int constId = (*extConstIds)[j];
00349 const char* constName = getConstantName(constId);
00350 int newConstId = newConstDualMap->insert(constName, i);
00351 assert(prevNewConstId + 1 == newConstId);
00352 prevNewConstId = newConstId;
00353 (*newExternalConstantsByType)[i]->append(newConstId);
00354 oldToNewConstIds[constId] = newConstId;
00355 if (constId != newConstId) constChanged = true;
00356 }
00357 }
00358
00359 if (!constChanged)
00360 {
00361 delete newConstDualMap;
00362 for (int i = 0; i < newConstantsByType->size(); i++)
00363 delete (*newConstantsByType)[i];
00364 delete newConstantsByType;
00365 for (int i = 0; i < newExternalConstantsByType->size(); i++)
00366 delete (*newExternalConstantsByType)[i];
00367 delete newExternalConstantsByType;
00368 return;
00369 }
00370
00371 delete constDualMap_;
00372 for (int i = 0; i < constantsByType_->size(); i++)
00373 delete (*constantsByType_)[i];
00374 delete constantsByType_;
00375 for (int i = 0; i < externalConstantsByType_->size(); i++)
00376 delete (*externalConstantsByType_)[i];
00377 delete externalConstantsByType_;
00378
00379 constDualMap_ = newConstDualMap;
00380 constantsByType_ = newConstantsByType;
00381 externalConstantsByType_ = newExternalConstantsByType;
00382
00383 constantsByType_->compress();
00384 for (int i = 0; i < constantsByType_->size(); i++)
00385 (*constantsByType_)[i]->compress();
00386 externalConstantsByType_->compress();
00387 for (int i = 0; i < externalConstantsByType_->size(); i++)
00388 (*externalConstantsByType_)[i]->compress();
00389 constDualMap_->compress();
00390
00391
00392 updatePerOldToNewIds(mln, oldToNewConstIds);
00393
00394
00395
00396 mln->rehashClauses();
00397 }
00398
00399
00400
00401 void Domain::reorderConstants(ConstDualMap* const & map,
00402 Array<Array<int>*>* const & cbt,
00403 Array<Array<int>*>* const & ecbt,
00404 MLN* const & mln)
00405 {
00406
00407 hash_map<int,int> oldToNewConstIds;
00408
00409
00410 for (int i = 0; i < externalConstantsByType_->size(); i++)
00411 delete (*externalConstantsByType_)[i];
00412 delete externalConstantsByType_;
00413 externalConstantsByType_ = new Array<Array<int>*>;
00414 for (int i = 0; i < cbt->size(); i++)
00415 externalConstantsByType_->append(new Array<int>);
00416
00417 int numConstants = map->getNumInt();
00418 for (int i = 0; i < numConstants; i++)
00419 {
00420 const char* name = map->getStr(i);
00421 if (isConstant(name))
00422 {
00423
00424 oldToNewConstIds[constDualMap_->getInt(name)] = i;
00425 }
00426 else
00427 {
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466 int mi = map->getInt(name);
00467 Array<int>* ints = map->getInt2(mi);
00468 for (int i = 0; i < ints->size(); i++)
00469 (*externalConstantsByType_)[(*ints)[i]]->append(mi);
00470 }
00471 }
00472
00473
00474 for (int i = 0; i < constantsByType_->size(); i++)
00475 {
00476 for (int j = 0; j < (*constantsByType_)[i]->size(); j++)
00477 {
00478 int oldId = (*(*constantsByType_)[i])[j];
00479 assert(oldToNewConstIds.find(oldId) != oldToNewConstIds.end());
00480 (*(*constantsByType_)[i])[j] = oldToNewConstIds[oldId];
00481 }
00482 (*constantsByType_)[i]->quicksort();
00483 }
00484
00485
00486 delete constDualMap_;
00487 constDualMap_ = map;
00488
00489
00490 updatePerOldToNewIds(mln, oldToNewConstIds);
00491
00492
00493
00494 mln->rehashClauses();
00495 }
00496
00497
00498 void Domain::changePredTermsToNewIds(Predicate* const & p,
00499 hash_map<int,int>& oldToNewConstIds)
00500 {
00501
00502 if (p)
00503 {
00504 for (int j = 0; j < p->getNumTerms(); j++)
00505 {
00506 Term* t = (Term*) p->getTerm(j);
00507 if (t->getType() == Term::CONSTANT)
00508 {
00509 int oldId = t->getId();
00510 assert(oldToNewConstIds.find(oldId) != oldToNewConstIds.end());
00511 t->setId(oldToNewConstIds[oldId]);
00512 }
00513 }
00514 }
00515 }
00516
00517
00518
00519 Predicate* Domain::createPredicate(const int& predId,
00520 const bool& includeEqualPreds) const
00521 {
00522 const PredicateTemplate* pt = getPredicateTemplate(predId);
00523 if (!includeEqualPreds && pt->isEqualPredicateTemplate()) return NULL;
00524 Predicate* pred = new Predicate(pt);
00525 pred->setSense(true);
00526 for (int j = 0; j < pt->getNumTerms(); j++)
00527 pred->appendTerm(new Term(-(j+1), (void*)pred, true));
00528 return pred;
00529 }
00530
00531
00532
00533 void Domain::createPredicates(Array<Predicate*>* const & preds,
00534 const bool& includeEqualPreds) const
00535 {
00536 for (int i = 0; i < getNumPredicates(); i++)
00537 {
00538 Predicate* p = createPredicate(i, includeEqualPreds);
00539 if (p) preds->append(p);
00540 }
00541 }
00542
00543
00544
00545 void Domain::createPredicates(Array<Predicate*>* const & preds,
00546 const Array<string>* const & predNames)
00547 {
00548 for (int i = 0; i < predNames->size(); i++)
00549 {
00550 int predId = getPredicateId((*predNames)[i].c_str());
00551 Predicate* p = createPredicate(predId, true);
00552 if (p) preds->append(p);
00553 }
00554 }
00555
00556 int Domain::getNumNonEvidenceAtoms() const
00557 {
00558 int numAtoms = 0;
00559 for (int i = 0; i < getNumPredicates(); i++)
00560 {
00561 numAtoms += (*numNonEvidAtomsPerPred_)[i];
00562 }
00563 return numAtoms;
00564 }
00565
00566
00567
00568
00569 Predicate* Domain::getNonEvidenceAtom(const int& index) const
00570 {
00571 int predId = -1;
00572 int numAtomsPerPred;
00573 int numAtoms = 0;
00574 for (int i = 0; i < getNumPredicates(); i++)
00575 {
00576 numAtomsPerPred = (*numNonEvidAtomsPerPred_)[i];
00577 if (numAtoms + numAtomsPerPred >= index + 1)
00578 {
00579 predId = i;
00580 break;
00581 }
00582 numAtoms += numAtomsPerPred;
00583 }
00584 assert(predId >= 0);
00585
00586
00587 Predicate* pred = createPredicate(predId, false);
00588
00589 bool foundNE = false;
00590 while(!foundNE)
00591 {
00592 for (int i = 0; i < pred->getNumTerms(); i++)
00593 {
00594 int termType = pred->getTermTypeAsInt(i);
00595 const Array<int>* constantsByType = getConstantsByType(termType);
00596 int constIdx = random() % constantsByType->size();
00597 pred->setTermToConstant(i, (*constantsByType)[constIdx]);
00598 }
00599 assert(pred->isGrounded());
00600 if (!db_->getEvidenceStatus(pred)) foundNE = true;
00601 }
00602 return pred;
00603 }
00604
00605 int Domain::addPredBlock(Predicate* const & predBlock) const
00606 {
00607 int idx = predBlocks_->append(predBlock);
00608
00609 Array<Predicate*> predArr;
00610 predBlock->createAllGroundings(this, predArr);
00611 blockSizes_->append(predArr.size());
00612 predArr.deleteItemsAndClear();
00613
00614
00615 Predicate* gndPred = NULL;
00616 truePredsInBlock_->append(gndPred);
00617 blockEvidence_->append(false);
00618 return idx;
00619 }
00620
00628 int Domain::getBlock(const Predicate* const & pred) const
00629 {
00630 assert(((Predicate*)pred)->isGrounded());
00631 for (int i = 0; i < predBlocks_->size(); i++)
00632 {
00633 if ((*predBlocks_)[i]->canBeGroundedAs((Predicate*)pred))
00634 return i;
00635 }
00636 return -1;
00637 }
00638
00646 int Domain::getBlock(const GroundPredicate* const & pred) const
00647 {
00648 for (int i = 0; i < predBlocks_->size(); i++)
00649 {
00650 if ((*predBlocks_)[i]->canBeGroundedAs(pred))
00651 return i;
00652 }
00653 return -1;
00654 }
00655
00656 int Domain::getNumPredBlocks() const
00657 {
00658 return predBlocks_->size();
00659 }
00660
00661 const Array<Predicate*>* Domain::getPredBlocks() const
00662 {
00663 return predBlocks_;
00664 }
00665
00666 const Predicate* Domain::getPredBlock(const int& index) const
00667 {
00668 return (*predBlocks_)[index];
00669 }
00670
00671 const Array<bool>* Domain::getBlockEvidenceArray() const
00672 {
00673 return blockEvidence_;
00674 }
00675
00676 const bool Domain::getBlockEvidence(const int& index) const
00677 {
00678 return (*blockEvidence_)[index];
00679 }
00680
00681 void Domain::setBlockEvidence(const int& index, const bool& value) const
00682 {
00683 (*blockEvidence_)[index] = value;
00684 }
00685
00689 void Domain::computeNumNonEvidAtoms()
00690 {
00691 if (numNonEvidAtomsPerPred_) delete numNonEvidAtomsPerPred_;
00692 numNonEvidAtomsPerPred_ = new Array<int>(getNumPredicates());
00693 numNonEvidAtomsPerPred_->growToSize(getNumPredicates());
00694 for (int i = 0; i < getNumPredicates(); i++)
00695 {
00696 (*numNonEvidAtomsPerPred_)[i] =
00697 (db_->getNumGroundings(i) - db_->getNumEvidenceGndPreds(i));
00698 }
00699 }
00700