#include <variablestate.h>
Public Member Functions | |
VariableState (GroundPredicateHashArray *const &unknownQueries, GroundPredicateHashArray *const &knownQueries, Array< TruthValue > *const &knownQueryValues, const Array< int > *const &allPredGndingsAreQueries, const bool &markHardGndClauses, const bool &trackParentClauseWts, const MLN *const &mln, const Domain *const &domain, const bool &lazy) | |
Constructor for a VariableState. | |
~VariableState () | |
Destructor. | |
void | addNewClauses (bool initial) |
New clauses are added to the state. | |
void | init () |
Information about the state is reset and initialized. | |
void | reinit () |
State is re-initialized with all new clauses and atoms. | |
void | initRandom () |
Makes a random truth assigment to all (active) atoms. | |
void | initBlocksRandom () |
Sets one atom in each block to true and the rest to false. | |
void | resetMakeBreakCostWatch () |
Resets the makeCost, breakCost and watch arrays. | |
void | initMakeBreakCostWatch () |
Re-initializes the makeCost, breakCost and watch arrays based on the current variable assignment. | |
void | initMakeBreakCostWatch (const int &startClause) |
Initialize makeCost and breakCost and watch arrays based on the current variable assignment. | |
int | getNumAtoms () |
int | getNumClauses () |
int | getNumDeadClauses () |
int | getIndexOfRandomAtom () |
Returns the absolute index of a random atom. | |
int | getIndexOfAtomInRandomFalseClause () |
Returns the absolute index of a random atom in a random unsatisfied pos. | |
int | getRandomFalseClauseIndex () |
Returns the index of a random unsatisfied pos. | |
long double | getCostOfFalseClauses () |
Returns the cost of the unsatisfied positive and satisfied negative clauses. | |
int | getNumFalseClauses () |
Returns the number of the unsatisfied positive and satisfied negative clauses. | |
bool | getValueOfAtom (const int &atomIdx) |
Returns the truth value of an atom. | |
bool | getValueOfLowAtom (const int &atomIdx) |
Returns the truth value of an atom in the best state. | |
void | setValueOfAtom (const int &atomIdx, const bool &value, const bool &activate, const int &blockIdx) |
Sets the truth value of an atom. | |
Array< int > & | getNegOccurenceArray (const int &atomIdx) |
Retrieves the negative occurence array of an atom. | |
Array< int > & | getPosOccurenceArray (const int &atomIdx) |
Retrieves the positive occurence array of an atom. | |
void | flipAtom (const int &toFlip, const int &blockIdx) |
Flip the truth value of an atom and update info arrays. | |
void | flipAtomValue (const int &atomIdx, const int &blockIdx) |
Flips the truth value of an atom. | |
long double | getImprovementByFlipping (const int &atomIdx) |
Calculates the improvement achieved by flipping an atom. | |
void | setActive (const int &atomIdx) |
Set the active status of an atom to true in the database if in lazy mode. | |
bool | activateAtom (const int &atomIdx, const bool &ignoreActivePreds, const bool &groundOnly) |
If in lazy mode, an atom is activated and all clauses activated by this atom are added to the state. | |
void | setInferenceMode (int mode) |
Set the inference mode currently being used. | |
bool | isFixedAtom (int atomIdx) |
Returns true, if an atom is fixed to true or false. | |
bool | isSatisfiedClause (const int &clauseIdx) |
void | setSatisfiedClause (const int &clauseIdx) |
void | updatePrevSatisfied () |
void | unitPropagation () |
Perform unit propagation on the current clauses. | |
void | addNewClauses (int addType, Array< GroundClause * > &clauses) |
Adds new clauses to the state. | |
void | updateSatisfiedClauses (const int &toFix) |
Updates isSatisfiedClause after an atom is fixed. | |
void | updateMakeBreakCostAfterFlip (const int &toFlip) |
Updates cost after flip. | |
bool | isActive (const int &atomIdx) |
Checks if an atom is active. | |
bool | isActive (const Predicate *pred) |
Checks if an atom is active. | |
Array< int > & | getOccurenceArray (const int &idx) |
Retrieves the occurence array of an atom. | |
int | incrementNumTrueLits (const int &clauseIdx) |
Increments the number of true literals in a clause. | |
int | decrementNumTrueLits (const int &clauseIdx) |
Decrements the number of true literals in a clause. | |
int | getNumTrueLits (const int &clauseIdx) |
Retrieves the number of true literals in a clause. | |
long double | getClauseCost (const int &clauseIdx) |
Retrieves the cost associated with a clause. | |
Array< int > & | getAtomsInClause (const int &clauseIdx) |
Retrieves the atoms in a clauses as an Array of ints. | |
void | addFalseClause (const int &clauseIdx) |
Marks a clause as false in the state. | |
void | removeFalseClause (const int &clauseIdx) |
Unmarks a clause as false in the state. | |
void | addBreakCost (const int &atomIdx, const long double &cost) |
Increases the breakcost of an atom. | |
void | subtractBreakCost (const int &atomIdx, const long double &cost) |
Decreases the breakcost of an atom. | |
void | addBreakCostToAtomsInClause (const int &clauseIdx, const long double &cost) |
Increases breakCost of all atoms in a given clause. | |
void | subtractBreakCostFromAtomsInClause (const int &clauseIdx, const long double &cost) |
Decreases breakCost of all atoms in a given clause. | |
void | addMakeCost (const int &atomIdx, const long double &cost) |
Increases makeCost of an atom. | |
void | subtractMakeCost (const int &atomIdx, const long double &cost) |
Decreases makeCost of an atom. | |
void | addMakeCostToAtomsInClause (const int &clauseIdx, const long double &cost) |
Increases makeCost of all atoms in a given clause. | |
void | subtractMakeCostFromAtomsInClause (const int &clauseIdx, const long double &cost) |
Decreases makeCost of all atoms in a given clause. | |
const int | getTrueLiteralOtherThan (const int &clauseIdx, const int &atomIdx1, const int &atomIdx2) |
Retrieves a true literal in a clause other than the two given. | |
const bool | isTrueLiteral (const int &literal) |
Checks if a literal is true (Negated and false or non-negated an true). | |
const int | getAtomInClause (const int &atomIdxInClause, const int &clauseIdx) |
Retrieves the absolute index of the nth atom in a clause. | |
const int | getRandomAtomInClause (const int &clauseIdx) |
Retrieves the absolute index of a random atom in a clause. | |
const int | getRandomTrueLitInClause (const int &clauseIdx) |
Retrieves the index of a random true literal in a clause. | |
const double | getMaxClauseWeight () |
const long double | getMakeCost (const int &atomIdx) |
const long double | getBreakCost (const int &atomIdx) |
const int | getClauseSize (const int &clauseIdx) |
const int | getWatch1 (const int &clauseIdx) |
void | setWatch1 (const int &clauseIdx, const int &atomIdx) |
const int | getWatch2 (const int &clauseIdx) |
void | setWatch2 (const int &clauseIdx, const int &atomIdx) |
const int | getBlockIndex (const int &atomIdx) |
Returns the index of the block which the atom with index atomIdx is in. | |
const long double | getLowCost () |
Returns the cost of bad clauses in the optimum state. | |
const int | getLowBad () |
Returns the number of bad clauses in the optimum state. | |
void | makeUnitCosts () |
Turns all costs into units. | |
void | saveLowState () |
Save current assignment as an optimum state. | |
int | getTrueFixedAtomInBlock (const int &blockIdx) |
Returns index of an atom if it is true and fixed in block, otherwise -1. | |
const GroundPredicateHashArray * | getGndPredHashArrayPtr () const |
const GroundPredicateHashArray * | getUnePreds () const |
const GroundPredicateHashArray * | getKnePreds () const |
const Array< TruthValue > * | getKnePredValues () const |
void | setGndClausesWtsToSumOfParentWts () |
Sets the weight of a ground clause to the sum of its parents' weights. | |
void | getNumClauseGndings (Array< double > *const &numGndings, bool tv) |
Gets the number of (true or false) clause groundings in this state. | |
void | getNumClauseGndingsWithUnknown (double numGndings[], int clauseCnt, bool tv, const Array< bool > *const &unknownPred) |
Gets the number of (true or false) clause groundings in this state. | |
void | setOthersInBlockToFalse (const int &atomIdx, const int &blockIdx) |
Sets the truth values of all atoms in a block except for the one given. | |
void | fixAtom (const int &atomIdx, const bool &value) |
Fixes an atom to a truth value. | |
Array< int > * | simplifyClauseFromFixedAtoms (const int &clauseIdx) |
Simplifies a clause using atoms which have been fixed. | |
const bool | isDeadClause (const int &clauseIdx) |
Checks if a clause is dead. | |
void | eliminateSoftClauses () |
Marks soft clauses as dead. | |
void | LoadDisEviValuesFromRst (const char *szDisEvi) |
void | killClauses (const int &startClause) |
Marks clauses as dead which were not good in the previous iteration of inference or are not picked according to a weighted coin flip. | |
const bool | clauseGoodInPrevious (const int &clauseIdx) |
Checks if a clause was good in the previous iteration of inference, i.e. | |
void | resetDeadClauses () |
Resets all dead clauses to be alive again. | |
void | resetFixedAtoms () |
Resets all fixed atoms to be not fixed again. | |
void | setLazy (const bool &l) |
const bool | getLazy () |
void | setUseThreshold (const bool &t) |
const bool | getUseThreshold () |
long double | getHardWt () |
const Domain * | getDomain () |
const MLN * | getMLN () |
void | printLowState (ostream &out) |
Prints the best state found to a stream. | |
void | printGndPred (const int &predIndex, ostream &out) |
Prints a ground predicate to a stream. | |
int | getIndexOfGroundPredicate (GroundPredicate *const &gndPred) |
Finds and returns the index of a ground predicate. | |
void | setAsEvidence (const GroundPredicate *const &predicate, const bool &trueEvidence) |
Sets a GroundPredicate to be evidence and sets its truth value. | |
void | setAsQuery (const GroundPredicate *const &predicate) |
Sets a GroundPredicate to be query. | |
GroundPredicate * | getGndPred (const int &index) |
Gets a pointer to a GroundPredicate. | |
GroundClause * | getGndClause (const int &index) |
Gets a pointer to a GroundClause. | |
void | saveLowStateToGndPreds () |
The atom assignment in the best state is saved to the ground predicates. | |
void | saveLowStateToDB () |
The atom assignment in the best state is saved to the database. | |
const int | getGndPredIndex (GroundPredicate *const &gndPred) |
Gets the index of a GroundPredicate in this state. | |
void | getActiveClauses (Predicate *inputPred, Array< GroundClause * > &activeClauses, bool const &active, bool const &ignoreActivePreds) |
Gets clauses and weights activated by the predicate inputPred, if active is true. | |
void | getActiveClauses (Array< GroundClause * > &allClauses, bool const &ignoreActivePreds) |
Get all the active clauses in the database. | |
int | getNumActiveAtoms () |
const void | setBreakHardClauses (const bool &breakHardClauses) |
Static Public Attributes | |
static const int | MODE_MWS = 0 |
static const int | MODE_HARD = 1 |
static const int | MODE_SAMPLESAT = 2 |
static const int | ADD_CLAUSE_INITIAL = 0 |
static const int | ADD_CLAUSE_REGULAR = 1 |
static const int | ADD_CLAUSE_DEAD = 2 |
static const int | ADD_CLAUSE_SAT = 3 |
Some of this code is based on the MaxWalkSat package of Kautz et al.
All inference algorithms should have a VariableState to access the information needed in its predicates and clauses.
Each atom has its own index starting at 1. The negation of an atom with index a is represented by -a (this is why the indices do not start at 0). Each clause has its own index starting at 0.
A VariableState is either eager or lazy. Eager states build an MRF upfront based on an MLN and a domain. Thus, all ground clauses and predicates are in memory after building the MRF. Lazy states activate atoms and clauses as they are needed from the MLN and domain. An atom is activated if it is in an unsatisfied clause with the assumption of all atoms being false or if it is looked at during inference (it is flipped or the cost of flipping it is computed). Active clauses are those which contain active atoms.
Definition at line 97 of file variablestate.h.
VariableState::VariableState | ( | GroundPredicateHashArray *const & | unknownQueries, | |
GroundPredicateHashArray *const & | knownQueries, | |||
Array< TruthValue > *const & | knownQueryValues, | |||
const Array< int > *const & | allPredGndingsAreQueries, | |||
const bool & | markHardGndClauses, | |||
const bool & | trackParentClauseWts, | |||
const MLN *const & | mln, | |||
const Domain *const & | domain, | |||
const bool & | lazy | |||
) | [inline] |
Constructor for a VariableState.
The hard clause weight is set. In lazy mode, the initial active atoms and clauses are retrieved and in eager mode, the MRF is built and the atoms and clauses are retrieved from it. In addition, all information arrays are filled with information.
unknownQueries | Query predicates with unknown values used to build MRF in eager mode. | |
knownQueries | Query predicates with known values used to build MRF in eager mode. | |
knownQueryValues | Truth values of the known query predicates. | |
allPredGndingsAreQueries | Array used to build MRF in eager mode. | |
mln | mln and domain are used to build MRF in eager state and to retrieve active atoms in lazy state. | |
domain | mln and domain are used to build MRF in eager state and to retrieve active atoms in lazy state. | |
lazy | Flag stating whether lazy mode is used or not. |
Definition at line 119 of file variablestate.h.
References addNewClauses(), HashArray< Type, HashFn, EqualFn >::append(), Array< Type >::clear(), Domain::computeNumNonEvidAtoms(), MRF::deleteGndPredsGndClauseSets(), getActiveClauses(), Domain::getDB(), MRF::getGndClauses(), MRF::getGndPreds(), getNumClauses(), Domain::getNumNonEvidenceAtoms(), initBlocksRandom(), MODE_MWS, Timer::printTime(), Database::setActiveStatus(), Database::setPerformingInference(), HashArray< Type, HashFn, EqualFn >::size(), Array< Type >::size(), and Timer::time().
00127 { 00128 stillActivating_ = true; 00129 breakHardClauses_ = false; 00130 // By default MaxWalkSAT mode 00131 inferenceMode_ = MODE_MWS; 00132 Timer timer; 00133 double startTime = timer.time(); 00134 00135 this->mln_ = (MLN*)mln; 00136 this->domain_ = (Domain*)domain; 00137 this->lazy_ = lazy; 00138 00139 // Instantiate information 00140 baseNumAtoms_ = 0; 00141 activeAtoms_ = 0; 00142 numFalseClauses_ = 0; 00143 costOfFalseClauses_ = 0.0; 00144 lowCost_ = LDBL_MAX; 00145 lowBad_ = INT_MAX; 00146 00147 // Clauses and preds are stored in gndClauses_ and gndPreds_ 00148 gndClauses_ = new GroundClauseHashArray; 00149 gndPreds_ = new Array<GroundPredicate*>; 00150 00151 // Set the hard clause weight 00152 setHardClauseWeight(); 00153 00154 // Lazy version: Produce state with initial active atoms and clauses 00155 if (lazy_) 00156 { 00157 // Set number of non-evidence atoms from domain 00158 domain_->computeNumNonEvidAtoms(); 00159 numNonEvAtoms_ = domain_->getNumNonEvidenceAtoms(); 00160 // Unknown preds are treated as false 00161 domain_->getDB()->setPerformingInference(true); 00162 clauseLimit_ = INT_MAX; 00163 noApprox_ = false; 00164 haveDeactivated_ = false; 00165 00167 // Get initial set of active atoms (atoms in unsat. clauses) 00168 // Assumption is: all atoms are initially false except those in blocks 00169 00170 // One atom in each block is set to true and activated 00171 initBlocksRandom(); 00172 00173 //bool ignoreActivePreds = false; 00174 bool ignoreActivePreds = true; 00175 cout << "Getting initial active atoms ... " << endl; 00176 getActiveClauses(newClauses_, ignoreActivePreds); 00177 cout << "done." << endl; 00178 int defaultCnt = newClauses_.size(); 00179 long double defaultCost = 0; 00180 00181 for (int i = 0; i < defaultCnt; i++) 00182 { 00183 if (newClauses_[i]->isHardClause()) 00184 defaultCost += hardWt_; 00185 else 00186 defaultCost += abs(newClauses_[i]->getWt()); 00187 } 00188 00189 // Clear ground clauses in the ground preds 00190 for (int i = 0; i < gndPredHashArray_.size(); i++) 00191 gndPredHashArray_[i]->removeGndClauses(); 00192 00193 // Delete new clauses 00194 for (int i = 0; i < newClauses_.size(); i++) 00195 delete newClauses_[i]; 00196 newClauses_.clear(); 00197 00198 baseNumAtoms_ = gndPredHashArray_.size(); 00199 cout << "Number of Baseatoms = " << baseNumAtoms_ << endl; 00200 cout << "Default => Cost\t" << "******\t" << " Clause Cnt\t" << endl; 00201 cout << " " << defaultCost << "\t" << "******\t" << defaultCnt 00202 << "\t" << endl << endl; 00203 00204 // Set base atoms as active in DB 00205 for (int i = 0; i < baseNumAtoms_; i++) 00206 { 00207 domain_->getDB()->setActiveStatus(gndPredHashArray_[i], true); 00208 activeAtoms_++; 00209 } 00210 00211 // Get the initial set of active clauses 00212 ignoreActivePreds = false; 00213 cout << "Getting initial active clauses ... "; 00214 getActiveClauses(newClauses_, ignoreActivePreds); 00215 cout << "done." << endl; 00216 } // End lazy version 00217 // Eager version: Use KBMC to produce the state 00218 else 00219 { 00220 unePreds_ = unknownQueries; 00221 knePreds_ = knownQueries; 00222 knePredValues_ = knownQueryValues; 00223 00224 // MRF is built on known and unknown queries 00225 int size = 0; 00226 if (unknownQueries) size += unknownQueries->size(); 00227 if (knownQueries) size += knownQueries->size(); 00228 GroundPredicateHashArray* queries = new GroundPredicateHashArray(size); 00229 if (unknownQueries) queries->append(unknownQueries); 00230 if (knownQueries) queries->append(knownQueries); 00231 mrf_ = new MRF(queries, allPredGndingsAreQueries, domain_, 00232 domain_->getDB(), mln_, markHardGndClauses, 00233 trackParentClauseWts, -1); 00234 00235 //delete to save space. Can be deleted because no more gndClauses are 00236 //appended to gndPreds beyond this point 00237 mrf_->deleteGndPredsGndClauseSets(); 00238 //do not delete the intArrRep in gndPreds_; 00239 delete queries; 00240 00241 // Put ground clauses in newClauses_ 00242 newClauses_ = *(Array<GroundClause*>*)mrf_->getGndClauses(); 00243 // Put ground preds in the hash array 00244 //const Array<GroundPredicate*>* gndPreds = mrf_->getGndPreds(); 00245 const GroundPredicateHashArray* gndPreds = mrf_->getGndPreds(); 00246 for (int i = 0; i < gndPreds->size(); i++) 00247 gndPredHashArray_.append((*gndPreds)[i]); 00248 00249 // baseNumAtoms_ are all atoms in eager version 00250 baseNumAtoms_ = gndPredHashArray_.size(); 00251 } // End eager version 00252 00253 // At this point, ground clauses are held in newClauses_ 00254 // and ground predicates are held in gndPredHashArray_ 00255 // for both versions 00256 00257 // Add the clauses and preds and fill info arrays 00258 bool initial = true; 00259 addNewClauses(initial); 00260 00261 cout << "[VS] "; 00262 Timer::printTime(cout,timer.time()-startTime); 00263 cout << endl; 00264 cout << ">>> DONE: Initial num. of clauses: " << getNumClauses() << endl; 00265 }
VariableState::~VariableState | ( | ) | [inline] |
Destructor.
MRF is deleted in eager version.
Definition at line 270 of file variablestate.h.
References HashArray< Type, HashFn, EqualFn >::size().
00271 { 00272 if (lazy_) 00273 { 00274 if (gndClauses_) 00275 for (int i = 0; i < gndClauses_->size(); i++) 00276 delete (*gndClauses_)[i]; 00277 00278 for (int i = 0; i < gndPredHashArray_.size(); i++) 00279 { 00280 gndPredHashArray_[i]->removeGndClauses(); 00281 delete gndPredHashArray_[i]; 00282 } 00283 } 00284 else 00285 { 00286 // MRF from eager version is deleted 00287 if (mrf_) 00288 { 00289 delete mrf_; 00290 mrf_ = NULL; 00291 } 00292 //if (unePreds_) delete unePreds_; 00293 //if (knePreds_) delete knePreds_; 00294 //if (knePredValues_) delete knePredValues_; 00295 } 00296 //delete gndClauses_; 00297 //delete gndPreds_; 00298 //delete mln_; 00299 }
void VariableState::addNewClauses | ( | bool | initial | ) | [inline] |
New clauses are added to the state.
If not the initialization, then makecost and breakcost are updated for the new atoms.
initial | If true, this is the first time new clauses have been added. |
Definition at line 309 of file variablestate.h.
References ADD_CLAUSE_INITIAL, and ADD_CLAUSE_REGULAR.
Referenced by activateAtom(), fixAtom(), getIndexOfRandomAtom(), initBlocksRandom(), reinit(), and VariableState().
00310 { 00311 if (initial) addNewClauses(ADD_CLAUSE_INITIAL, newClauses_); 00312 else addNewClauses(ADD_CLAUSE_REGULAR, newClauses_); 00313 }
void VariableState::initRandom | ( | ) | [inline] |
Makes a random truth assigment to all (active) atoms.
Blocks are taken into account: exactly one atom in the block is set to true and the others are set to false.
Definition at line 362 of file variablestate.h.
References activateAtom(), getBlockIndex(), getNegOccurenceArray(), getPosOccurenceArray(), init(), initBlocksRandom(), isActive(), MODE_SAMPLESAT, setValueOfAtom(), Array< Type >::size(), and unitPropagation().
Referenced by MaxWalkSat::init().
00363 { 00364 // If performing SampleSat, do a run of UP 00365 if (inferenceMode_ == MODE_SAMPLESAT) 00366 { 00367 unitPropagation(); 00368 } 00369 00370 // Set one in each block to true randomly 00371 initBlocksRandom(); 00372 00373 for (int i = 1; i <= gndPreds_->size(); i++) 00374 { 00375 if (fixedAtom_[i] != 0) 00376 { 00377 continue; 00378 } 00379 00380 // Blocks are initialized above 00381 if (getBlockIndex(i - 1) >= 0) 00382 { 00383 if (vsdebug) cout << "Atom " << i << " in block" << endl; 00384 continue; 00385 } 00386 // Not fixed and not in block 00387 else 00388 { 00389 if (vsdebug) cout << "Atom " << i << " not in block" << endl; 00390 00391 // only need to activate inactive true atoms 00392 if (!lazy_ || isActive(i)) 00393 { 00394 bool isTrue = random() % 2; 00395 bool activate = false; 00396 setValueOfAtom(i, isTrue, activate, -1); 00397 } 00398 else if (inferenceMode_ == MODE_SAMPLESAT) 00399 { 00400 bool isInPrevUnsat = false; 00401 Array<int> oca = getPosOccurenceArray(i); 00402 for (int k = 0; k < oca.size(); k++) 00403 if (!prevSatisfiedClause_[oca[k]]) 00404 { 00405 isInPrevUnsat = true; 00406 break; 00407 } 00408 00409 if (!isInPrevUnsat) 00410 { 00411 oca = getNegOccurenceArray(i); 00412 for (int k = 0; k < oca.size(); k++) 00413 if (!prevSatisfiedClause_[oca[k]]) 00414 { 00415 isInPrevUnsat = true; 00416 break; 00417 } 00418 } 00419 if (isInPrevUnsat) 00420 { 00421 bool isTrue = random() % 2; 00422 if (isTrue) 00423 { 00424 if (activateAtom(i, false, false)) 00425 setValueOfAtom(i, true, false, -1); 00426 } 00427 } 00428 } 00429 } 00430 } 00431 00432 init(); 00433 }
void VariableState::initMakeBreakCostWatch | ( | const int & | startClause | ) | [inline] |
Initialize makeCost and breakCost and watch arrays based on the current variable assignment.
startClause | All clauses with index of this or greater are initialized. |
Definition at line 553 of file variablestate.h.
References getClauseSize(), getNumClauses(), and isTrueLiteral().
00554 { 00555 int theTrueLit = -1; 00556 // Initialize breakCost and makeCost in the following: 00557 for (int i = startClause; i < getNumClauses(); i++) 00558 { 00559 // Don't look at dead or satisfied clauses 00560 if (deadClause_[i] || isSatisfied_[i]) continue; 00561 00562 int trueLit1 = 0; 00563 int trueLit2 = 0; 00564 long double cost = clauseCost_[i]; 00565 numTrueLits_[i] = 0; 00566 for (int j = 0; j < getClauseSize(i); j++) 00567 { 00568 if (isTrueLiteral(clause_[i][j])) 00569 { // ij is true lit 00570 numTrueLits_[i]++; 00571 theTrueLit = abs(clause_[i][j]); 00572 if (!trueLit1) trueLit1 = theTrueLit; 00573 else if (trueLit1 && !trueLit2) trueLit2 = theTrueLit; 00574 } 00575 } 00576 00577 // Unsatisfied positive-weighted clauses or 00578 // Satisfied negative-weighted clauses 00579 if ((numTrueLits_[i] == 0 && cost >= 0) || 00580 (numTrueLits_[i] > 0 && cost < 0)) 00581 { 00582 whereFalse_[i] = numFalseClauses_; 00583 falseClause_[numFalseClauses_] = i; 00584 numFalseClauses_++; 00585 costOfFalseClauses_ += abs(cost); 00586 if (highestCost_ == abs(cost)) {eqHighest_ = true; numHighest_++;} 00587 00588 // Unsat. pos. clause: increase makeCost_ of all atoms 00589 if (numTrueLits_[i] == 0) 00590 for (int j = 0; j < getClauseSize(i); j++) 00591 { 00592 makeCost_[abs(clause_[i][j])] += cost; 00593 } 00594 00595 // Sat. neg. clause: increase makeCost_ if one true literal 00596 if (numTrueLits_[i] == 1) 00597 { 00598 // Subtract neg. cost 00599 makeCost_[theTrueLit] -= cost; 00600 watch1_[i] = theTrueLit; 00601 } 00602 else if (numTrueLits_[i] > 1) 00603 { 00604 watch1_[i] = trueLit1; 00605 watch2_[i] = trueLit2; 00606 } 00607 } 00608 // Pos. clauses satisfied by one true literal 00609 else if (numTrueLits_[i] == 1 && cost >= 0) 00610 { 00611 breakCost_[theTrueLit] += cost; 00612 watch1_[i] = theTrueLit; 00613 } 00614 // Pos. clauses satisfied by 2 or more true literals 00615 else if (cost >= 0) 00616 { /*if (numtruelit[i] == 2)*/ 00617 watch1_[i] = trueLit1; 00618 watch2_[i] = trueLit2; 00619 } 00620 // Unsat. neg. clauses: increase breakCost of all atoms 00621 else if (numTrueLits_[i] == 0 && cost < 0) 00622 { 00623 for (int j = 0; j < getClauseSize(i); j++) 00624 breakCost_[abs(clause_[i][j])] -= cost; 00625 } 00626 } // for all clauses 00627 }
int VariableState::getIndexOfRandomAtom | ( | ) | [inline] |
Returns the absolute index of a random atom.
If in lazy mode, a non-evidence atom which is not in memory could be picked. In this case, the atom is added to the set in memory and its index is returned.
Definition at line 646 of file variablestate.h.
References addNewClauses(), HashArray< Type, HashFn, EqualFn >::append(), HashArray< Type, HashFn, EqualFn >::find(), Domain::getNonEvidenceAtom(), getNumAtoms(), GroundPredicate::print(), and HashArray< Type, HashFn, EqualFn >::size().
Referenced by MaxWalkSat::pickSS().
00647 { 00648 // Lazy: pick a random non-evidence atom from domain 00649 if (lazy_) 00650 { 00651 Predicate* pred = domain_->getNonEvidenceAtom(random() % numNonEvAtoms_); 00652 GroundPredicate* gndPred = new GroundPredicate(pred); 00653 delete pred; 00654 00655 int idx = gndPredHashArray_.find(gndPred); 00656 // Already present, then return index 00657 if (idx >= 0) 00658 { 00659 delete gndPred; 00660 return idx + 1; 00661 } 00662 // Not present, add it to the state 00663 else 00664 { 00665 if (vsdebug) 00666 { 00667 cout << "Adding randomly "; 00668 gndPred->print(cout, domain_); 00669 cout << " to the state" << endl; 00670 } 00671 gndPredHashArray_.append(gndPred); 00672 bool initial = false; 00673 addNewClauses(initial); 00674 // index to return is the last element 00675 return gndPredHashArray_.size(); 00676 } 00677 } 00678 // Eager: pick an atom from the state 00679 else 00680 { 00681 int numAtoms = getNumAtoms(); 00682 if (numAtoms == 0) return NOVALUE; 00683 return random()%numAtoms + 1; 00684 } 00685 }
int VariableState::getIndexOfAtomInRandomFalseClause | ( | ) | [inline] |
Returns the absolute index of a random atom in a random unsatisfied pos.
clause or the absolute index of a random true literal in a random satisfied clause.
Definition at line 692 of file variablestate.h.
References getClauseSize(), and getRandomTrueLitInClause().
Referenced by MaxWalkSat::pickRandom().
00693 { 00694 if (numFalseClauses_ == 0) return NOVALUE; 00695 int clauseIdx = falseClause_[random()%numFalseClauses_]; 00696 00697 // Pos. clause: return index of random atom 00698 if (clauseCost_[clauseIdx] >= 0) 00699 { 00700 // Q: Can all atoms in a clause be fixed? 00701 while (true) 00702 { 00703 int i = random()%getClauseSize(clauseIdx); 00704 if (!fixedAtom_[abs(clause_[clauseIdx][i])]) 00705 return abs(clause_[clauseIdx][i]); 00706 } 00707 } 00708 // Neg. clause: find random true lit 00709 else 00710 return getRandomTrueLitInClause(clauseIdx); 00711 }
int VariableState::getRandomFalseClauseIndex | ( | ) | [inline] |
Returns the index of a random unsatisfied pos.
clause or a random satisfied neg. clause.
Definition at line 717 of file variablestate.h.
Referenced by MaxWalkSat::pickBest(), and MaxWalkSat::pickTabu().
00718 { 00719 if (numFalseClauses_ == 0) return NOVALUE; 00720 return falseClause_[random()%numFalseClauses_]; 00721 }
bool VariableState::getValueOfAtom | ( | const int & | atomIdx | ) | [inline] |
Returns the truth value of an atom.
Index of atom whose truth value is returned.
Definition at line 747 of file variablestate.h.
Referenced by MaxWalkSat::calculateImprovement(), flipAtom(), MaxWalkSat::pickRandom(), updateMakeBreakCostAfterFlip(), and updateSatisfiedClauses().
bool VariableState::getValueOfLowAtom | ( | const int & | atomIdx | ) | [inline] |
Returns the truth value of an atom in the best state.
Index of atom whose truth value is returned.
Definition at line 758 of file variablestate.h.
Referenced by UnitPropagation::getChangedPreds(), SAT::getChangedPreds(), UnitPropagation::getProbability(), SAT::getProbability(), SAT::getProbabilityH(), UnitPropagation::printProbabilities(), SAT::printProbabilities(), UnitPropagation::printTruePreds(), SAT::printTruePreds(), SAT::printTruePredsH(), and MCMC::saveLowStateToChain().
void VariableState::setValueOfAtom | ( | const int & | atomIdx, | |
const bool & | value, | |||
const bool & | activate, | |||
const int & | blockIdx | |||
) | [inline] |
Sets the truth value of an atom.
The truth value is propagated to the database.
atomIdx | Index of atom whose value is to be set. | |
value | Truth value being set. | |
activate | If true, atom is activated if not active. | |
blockIdx | Indicates which block the atom being flipped is in. If not in one, this is -1. |
Definition at line 773 of file variablestate.h.
References activateAtom(), Domain::getDB(), isActive(), Predicate::printWithStrVar(), Domain::setTruePredInBlock(), and Database::setValue().
Referenced by activateAtom(), fixAtom(), flipAtomValue(), initBlocksRandom(), initRandom(), and setOthersInBlockToFalse().
00775 { 00776 // If atom already has this value, then do nothing 00777 if (atom_[atomIdx] == value) return; 00778 if (vsdebug) cout << "Setting value of atom " << atomIdx 00779 << " to " << value << endl; 00780 // Propagate assignment to DB 00781 GroundPredicate* p = gndPredHashArray_[atomIdx - 1]; 00782 if (value) 00783 domain_->getDB()->setValue(p, TRUE); 00784 else 00785 domain_->getDB()->setValue(p, FALSE); 00786 00787 // If not active, then activate it 00788 if (activate && lazy_ && !isActive(atomIdx)) 00789 { 00790 bool ignoreActivePreds = false; 00791 bool groundOnly = false; 00792 activateAtom(atomIdx, ignoreActivePreds, groundOnly); 00793 } 00794 atom_[atomIdx] = value; 00795 00796 // If in block and setting to true, tell the domain 00797 if (blockIdx > -1 && value) 00798 { 00799 Predicate* pred = p->createEquivalentPredicate(domain_); 00800 domain_->setTruePredInBlock(blockIdx, pred); 00801 if (vsdebug) 00802 { 00803 cout << "Set true pred in block " << blockIdx << " to "; 00804 pred->printWithStrVar(cout, domain_); 00805 cout << endl; 00806 } 00807 } 00808 }
void VariableState::flipAtom | ( | const int & | toFlip, | |
const int & | blockIdx | |||
) | [inline] |
Flip the truth value of an atom and update info arrays.
toFlip | Index of atom to be flipped. | |
blockIdx | Index of block in which atom is, or -1 if not in one. |
Definition at line 834 of file variablestate.h.
References addBreakCost(), addBreakCostToAtomsInClause(), addFalseClause(), addMakeCost(), addMakeCostToAtomsInClause(), decrementNumTrueLits(), flipAtomValue(), getClauseCost(), getOccurenceArray(), getTrueLiteralOtherThan(), getValueOfAtom(), getWatch1(), getWatch2(), incrementNumTrueLits(), removeFalseClause(), setWatch1(), setWatch2(), and Array< Type >::size().
Referenced by MaxWalkSat::flipAtom().
00835 { 00836 bool toFlipValue = getValueOfAtom(toFlip); 00837 register int clauseIdx; 00838 int sign; 00839 int oppSign; 00840 int litIdx; 00841 if (toFlipValue) 00842 sign = 1; 00843 else 00844 sign = 0; 00845 oppSign = sign ^ 1; 00846 00847 flipAtomValue(toFlip, blockIdx); 00848 // Update all clauses in which the atom occurs as a true literal 00849 litIdx = 2*toFlip - sign; 00850 Array<int>& posOccArray = getOccurenceArray(litIdx); 00851 for (int i = 0; i < posOccArray.size(); i++) 00852 { 00853 clauseIdx = posOccArray[i]; 00854 // Don't look at dead or satisfied clauses 00855 if (deadClause_[clauseIdx] || isSatisfied_[clauseIdx]) continue; 00856 00857 // The true lit became a false lit 00858 int numTrueLits = decrementNumTrueLits(clauseIdx); 00859 long double cost = getClauseCost(clauseIdx); 00860 int watch1 = getWatch1(clauseIdx); 00861 int watch2 = getWatch2(clauseIdx); 00862 // 1. If last true lit was flipped, then we have to update 00863 // the makecost / breakcost info accordingly 00864 if (numTrueLits == 0) 00865 { 00866 // Pos. clause 00867 if (cost >= 0) 00868 { 00869 // Add this clause as false in the state 00870 addFalseClause(clauseIdx); 00871 // Decrease toFlip's breakcost (add neg. cost) 00872 addBreakCost(toFlip, -cost); 00873 // Increase makecost of all vars in clause (add pos. cost) 00874 addMakeCostToAtomsInClause(clauseIdx, cost); 00875 } 00876 // Neg. clause 00877 else 00878 { 00879 assert(cost < 0); 00880 // Remove this clause as false in the state 00881 removeFalseClause(clauseIdx); 00882 // Increase breakcost of all vars in clause (add pos. cost) 00883 addBreakCostToAtomsInClause(clauseIdx, -cost); 00884 // Decrease toFlip's makecost (add neg. cost) 00885 addMakeCost(toFlip, cost); 00886 } 00887 } 00888 // 2. If there is now one true lit left, then move watch2 00889 // up to watch1 and increase the breakcost / makecost of watch1 00890 else if (numTrueLits == 1) 00891 { 00892 if (watch1 == toFlip) 00893 { 00894 assert(watch1 != watch2); 00895 setWatch1(clauseIdx, watch2); 00896 watch1 = getWatch1(clauseIdx); 00897 } 00898 00899 // Pos. clause: Increase toFlip's breakcost (add pos. cost) 00900 if (cost >= 0) 00901 { 00902 addBreakCost(watch1, cost); 00903 } 00904 // Neg. clause: Increase toFlip's makecost (add pos. cost) 00905 else 00906 { 00907 assert(cost < 0); 00908 addMakeCost(watch1, -cost); 00909 } 00910 } 00911 // 3. If there are 2 or more true lits left, then we have to 00912 // find a new true lit to watch if one was flipped 00913 else 00914 { /* numtruelit[clauseIdx] >= 2 */ 00915 // If watch1[clauseIdx] has been flipped 00916 if (watch1 == toFlip) 00917 { 00918 // find a different true literal to watch 00919 int diffTrueLit = getTrueLiteralOtherThan(clauseIdx, watch1, watch2); 00920 setWatch1(clauseIdx, diffTrueLit); 00921 } 00922 // If watch2[clauseIdx] has been flipped 00923 else if (watch2 == toFlip) 00924 { 00925 // find a different true literal to watch 00926 int diffTrueLit = getTrueLiteralOtherThan(clauseIdx, watch1, watch2); 00927 setWatch2(clauseIdx, diffTrueLit); 00928 } 00929 } 00930 } 00931 00932 // Update all clauses in which the atom occurs as a false literal 00933 litIdx = 2*toFlip - oppSign; 00934 Array<int>& negOccArray = getOccurenceArray(litIdx); 00935 for (int i = 0; i < negOccArray.size(); i++) 00936 { 00937 clauseIdx = negOccArray[i]; 00938 // Don't look at dead or satisfied clauses 00939 if (deadClause_[clauseIdx] || isSatisfied_[clauseIdx]) continue; 00940 00941 // The false lit became a true lit 00942 int numTrueLits = incrementNumTrueLits(clauseIdx); 00943 long double cost = getClauseCost(clauseIdx); 00944 int watch1 = getWatch1(clauseIdx); 00945 00946 // 1. If this is the only true lit, then we have to update 00947 // the makecost / breakcost info accordingly 00948 if (numTrueLits == 1) 00949 { 00950 // Pos. clause 00951 if (cost >= 0) 00952 { 00953 // Remove this clause as false in the state 00954 removeFalseClause(clauseIdx); 00955 // Increase toFlip's breakcost (add pos. cost) 00956 addBreakCost(toFlip, cost); 00957 // Decrease makecost of all vars in clause (add neg. cost) 00958 addMakeCostToAtomsInClause(clauseIdx, -cost); 00959 } 00960 // Neg. clause 00961 else 00962 { 00963 assert(cost < 0); 00964 // Add this clause as false in the state 00965 addFalseClause(clauseIdx); 00966 // Decrease breakcost of all vars in clause (add neg. cost) 00967 addBreakCostToAtomsInClause(clauseIdx, cost); 00968 // Increase toFlip's makecost (add pos. cost) 00969 addMakeCost(toFlip, -cost); 00970 } 00971 // Watch this atom 00972 setWatch1(clauseIdx, toFlip); 00973 } 00974 // 2. If there are now exactly 2 true lits, then watch second atom 00975 // and update breakcost 00976 else 00977 if (numTrueLits == 2) 00978 { 00979 if (cost >= 0) 00980 { 00981 // Pos. clause 00982 // Decrease breakcost of first atom being watched (add neg. cost) 00983 addBreakCost(watch1, -cost); 00984 } 00985 else 00986 { 00987 // Neg. clause 00988 assert(cost < 0); 00989 // Decrease makecost of first atom being watched (add neg. cost) 00990 addMakeCost(watch1, cost); 00991 } 00992 00993 // Watch second atom 00994 setWatch2(clauseIdx, toFlip); 00995 } 00996 } 00997 }
void VariableState::flipAtomValue | ( | const int & | atomIdx, | |
const int & | blockIdx | |||
) | [inline] |
Flips the truth value of an atom.
If in lazy mode, the truth value is propagated to the database.
atomIdx | Index of atom to be flipped. | |
blockIdx | Index of block in which the atom is in, or -1 if not in one. |
Definition at line 1006 of file variablestate.h.
References setValueOfAtom().
Referenced by flipAtom(), and MaxWalkSat::reconstructLowState().
01007 { 01008 bool opposite = !atom_[atomIdx]; 01009 bool activate = true; 01010 setValueOfAtom(atomIdx, opposite, activate, blockIdx); 01011 }
long double VariableState::getImprovementByFlipping | ( | const int & | atomIdx | ) | [inline] |
Calculates the improvement achieved by flipping an atom.
This is the cost of clauses which flipping the atom makes good minus the cost of clauses which flipping the atom makes bad. In lazy mode, if the atom is not active, then the atom is activated and the makecost and breakcost are updated.
atomIdx | Index of atom to flip. |
Definition at line 1024 of file variablestate.h.
Referenced by MaxWalkSat::calculateImprovement(), and MaxWalkSat::pickRandom().
01025 { 01026 if (!breakHardClauses_ && breakCost_[atomIdx] >= hardWt_) 01027 return -LDBL_MAX; 01028 long double improvement = makeCost_[atomIdx] - breakCost_[atomIdx]; 01029 return improvement; 01030 }
void VariableState::setActive | ( | const int & | atomIdx | ) | [inline] |
Set the active status of an atom to true in the database if in lazy mode.
Index of the atom to be set to active.
Definition at line 1037 of file variablestate.h.
References Domain::getDB(), and Database::setActiveStatus().
Referenced by MaxWalkSat::pickSS().
01038 { 01039 if (lazy_) 01040 { 01041 Predicate* p = 01042 gndPredHashArray_[atomIdx - 1]->createEquivalentPredicate(domain_); 01043 domain_->getDB()->setActiveStatus(p, true); 01044 activeAtoms_++; 01045 delete p; 01046 } 01047 }
bool VariableState::activateAtom | ( | const int & | atomIdx, | |
const bool & | ignoreActivePreds, | |||
const bool & | groundOnly | |||
) | [inline] |
If in lazy mode, an atom is activated and all clauses activated by this atom are added to the state.
If in eager mode, nothing happens.
atomIdx | Index of atom to be activated. | |
ignoreActivePreds | If true, active atoms are ignored when getting active clauses (just gets unsatisfied clauses). | |
groundOnly | If true, atom itself is not activated, just its active clauses are retrieved. |
Definition at line 1059 of file variablestate.h.
References ADD_CLAUSE_DEAD, ADD_CLAUSE_REGULAR, ADD_CLAUSE_SAT, addNewClauses(), Array< Type >::append(), Array< Type >::clear(), fixAtom(), getActiveClauses(), Database::getActiveStatus(), Domain::getDB(), isActive(), Database::setActiveStatus(), setValueOfAtom(), Array< Type >::size(), and updateMakeBreakCostAfterFlip().
Referenced by initRandom(), MaxWalkSat::pickBest(), MaxWalkSat::pickRandom(), MaxWalkSat::pickSS(), MaxWalkSat::pickTabu(), and setValueOfAtom().
01061 { 01062 /*** 01063 * . [IF MCSAT] 01064 * - check neg-wt clauses 01065 * . if alive, fix 01066 * . else mark dead 01067 * - if not fixed, add all pos-wt, killClauses 01068 * - else add dead 01069 * . [ELSE] add all 01070 ***/ 01071 if (lazy_ && !isActive(atomIdx)) 01072 { 01073 if (vsdebug) 01074 { 01075 cout << "\tActivating "; 01076 gndPredHashArray_[atomIdx-1]->print(cout,domain_); 01077 cout << " " << 01078 domain_->getDB()->getActiveStatus(gndPredHashArray_[atomIdx-1]) << endl; 01079 } 01080 01081 // Set atom to true first, see if avoid getting clauses already active 01082 bool needToFlipBack = false; 01083 if (!atom_[atomIdx]) 01084 { 01085 bool activate = false; 01086 setValueOfAtom(atomIdx, true, activate, -1); 01087 updateMakeBreakCostAfterFlip(atomIdx); 01088 needToFlipBack = true; 01089 } 01090 01091 // gather active clauses 01092 Predicate* p = 01093 gndPredHashArray_[atomIdx - 1]->createEquivalentPredicate(domain_); 01094 01095 bool isFixed = false; 01096 Array<GroundClause*> unsatClauses; 01097 Array<GroundClause*> deadClauses; 01098 Array<GroundClause*> toAddClauses; 01099 01100 // Using newClauses_ causes problem since it's shared across the board 01101 getActiveClauses(p, unsatClauses, true, ignoreActivePreds); 01102 01103 // if MC-SAT: check neg-wt or unit clauses and see if can fix 01104 if (useThreshold_) 01105 { 01106 // first append deadClauses, then pos-wt 01107 for (int ni = 0; ni < unsatClauses.size(); ni++) 01108 { 01109 GroundClause* c = unsatClauses[ni]; 01110 if (c->getWt() < 0) 01111 { // Neg. weighted clause 01112 // since newClauses_ excludes clauses in memory, c must be 01113 // goodInPrevious 01114 double threshold = RAND_MAX*(1 - exp(c->getWt())); 01115 if (random() <= threshold) 01116 { 01117 // fix 01118 isFixed = true; 01119 01120 // append dead and fixed clauses first, so fixed atoms do not 01121 // activate them unnecessarily 01122 if (deadClauses.size() > 0) 01123 addNewClauses(ADD_CLAUSE_DEAD, deadClauses); 01124 01125 Array<GroundClause*> fixedClauses; 01126 fixedClauses.append(c); 01127 addNewClauses(ADD_CLAUSE_SAT, fixedClauses); 01128 01129 // --- fix the atoms 01130 for (int pi = 0; pi < c->getNumGroundPredicates(); pi++) 01131 { 01132 int lit = c->getGroundPredicateIndex(pi); 01133 fixAtom(abs(lit), (lit < 0)); 01134 } 01135 01136 // - can quit now 01137 break; 01138 } 01139 else 01140 { 01141 // dead 01142 deadClauses.append(c); 01143 } 01144 } 01145 else if (c->getNumGroundPredicates() == 1) 01146 { // Unit clause 01147 double threshold = RAND_MAX*(1 - exp(-c->getWt())); 01148 if (random() <= threshold) 01149 { 01150 // fix 01151 isFixed = true; 01152 01153 // append dead and fixed clauses first, so fixed atoms do not 01154 // activate them unnecessarily 01155 if (deadClauses.size() > 0) 01156 addNewClauses(ADD_CLAUSE_DEAD, deadClauses); 01157 01158 Array<GroundClause*> fixedClauses; 01159 fixedClauses.append(c); 01160 addNewClauses(ADD_CLAUSE_SAT, fixedClauses); 01161 01162 // --- fix the atoms 01163 int lit = c->getGroundPredicateIndex(0); 01164 fixAtom(abs(lit), (lit > 0)); // this is positive wt 01165 01166 // - can quit now 01167 break; 01168 } 01169 else 01170 { 01171 // dead 01172 deadClauses.append(c); 01173 } 01174 } 01175 else toAddClauses.append(c); 01176 } 01177 } 01178 01179 // Add the clauses and preds and fill info arrays 01180 if (!isFixed) 01181 { 01182 if (deadClauses.size() > 0) 01183 addNewClauses(ADD_CLAUSE_DEAD, deadClauses); 01184 01185 if (useThreshold_) 01186 addNewClauses(ADD_CLAUSE_REGULAR, toAddClauses); 01187 else // lazy-sat 01188 addNewClauses(ADD_CLAUSE_REGULAR, unsatClauses); 01189 01190 // To avoid append already-active: if not fixed, might need to 01191 // flip back 01192 if (needToFlipBack) 01193 { 01194 bool activate = false; 01195 setValueOfAtom(atomIdx, false, activate, -1); 01196 updateMakeBreakCostAfterFlip(atomIdx); 01197 } 01198 01199 if (!groundOnly) 01200 { 01201 // Set active status in db 01202 domain_->getDB()->setActiveStatus(p, true); 01203 activeAtoms_++; 01204 } 01205 } 01206 01207 delete p; 01208 unsatClauses.clear(); 01209 deadClauses.clear(); 01210 toAddClauses.clear(); 01211 01212 return !isFixed; // if !isFixed, then activated 01213 } 01214 return true; 01215 }
void VariableState::addNewClauses | ( | int | addType, | |
Array< GroundClause * > & | clauses | |||
) | [inline] |
Adds new clauses to the state.
addType | How the clauses are being added (see ADD_CLAUSE_*) | |
clauses | Array of ground clauses to be added to the state. |
Definition at line 1367 of file variablestate.h.
References ADD_CLAUSE_DEAD, ADD_CLAUSE_REGULAR, ADD_CLAUSE_SAT, Array< Type >::append(), HashArray< Type, HashFn, EqualFn >::append(), Array< Type >::clear(), Domain::getDB(), getNumAtoms(), getNumClauses(), Database::getValue(), Array< Type >::growToSize(), initMakeBreakCostWatch(), killClauses(), MODE_HARD, MODE_SAMPLESAT, HashArray< Type, HashFn, EqualFn >::size(), and Array< Type >::size().
01368 { 01369 if (vsdebug) 01370 cout << "Adding " << clauses.size() << " new clauses.." << endl; 01371 01372 // Must be in mc-sat to add dead or sat 01373 if (!useThreshold_ && 01374 (addType == ADD_CLAUSE_DEAD || addType == ADD_CLAUSE_SAT)) 01375 { 01376 cout << ">>> [ERR] add_dead/sat but useThreshold_ is false" << endl; 01377 exit(0); 01378 } 01379 01380 // Store the old number of clauses and atoms 01381 int oldNumClauses = getNumClauses(); 01382 int oldNumAtoms = getNumAtoms(); 01383 01384 //gndClauses_->append(clauses); 01385 for (int i = 0; i < clauses.size(); i++) 01386 { 01387 gndClauses_->append(clauses[i]); 01388 clauses[i]->appendToGndPreds(&gndPredHashArray_); 01389 } 01390 01391 gndPreds_->growToSize(gndPredHashArray_.size()); 01392 01393 int numAtoms = getNumAtoms(); 01394 int numClauses = getNumClauses(); 01395 // If no new atoms or clauses have been added, then do nothing 01396 if (numAtoms == oldNumAtoms && numClauses == oldNumClauses) return; 01397 01398 if (vsdebug) cout << "Clauses: " << numClauses << endl; 01399 // atomIdx starts at 1 01400 atom_.growToSize(numAtoms + 1, false); 01401 01402 makeCost_.growToSize(numAtoms + 1, 0.0); 01403 breakCost_.growToSize(numAtoms + 1, 0.0); 01404 lowAtom_.growToSize(numAtoms + 1, false); 01405 fixedAtom_.growToSize(numAtoms + 1, 0); 01406 01407 // Copy ground preds to gndPreds_ and set values in atom and lowAtom 01408 for (int i = oldNumAtoms; i < gndPredHashArray_.size(); i++) 01409 { 01410 (*gndPreds_)[i] = gndPredHashArray_[i]; 01411 01412 if (vsdebug) 01413 { 01414 cout << "New pred " << i + 1 << ": "; 01415 (*gndPreds_)[i]->print(cout, domain_); 01416 cout << endl; 01417 } 01418 01419 lowAtom_[i + 1] = atom_[i + 1] = 01420 (domain_->getDB()->getValue((*gndPreds_)[i]) == TRUE) ? true : false; 01421 } 01422 clauses.clear(); 01423 01424 clause_.growToSize(numClauses); 01425 clauseCost_.growToSize(numClauses); 01426 falseClause_.growToSize(numClauses); 01427 whereFalse_.growToSize(numClauses); 01428 numTrueLits_.growToSize(numClauses); 01429 watch1_.growToSize(numClauses); 01430 watch2_.growToSize(numClauses); 01431 isSatisfied_.growToSize(numClauses, false); 01432 deadClause_.growToSize(numClauses, false); 01433 threshold_.growToSize(numClauses, false); 01434 01435 occurence_.growToSize(2*numAtoms + 1); 01436 01437 for (int i = oldNumClauses; i < numClauses; i++) 01438 { 01439 GroundClause* gndClause = (*gndClauses_)[i]; 01440 01441 if (vsdebug) 01442 { 01443 cout << "New clause " << i << ": "; 01444 gndClause->print(cout, domain_, &gndPredHashArray_); 01445 cout << endl; 01446 } 01447 01448 // Set thresholds for clause selection 01449 if (gndClause->isHardClause()) threshold_[i] = RAND_MAX; 01450 else 01451 { 01452 double w = gndClause->getWt(); 01453 threshold_[i] = RAND_MAX*(1 - exp(-abs(w))); 01454 if (vsdebug) 01455 { 01456 cout << "Weight: " << w << endl; 01457 } 01458 } 01459 if (vsdebug) 01460 cout << "Threshold: " << threshold_[i] << endl; 01461 01462 int numGndPreds = gndClause->getNumGroundPredicates(); 01463 clause_[i].growToSize(numGndPreds); 01464 for (int j = 0; j < numGndPreds; j++) 01465 { 01466 int lit = gndClause->getGroundPredicateIndex(j); 01467 clause_[i][j] = lit; 01468 int litIdx = 2*abs(lit) - (lit > 0); 01469 occurence_[litIdx].append(i); 01470 } 01471 01472 // Hard clause weight has been previously determined 01473 if (inferenceMode_==MODE_SAMPLESAT) 01474 { 01475 if (gndClause->getWt()>0) clauseCost_[i] = 1; 01476 else clauseCost_[i] = -1; 01477 } 01478 else if (gndClause->isHardClause()) 01479 clauseCost_[i] = hardWt_; 01480 else 01481 clauseCost_[i] = gndClause->getWt(); 01482 01483 // filter hard clauses 01484 if (inferenceMode_ == MODE_HARD && !gndClause->isHardClause()) 01485 { 01486 // mark dead 01487 deadClause_[i] = true; 01488 } 01489 } 01490 01491 if (addType == ADD_CLAUSE_DEAD) 01492 { 01493 // set to dead: no need for initMakeBreakCost, won't impact anyone 01494 for (int i = oldNumClauses; i < numClauses; i++) 01495 { 01496 deadClause_[i] = true; 01497 } 01498 } 01499 else if (addType == ADD_CLAUSE_SAT) 01500 { 01501 // set to dead: no need for initMakeBreakCost, won't impact anyone 01502 for (int i = oldNumClauses; i < numClauses; i++) 01503 { 01504 isSatisfied_[i]=true; 01505 } 01506 } 01507 else if (addType == ADD_CLAUSE_REGULAR) 01508 { 01509 if (useThreshold_) 01510 killClauses(oldNumClauses); 01511 else 01512 initMakeBreakCostWatch(oldNumClauses); 01513 } 01514 01515 if (vsdebug) 01516 cout << "Done adding new clauses.." << endl; 01517 }
void VariableState::updateSatisfiedClauses | ( | const int & | toFix | ) | [inline] |
Updates isSatisfiedClause after an atom is fixed.
ASSUME: fixed val already applied, and updateMakeBreakCost called
toFix | Index of atom fixed |
Definition at line 1525 of file variablestate.h.
References getClauseCost(), getOccurenceArray(), getValueOfAtom(), and Array< Type >::size().
Referenced by fixAtom().
01526 { 01527 // Already flipped 01528 bool toFlipValue = getValueOfAtom(toFix); 01529 01530 register int clauseIdx; 01531 int sign; 01532 int litIdx; 01533 if (toFlipValue) 01534 sign = 1; 01535 else 01536 sign = 0; 01537 01538 // Set isSatisfied: all clauses in which the atom occurs as a true literal 01539 litIdx = 2*toFix - sign; 01540 Array<int>& posOccArray = getOccurenceArray(litIdx); 01541 for (int i = 0; i < posOccArray.size(); i++) 01542 { 01543 clauseIdx = posOccArray[i]; 01544 // Don't look at dead clauses 01545 // ignore already sat 01546 if (deadClause_[clauseIdx] || isSatisfied_[clauseIdx]) continue; 01547 01548 if (getClauseCost(clauseIdx) < 0) 01549 { 01550 cout << "ERR: in MC-SAT, active neg-wt clause (" << clauseIdx 01551 << ") is sat by fixed "<<endl; 01552 exit(0); 01553 } 01554 isSatisfied_[clauseIdx] = true; 01555 } 01556 }
void VariableState::updateMakeBreakCostAfterFlip | ( | const int & | toFlip | ) | [inline] |
Updates cost after flip.
Assume: already activated and flipped. Called by flipAtom and fixAtom (if fixed to the opposite val)
Definition at line 1563 of file variablestate.h.
References addBreakCost(), addBreakCostToAtomsInClause(), addFalseClause(), addMakeCost(), addMakeCostToAtomsInClause(), decrementNumTrueLits(), getClauseCost(), getOccurenceArray(), getTrueLiteralOtherThan(), getValueOfAtom(), getWatch1(), getWatch2(), incrementNumTrueLits(), removeFalseClause(), setWatch1(), setWatch2(), and Array< Type >::size().
Referenced by activateAtom(), and fixAtom().
01564 { 01565 // Already flipped 01566 bool toFlipValue = !getValueOfAtom(toFlip); 01567 01568 register int clauseIdx; 01569 int sign; 01570 int oppSign; 01571 int litIdx; 01572 if (toFlipValue) 01573 sign = 1; 01574 else 01575 sign = 0; 01576 oppSign = sign ^ 1; 01577 01578 // Update all clauses in which the atom occurs as a true literal 01579 litIdx = 2*toFlip - sign; 01580 Array<int>& posOccArray = getOccurenceArray(litIdx); 01581 01582 for (int i = 0; i < posOccArray.size(); i++) 01583 { 01584 clauseIdx = posOccArray[i]; 01585 // Don't look at dead clauses and sat clauses 01586 if (deadClause_[clauseIdx] || isSatisfied_[clauseIdx]) continue; 01587 01588 // The true lit became a false lit 01589 int numTrueLits = decrementNumTrueLits(clauseIdx); 01590 long double cost = getClauseCost(clauseIdx); 01591 int watch1 = getWatch1(clauseIdx); 01592 int watch2 = getWatch2(clauseIdx); 01593 01594 // 1. If last true lit was flipped, then we have to update 01595 // the makecost / breakcost info accordingly 01596 if (numTrueLits == 0) 01597 { 01598 // Pos. clause 01599 if (cost >= 0) 01600 { 01601 // Add this clause as false in the state 01602 addFalseClause(clauseIdx); 01603 // Decrease toFlip's breakcost (add neg. cost) 01604 addBreakCost(toFlip, -cost); 01605 // Increase makecost of all vars in clause (add pos. cost) 01606 addMakeCostToAtomsInClause(clauseIdx, cost); 01607 } 01608 // Neg. clause 01609 else 01610 { 01611 assert(cost < 0); 01612 // Remove this clause as false in the state 01613 removeFalseClause(clauseIdx); 01614 // Increase breakcost of all vars in clause (add pos. cost) 01615 addBreakCostToAtomsInClause(clauseIdx, -cost); 01616 // Decrease toFlip's makecost (add neg. cost) 01617 addMakeCost(toFlip, cost); 01618 } 01619 } 01620 // 2. If there is now one true lit left, then move watch2 01621 // up to watch1 and increase the breakcost / makecost of watch1 01622 else if (numTrueLits == 1) 01623 { 01624 if (watch1 == toFlip) 01625 { 01626 assert(watch1 != watch2); 01627 setWatch1(clauseIdx, watch2); 01628 watch1 = getWatch1(clauseIdx); 01629 } 01630 // Pos. clause: Increase toFlip's breakcost (add pos. cost) 01631 if (cost >= 0) 01632 { 01633 addBreakCost(watch1, cost); 01634 } 01635 // Neg. clause: Increase toFlip's makecost (add pos. cost) 01636 else 01637 { 01638 assert(cost < 0); 01639 addMakeCost(watch1, -cost); 01640 } 01641 } 01642 // 3. If there are 2 or more true lits left, then we have to 01643 // find a new true lit to watch if one was flipped 01644 else 01645 { 01646 /* numtruelit[clauseIdx] >= 2 */ 01647 // If watch1[clauseIdx] has been flipped 01648 if (watch1 == toFlip) 01649 { 01650 // find a different true literal to watch 01651 int diffTrueLit = getTrueLiteralOtherThan(clauseIdx, watch1, watch2); 01652 setWatch1(clauseIdx, diffTrueLit); 01653 } 01654 // If watch2[clauseIdx] has been flipped 01655 else if (watch2 == toFlip) 01656 { 01657 // find a different true literal to watch 01658 int diffTrueLit = getTrueLiteralOtherThan(clauseIdx, watch1, watch2); 01659 setWatch2(clauseIdx, diffTrueLit); 01660 } 01661 } 01662 } 01663 01664 // Update all clauses in which the atom occurs as a false literal 01665 litIdx = 2*toFlip - oppSign; 01666 Array<int>& negOccArray = getOccurenceArray(litIdx); 01667 for (int i = 0; i < negOccArray.size(); i++) 01668 { 01669 clauseIdx = negOccArray[i]; 01670 // Don't look at dead clauses or satisfied clauses 01671 if (deadClause_[clauseIdx] || isSatisfied_[clauseIdx]) continue; 01672 01673 // The false lit became a true lit 01674 int numTrueLits = incrementNumTrueLits(clauseIdx); 01675 long double cost = getClauseCost(clauseIdx); 01676 int watch1 = getWatch1(clauseIdx); 01677 01678 // 1. If this is the only true lit, then we have to update 01679 // the makecost / breakcost info accordingly 01680 if (numTrueLits == 1) 01681 { 01682 // Pos. clause 01683 if (cost >= 0) 01684 { 01685 // Remove this clause as false in the state 01686 removeFalseClause(clauseIdx); 01687 // Increase toFlip's breakcost (add pos. cost) 01688 addBreakCost(toFlip, cost); 01689 // Decrease makecost of all vars in clause (add neg. cost) 01690 addMakeCostToAtomsInClause(clauseIdx, -cost); 01691 } 01692 // Neg. clause 01693 else 01694 { 01695 assert(cost < 0); 01696 // Add this clause as false in the state 01697 addFalseClause(clauseIdx); 01698 // Decrease breakcost of all vars in clause (add neg. cost) 01699 addBreakCostToAtomsInClause(clauseIdx, cost); 01700 // Increase toFlip's makecost (add pos. cost) 01701 addMakeCost(toFlip, -cost); 01702 } 01703 // Watch this atom 01704 setWatch1(clauseIdx, toFlip); 01705 } 01706 // 2. If there are now exactly 2 true lits, then watch second atom 01707 // and update breakcost 01708 else if (numTrueLits == 2) 01709 { 01710 if (cost >= 0) 01711 { 01712 // Pos. clause 01713 // Decrease breakcost of first atom being watched (add neg. cost) 01714 addBreakCost(watch1, -cost); 01715 } 01716 else 01717 { 01718 // Neg. clause 01719 assert(cost < 0); 01720 // Decrease makecost of first atom being watched (add neg. cost) 01721 addMakeCost(watch1, cost); 01722 } 01723 // Watch second atom 01724 setWatch2(clauseIdx, toFlip); 01725 } 01726 } 01727 }
bool VariableState::isActive | ( | const int & | atomIdx | ) | [inline] |
Checks if an atom is active.
atomIdx | Index of atom to be checked. |
Definition at line 1736 of file variablestate.h.
References Database::getActiveStatus(), and Domain::getDB().
Referenced by activateAtom(), fixAtom(), initRandom(), and setValueOfAtom().
01737 { 01738 return domain_->getDB()->getActiveStatus(gndPredHashArray_[atomIdx-1]); 01739 }
bool VariableState::isActive | ( | const Predicate * | pred | ) | [inline] |
Checks if an atom is active.
pred | Predicate to be checked. |
Definition at line 1747 of file variablestate.h.
References Database::getActiveStatus(), and Domain::getDB().
01748 { 01749 return domain_->getDB()->getActiveStatus(pred); 01750 }
void VariableState::addBreakCostToAtomsInClause | ( | const int & | clauseIdx, | |
const long double & | cost | |||
) | [inline] |
Increases breakCost of all atoms in a given clause.
clauseIdx | Index of clause whose atoms' breakCost is altered. | |
cost | Cost to be added to atoms' breakCost. |
Definition at line 1844 of file variablestate.h.
References getClauseSize().
Referenced by flipAtom(), and updateMakeBreakCostAfterFlip().
01846 { 01847 register int size = getClauseSize(clauseIdx); 01848 for (int i = 0; i < size; i++) 01849 { 01850 register int lit = clause_[clauseIdx][i]; 01851 breakCost_[abs(lit)] += cost; 01852 } 01853 }
void VariableState::subtractBreakCostFromAtomsInClause | ( | const int & | clauseIdx, | |
const long double & | cost | |||
) | [inline] |
Decreases breakCost of all atoms in a given clause.
clauseIdx | Index of clause whose atoms' breakCost is altered. | |
cost | Cost to be subtracted from atoms' breakCost. |
Definition at line 1861 of file variablestate.h.
References getClauseSize().
01863 { 01864 register int size = getClauseSize(clauseIdx); 01865 for (int i = 0; i < size; i++) 01866 { 01867 register int lit = clause_[clauseIdx][i]; 01868 breakCost_[abs(lit)] -= cost; 01869 } 01870 }
void VariableState::addMakeCost | ( | const int & | atomIdx, | |
const long double & | cost | |||
) | [inline] |
Increases makeCost of an atom.
atomIdx | Index of atom whose makeCost is altered. | |
cost | Cost to be added to atom's makeCost. |
Definition at line 1878 of file variablestate.h.
Referenced by flipAtom(), and updateMakeBreakCostAfterFlip().
void VariableState::subtractMakeCost | ( | const int & | atomIdx, | |
const long double & | cost | |||
) | [inline] |
Decreases makeCost of an atom.
atomIdx | Index of atom whose makeCost is altered. | |
cost | Cost to be subtracted from atom's makeCost. |
Definition at line 1889 of file variablestate.h.
void VariableState::addMakeCostToAtomsInClause | ( | const int & | clauseIdx, | |
const long double & | cost | |||
) | [inline] |
Increases makeCost of all atoms in a given clause.
clauseIdx | Index of clause whose atoms' makeCost is altered. | |
cost | Cost to be added to atoms' makeCost. |
Definition at line 1900 of file variablestate.h.
References getClauseSize().
Referenced by flipAtom(), and updateMakeBreakCostAfterFlip().
01902 { 01903 register int size = getClauseSize(clauseIdx); 01904 for (int i = 0; i < size; i++) 01905 { 01906 register int lit = clause_[clauseIdx][i]; 01907 makeCost_[abs(lit)] += cost; 01908 } 01909 }
void VariableState::subtractMakeCostFromAtomsInClause | ( | const int & | clauseIdx, | |
const long double & | cost | |||
) | [inline] |
Decreases makeCost of all atoms in a given clause.
clauseIdx | Index of clause whose atoms' makeCost is altered. | |
cost | Cost to be subtracted from atoms' makeCost. |
Definition at line 1917 of file variablestate.h.
References getClauseSize().
01919 { 01920 register int size = getClauseSize(clauseIdx); 01921 for (int i = 0; i < size; i++) 01922 { 01923 register int lit = clause_[clauseIdx][i]; 01924 makeCost_[abs(lit)] -= cost; 01925 } 01926 }
const int VariableState::getTrueLiteralOtherThan | ( | const int & | clauseIdx, | |
const int & | atomIdx1, | |||
const int & | atomIdx2 | |||
) | [inline] |
Retrieves a true literal in a clause other than the two given.
clauseIdx | Index of clause from which literal is retrieved. | |
atomIdx1 | Index of first atom excluded from search. | |
atomIdx2 | Index of second atom excluded from search. |
Definition at line 1937 of file variablestate.h.
References getClauseSize(), and isTrueLiteral().
Referenced by flipAtom(), and updateMakeBreakCostAfterFlip().
01940 { 01941 register int size = getClauseSize(clauseIdx); 01942 for (int i = 0; i < size; i++) 01943 { 01944 register int lit = clause_[clauseIdx][i]; 01945 register int v = abs(lit); 01946 if (isTrueLiteral(lit) && v != atomIdx1 && v != atomIdx2) 01947 return v; 01948 } 01949 // If we're here, then no other true lit exists 01950 assert(false); 01951 return -1; 01952 }
const int VariableState::getRandomTrueLitInClause | ( | const int & | clauseIdx | ) | [inline] |
Retrieves the index of a random true literal in a clause.
clauseIdx | Index of clause from which the literal is retrieved. |
Definition at line 1984 of file variablestate.h.
References getClauseSize(), and isTrueLiteral().
Referenced by getIndexOfAtomInRandomFalseClause().
01985 { 01986 assert(numTrueLits_[clauseIdx] > 0); 01987 int trueLit = random()%numTrueLits_[clauseIdx]; 01988 int whichTrueLit = 0; 01989 for (int i = 0; i < getClauseSize(clauseIdx); i++) 01990 { 01991 int lit = clause_[clauseIdx][i]; 01992 int atm = abs(lit); 01993 // True literal 01994 if (isTrueLiteral(lit)) 01995 if (trueLit == whichTrueLit++) 01996 return atm; 01997 } 01998 // If we're here, then no other true lit exists 01999 assert(false); 02000 return -1; 02001 }
const int VariableState::getBlockIndex | ( | const int & | atomIdx | ) | [inline] |
Returns the index of the block which the atom with index atomIdx is in.
If not in any, returns -1.
Definition at line 2053 of file variablestate.h.
References Domain::getBlock().
Referenced by MaxWalkSat::calculateImprovement(), fixAtom(), SimulatedTempering::infer(), initRandom(), MCMC::performGibbsStep(), MaxWalkSat::pickRandom(), and MCMC::randomInitGndPredsTruthValues().
02054 { 02055 const GroundPredicate* gndPred = (*gndPreds_)[atomIdx]; 02056 return domain_->getBlock(gndPred); 02057 }
void VariableState::makeUnitCosts | ( | ) | [inline] |
Turns all costs into units.
Positive costs are converted to 1, negative costs are converted to -1
Definition at line 2079 of file variablestate.h.
References initMakeBreakCostWatch(), and Array< Type >::size().
Referenced by setInferenceMode().
02080 { 02081 for (int i = 0; i < clauseCost_.size(); i++) 02082 { 02083 if (clauseCost_[i] >= 0) clauseCost_[i] = 1.0; 02084 else 02085 { 02086 assert(clauseCost_[i] < 0); 02087 clauseCost_[i] = -1.0; 02088 } 02089 } 02090 if (vsdebug) cout << "Made unit costs" << endl; 02091 initMakeBreakCostWatch(); 02092 }
void VariableState::getNumClauseGndings | ( | Array< double > *const & | numGndings, | |
bool | tv | |||
) | [inline] |
Gets the number of (true or false) clause groundings in this state.
If eager, the first order clause frequencies in the mrf are used. If lazy, the f.o. clause frequencies computed when activating are used.
numGndings | Array being filled with values. | |
clauseCnt | Number of first-order clauses (size of numGndings) | |
tv | If true, number of true groundings are retrieved, otherwise false groundings are retrieved. |
Definition at line 2197 of file variablestate.h.
References MLN::getClause(), MLN::getNumClauses(), Domain::getNumNonEvidGroundings(), isTrueLiteral(), HashArray< Type, HashFn, EqualFn >::size(), and Array< Type >::size().
Referenced by SimulatedTempering::infer(), MCMC::performGibbsStep(), and Inference::tallyCntsFromState().
02198 { 02199 // If lazy, then all groundings of pos. clauses not materialized in this 02200 // state are true (non-materialized groundings of neg. clauses are false), 02201 // so if tv is false and clause is pos. (or tv is true and clause is 02202 // neg.), then eager and lazy counts are equivalent. If tv is 02203 // true and clause is pos. (or tv is false and clause is neg.), then we 02204 // need to keep track of true *and* false groundings and 02205 // then subtract from total groundings of the f.o. clause. 02206 02207 // This holds the false groundings when tv is true and lazy. 02208 Array<double> lazyFalseGndings(numGndings->size(), 0); 02209 Array<double> lazyTrueGndings(numGndings->size(), 0); 02210 02211 IntBoolPairItr itr; 02212 IntBoolPair *clauseFrequencies; 02213 02214 // numGndings should have been initialized with non-negative values 02215 int clauseCnt = numGndings->size(); 02216 assert(clauseCnt == mln_->getNumClauses()); 02217 for (int clauseno = 0; clauseno < clauseCnt; clauseno++) 02218 assert ((*numGndings)[clauseno] >= 0); 02219 02220 for (int i = 0; i < gndClauses_->size(); i++) 02221 { 02222 GroundClause *gndClause = (*gndClauses_)[i]; 02223 int satLitcnt = 0; 02224 for (int j = 0; j < gndClause->getNumGroundPredicates(); j++) 02225 { 02226 int lit = gndClause->getGroundPredicateIndex(j); 02227 if (isTrueLiteral(lit)) satLitcnt++; 02228 } 02229 02230 clauseFrequencies = gndClause->getClauseFrequencies(); 02231 for (itr = clauseFrequencies->begin(); 02232 itr != clauseFrequencies->end(); itr++) 02233 { 02234 int clauseno = itr->first; 02235 int frequency = itr->second.first; 02236 bool invertWt = itr->second.second; 02237 // If flipped unit clause, then we want opposite kind of grounding 02238 if (invertWt) 02239 { 02240 // Want true and is true => don't count it 02241 if (tv && satLitcnt > 0) 02242 { 02243 // Lazy: We need to keep track of false groundings also 02244 if (lazy_) lazyFalseGndings[clauseno] += frequency; 02245 continue; 02246 } 02247 // Want false and is false => don't count it 02248 if (!tv && satLitcnt == 0) 02249 { 02250 // Lazy: We need to keep track of false groundings also 02251 if (lazy_) lazyTrueGndings[clauseno] += frequency; 02252 continue; 02253 } 02254 } 02255 else 02256 { 02257 // Want true and is false => don't count it 02258 if (tv && satLitcnt == 0) 02259 { 02260 // Lazy: We need to keep track of false groundings also 02261 if (lazy_) lazyFalseGndings[clauseno] += frequency; 02262 continue; 02263 } 02264 // Want false and is true => don't count it 02265 if (!tv && satLitcnt > 0) 02266 { 02267 // Lazy: We need to keep track of false groundings also 02268 if (lazy_) lazyTrueGndings[clauseno] += frequency; 02269 continue; 02270 } 02271 } 02272 (*numGndings)[clauseno] += frequency; 02273 } 02274 } 02275 02276 // Getting true counts in lazy: we have to add the remaining groundings 02277 // not materialized (they are by definition true groundings if clause is 02278 // positive, or false groundings if clause is negative) 02279 if (lazy_) 02280 { 02281 for (int c = 0; c < mln_->getNumClauses(); c++) 02282 { 02283 const Clause* clause = mln_->getClause(c); 02284 // Getting true counts and positive clause 02285 if (tv && clause->getWt() >= 0) 02286 { 02287 double totalGndings = domain_->getNumNonEvidGroundings(c); 02288 assert(totalGndings >= (*numGndings)[c] + lazyFalseGndings[c]); 02289 double remainingTrueGndings = totalGndings - lazyFalseGndings[c] - 02290 (*numGndings)[c]; 02291 (*numGndings)[c] += remainingTrueGndings; 02292 } 02293 // Getting false counts and negative clause 02294 else if (!tv && clause->getWt() < 0) 02295 { 02296 double totalGndings = domain_->getNumNonEvidGroundings(c); 02297 assert(totalGndings >= (*numGndings)[c] + lazyTrueGndings[c]); 02298 double remainingFalseGndings = totalGndings - lazyTrueGndings[c] - 02299 (*numGndings)[c]; 02300 (*numGndings)[c] += remainingFalseGndings; 02301 } 02302 } 02303 } 02304 02305 }
void VariableState::getNumClauseGndingsWithUnknown | ( | double | numGndings[], | |
int | clauseCnt, | |||
bool | tv, | |||
const Array< bool > *const & | unknownPred | |||
) | [inline] |
Gets the number of (true or false) clause groundings in this state.
If eager, the first order clause frequencies in the mrf are used.
numGndings | Will hold the number of groundings for each first-order clause. | |
clauseCnt | Number of first-order clauses whose groundings are being counted. | |
tv | If true, true groundings are counted, otherwise false groundings. | |
unknownPred | If pred is marked as unknown, it is ignored in the count |
Definition at line 2318 of file variablestate.h.
References getNumAtoms(), isTrueLiteral(), HashArray< Type, HashFn, EqualFn >::size(), and Array< Type >::size().
02321 { 02322 // TODO: lazy version 02323 assert(unknownPred->size() == getNumAtoms()); 02324 IntBoolPairItr itr; 02325 IntBoolPair *clauseFrequencies; 02326 02327 for (int clauseno = 0; clauseno < clauseCnt; clauseno++) 02328 numGndings[clauseno] = 0; 02329 02330 for (int i = 0; i < gndClauses_->size(); i++) 02331 { 02332 GroundClause *gndClause = (*gndClauses_)[i]; 02333 int satLitcnt = 0; 02334 bool unknown = false; 02335 for (int j = 0; j < gndClause->getNumGroundPredicates(); j++) 02336 { 02337 int lit = gndClause->getGroundPredicateIndex(j); 02338 if ((*unknownPred)[abs(lit) - 1]) 02339 { 02340 unknown = true; 02341 continue; 02342 } 02343 if (isTrueLiteral(lit)) satLitcnt++; 02344 } 02345 02346 clauseFrequencies = gndClause->getClauseFrequencies(); 02347 for (itr = clauseFrequencies->begin(); 02348 itr != clauseFrequencies->end(); itr++) 02349 { 02350 int clauseno = itr->first; 02351 int frequency = itr->second.first; 02352 bool invertWt = itr->second.second; 02353 // If flipped unit clause, then we want opposite kind of grounding 02354 if (invertWt) 02355 { 02356 // Want true and is true or unknown => don't count it 02357 if (tv && (satLitcnt > 0 || unknown)) 02358 continue; 02359 // Want false and is false => don't count it 02360 if (!tv && satLitcnt == 0) 02361 continue; 02362 } 02363 else 02364 { 02365 // Want true and is false => don't count it 02366 if (tv && satLitcnt == 0) 02367 continue; 02368 // Want false and is true => don't count it 02369 if (!tv && (satLitcnt > 0 || unknown)) 02370 continue; 02371 } 02372 numGndings[clauseno] += frequency; 02373 } 02374 } 02375 }
void VariableState::setOthersInBlockToFalse | ( | const int & | atomIdx, | |
const int & | blockIdx | |||
) | [inline] |
Sets the truth values of all atoms in a block except for the one given.
atomIdx | Absolute index of atom in block exempt from being set to false. | |
blockIdx | Index of block whose atoms are set to false. |
Definition at line 2384 of file variablestate.h.
References HashArray< Type, HashFn, EqualFn >::find(), Domain::getBlockSize(), Domain::getPredInBlock(), GroundPredicate::print(), and setValueOfAtom().
Referenced by initBlocksRandom().
02385 { 02386 if (vsdebug) 02387 { 02388 cout << "Set all in block " << blockIdx << " to false except " 02389 << atomIdx << endl; 02390 } 02391 int blockSize = domain_->getBlockSize(blockIdx); 02392 for (int i = 0; i < blockSize; i++) 02393 { 02394 const Predicate* pred = domain_->getPredInBlock(i, blockIdx); 02395 GroundPredicate* gndPred = new GroundPredicate((Predicate*)pred); 02396 int idx = gndPredHashArray_.find(gndPred); 02397 02398 if (vsdebug) 02399 { 02400 cout << "Gnd pred in block "; 02401 gndPred->print(cout, domain_); 02402 cout << " (idx " << idx << ")" << endl; 02403 } 02404 02405 delete gndPred; 02406 delete pred; 02407 02408 // Pred already present: check that it's not fixed 02409 if (idx >= 0) 02410 { 02411 // Atom not the one specified and not fixed 02412 if (idx != atomIdx && fixedAtom_[idx + 1] == 0) 02413 { 02414 if (vsdebug) 02415 cout << "Set " << idx + 1 << " to false" << endl; 02416 02417 bool activate = true; 02418 setValueOfAtom(idx + 1, false, activate, -1); 02419 } 02420 } 02421 } 02422 }
void VariableState::fixAtom | ( | const int & | atomIdx, | |
const bool & | value | |||
) | [inline] |
Fixes an atom to a truth value.
This means the atom can not change its truth value again. If the atom has already been fixed to the opposite value, then we have a contradiction and the program terminates.
atomIdx | Index of atom to be fixed. | |
value | Truth value to which the atom is fixed. |
Definition at line 2433 of file variablestate.h.
References ADD_CLAUSE_REGULAR, addNewClauses(), HashArray< Type, HashFn, EqualFn >::find(), getActiveClauses(), getBlockIndex(), Domain::getBlockSize(), Domain::getDB(), Domain::getPredInBlock(), isActive(), Database::setActiveStatus(), setValueOfAtom(), updateMakeBreakCostAfterFlip(), and updateSatisfiedClauses().
Referenced by activateAtom(), UnitPropagation::infer(), and unitPropagation().
02434 { 02435 assert(atomIdx > 0); 02436 if (vsdebug) 02437 { 02438 cout << "Fixing "; 02439 (*gndPreds_)[atomIdx - 1]->print(cout, domain_); 02440 cout << " to " << value << endl; 02441 } 02442 02443 // Must be in mc-sat 02444 if (!useThreshold_) 02445 { 02446 cout << ">>> [ERR] useThreshold_ is false" << endl; 02447 exit(0); 02448 } 02449 02450 // If already fixed to opp. sense, then contradiction 02451 if ((fixedAtom_[atomIdx] == 1 && value == false) || 02452 (fixedAtom_[atomIdx] == -1 && value == true)) 02453 { 02454 cout << "Contradiction: Tried to fix atom " << atomIdx << 02455 " to true and false ... exiting." << endl; 02456 exit(0); 02457 } 02458 02459 // already fixed 02460 if (fixedAtom_[atomIdx] != 0) return; 02461 02462 fixedAtom_[atomIdx] = (value) ? 1 : -1; 02463 if (atom_[atomIdx] != value) 02464 { 02465 bool activate = false; 02466 int blockIdx = getBlockIndex(atomIdx - 1); 02467 setValueOfAtom(atomIdx, value, activate, blockIdx); 02468 updateMakeBreakCostAfterFlip(atomIdx); 02469 } 02470 02471 // update isSat 02472 updateSatisfiedClauses(atomIdx); 02473 02474 // If in a block and fixing to true, fix all others to false 02475 if (value) 02476 { 02477 int blockIdx = getBlockIndex(atomIdx - 1); 02478 if (blockIdx > -1) 02479 { 02480 int blockSize = domain_->getBlockSize(blockIdx); 02481 for (int i = 0; i < blockSize; i++) 02482 { 02483 const Predicate* pred = domain_->getPredInBlock(i, blockIdx); 02484 GroundPredicate* gndPred = new GroundPredicate((Predicate*)pred); 02485 int idx = gndPredHashArray_.find(gndPred); 02486 delete gndPred; 02487 delete pred; 02488 02489 // Pred already present: check that it's not fixed 02490 if (idx >= 0) 02491 { 02492 // Atom not the one specified 02493 if (idx != (atomIdx - 1)) 02494 { 02495 // If already fixed to true, then contradiction 02496 if (fixedAtom_[idx + 1] == 1) 02497 { 02498 cout << "Contradiction: Tried to fix atom " << idx + 1 << 02499 " to true and false ... exiting." << endl; 02500 exit(0); 02501 } 02502 // already fixed 02503 if (fixedAtom_[idx + 1] == -1) continue; 02504 if (vsdebug) 02505 { 02506 cout << "Fixing "; 02507 (*gndPreds_)[idx]->print(cout, domain_); 02508 cout << " to 0" << endl; 02509 } 02510 fixedAtom_[idx + 1] = -1; 02511 if (atom_[idx + 1] != false) 02512 { 02513 bool activate = false; 02514 setValueOfAtom(idx + 1, value, activate, blockIdx); 02515 updateMakeBreakCostAfterFlip(idx + 1); 02516 } 02517 // update isSat 02518 updateSatisfiedClauses(idx + 1); 02519 } 02520 } 02521 } 02522 } 02523 } 02524 02525 // activate clauses falsified 02526 // need to updateMakeBreakCost for new clauses only, which aren't sat 02527 if (lazy_ && !isActive(atomIdx) && value) 02528 { 02529 // if inactive fixed to true, need to activate falsified 02530 // inactive clauses gather active clauses 02531 Predicate* p = 02532 gndPredHashArray_[atomIdx - 1]->createEquivalentPredicate(domain_); 02533 02534 bool ignoreActivePreds = false; // only get currently unsat ones 02535 Array<GroundClause*> unsatClauses; 02536 getActiveClauses(p, unsatClauses, true, ignoreActivePreds); 02537 02538 // filter out clauses already added (can also add directly, but 02539 // will elicit warning) 02540 addNewClauses(ADD_CLAUSE_REGULAR, unsatClauses); 02541 02542 // Set active status in db 02543 domain_->getDB()->setActiveStatus(p, true); 02544 activeAtoms_++; 02545 02546 delete p; 02547 } 02548 }
Array<int>* VariableState::simplifyClauseFromFixedAtoms | ( | const int & | clauseIdx | ) | [inline] |
Simplifies a clause using atoms which have been fixed.
If clause is satisfied from the fixed atoms, this is marked in isSatisfied_ and an empty array is returned. If clause is empty and not satisfied, then a contradiction has occured. Otherwise, the simplified clause is returned.
Returned array should be deleted.
clauseIdx | Index of the clause to be simplified |
Definition at line 2561 of file variablestate.h.
References Array< Type >::append(), Array< Type >::clear(), and getClauseSize().
Referenced by UnitPropagation::infer().
02562 { 02563 Array<int>* returnArray = new Array<int>; 02564 // If already satisfied from fixed atoms, then return empty array 02565 if (isSatisfied_[clauseIdx]) return returnArray; 02566 02567 // Keeps track of pos. clause being satisfied or 02568 // neg. clause being unsatisfied due to fixed atoms 02569 bool isGood = (clauseCost_[clauseIdx] >= 0) ? false : true; 02570 // Keeps track of all atoms being fixed to false in a pos. clause 02571 bool allFalseAtoms = (clauseCost_[clauseIdx] >= 0) ? true : false; 02572 // Check each literal in clause 02573 for (int i = 0; i < getClauseSize(clauseIdx); i++) 02574 { 02575 int lit = clause_[clauseIdx][i]; 02576 int fixedValue = fixedAtom_[abs(lit)]; 02577 02578 if (clauseCost_[clauseIdx] >= 0) 02579 { // Pos. clause: check if clause is satisfied 02580 if ((fixedValue == 1 && lit > 0) || 02581 (fixedValue == -1 && lit < 0)) 02582 { // True fixed lit 02583 isGood = true; 02584 allFalseAtoms = false; 02585 returnArray->clear(); 02586 break; 02587 } 02588 else if (fixedValue == 0) 02589 { // Lit not fixed 02590 allFalseAtoms = false; 02591 returnArray->append(lit); 02592 } 02593 } 02594 else 02595 { // Neg. clause: 02596 assert(clauseCost_[clauseIdx] < 0); 02597 if ((fixedValue == 1 && lit > 0) || 02598 (fixedValue == -1 && lit < 0)) 02599 { // True fixed lit 02600 cout << "Contradiction: Tried to fix atom " << abs(lit) << 02601 " to true in a negative clause ... exiting." << endl; 02602 exit(0); 02603 } 02604 else 02605 { // False fixed lit or non-fixed lit 02606 returnArray->append(lit); 02607 // Non-fixed lit 02608 if (fixedValue == 0) isGood = false; 02609 } 02610 } 02611 } 02612 if (allFalseAtoms) 02613 { 02614 cout << "Contradiction: All atoms in clause " << clauseIdx << 02615 " fixed to false ... exiting." << endl; 02616 exit(0); 02617 } 02618 if (isGood) isSatisfied_[clauseIdx] = true; 02619 return returnArray; 02620 }
const bool VariableState::isDeadClause | ( | const int & | clauseIdx | ) | [inline] |
Checks if a clause is dead.
clauseIdx | Index of clause being checked. |
Definition at line 2628 of file variablestate.h.
Referenced by UnitPropagation::infer(), and unitPropagation().
void VariableState::killClauses | ( | const int & | startClause | ) | [inline] |
Marks clauses as dead which were not good in the previous iteration of inference or are not picked according to a weighted coin flip.
startClause | All clauses with index of this or greater are looked at to be killed. |
Definition at line 2704 of file variablestate.h.
References clauseGoodInPrevious(), getNumClauses(), initMakeBreakCostWatch(), and MODE_HARD.
Referenced by addNewClauses().
02704 { 02705 // for hard init, no need to killClauses 02706 if (inferenceMode_ != MODE_HARD) 02707 { 02708 for (int i = startClause; i < getNumClauses(); i++) 02709 { 02710 GroundClause* clause = (*gndClauses_)[i]; 02711 if ((clauseGoodInPrevious(i)) && 02712 (clause->isHardClause() || random() <= threshold_[i])) 02713 { 02714 if (vsdebug) 02715 { 02716 cout << "Keeping clause "<< i << " "; 02717 clause->print(cout, domain_, &gndPredHashArray_); 02718 cout << endl; 02719 } 02720 deadClause_[i] = false; 02721 } 02722 else 02723 { 02724 deadClause_[i] = true; 02725 } 02726 } 02727 } 02728 02729 initMakeBreakCostWatch(startClause); 02730 } 02731
const bool VariableState::clauseGoodInPrevious | ( | const int & | clauseIdx | ) | [inline] |
Checks if a clause was good in the previous iteration of inference, i.e.
if it is positive and satisfied or negative and unsatisfied.
clauseIdx | Index of clause being checked. |
Definition at line 2741 of file variablestate.h.
References Array< Type >::size().
Referenced by killClauses().
02741 { 02742 // satisfied: wt-sat 02743 return (clauseIdx >= prevSatisfiedClause_.size() || 02744 prevSatisfiedClause_[clauseIdx]); 02745 } 02746
void VariableState::printLowState | ( | ostream & | out | ) | [inline] |
Prints the best state found to a stream.
out | Stream to which the state is printed. |
Definition at line 2786 of file variablestate.h.
References getNumAtoms().
Referenced by MCSAT::init().
02786 { 02787 for (int i = 0; i < getNumAtoms(); i++) 02788 { 02789 (*gndPreds_)[i]->print(out, domain_); 02790 out << " " << lowAtom_[i + 1] << endl; 02791 } 02792 } 02793
void VariableState::printGndPred | ( | const int & | predIndex, | |
ostream & | out | |||
) | [inline] |
Prints a ground predicate to a stream.
predIndex | Index of predicate to be printed. | |
out | Stream to which predicate is printed. |
Definition at line 2801 of file variablestate.h.
Referenced by UnitPropagation::getChangedPreds(), SAT::getChangedPreds(), MCMC::getChangedPreds(), UnitPropagation::printProbabilities(), SAT::printProbabilities(), MCMC::printProbabilities(), UnitPropagation::printTruePreds(), SAT::printTruePreds(), MCMC::printTruePreds(), and SAT::printTruePredsH().
int VariableState::getIndexOfGroundPredicate | ( | GroundPredicate *const & | gndPred | ) | [inline] |
Finds and returns the index of a ground predicate.
gndPred | GroundPredicate to be found. |
Definition at line 2812 of file variablestate.h.
References HashArray< Type, HashFn, EqualFn >::find().
Referenced by MaxWalkSat::calculateImprovement(), MCMC::gibbsSampleFromBlock(), SimulatedTempering::infer(), MCMC::performGibbsStep(), MaxWalkSat::pickRandom(), MCMC::randomInitGndPredsTruthValues(), and MCMC::setOthersInBlockToFalse().
02812 { 02813 return gndPredHashArray_.find(gndPred); 02814 } 02815
void VariableState::setAsEvidence | ( | const GroundPredicate *const & | predicate, | |
const bool & | trueEvidence | |||
) | [inline] |
Sets a GroundPredicate to be evidence and sets its truth value.
If it is already present as evidence with the given truth value, then nothing happens. If the predicate was a query, then additional clauses may be eliminated. reinit() should be called after this in order to ensure that the clause and atom information is correct.
predicate | GroundPredicate to be set as evidence. | |
trueEvidence | The truth value of the predicate is set to this. |
Definition at line 2827 of file variablestate.h.
References Array< Type >::bubbleSort(), Array< Type >::compress(), HashArray< Type, HashFn, EqualFn >::compress(), HashArray< Type, HashFn, EqualFn >::find(), Domain::getDB(), getNegOccurenceArray(), getPosOccurenceArray(), Database::getValue(), GroundPredicate::print(), HashArray< Type, HashFn, EqualFn >::removeItem(), Array< Type >::removeItemFastDisorder(), HashArray< Type, HashFn, EqualFn >::removeItemFastDisorder(), Database::setValue(), HashArray< Type, HashFn, EqualFn >::size(), and Array< Type >::size().
02828 { 02829 if (vsdebug) 02830 { 02831 cout << "Setting to evidence " ; 02832 predicate->print(cout, domain_); 02833 cout << endl; 02834 } 02835 Database* db = domain_->getDB(); 02836 int atomIdx = gndPredHashArray_.find((GroundPredicate*)predicate); 02837 // If already evidence, then check its truth value 02838 if (atomIdx <= 0) 02839 { 02840 // If predicate already evidence with same truth value, then do nothing 02841 if (db->getValue(predicate) == trueEvidence) 02842 return; 02843 02844 // Changing truth value of evidence 02845 if (trueEvidence) 02846 db->setValue(predicate, TRUE); 02847 else 02848 db->setValue(predicate, FALSE); 02849 } 02850 else 02851 { 02852 Array<int> gndClauseIndexes; 02853 int deleted; 02854 gndClauseIndexes = getNegOccurenceArray(atomIdx + 1); 02855 gndClauseIndexes.bubbleSort(); 02856 // Keep track of how many clauses deleted, because the indices are 02857 // are adjusted when we remove an element from HashArray 02858 deleted = 0; 02859 for (int i = 0; i < gndClauseIndexes.size(); i++) 02860 { 02861 // Atom appears neg. in these clauses, so remove the atom from 02862 // these clauses if true evidence, or remove clause if false evidence 02863 // or a unit clause 02864 if (!trueEvidence || 02865 (*gndClauses_)[gndClauseIndexes[i]]->getNumGroundPredicates() == 1) 02866 { 02867 if (vsdebug) 02868 cout << "Deleting ground clause " << gndClauseIndexes[i] << endl; 02869 // Real index is old index adjusted one lower for every element 02870 // deleted up until now 02871 delete (*gndClauses_)[gndClauseIndexes[i] - deleted]; 02872 gndClauses_->removeItem(gndClauseIndexes[i] - deleted); 02873 deleted++; 02874 } 02875 else 02876 { 02877 if (vsdebug) 02878 { 02879 cout << "Removing gnd pred " << -(atomIdx + 1) 02880 << " from ground clause " << gndClauseIndexes[i] << endl; 02881 } 02882 (*gndClauses_)[gndClauseIndexes[i]]->removeGndPred(-(atomIdx + 1)); 02883 } 02884 } 02885 02886 gndClauseIndexes = getPosOccurenceArray(atomIdx + 1); 02887 gndClauseIndexes.bubbleSort(); 02888 // Keep track of how many clauses deleted, because the indices are 02889 // are adjusted when we remove an element from HashArray 02890 deleted = 0; 02891 for (int i = 0; i < gndClauseIndexes.size(); i++) 02892 { 02893 // Atom appears pos. in these clauses, so remove the atom from 02894 // these clauses if false evidence, or remove clause if true evidence 02895 // or a unit clause 02896 if (trueEvidence || 02897 (*gndClauses_)[gndClauseIndexes[i]]->getNumGroundPredicates() == 1) 02898 { 02899 if (vsdebug) 02900 cout << "Deleting ground clause " << gndClauseIndexes[i] << endl; 02901 // Real index is old index adjusted one lower for every element 02902 // deleted up until now 02903 delete (*gndClauses_)[gndClauseIndexes[i] - deleted]; 02904 gndClauses_->removeItem(gndClauseIndexes[i] - deleted); 02905 deleted++; 02906 } 02907 else 02908 { 02909 if (vsdebug) 02910 { 02911 cout << "Removing gnd pred " << -(atomIdx + 1) 02912 << " from ground clause " << gndClauseIndexes[i] << endl; 02913 } 02914 (*gndClauses_)[gndClauseIndexes[i]]->removeGndPred(atomIdx + 1); 02915 } 02916 } 02917 02918 gndPredHashArray_.removeItemFastDisorder(atomIdx); 02919 gndPredHashArray_.compress(); 02920 gndPreds_->removeItemFastDisorder(atomIdx); 02921 gndPreds_->compress(); 02922 // By removing a pred, the pred at the end of the array gets the 02923 // index of the pred deleted, so we have to update to the new index 02924 // in all clauses 02925 int oldIdx = gndPredHashArray_.size(); 02926 replaceAtomIndexInAllClauses(oldIdx, atomIdx); 02927 } 02928 } 02929
void VariableState::setAsQuery | ( | const GroundPredicate *const & | predicate | ) | [inline] |
Sets a GroundPredicate to be query.
If it is already present as query, then nothing happens. If the predicate was evidence, then additional clauses may be added. reinit() should be called after this in order to ensure that the clause and atom information is correct.
predicate | GroundPredicate to be set as a query. |
Definition at line 2939 of file variablestate.h.
References HashArray< Type, HashFn, EqualFn >::append(), HashArray< Type, HashFn, EqualFn >::contains(), getActiveClauses(), Domain::getDB(), GroundPredicate::print(), and Database::setEvidenceStatus().
02939 { 02940 if (vsdebug) 02941 { 02942 cout << "Setting to query " ; 02943 predicate->print(cout, domain_); 02944 cout << endl; 02945 } 02946 Database* db = domain_->getDB(); 02947 // If already non-evidence, then do nothing 02948 if (gndPredHashArray_.contains((GroundPredicate*)predicate)) 02949 return; 02950 else 02951 { 02952 // Evidence -> query 02953 // Add predicate to query set and get clauses 02954 gndPredHashArray_.append((GroundPredicate*)predicate); 02955 Predicate* p = predicate->createEquivalentPredicate(domain_); 02956 db->setEvidenceStatus(p, false); 02957 bool ignoreActivePreds = true; 02958 getActiveClauses(p, newClauses_, true, ignoreActivePreds); 02959 } 02960 } 02961
GroundPredicate* VariableState::getGndPred | ( | const int & | index | ) | [inline] |
Gets a pointer to a GroundPredicate.
index | Index of the GroundPredicate to be retrieved. |
Definition at line 2971 of file variablestate.h.
Referenced by MCMC::getProbabilityOfPred(), MCMC::gndPredFlippedUpdates(), SimulatedTempering::infer(), MCSAT::infer(), GibbsSampler::infer(), MCMC::initNumTrueLits(), MCMC::performGibbsStep(), and MCMC::randomInitGndPredsTruthValues().
GroundClause* VariableState::getGndClause | ( | const int & | index | ) | [inline] |
Gets a pointer to a GroundClause.
index | Index of the GroundClause to be retrieved. |
Definition at line 2982 of file variablestate.h.
Referenced by MCMC::gndPredFlippedUpdates(), MCMC::initNumTrueLits(), and MCMC::updateWtsForGndPreds().
const int VariableState::getGndPredIndex | ( | GroundPredicate *const & | gndPred | ) | [inline] |
Gets the index of a GroundPredicate in this state.
gndPred | GroundPredicate whose index is being found. |
Definition at line 3022 of file variablestate.h.
References Array< Type >::find().
Referenced by UnitPropagation::getProbability(), SAT::getProbability(), MCMC::getProbability(), and SAT::getProbabilityH().
03022 { 03023 return gndPreds_->find(gndPred); 03024 } 03025
void VariableState::getActiveClauses | ( | Predicate * | inputPred, | |
Array< GroundClause * > & | activeClauses, | |||
bool const & | active, | |||
bool const & | ignoreActivePreds | |||
) | [inline] |
Gets clauses and weights activated by the predicate inputPred, if active is true.
If false, inactive clauses (and their weights) containing inputPred are retrieved. If inputPred is NULL, then all active (or inactive) clauses and their weights are retrieved.
inputPred | Only clauses containing this Predicate are looked at. If NULL, then all active clauses are retrieved. | |
activeClauses | New active clauses are put here. | |
active | If true, active clauses are retrieved, otherwise inactive. | |
ignoreActivePreds | If true, active preds are not taken into account. This results in the retrieval of all unsatisfied clauses. |
Definition at line 3046 of file variablestate.h.
References Array< Type >::append(), HashArray< Type, HashFn, EqualFn >::append(), HashArray< Type, HashFn, EqualFn >::clear(), Array< Type >::clear(), HashArray< Type, HashFn, EqualFn >::find(), MLN::findClauseIdx(), Clause::getActiveClauses(), MLN::getClause(), MLN::getClausesContainingPred(), Domain::getDB(), Database::getDeactivatedStatus(), GroundClause::getGroundPredicateSense(), Predicate::getId(), MLN::getNumClauses(), GroundClause::getNumGroundPredicates(), GroundClause::getWt(), Clause::getWt(), Clause::getWtPtr(), GroundClause::incrementClauseFrequency(), Clause::isHardClause(), GroundClause::print(), Clause::print(), GroundClause::setGroundPredicateSense(), GroundClause::setWt(), HashArray< Type, HashFn, EqualFn >::size(), Array< Type >::size(), and Timer::time().
Referenced by activateAtom(), fixAtom(), getActiveClauses(), setAsQuery(), and VariableState().
03049 { 03050 Timer timer; 03051 double currTime; 03052 03053 Clause *fclause; 03054 GroundClause* newClause; 03055 int clauseCnt; 03056 GroundClauseHashArray clauseHashArray; 03057 03058 Array<GroundClause*>* newClauses = new Array<GroundClause*>; 03059 03060 const Array<IndexClause*>* indexClauses = NULL; 03061 03062 // inputPred is null: all active clauses should be retrieved 03063 if (inputPred == NULL) 03064 { 03065 clauseCnt = mln_->getNumClauses(); 03066 } 03067 // Otherwise, look at all first order clauses containing the pred 03068 else 03069 { 03070 if (domain_->getDB()->getDeactivatedStatus(inputPred)) return; 03071 int predId = inputPred->getId(); 03072 indexClauses = mln_->getClausesContainingPred(predId); 03073 clauseCnt = indexClauses->size(); 03074 } 03075 03076 // Look at each first-order clause and get active groundings 03077 int clauseno = 0; 03078 03079 while (clauseno < clauseCnt) 03080 { 03081 if (inputPred) 03082 fclause = (Clause *) (*indexClauses)[clauseno]->clause; 03083 else 03084 fclause = (Clause *) mln_->getClause(clauseno); 03085 03086 if (vsdebug) 03087 { 03088 cout << "Getting active clauses for FO clause: "; 03089 fclause->print(cout, domain_); 03090 cout << endl; 03091 } 03092 03093 currTime = timer.time(); 03094 03095 const double* parentWtPtr = NULL; 03096 if (!fclause->isHardClause()) parentWtPtr = fclause->getWtPtr(); 03097 const int clauseId = mln_->findClauseIdx(fclause); 03098 newClauses->clear(); 03099 03100 if (stillActivating_) 03101 stillActivating_ = fclause->getActiveClauses(inputPred, domain_, 03102 newClauses, 03103 &gndPredHashArray_, 03104 ignoreActivePreds); 03105 03106 for (int i = 0; i < newClauses->size(); i++) 03107 { 03108 long double wt = fclause->getWt(); 03109 newClause = (*newClauses)[i]; 03110 03111 // If already active, ignore; assume all gndClauses_ are active 03112 if (gndClauses_->find(newClause) >= 0) 03113 { 03114 delete newClause; 03115 continue; 03116 } 03117 03118 bool invertWt = false; 03119 // We want to normalize soft unit clauses to all be positives 03120 if (!fclause->isHardClause() && 03121 newClause->getNumGroundPredicates() == 1 && 03122 !newClause->getGroundPredicateSense(0)) 03123 { 03124 newClause->setGroundPredicateSense(0, true); 03125 newClause->setWt(-newClause->getWt()); 03126 wt = -wt; 03127 invertWt = true; 03128 int addToIndex = gndClauses_->find(newClause); 03129 if (addToIndex >= 0) 03130 { 03131 (*gndClauses_)[addToIndex]->addWt(wt); 03132 if (parentWtPtr) 03133 (*gndClauses_)[addToIndex]->incrementClauseFrequency(clauseId, 1, 03134 invertWt); 03135 delete newClause; 03136 continue; 03137 } 03138 } 03139 03140 int pos = clauseHashArray.find(newClause); 03141 // If clause already present, then just add weight 03142 if (pos >= 0) 03143 { 03144 // Check if already added from this f.o. clause. If so, don't add 03145 // again 03146 if (clauseHashArray[pos]->getClauseFrequency(clauseId) > 0) 03147 { 03148 delete newClause; 03149 continue; 03150 } 03151 if (vsdebug) 03152 { 03153 cout << "Adding weight " << wt << " to clause "; 03154 clauseHashArray[pos]->print(cout, domain_, &gndPredHashArray_); 03155 cout << endl; 03156 } 03157 clauseHashArray[pos]->addWt(wt); 03158 if (parentWtPtr) 03159 clauseHashArray[pos]->incrementClauseFrequency(clauseId, 1, 03160 invertWt); 03161 delete newClause; 03162 continue; 03163 } 03164 03165 // If here, then clause is not yet present 03166 newClause->setWt(wt); 03167 if (parentWtPtr) 03168 newClause->incrementClauseFrequency(clauseId, 1, invertWt); 03169 03170 if (vsdebug) 03171 { 03172 cout << "Appending clause "; 03173 newClause->print(cout, domain_, &gndPredHashArray_); 03174 cout << endl; 03175 } 03176 clauseHashArray.append(newClause); 03177 } 03178 clauseno++; 03179 03180 } //while (clauseno < clauseCnt) 03181 03182 for (int i = 0; i < clauseHashArray.size(); i++) 03183 { 03184 newClause = clauseHashArray[i]; 03185 activeClauses.append(newClause); 03186 } 03187 03188 newClauses->clear(); 03189 delete newClauses; 03190 03191 clauseHashArray.clear(); 03192 } 03193
void VariableState::getActiveClauses | ( | Array< GroundClause * > & | allClauses, | |
bool const & | ignoreActivePreds | |||
) | [inline] |
Get all the active clauses in the database.
allClauses | Active clauses are retrieved into this Array. | |
ignoreActivePreds | If true, active preds are ignored; this means all unsatisfied clauses are retrieved. |
Definition at line 3202 of file variablestate.h.
References getActiveClauses().
03203 { 03204 getActiveClauses(NULL, allClauses, true, ignoreActivePreds); 03205 } 03206