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 #ifndef VARIABLESTATE_H_
00067 #define VARIABLESTATE_H_
00068
00069 #include "mrf.h"
00070
00071 const int NOVALUE = -1;
00072 const bool vsdebug = false;
00073
00093 class VariableState
00094 {
00095 public:
00096
00115 VariableState(GroundPredicateHashArray* const& unknownQueries,
00116 GroundPredicateHashArray* const& knownQueries,
00117 Array<TruthValue>* const & knownQueryValues,
00118 const Array<int>* const & allPredGndingsAreQueries,
00119 const bool& markHardGndClauses,
00120 const bool& trackParentClauseWts,
00121 const MLN* const & mln, const Domain* const & domain,
00122 const bool& lazy)
00123 {
00124 this->mln_ = (MLN*)mln;
00125 this->domain_ = (Domain*)domain;
00126 this->lazy_ = lazy;
00127
00128
00129 baseNumAtoms_ = 0;
00130 activeAtoms_ = 0;
00131 numFalseClauses_ = 0;
00132 costOfFalseClauses_ = 0.0;
00133 lowCost_ = LDBL_MAX;
00134 lowBad_ = INT_MAX;
00135
00136
00137 gndClauses_ = new Array<GroundClause*>;
00138 gndPreds_ = new Array<GroundPredicate*>;
00139
00140
00141 setHardClauseWeight();
00142
00143
00144 if (lazy_)
00145 {
00146
00147 domain_->getDB()->setPerformingInference(true);
00148
00149
00150 initLazyBlocks();
00151
00152 clauseLimit_ = INT_MAX;
00153 noApprox_ = false;
00154 haveDeactivated_ = false;
00155
00157
00158
00159
00160
00161 addOneAtomToEachBlock();
00162
00163
00164
00165
00166
00167 bool ignoreActivePreds = false;
00168 getActiveClauses(newClauses_, ignoreActivePreds);
00169 int defaultCnt = newClauses_.size();
00170 long double defaultCost = 0;
00171
00172 for (int i = 0; i < defaultCnt; i++)
00173 {
00174 if (newClauses_[i]->isHardClause())
00175 defaultCost += hardWt_;
00176 else
00177 defaultCost += abs(newClauses_[i]->getWt());
00178 }
00179
00180
00181 for (int i = 0; i < gndPredHashArray_.size(); i++)
00182 gndPredHashArray_[i]->removeGndClauses();
00183
00184
00185 for (int i = 0; i < newClauses_.size(); i++)
00186 delete newClauses_[i];
00187 newClauses_.clear();
00188
00189 baseNumAtoms_ = gndPredHashArray_.size();
00190 cout << "Number of Baseatoms = " << baseNumAtoms_ << endl;
00191 cout << "Default => Cost\t" << "******\t" << " Clause Cnt\t" << endl;
00192 cout << " " << defaultCost << "\t" << "******\t" << defaultCnt
00193 << "\t" << endl << endl;
00194
00195
00196 for (int i = 0; i < baseNumAtoms_; i++)
00197 {
00198 domain_->getDB()->setActiveStatus(gndPredHashArray_[i], true);
00199 activeAtoms_++;
00200 }
00201
00202
00203 fillLazyBlocks();
00204
00205
00206 ignoreActivePreds = false;
00207 getActiveClauses(newClauses_, ignoreActivePreds);
00208 }
00209
00210 else
00211 {
00212 unePreds_ = unknownQueries;
00213 knePreds_ = knownQueries;
00214 knePredValues_ = knownQueryValues;
00215
00216
00217 int size = 0;
00218 if (unknownQueries) size += unknownQueries->size();
00219 if (knownQueries) size += knownQueries->size();
00220 GroundPredicateHashArray* queries = new GroundPredicateHashArray(size);
00221 if (unknownQueries) queries->append(unknownQueries);
00222 if (knownQueries) queries->append(knownQueries);
00223 mrf_ = new MRF(queries, allPredGndingsAreQueries, domain_,
00224 domain_->getDB(), mln_, markHardGndClauses,
00225 trackParentClauseWts, -1);
00226
00227
00228 mrf_->deleteGndPredsGndClauseSets();
00229
00230 delete queries;
00231
00232
00233 blocks_ = mrf_->getBlocks();
00234 blockEvidence_ = mrf_->getBlockEvidence();
00235
00236
00237 newClauses_ = *(Array<GroundClause*>*)mrf_->getGndClauses();
00238
00239
00240 const GroundPredicateHashArray* gndPreds = mrf_->getGndPreds();
00241 for (int i = 0; i < gndPreds->size(); i++)
00242 gndPredHashArray_.append((*gndPreds)[i]);
00243
00244
00245 baseNumAtoms_ = gndPredHashArray_.size();
00246 }
00247
00248
00249
00250
00251
00252
00253 bool initial = true;
00254 addNewClauses(initial);
00255
00256 cout << "Initial num. of clauses: " << getNumClauses() << endl;
00257 }
00258
00263 ~VariableState()
00264 {
00265 if (lazy_)
00266 {
00267
00268 for (int i = 0; i < blocks_->size(); i++)
00269 (*blocks_)[i].clearAndCompress();
00270 delete blocks_;
00271
00272 delete blockEvidence_;
00273 }
00274 else
00275 {
00276
00277 if (mrf_) delete mrf_;
00278
00279
00280
00281 }
00282 }
00283
00284
00292 void addNewClauses(bool initial)
00293 {
00294 if (vsdebug)
00295 cout << "Adding " << newClauses_.size() << " new clauses.." << endl;
00296
00297
00298 int oldNumClauses = getNumClauses();
00299 int oldNumAtoms = getNumAtoms();
00300
00301 gndClauses_->append(newClauses_);
00302 gndPreds_->growToSize(gndPredHashArray_.size());
00303
00304 int numAtoms = getNumAtoms();
00305 int numClauses = getNumClauses();
00306
00307 if (numAtoms == oldNumAtoms && numClauses == oldNumClauses) return;
00308
00309 if (vsdebug) cout << "Clauses: " << numClauses << endl;
00310
00311
00312 atom_.growToSize(numAtoms + 1, false);
00313 makeCost_.growToSize(numAtoms + 1, 0.0);
00314 breakCost_.growToSize(numAtoms + 1, 0.0);
00315 lowAtom_.growToSize(numAtoms + 1, false);
00316 fixedAtom_.growToSize(numAtoms + 1, 0);
00317
00318
00319 for (int i = oldNumAtoms; i < gndPredHashArray_.size(); i++)
00320 {
00321 (*gndPreds_)[i] = gndPredHashArray_[i];
00322
00323 if (vsdebug)
00324 {
00325 cout << "New pred: ";
00326 (*gndPreds_)[i]->print(cout, domain_);
00327 cout << endl;
00328 }
00329
00330 lowAtom_[i + 1] = atom_[i + 1] =
00331 (domain_->getDB()->getValue((*gndPreds_)[i]) == TRUE) ? true : false;
00332 }
00333 newClauses_.clear();
00334
00335 clause_.growToSize(numClauses);
00336 clauseCost_.growToSize(numClauses);
00337 falseClause_.growToSize(numClauses);
00338 whereFalse_.growToSize(numClauses);
00339 numTrueLits_.growToSize(numClauses);
00340 watch1_.growToSize(numClauses);
00341 watch2_.growToSize(numClauses);
00342 isSatisfied_.growToSize(numClauses, false);
00343 deadClause_.growToSize(numClauses, false);
00344 threshold_.growToSize(numClauses, false);
00345
00346 occurence_.growToSize(2*numAtoms + 1);
00347
00348 for (int i = oldNumClauses; i < numClauses; i++)
00349 {
00350 GroundClause* gndClause = (*gndClauses_)[i];
00351
00352 if (vsdebug)
00353 {
00354 cout << "New clause: ";
00355 gndClause->print(cout, domain_, &gndPredHashArray_);
00356 cout << endl;
00357 }
00358
00359
00360 if (gndClause->isHardClause()) threshold_[i] = RAND_MAX;
00361 else
00362 {
00363 double w = gndClause->getWt();
00364 threshold_[i] = RAND_MAX*(1 - exp(-abs(w)));
00365 if (vsdebug)
00366 {
00367 cout << "Weight: " << w << endl;
00368 }
00369 }
00370 if (vsdebug)
00371 cout << "Threshold: " << threshold_[i] << endl;
00372
00373 int numGndPreds = gndClause->getNumGroundPredicates();
00374 clause_[i].growToSize(numGndPreds);
00375
00376 for (int j = 0; j < numGndPreds; j++)
00377 {
00378
00379
00380
00381
00382
00383 int lit = gndClause->getGroundPredicateIndex(j);
00384 clause_[i][j] = lit;
00385 int litIdx = 2*abs(lit) - (lit > 0);
00386 occurence_[litIdx].append(i);
00387 }
00388
00389
00390 if (gndClause->isHardClause())
00391 clauseCost_[i] = hardWt_;
00392 else
00393 clauseCost_[i] = gndClause->getWt();
00394 }
00395
00396 if (!initial)
00397 {
00398
00399 if (useThreshold_)
00400 {
00401 killClauses(oldNumClauses);
00402 }
00403 else
00404 {
00405 initMakeBreakCostWatch(oldNumClauses);
00406 }
00407 }
00408 if (vsdebug) cout << "Done adding new clauses.." << endl;
00409 }
00410
00414 void init()
00415 {
00416
00417 for (int i = 0; i < getNumClauses(); i++) numTrueLits_[i] = 0;
00418 numFalseClauses_ = 0;
00419 costOfFalseClauses_ = 0.0;
00420 lowCost_ = LDBL_MAX;
00421 lowBad_ = INT_MAX;
00422
00423
00424 initMakeBreakCostWatch(0);
00425 }
00426
00432 void initRandom()
00433 {
00434
00435 initBlocksRandom();
00436
00437
00438 for (int i = 1; i <= baseNumAtoms_; i++)
00439 {
00440
00441 if (fixedAtom_[i] != 0) setValueOfAtom(i, (fixedAtom_[i] == 1));
00442
00443 if (getBlockIndex(i - 1) >= 0)
00444 {
00445 if (vsdebug) cout << "Atom " << i << " in block" << endl;
00446 continue;
00447 }
00448
00449 else
00450 {
00451 if (vsdebug) cout << "Atom " << i << " not in block" << endl;
00452 setValueOfAtom(i, random() % 2);
00453 }
00454 }
00455 init();
00456 }
00457
00461 void initBlocksRandom()
00462 {
00463 if (vsdebug)
00464 {
00465 cout << "Initializing blocks randomly" << endl;
00466 cout << "Num. of blocks: " << blocks_->size() << endl;
00467 }
00468
00469
00470 for (int i = 0; i < blocks_->size(); i++)
00471 {
00472
00473 if (int trueFixedAtomInBlock = getTrueFixedAtomInBlock(i) >= 0)
00474 {
00475 if (vsdebug) cout << "True fixed atom in block " << i << endl;
00476 setOthersInBlockToFalse(trueFixedAtomInBlock, i);
00477 continue;
00478 }
00479
00480
00481 if ((*blockEvidence_)[i])
00482 {
00483
00484 setOthersInBlockToFalse(-1, i);
00485 continue;
00486 }
00487
00488
00489 Array<int>& block = (*blocks_)[i];
00490 bool ok = false;
00491 while (!ok)
00492 {
00493 int chosen = random() % block.size();
00494
00495 if (fixedAtom_[block[chosen] + 1] == 0)
00496 {
00497 if (vsdebug) cout << "Atom " << block[chosen] + 1
00498 << " chosen in block" << endl;
00499 setValueOfAtom(block[chosen] + 1, true);
00500 setOthersInBlockToFalse(chosen, i);
00501 ok = true;
00502 }
00503 }
00504 }
00505 if (vsdebug) cout << "Done initializing blocks randomly" << endl;
00506 }
00507
00515 void initMakeBreakCostWatch(const int& startClause)
00516 {
00517 int theTrueLit = -1;
00518
00519 for (int i = startClause; i < getNumClauses(); i++)
00520 {
00521
00522 if (deadClause_[i]) continue;
00523 int trueLit1 = 0;
00524 int trueLit2 = 0;
00525 long double cost = clauseCost_[i];
00526 numTrueLits_[i] = 0;
00527 for (int j = 0; j < getClauseSize(i); j++)
00528 {
00529 if (isTrueLiteral(clause_[i][j]))
00530 {
00531 numTrueLits_[i]++;
00532 theTrueLit = abs(clause_[i][j]);
00533 if (!trueLit1) trueLit1 = theTrueLit;
00534 else if (trueLit1 && !trueLit2) trueLit2 = theTrueLit;
00535 }
00536 }
00537
00538
00539
00540 if ((numTrueLits_[i] == 0 && cost > 0) ||
00541 (numTrueLits_[i] > 0 && cost < 0))
00542 {
00543 whereFalse_[i] = numFalseClauses_;
00544 falseClause_[numFalseClauses_] = i;
00545 numFalseClauses_++;
00546 costOfFalseClauses_ += abs(cost);
00547 if (highestCost_ == abs(cost)) {eqHighest_ = true; numHighest_++;}
00548
00549
00550 if (numTrueLits_[i] == 0)
00551 for (int j = 0; j < getClauseSize(i); j++)
00552 {
00553 makeCost_[abs(clause_[i][j])] += cost;
00554 }
00555
00556
00557 if (numTrueLits_[i] == 1)
00558 {
00559
00560 makeCost_[theTrueLit] -= cost;
00561 watch1_[i] = theTrueLit;
00562 }
00563 else if (numTrueLits_[i] > 1)
00564 {
00565 watch1_[i] = trueLit1;
00566 watch2_[i] = trueLit2;
00567 }
00568 }
00569
00570 else if (numTrueLits_[i] == 1 && cost > 0)
00571 {
00572 breakCost_[theTrueLit] += cost;
00573 watch1_[i] = theTrueLit;
00574 }
00575
00576 else if (cost > 0)
00577 {
00578 watch1_[i] = trueLit1;
00579 watch2_[i] = trueLit2;
00580 }
00581
00582 else if (numTrueLits_[i] == 0 && cost < 0)
00583 {
00584 for (int j = 0; j < getClauseSize(i); j++)
00585 breakCost_[abs(clause_[i][j])] -= cost;
00586 }
00587 }
00588 }
00589
00590 int getNumAtoms() { return gndPreds_->size(); }
00591
00592 int getNumClauses() { return gndClauses_->size(); }
00593
00594 int getNumDeadClauses()
00595 {
00596 int count = 0;
00597 for (int i = 0; i < deadClause_.size(); i++)
00598 if (deadClause_[i]) count++;
00599 return count;
00600 }
00601
00605 int getIndexOfRandomAtom()
00606 {
00607 int numAtoms = getNumAtoms();
00608 if (numAtoms == 0) return NOVALUE;
00609 return random()%numAtoms + 1;
00610 }
00611
00617 int getIndexOfAtomInRandomFalseClause()
00618 {
00619 if (numFalseClauses_ == 0) return NOVALUE;
00620 int clauseIdx = falseClause_[random()%numFalseClauses_];
00621
00622 if (clauseCost_[clauseIdx] > 0)
00623 return abs(clause_[clauseIdx][random()%getClauseSize(clauseIdx)]);
00624
00625 else
00626 return getRandomTrueLitInClause(clauseIdx);
00627 }
00628
00633 int getRandomFalseClauseIndex()
00634 {
00635 if (numFalseClauses_ == 0) return NOVALUE;
00636 return falseClause_[random()%numFalseClauses_];
00637 }
00638
00643 long double getCostOfFalseClauses()
00644 {
00645 return costOfFalseClauses_;
00646 }
00647
00652 int getNumFalseClauses()
00653 {
00654 return numFalseClauses_;
00655 }
00656
00663 bool getValueOfAtom(const int& atomIdx)
00664 {
00665 return atom_[atomIdx];
00666 }
00667
00674 bool getValueOfLowAtom(const int& atomIdx)
00675 {
00676 return lowAtom_[atomIdx];
00677 }
00678
00686 void setValueOfAtom(const int& atomIdx, const bool& value)
00687 {
00688 if (vsdebug) cout << "Setting value of atom " << atomIdx
00689 << " to " << value << endl;
00690
00691 if (atom_[atomIdx] == value) return;
00692
00693 GroundPredicate* p = gndPredHashArray_[atomIdx - 1];
00694 if (value)
00695 {
00696 domain_->getDB()->setValue(p, TRUE);
00697 }
00698 else
00699 {
00700 domain_->getDB()->setValue(p, FALSE);
00701 }
00702
00703 if (lazy_ && !isActive(atomIdx))
00704 {
00705 bool ignoreActivePreds = false;
00706 activateAtom(atomIdx, ignoreActivePreds);
00707 }
00708 atom_[atomIdx] = value;
00709 }
00710
00714 Array<int>& getNegOccurenceArray(const int& atomIdx)
00715 {
00716 int litIdx = 2*atomIdx;
00717 return getOccurenceArray(litIdx);
00718 }
00719
00723 Array<int>& getPosOccurenceArray(const int& atomIdx)
00724 {
00725 int litIdx = 2*atomIdx - 1;
00726 return getOccurenceArray(litIdx);
00727 }
00728
00734 void flipAtom(const int& toFlip)
00735 {
00736 bool toFlipValue = getValueOfAtom(toFlip);
00737 register int clauseIdx;
00738 int sign;
00739 int oppSign;
00740 int litIdx;
00741 if (toFlipValue)
00742 sign = 1;
00743 else
00744 sign = 0;
00745 oppSign = sign ^ 1;
00746
00747 flipAtomValue(toFlip);
00748
00749
00750 litIdx = 2*toFlip - sign;
00751 Array<int>& posOccArray = getOccurenceArray(litIdx);
00752 for (int i = 0; i < posOccArray.size(); i++)
00753 {
00754 clauseIdx = posOccArray[i];
00755
00756 if (deadClause_[clauseIdx]) continue;
00757
00758 int numTrueLits = decrementNumTrueLits(clauseIdx);
00759 long double cost = getClauseCost(clauseIdx);
00760 int watch1 = getWatch1(clauseIdx);
00761 int watch2 = getWatch2(clauseIdx);
00762
00763
00764
00765 if (numTrueLits == 0)
00766 {
00767
00768 if (cost > 0)
00769 {
00770
00771 addFalseClause(clauseIdx);
00772
00773 addBreakCost(toFlip, -cost);
00774
00775 addMakeCostToAtomsInClause(clauseIdx, cost);
00776 }
00777
00778 else
00779 {
00780 assert(cost < 0);
00781
00782 removeFalseClause(clauseIdx);
00783
00784 addBreakCostToAtomsInClause(clauseIdx, -cost);
00785
00786 addMakeCost(toFlip, cost);
00787 }
00788 }
00789
00790
00791 else if (numTrueLits == 1)
00792 {
00793 if (watch1 == toFlip)
00794 {
00795 assert(watch1 != watch2);
00796 setWatch1(clauseIdx, watch2);
00797 watch1 = getWatch1(clauseIdx);
00798 }
00799
00800
00801 if (cost > 0)
00802 {
00803 addBreakCost(watch1, cost);
00804 }
00805
00806 else
00807 {
00808 assert(cost < 0);
00809 addMakeCost(watch1, -cost);
00810 }
00811 }
00812
00813
00814 else
00815 {
00816
00817 if (watch1 == toFlip)
00818 {
00819
00820 int diffTrueLit = getTrueLiteralOtherThan(clauseIdx, watch1, watch2);
00821 setWatch1(clauseIdx, diffTrueLit);
00822 }
00823
00824 else if (watch2 == toFlip)
00825 {
00826
00827 int diffTrueLit = getTrueLiteralOtherThan(clauseIdx, watch1, watch2);
00828 setWatch2(clauseIdx, diffTrueLit);
00829 }
00830 }
00831 }
00832
00833
00834 litIdx = 2*toFlip - oppSign;
00835 Array<int>& negOccArray = getOccurenceArray(litIdx);
00836 for (int i = 0; i < negOccArray.size(); i++)
00837 {
00838 clauseIdx = negOccArray[i];
00839
00840 if (deadClause_[clauseIdx]) continue;
00841
00842 int numTrueLits = incrementNumTrueLits(clauseIdx);
00843 long double cost = getClauseCost(clauseIdx);
00844 int watch1 = getWatch1(clauseIdx);
00845
00846
00847
00848 if (numTrueLits == 1)
00849 {
00850
00851 if (cost > 0)
00852 {
00853
00854 removeFalseClause(clauseIdx);
00855
00856 addBreakCost(toFlip, cost);
00857
00858 addMakeCostToAtomsInClause(clauseIdx, -cost);
00859 }
00860
00861 else
00862 {
00863 assert(cost < 0);
00864
00865 addFalseClause(clauseIdx);
00866
00867 addBreakCostToAtomsInClause(clauseIdx, cost);
00868
00869 addMakeCost(toFlip, -cost);
00870 }
00871
00872 setWatch1(clauseIdx, toFlip);
00873 }
00874
00875
00876 else
00877 if (numTrueLits == 2)
00878 {
00879 if (cost > 0)
00880 {
00881
00882
00883 addBreakCost(watch1, -cost);
00884 }
00885 else
00886 {
00887
00888 assert(cost < 0);
00889
00890 addMakeCost(watch1, cost);
00891 }
00892
00893
00894 setWatch2(clauseIdx, toFlip);
00895 }
00896 }
00897 }
00898
00903 void flipAtomValue(const int& atomIdx)
00904 {
00905 bool opposite = !atom_[atomIdx];
00906 setValueOfAtom(atomIdx, opposite);
00907 }
00908
00920 long double getImprovementByFlipping(const int& atomIdx)
00921 {
00922 if (lazy_ && !isActive(atomIdx))
00923 {
00924
00925 flipAtom(atomIdx);
00926 flipAtom(atomIdx);
00927 }
00928 long double improvement = makeCost_[atomIdx] - breakCost_[atomIdx];
00929 return improvement;
00930 }
00931
00938 void activateAtom(const int& atomIdx, const bool& ignoreActivePreds)
00939 {
00940
00941
00942 if (lazy_ && !isActive(atomIdx))
00943 {
00944 Predicate* p =
00945 gndPredHashArray_[atomIdx - 1]->createEquivalentPredicate(domain_);
00946 getActiveClauses(p, newClauses_, true, ignoreActivePreds);
00947
00948 bool initial = false;
00949 addNewClauses(initial);
00950
00951 domain_->getDB()->setActiveStatus(p, true);
00952 activeAtoms_++;
00953 delete p;
00954 }
00955 }
00956
00963 bool isActive(const int& atomIdx)
00964 {
00965 return domain_->getDB()->getActiveStatus(gndPredHashArray_[atomIdx-1]);
00966 }
00967
00974 bool isActive(const Predicate* pred)
00975 {
00976 return domain_->getDB()->getActiveStatus(pred);
00977 }
00978
00982 Array<int>& getOccurenceArray(const int& idx)
00983 {
00984 return occurence_[idx];
00985 }
00986
00990 int incrementNumTrueLits(const int& clauseIdx)
00991 {
00992 return ++numTrueLits_[clauseIdx];
00993 }
00994
00998 int decrementNumTrueLits(const int& clauseIdx)
00999 {
01000 return --numTrueLits_[clauseIdx];
01001 }
01002
01006 int getNumTrueLits(const int& clauseIdx)
01007 {
01008 return numTrueLits_[clauseIdx];
01009 }
01010
01014 long double getClauseCost(const int& clauseIdx)
01015 {
01016 return clauseCost_[clauseIdx];
01017 }
01018
01022 Array<int>& getAtomsInClause(const int& clauseIdx)
01023 {
01024 return clause_[clauseIdx];
01025 }
01026
01030 void addFalseClause(const int& clauseIdx)
01031 {
01032 falseClause_[numFalseClauses_] = clauseIdx;
01033 whereFalse_[clauseIdx] = numFalseClauses_;
01034 numFalseClauses_++;
01035 costOfFalseClauses_ += abs(clauseCost_[clauseIdx]);
01036 }
01037
01041 void removeFalseClause(const int& clauseIdx)
01042 {
01043 numFalseClauses_--;
01044 falseClause_[whereFalse_[clauseIdx]] = falseClause_[numFalseClauses_];
01045 whereFalse_[falseClause_[numFalseClauses_]] = whereFalse_[clauseIdx];
01046 costOfFalseClauses_ -= abs(clauseCost_[clauseIdx]);
01047 }
01048
01052 void addBreakCost(const int& atomIdx, const long double& cost)
01053 {
01054 breakCost_[atomIdx] += cost;
01055 }
01056
01060 void subtractBreakCost(const int& atomIdx, const long double& cost)
01061 {
01062 breakCost_[atomIdx] -= cost;
01063 }
01064
01071 void addBreakCostToAtomsInClause(const int& clauseIdx,
01072 const long double& cost)
01073 {
01074 register int size = getClauseSize(clauseIdx);
01075 for (int i = 0; i < size; i++)
01076 {
01077 register int lit = clause_[clauseIdx][i];
01078 breakCost_[abs(lit)] += cost;
01079 }
01080 }
01081
01088 void subtractBreakCostFromAtomsInClause(const int& clauseIdx,
01089 const long double& cost)
01090 {
01091 register int size = getClauseSize(clauseIdx);
01092 for (int i = 0; i < size; i++)
01093 {
01094 register int lit = clause_[clauseIdx][i];
01095 breakCost_[abs(lit)] -= cost;
01096 }
01097 }
01098
01105 void addMakeCost(const int& atomIdx, const long double& cost)
01106 {
01107 makeCost_[atomIdx] += cost;
01108 }
01109
01116 void subtractMakeCost(const int& atomIdx, const long double& cost)
01117 {
01118 makeCost_[atomIdx] -= cost;
01119 }
01120
01127 void addMakeCostToAtomsInClause(const int& clauseIdx,
01128 const long double& cost)
01129 {
01130 register int size = getClauseSize(clauseIdx);
01131 for (int i = 0; i < size; i++)
01132 {
01133 register int lit = clause_[clauseIdx][i];
01134 makeCost_[abs(lit)] += cost;
01135 }
01136 }
01137
01144 void subtractMakeCostFromAtomsInClause(const int& clauseIdx,
01145 const long double& cost)
01146 {
01147 register int size = getClauseSize(clauseIdx);
01148 for (int i = 0; i < size; i++)
01149 {
01150 register int lit = clause_[clauseIdx][i];
01151 makeCost_[abs(lit)] -= cost;
01152 }
01153 }
01154
01164 const int getTrueLiteralOtherThan(const int& clauseIdx,
01165 const int& atomIdx1,
01166 const int& atomIdx2)
01167 {
01168 register int size = getClauseSize(clauseIdx);
01169 for (int i = 0; i < size; i++)
01170 {
01171 register int lit = clause_[clauseIdx][i];
01172 register int v = abs(lit);
01173 if (isTrueLiteral(lit) && v != atomIdx1 && v != atomIdx2)
01174 return v;
01175 }
01176
01177 assert(false);
01178 return -1;
01179 }
01180
01184 const bool isTrueLiteral(const int& literal)
01185 {
01186 return ((literal > 0) == atom_[abs(literal)]);
01187 }
01188
01192 const int getAtomInClause(const int& atomIdxInClause, const int& clauseIdx)
01193 {
01194 return clause_[clauseIdx][atomIdxInClause];
01195 }
01196
01200 const int getRandomAtomInClause(const int& clauseIdx)
01201 {
01202 return clause_[clauseIdx][random()%getClauseSize(clauseIdx)];
01203 }
01204
01211 const int getRandomTrueLitInClause(const int& clauseIdx)
01212 {
01213 assert(numTrueLits_[clauseIdx] > 0);
01214 int trueLit = random()%numTrueLits_[clauseIdx];
01215 int whichTrueLit = 0;
01216 for (int i = 0; i < getClauseSize(clauseIdx); i++)
01217 {
01218 int lit = clause_[clauseIdx][i];
01219 int atm = abs(lit);
01220
01221 if (isTrueLiteral(lit))
01222 if (trueLit == whichTrueLit++)
01223 return atm;
01224 }
01225
01226 assert(false);
01227 return -1;
01228 }
01229
01230 const double getMaxClauseWeight()
01231 {
01232 double maxWeight = 0.0;
01233 for (int i = 0; i < getNumClauses(); i++)
01234 {
01235 double weight = abs(clauseCost_[i]);
01236 if (weight > maxWeight) maxWeight = weight;
01237 }
01238 return maxWeight;
01239 }
01240
01241 const long double getMakeCost(const int& atomIdx)
01242 {
01243 return makeCost_[atomIdx];
01244 }
01245
01246 const long double getBreakCost(const int& atomIdx)
01247 {
01248 return breakCost_[atomIdx];
01249 }
01250
01251 const int getClauseSize(const int& clauseIdx)
01252 {
01253 return clause_[clauseIdx].size();
01254 }
01255
01256 const int getWatch1(const int& clauseIdx)
01257 {
01258 return watch1_[clauseIdx];
01259 }
01260
01261 void setWatch1(const int& clauseIdx, const int& atomIdx)
01262 {
01263 watch1_[clauseIdx] = atomIdx;
01264 }
01265
01266 const int getWatch2(const int& clauseIdx)
01267 {
01268 return watch2_[clauseIdx];
01269 }
01270
01271 void setWatch2(const int& clauseIdx, const int& atomIdx)
01272 {
01273 watch2_[clauseIdx] = atomIdx;
01274 }
01275
01276 const bool isBlockEvidence(const int& blockIdx)
01277 {
01278 return (*blockEvidence_)[blockIdx];
01279 }
01280
01281 const int getBlockSize(const int& blockIdx)
01282 {
01283
01284 if (lazy_)
01285 return domain_->getPredBlock(blockIdx)->size();
01286 else
01287 return (*blocks_)[blockIdx].size();
01288 }
01289
01294 const int getBlockIndex(const int& atomIdx)
01295 {
01296 for (int i = 0; i < blocks_->size(); i++)
01297 {
01298 int blockIdx = (*blocks_)[i].find(atomIdx);
01299 if (blockIdx >= 0)
01300 return i;
01301 }
01302 return -1;
01303 }
01304
01305 Array<int>& getBlockArray(const int& blockIdx)
01306 {
01307 return (*blocks_)[blockIdx];
01308 }
01309
01310 bool getBlockEvidence(const int& blockIdx)
01311 {
01312 return (*blockEvidence_)[blockIdx];
01313 }
01314
01315 int getNumBlocks()
01316 {
01317 return blocks_->size();
01318 }
01319
01323 const long double getLowCost()
01324 {
01325 return lowCost_;
01326 }
01327
01331 const int getLowBad()
01332 {
01333 return lowBad_;
01334 }
01335
01340 void makeUnitCosts()
01341 {
01342 for (int i = 0; i < clauseCost_.size(); i++)
01343 {
01344 if (clauseCost_[i] > 0) clauseCost_[i] = 1.0;
01345 else
01346 {
01347 assert(clauseCost_[i] < 0);
01348 clauseCost_[i] = -1.0;
01349 }
01350 }
01351 }
01352
01356 void saveLowState()
01357 {
01358 if (vsdebug) cout << "Saving low state: " << endl;
01359 for (int i = 1; i <= getNumAtoms(); i++)
01360 {
01361 lowAtom_[i] = atom_[i];
01362 if (vsdebug) cout << lowAtom_[i] << endl;
01363 }
01364 lowCost_ = costOfFalseClauses_;
01365 lowBad_ = numFalseClauses_;
01366 }
01367
01371 int getTrueFixedAtomInBlock(const int blockIdx)
01372 {
01373 Array<int>& block = (*blocks_)[blockIdx];
01374 for (int i = 0; i < block.size(); i++)
01375 if (fixedAtom_[block[i] + 1] > 0) return i;
01376 return -1;
01377 }
01378
01379 const GroundPredicateHashArray* getGndPredHashArrayPtr() const
01380 {
01381 return &gndPredHashArray_;
01382 }
01383
01384 const GroundPredicateHashArray* getUnePreds() const
01385 {
01386 return unePreds_;
01387 }
01388
01389 const GroundPredicateHashArray* getKnePreds() const
01390 {
01391 return knePreds_;
01392 }
01393
01394 const Array<TruthValue>* getKnePredValues() const
01395 {
01396 return knePredValues_;
01397 }
01398
01402 void setGndClausesWtsToSumOfParentWts()
01403 {
01404 for (int i = 0; i < gndClauses_->size(); i++)
01405 {
01406 (*gndClauses_)[i]->setWtToSumOfParentWts();
01407 if (vsdebug) cout << "Setting cost of clause " << i << " to "
01408 << (*gndClauses_)[i]->getWt() << endl;
01409 clauseCost_[i] = (*gndClauses_)[i]->getWt();
01410 }
01411 }
01412
01421 void getNumClauseGndings(Array<double>* const & numGndings, bool tv)
01422 {
01423
01424 IntPairItr itr;
01425 IntPair *clauseFrequencies;
01426
01427
01428 int clauseCnt = numGndings->size();
01429 assert(clauseCnt == mln_->getNumClauses());
01430 for (int clauseno = 0; clauseno < clauseCnt; clauseno++)
01431 assert ((*numGndings)[clauseno] >= 0);
01432
01433 for (int i = 0; i < gndClauses_->size(); i++)
01434 {
01435 GroundClause *gndClause = (*gndClauses_)[i];
01436 int satLitcnt = getNumTrueLits(i);
01437 if (tv && satLitcnt == 0)
01438 continue;
01439 if (!tv && satLitcnt > 0)
01440 continue;
01441
01442 clauseFrequencies = gndClause->getClauseFrequencies();
01443 for (itr = clauseFrequencies->begin();
01444 itr != clauseFrequencies->end(); itr++)
01445 {
01446 int clauseno = itr->first;
01447 int frequency = itr->second;
01448 (*numGndings)[clauseno] += frequency;
01449 }
01450 }
01451 }
01452
01461 void getNumClauseGndings(double numGndings[], int clauseCnt, bool tv)
01462 {
01463
01464 IntPairItr itr;
01465 IntPair *clauseFrequencies;
01466
01467 for (int clauseno = 0; clauseno < clauseCnt; clauseno++)
01468 numGndings[clauseno] = 0;
01469
01470 for (int i = 0; i < gndClauses_->size(); i++)
01471 {
01472 GroundClause *gndClause = (*gndClauses_)[i];
01473 int satLitcnt = getNumTrueLits(i);
01474 if (tv && satLitcnt == 0)
01475 continue;
01476 if (!tv && satLitcnt > 0)
01477 continue;
01478
01479 clauseFrequencies = gndClause->getClauseFrequencies();
01480 for (itr = clauseFrequencies->begin();
01481 itr != clauseFrequencies->end(); itr++)
01482 {
01483 int clauseno = itr->first;
01484 int frequency = itr->second;
01485 numGndings[clauseno] += frequency;
01486 }
01487 }
01488 }
01489
01501 void getNumClauseGndingsWithUnknown(double numGndings[], int clauseCnt,
01502 bool tv,
01503 const Array<bool>* const& unknownPred)
01504 {
01505 assert(unknownPred->size() == getNumAtoms());
01506 IntPairItr itr;
01507 IntPair *clauseFrequencies;
01508
01509 for (int clauseno = 0; clauseno < clauseCnt; clauseno++)
01510 numGndings[clauseno] = 0;
01511
01512 for (int i = 0; i < gndClauses_->size(); i++)
01513 {
01514 GroundClause *gndClause = (*gndClauses_)[i];
01515 int satLitcnt = 0;
01516 bool unknown = false;
01517 for (int j = 0; j < gndClause->getNumGroundPredicates(); j++)
01518 {
01519 int lit = gndClause->getGroundPredicateIndex(j);
01520 if ((*unknownPred)[abs(lit) - 1])
01521 {
01522 unknown = true;
01523 continue;
01524 }
01525 if (isTrueLiteral(lit)) satLitcnt++;
01526 }
01527
01528 if (tv && satLitcnt == 0)
01529 continue;
01530 if (!tv && (satLitcnt > 0 || unknown))
01531 continue;
01532
01533 clauseFrequencies = gndClause->getClauseFrequencies();
01534 for (itr = clauseFrequencies->begin();
01535 itr != clauseFrequencies->end(); itr++)
01536 {
01537 int clauseno = itr->first;
01538 int frequency = itr->second;
01539 numGndings[clauseno] += frequency;
01540 }
01541 }
01542 }
01543
01550 void setOthersInBlockToFalse(const int& atomIdx,
01551 const int& blockIdx)
01552 {
01553 Array<int>& block = (*blocks_)[blockIdx];
01554 for (int i = 0; i < block.size(); i++)
01555 {
01556
01557 if (i != atomIdx && fixedAtom_[block[i] + 1] == 0)
01558 setValueOfAtom(block[i] + 1, false);
01559 }
01560 }
01561
01562
01571 void fixAtom(const int& atomIdx, const bool& value)
01572 {
01573 assert(atomIdx > 0);
01574
01575 if ((fixedAtom_[atomIdx] == 1 && value == false) ||
01576 (fixedAtom_[atomIdx] == -1 && value == true))
01577 {
01578 cout << "Contradiction: Tried to fix atom " << atomIdx <<
01579 " to true and false ... exiting." << endl;
01580 exit(0);
01581 }
01582
01583 if (vsdebug)
01584 {
01585 cout << "Fixing ";
01586 (*gndPreds_)[atomIdx - 1]->print(cout, domain_);
01587 cout << " to " << value << endl;
01588 }
01589
01590 setValueOfAtom(atomIdx, value);
01591 fixedAtom_[atomIdx] = (value) ? 1 : -1;
01592 }
01593
01605 Array<int>* simplifyClauseFromFixedAtoms(const int& clauseIdx)
01606 {
01607 Array<int>* returnArray = new Array<int>;
01608
01609 if (isSatisfied_[clauseIdx]) return returnArray;
01610
01611
01612
01613 bool isGood = (clauseCost_[clauseIdx] > 0) ? false : true;
01614
01615 bool allFalseAtoms = (clauseCost_[clauseIdx] > 0) ? true : false;
01616
01617 for (int i = 0; i < getClauseSize(clauseIdx); i++)
01618 {
01619 int lit = clause_[clauseIdx][i];
01620 int fixedValue = fixedAtom_[abs(lit)];
01621
01622 if (clauseCost_[clauseIdx] > 0)
01623 {
01624 if ((fixedValue == 1 && lit > 0) ||
01625 (fixedValue == -1 && lit < 0))
01626 {
01627 isGood = true;
01628 allFalseAtoms = false;
01629 returnArray->clear();
01630 break;
01631 }
01632 else if (fixedValue == 0)
01633 {
01634 allFalseAtoms = false;
01635 returnArray->append(lit);
01636 }
01637 }
01638 else
01639 {
01640 assert(clauseCost_[clauseIdx] < 0);
01641 if ((fixedValue == 1 && lit > 0) ||
01642 (fixedValue == -1 && lit < 0))
01643 {
01644 cout << "Contradiction: Tried to fix atom " << abs(lit) <<
01645 " to true in a negative clause ... exiting." << endl;
01646 exit(0);
01647 }
01648 else
01649 {
01650 returnArray->append(lit);
01651
01652 if (fixedValue == 0) isGood = false;
01653 }
01654 }
01655 }
01656 if (allFalseAtoms)
01657 {
01658 cout << "Contradiction: All atoms in clause " << clauseIdx <<
01659 " fixed to false ... exiting." << endl;
01660 exit(0);
01661 }
01662 if (isGood) isSatisfied_[clauseIdx] = true;
01663 return returnArray;
01664 }
01665
01672 const bool isDeadClause(const int& clauseIdx)
01673 {
01674 return deadClause_[clauseIdx];
01675 }
01676
01680 void eliminateSoftClauses()
01681 {
01682 bool atLeastOneDead = false;
01683 for (int i = 0; i < getNumClauses(); i++)
01684 {
01685 if (!(*gndClauses_)[i]->isHardClause())
01686 {
01687 atLeastOneDead = true;
01688 deadClause_[i] = true;
01689 }
01690 }
01691 if (atLeastOneDead) initMakeBreakCostWatch(0);
01692 }
01693
01701 void killClauses(const int& startClause)
01702 {
01703 for (int i = startClause; i < getNumClauses(); i++)
01704 {
01705 GroundClause* clause = (*gndClauses_)[i];
01706 if ((clauseGoodInPrevious(i)) &&
01707 (clause->isHardClause() || random() <= threshold_[i]))
01708 {
01709 if (vsdebug)
01710 {
01711 cout << "Keeping clause "<< i << " ";
01712 clause->print(cout, domain_, &gndPredHashArray_);
01713 cout << endl;
01714 }
01715 deadClause_[i] = false;
01716 }
01717 else
01718 {
01719 deadClause_[i] = true;
01720 }
01721 }
01722 initMakeBreakCostWatch(startClause);
01723 }
01724
01725
01733 const bool clauseGoodInPrevious(const int& clauseIdx)
01734 {
01735
01736 int numSatLits = numTrueLits_[clauseIdx];
01737
01738 if ((numSatLits > 0 && clauseCost_[clauseIdx] > 0.0) ||
01739 (numSatLits == 0 && clauseCost_[clauseIdx] < 0.0))
01740 return true;
01741 else
01742 return false;
01743 }
01744
01748 void resetDeadClauses()
01749 {
01750 for (int i = 0; i < deadClause_.size(); i++)
01751 deadClause_[i] = false;
01752 initMakeBreakCostWatch(0);
01753 }
01754
01758 void resetFixedAtoms()
01759 {
01760 for (int i = 0; i < fixedAtom_.size(); i++)
01761 fixedAtom_[i] = 0;
01762 for (int i = 0; i < isSatisfied_.size(); i++)
01763 isSatisfied_[i] = false;
01764 }
01765
01766 void setLazy(const bool& l) { lazy_ = l; }
01767 const bool getLazy() { return lazy_; }
01768
01769 void setUseThreshold(const bool& t) { useThreshold_ = t;}
01770 const bool getUseThreshold() { return useThreshold_; }
01771
01772 long double getHardWt() { return hardWt_; }
01773
01774 const Domain* getDomain() { return domain_; }
01775
01776 const MLN* getMLN() { return mln_; }
01777
01783 void printLowState(ostream& out)
01784 {
01785 for (int i = 0; i < getNumAtoms(); i++)
01786 {
01787 (*gndPreds_)[i]->print(out, domain_);
01788 out << " " << lowAtom_[i + 1] << endl;
01789 }
01790 }
01791
01798 void printGndPred(const int& predIndex, ostream& out)
01799 {
01800 (*gndPreds_)[predIndex]->print(out, domain_);
01801 }
01802
01804
01811 GroundPredicate* getGndPred(const int& index)
01812 {
01813 return (*gndPreds_)[index];
01814 }
01815
01822 GroundClause* getGndClause(const int& index)
01823 {
01824 return (*gndClauses_)[index];
01825 }
01826
01830 void saveLowStateToGndPreds()
01831 {
01832 for (int i = 0; i < getNumAtoms(); i++)
01833 (*gndPreds_)[i]->setTruthValue(lowAtom_[i + 1]);
01834 }
01835
01839 void saveLowStateToDB()
01840 {
01841 for (int i = 0; i < getNumAtoms(); i++)
01842 {
01843 GroundPredicate* p = gndPredHashArray_[i];
01844 bool value = lowAtom_[i + 1];
01845 if (value)
01846 {
01847 domain_->getDB()->setValue(p, TRUE);
01848 }
01849 else
01850 {
01851 domain_->getDB()->setValue(p, FALSE);
01852 }
01853 }
01854 }
01855
01862 const int getGndPredIndex(GroundPredicate* const& gndPred)
01863 {
01864 return gndPreds_->find(gndPred);
01865 }
01866
01867
01869
01870
01872
01886 void getActiveClauses(Predicate *inputPred,
01887 Array<GroundClause*>& activeClauses,
01888 bool const & active,
01889 bool const & ignoreActivePreds)
01890 {
01891 Clause *fclause;
01892 GroundClause* newClause;
01893 int clauseCnt;
01894 GroundClauseHashArray clauseHashArray;
01895
01896 Array<GroundClause*>* newClauses = new Array<GroundClause*>;
01897
01898 const Array<IndexClause*>* indexClauses = NULL;
01899
01900
01901 if (inputPred == NULL)
01902 {
01903 clauseCnt = mln_->getNumClauses();
01904 }
01905
01906 else
01907 {
01908 if (domain_->getDB()->getDeactivatedStatus(inputPred)) return;
01909 int predId = inputPred->getId();
01910 indexClauses = mln_->getClausesContainingPred(predId);
01911 clauseCnt = indexClauses->size();
01912 }
01913
01914
01915 int clauseno = 0;
01916 while (clauseno < clauseCnt)
01917 {
01918 if (inputPred)
01919 fclause = (Clause *) (*indexClauses)[clauseno]->clause;
01920 else
01921 fclause = (Clause *) mln_->getClause(clauseno);
01922
01923 if (vsdebug)
01924 {
01925 cout << "Getting active clauses for FO clause: ";
01926 fclause->print(cout, domain_);
01927 cout << endl;
01928 }
01929
01930 long double wt = fclause->getWt();
01931 const double* parentWtPtr = NULL;
01932 if (!fclause->isHardClause()) parentWtPtr = fclause->getWtPtr();
01933 const int clauseId = mln_->findClauseIdx(fclause);
01934 newClauses->clear();
01935
01936 fclause->getActiveClauses(inputPred, domain_, newClauses,
01937 &gndPredHashArray_, ignoreActivePreds);
01938
01939 for (int i = 0; i < newClauses->size(); i++)
01940 {
01941 newClause = (*newClauses)[i];
01942 int pos = clauseHashArray.find(newClause);
01943
01944 if (pos >= 0)
01945 {
01946 if (vsdebug)
01947 {
01948 cout << "Adding weight " << wt << " to clause ";
01949 clauseHashArray[pos]->print(cout, domain_, &gndPredHashArray_);
01950 cout << endl;
01951 }
01952 clauseHashArray[pos]->addWt(wt);
01953 if (parentWtPtr)
01954 {
01955 clauseHashArray[pos]->appendParentWtPtr(parentWtPtr);
01956 clauseHashArray[pos]->incrementClauseFrequency(clauseId, 1);
01957 }
01958 delete newClause;
01959 continue;
01960 }
01961
01962
01963 newClause->setWt(wt);
01964 newClause->appendToGndPreds(&gndPredHashArray_);
01965 if (parentWtPtr)
01966 {
01967 newClause->appendParentWtPtr(parentWtPtr);
01968 newClause->incrementClauseFrequency(clauseId, 1);
01969 assert(newClause->getWt() == *parentWtPtr);
01970 }
01971
01972 if (vsdebug)
01973 {
01974 cout << "Appending clause ";
01975 newClause->print(cout, domain_, &gndPredHashArray_);
01976 cout << endl;
01977 }
01978 clauseHashArray.append(newClause);
01979 }
01980 clauseno++;
01981 }
01982
01983 for (int i = 0; i < clauseHashArray.size(); i++)
01984 {
01985 newClause = clauseHashArray[i];
01986 activeClauses.append(newClause);
01987 }
01988 delete newClauses;
01989 }
01990
01998 void getActiveClauses(Array<GroundClause*> &allClauses,
01999 bool const & ignoreActivePreds)
02000 {
02001 getActiveClauses(NULL, allClauses, true, ignoreActivePreds);
02002 }
02003
02004 int getNumActiveAtoms()
02005 {
02006 return activeAtoms_;
02007 }
02008
02013 void addOneAtomToEachBlock()
02014 {
02015 assert(lazy_);
02016
02017 for (int i = 0; i < blocks_->size(); i++)
02018 {
02019
02020 if ((*blockEvidence_)[i])
02021 {
02022
02023 setOthersInBlockToFalse(-1, i);
02024 continue;
02025 }
02026
02027
02028
02029 const Array<Predicate*>* block = domain_->getPredBlock(i);
02030
02031 int chosen = random() % block->size();
02032 Predicate* pred = (*block)[chosen];
02033 GroundPredicate* groundPred = new GroundPredicate(pred);
02034
02035
02036 int index = gndPredHashArray_.find(groundPred);
02037 if (index < 0)
02038 {
02039
02040 index = gndPredHashArray_.append(groundPred);
02041 (*blocks_)[i].append(index);
02042 chosen = (*blocks_)[i].size() - 1;
02043
02044
02045 bool initial = false;
02046 addNewClauses(initial);
02047 }
02048 else
02049 {
02050 delete groundPred;
02051 chosen = (*blocks_)[i].find(index);
02052 }
02053 setValueOfAtom(index + 1, true);
02054 setOthersInBlockToFalse(chosen, i);
02055 }
02056 }
02057
02061 void initLazyBlocks()
02062 {
02063 assert(lazy_);
02064 blocks_ = new Array<Array<int> >;
02065 blocks_->growToSize(domain_->getNumPredBlocks());
02066 blockEvidence_ = new Array<bool>(*(domain_->getBlockEvidenceArray()));
02067 }
02068
02072 void fillLazyBlocks()
02073 {
02074 assert(lazy_);
02075 const Array<Array<Predicate*>*>* blocks = domain_->getPredBlocks();
02076 for (int i = 0; i < blocks->size(); i++)
02077 {
02078 if (vsdebug) cout << "Block " << i << endl;
02079 Array<Predicate*>* block = (*blocks)[i];
02080 for (int j = 0; j < block->size(); j++)
02081 {
02082 Predicate* pred = (*block)[j];
02083 if (vsdebug)
02084 {
02085 cout << "\tPred: ";
02086 pred->printWithStrVar(cout, domain_);
02087 cout << endl;
02088 }
02089
02090 if (domain_->getDB()->getEvidenceStatus(pred))
02091 continue;
02092 GroundPredicate* groundPred = new GroundPredicate(pred);
02093
02094
02095 int index = gndPredHashArray_.find(groundPred);
02096 if (index < 0)
02097 index = gndPredHashArray_.append(groundPred);
02098 else
02099 delete groundPred;
02100
02101
02102 if (!(*blocks_)[i].contains(index))
02103 (*blocks_)[i].append(index);
02104 }
02105 }
02106
02107 bool initial = true;
02108 addNewClauses(initial);
02109 }
02110
02112
02113
02114 private:
02115
02121 void setHardClauseWeight()
02122 {
02123
02124 long double sumSoftWts = 0.0;
02125
02126 int clauseCnt = mln_->getNumClauses();
02127
02128 for (int i = 0; i < clauseCnt; i++)
02129 {
02130 Clause* fclause = (Clause *) mln_->getClause(i);
02131
02132 if (fclause->isHardClause()) continue;
02133
02134 long double wt = abs(fclause->getWt());
02135 long double numGndings = fclause->getNumGroundings(domain_);
02136 sumSoftWts += wt*numGndings;
02137 }
02138 assert(sumSoftWts >= 0);
02139
02140 hardWt_ = sumSoftWts + 10.0;
02141 cout << "Set hard weight to " << hardWt_ << endl;
02142 }
02143
02144 private:
02145
02146
02147 bool lazy_;
02148
02149
02150 long double hardWt_;
02151
02152
02153
02154 MLN* mln_;
02155 Domain* domain_;
02156
02157
02158
02159 Array<GroundPredicate*>* gndPreds_;
02160 Array<GroundClause*>* gndClauses_;
02161
02162
02163
02164 GroundPredicateHashArray* unePreds_;
02165
02166
02167
02168 GroundPredicateHashArray* knePreds_;
02169
02170 Array<TruthValue>* knePredValues_;
02171
02172
02174
02175 int baseNumAtoms_;
02176
02177 bool noApprox_;
02178
02179 bool haveDeactivated_;
02180
02181 int memLimit_;
02182
02183 int clauseLimit_;
02185
02186
02188
02189 MRF* mrf_;
02191
02192
02193 Array<GroundClause*> newClauses_;
02194
02195 Array<GroundPredicate*> newPreds_;
02196
02197
02198 GroundPredicateHashArray gndPredHashArray_;
02199
02200
02201
02202 Array<Array<int> > clause_;
02203
02204 Array<long double> clauseCost_;
02205
02206 long double highestCost_;
02207
02208 bool eqHighest_;
02209
02210 int numHighest_;
02211
02212 Array<int> falseClause_;
02213
02214 Array<int> whereFalse_;
02215
02216 Array<int> numTrueLits_;
02217
02218 Array<int> watch1_;
02219
02220 Array<int> watch2_;
02221
02222 Array<bool> isSatisfied_;
02223
02224 Array<bool> deadClause_;
02225
02226 bool useThreshold_;
02227
02228 Array<long double> threshold_;
02229
02230
02231
02232 Array<Array<int> > occurence_;
02233
02234
02235 Array<bool> atom_;
02236
02237 Array<long double> makeCost_;
02238
02239 Array<long double> breakCost_;
02240
02241
02242 Array<int> fixedAtom_;
02243
02244
02245 Array<bool> lowAtom_;
02246
02247 long double lowCost_;
02248
02249 int lowBad_;
02250
02251
02252 int numFalseClauses_;
02253
02254 long double costOfFalseClauses_;
02255
02256
02257
02258 Array<Array<int> >* blocks_;
02259
02260
02261 Array<bool >* blockEvidence_;
02262
02263
02264 int activeAtoms_;
02265
02266 };
02267
02268 #endif