00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #include "clause.h"
00067 #include "mrf.h"
00068
00069
00070 ClauseSampler* Clause::clauseSampler_ = NULL;
00071 double Clause::fixedSizeB_ = -1;
00072 double AuxClauseData::fixedSizeB_ = -1;
00073
00074
00075
00076
00077 bool Clause::createAndAddUnknownClause(
00078 Array<GroundClause*>* const& unknownGndClauses,
00079 Array<Clause*>* const& unknownClauses,
00080 double* const & numUnknownClauses,
00081 const AddGroundClauseStruct* const & agcs)
00082 {
00083 PredicateSet predSet;
00084 PredicateSet::iterator iter;
00085
00086 Clause* clause = NULL;
00087 for (int i = 0; i < predicates_->size(); i++)
00088 {
00089 Predicate* predicate = (*predicates_)[i];
00090 assert(predicate->isGrounded());
00091
00092
00093
00094 if (predicate->getTruthValue() == UNKNOWN)
00095 {
00096 if ( (iter=predSet.find(predicate)) != predSet.end() )
00097 {
00098
00099 if ((*iter)->getSense() != predicate->getSense())
00100 {
00101 if (clause) delete clause;
00102 return true;
00103 }
00104
00105 continue;
00106 }
00107 else
00108 predSet.insert(predicate);
00109
00110 if (clause == NULL) clause = new Clause();
00111 Predicate* pred = new Predicate(*predicate, clause);
00112 clause->appendPredicate(pred);
00113 }
00114 }
00115
00116 if (clause)
00117 {
00118 if (numUnknownClauses) (*numUnknownClauses)++;
00119
00120 clause->setWt(wt_);
00121 clause->canonicalizeWithoutVariables();
00122
00123 if (agcs) MRF::addUnknownGndClause(agcs, this, clause, isHardClause_);
00124
00125
00126 if (unknownGndClauses)
00127 {
00128 unknownGndClauses->append(new GroundClause(clause, agcs->gndPreds));
00129 if (isHardClause_) unknownGndClauses->lastItem()->setWtToHardWt();
00130 }
00131 else
00132 if (unknownClauses)
00133 {
00134 unknownClauses->append(clause);
00135 if (isHardClause_) clause->setIsHardClause(true);
00136 }
00137 if (unknownClauses == NULL) delete clause;
00138 }
00139 return false;
00140 }
00141
00142 void addPredicateToHash(const Clause* const & c,
00143 PredicateHashArray* const & predHashArray)
00144 {
00145 int numPreds = c->getNumPredicates();
00146
00147 for (int i = 0; i < numPreds; i++)
00148 {
00149 Predicate* pred = new Predicate(*(c->getPredicate(i)));
00150 int index = predHashArray->find(pred);
00151 if(index < 0 )
00152 {
00153 index = predHashArray->append(pred) + 1;
00154 }
00155 else
00156 {
00157 delete pred;
00158 index++;
00159 }
00160 }
00161 }
00162
00163
00175 bool Clause::createAndAddActiveClause(
00176 Array<GroundClause *> * const & activeGroundClauses,
00177 GroundPredicateHashArray* const& seenGndPreds,
00178 const Database* const & db,
00179 bool const & getSatisfied)
00180 {
00181 bool accumulateClauses = activeGroundClauses;
00182 Predicate *cpred;
00183 PredicateSet predSet;
00184 PredicateSet::iterator iter;
00185
00186 GroundClause *groundClause;
00187
00188 Clause* clause = NULL;
00189 bool isEmpty = true;
00190 for (int i = 0; i < predicates_->size(); i++)
00191 {
00192 Predicate* predicate = (*predicates_)[i];
00193 assert(predicate);
00194 assert(predicate->isGrounded());
00195 if ( (iter = predSet.find(predicate)) != predSet.end() )
00196 {
00197
00198
00199 if (wt_ >= 0 && !getSatisfied &&
00200 (*iter)->getSense() != predicate->getSense())
00201 {
00202 if (clause) delete clause;
00203 return false;
00204 }
00205
00206
00207 continue;
00208 }
00209 else
00210 predSet.insert(predicate);
00211
00212 bool isEvidence = db->getEvidenceStatus(predicate);
00213
00214
00215
00216 if (!isEvidence)
00217 isEmpty = false;
00218
00219
00220 if (wt_ < 0 && isEvidence && !getSatisfied &&
00221 db->sameTruthValueAndSense(db->getValue(predicate),
00222 predicate->getSense()))
00223 {
00224 if (clause) delete clause;
00225 return false;
00226 }
00227
00228
00229 if (accumulateClauses && !isEvidence)
00230 {
00231 if (!clause) clause = new Clause();
00232
00233 cpred = new Predicate(*predicate, clause);
00234 assert(cpred);
00235 clause->appendPredicate(cpred);
00236 }
00237 }
00238
00239
00240
00241 if (isEmpty)
00242 {
00243 assert(!clause);
00244 return false;
00245 }
00246
00247 else
00248 {
00249
00250 if (accumulateClauses)
00251 {
00252 assert(clause);
00253 clause->canonicalizeWithoutVariables();
00254
00255 groundClause = new GroundClause(clause, seenGndPreds);
00256 if (isHardClause_)
00257 groundClause->setWtToHardWt();
00258 activeGroundClauses->append(groundClause);
00259 delete clause;
00260 }
00261 return true;
00262 }
00263 }
00264
00265