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 "groundclause.h"
00067 #include "groundpredicate.h"
00068 #include "clause.h"
00069 #include "mln.h"
00070
00071 GroundClause::GroundClause(const Clause* const & c,
00072 GroundPredicateHashArray* const & gndPredHashArray)
00073 : wt_(c->getWt()), foClauseFrequencies_(NULL)
00074 {
00075 if (gcdebug) cout << "Constructing GroundClause" << endl;
00076 if (gcdebug)
00077 {
00078 cout << "Seen ground preds: " << endl;
00079 for (int i = 0; i < gndPredHashArray->size(); i++)
00080 {
00081 (*gndPredHashArray)[i]->print(cout);
00082 cout << endl;
00083 }
00084 }
00085
00086 int numPreds = c->getNumPredicates();
00087 if (gcdebug) cout << "Number of preds: " << numPreds << endl;
00088 gndPredIndexes_ = new Array<int>;
00089 Array<unsigned int>* intArrRep = new Array<unsigned int>;
00090
00091
00092 for (int i = 0; i < numPreds; i++)
00093 {
00094 if (gcdebug) cout << "Looking at pred " << i << endl;
00095 bool deletePred = false;
00096 Predicate* pred = c->getPredicate(i);
00097
00098 if (gcdebug) cout << "Constructing GroundPredicate" << endl;
00099 GroundPredicate* gndPred = new GroundPredicate(pred);
00100 assert(gndPred);
00101 if (gcdebug)
00102 {
00103 cout << "Constructed GroundPredicate ";
00104 gndPred->print(cout);
00105 cout << endl;
00106 }
00107
00108 if (gcdebug) cout << "Finding GroundPredicate" << endl;
00109 int index = gndPredHashArray->find(gndPred);
00110
00111 if (gcdebug) cout << "Index of GroundPredicate: " << index << endl;
00112 if (index < 0 )
00113 {
00114 if (gcdebug) cout << "Pred " << i << " not seen before" << endl;
00115 index = gndPredHashArray->append(gndPred) + 1;
00116 if (gcdebug) cout << "Appended it" << endl;
00117 }
00118 else
00119 {
00120 if (gcdebug) cout << "Pred " << i << " seen before" << endl;
00121 deletePred = true;
00122 index++;
00123 }
00124
00125 int wlit;
00126 if (pred->getSense())
00127 {
00128 wlit = index;
00129 intArrRep->append(1);
00130 }
00131 else
00132 {
00133 wlit = -index;
00134 intArrRep->append((unsigned int)0);
00135 }
00136 intArrRep->append(index);
00137 gndPredIndexes_->append(wlit);
00138
00139 if (deletePred) delete gndPred;
00140 }
00141
00142 hashCode_ = Hash::hash(*intArrRep);
00143 delete intArrRep;
00144 gndPredIndexes_->compress();
00145 }
00146
00153 void GroundClause::appendToGndPreds(
00154 GroundPredicateHashArray* const & gndPredHashArray)
00155 {
00156
00157 for (int i = 0; i < gndPredIndexes_->size(); i++)
00158 {
00159 bool sense = ((*gndPredIndexes_)[i] > 0);
00160 int index = abs((*gndPredIndexes_)[i]) - 1;
00161
00162 (*gndPredHashArray)[index]->appendGndClause(this, sense);
00163
00164 }
00165 }
00166
00174 void GroundClause::setWtToSumOfParentWts(const MLN* const & mln)
00175 {
00176 wt_ = 0;
00177
00178 IntBoolPairItr itr;
00179 for (itr = foClauseFrequencies_->begin();
00180 itr != foClauseFrequencies_->end(); itr++)
00181 {
00182 int clauseno = itr->first;
00183 int frequency = itr->second.first;
00184 bool invertWt = itr->second.second;
00185 double parentWeight = mln->getClause(clauseno)->getWt();
00186 if (invertWt) wt_ -= parentWeight*frequency;
00187 else wt_ += parentWeight*frequency;
00188 }
00189 }
00190
00191 void GroundClause::printWithoutWt(ostream& out) const
00192 {
00193 for (int i = 0; i < gndPredIndexes_->size(); i++)
00194 {
00195 out << (*gndPredIndexes_)[i];
00196 if (i < gndPredIndexes_->size() - 1) out << " v ";
00197 }
00198 }
00199
00200 void GroundClause::print(ostream& out) const
00201 { out << wt_ << " "; printWithoutWt(out); }
00202
00203 ostream&
00204 GroundClause::print(ostream& out, const Domain* const& domain,
00205 const bool& withWt, const bool& asInt,
00206 const bool& withStrVar,
00207 const GroundPredicateHashArray* const & predHashArray) const
00208 {
00209 if (withWt) out << wt_ << " ";
00210
00211 Array<Predicate*> eqPreds;
00212 Array<Predicate*> internalPreds;
00213 for (int i = 0; i < gndPredIndexes_->size(); i++)
00214 {
00215 int index = (*gndPredIndexes_)[i];
00216 Predicate* pred =
00217 (*predHashArray)[abs(index)-1]->createEquivalentPredicate(domain);
00218
00219
00220 assert(pred);
00221 if (index < 0) pred->setSense(false);
00222 else pred->setSense(true);
00223
00224 if (pred->isEqualPred())
00225 eqPreds.append(pred);
00226 else
00227 if (pred->isInternalPred())
00228 internalPreds.append(pred);
00229 else
00230 {
00231 if (asInt) pred->printAsInt(out);
00232 else if (withStrVar) pred->printWithStrVar(out, domain);
00233 else pred->print(out,domain);
00234 if (i < gndPredIndexes_->size() - 1 || !eqPreds.empty() ||
00235 !internalPreds.empty()) out << " v ";
00236 delete pred;
00237 }
00238 }
00239
00240 for (int i = 0; i < eqPreds.size(); i++)
00241 {
00242 if (asInt) eqPreds[i]->printAsInt(out);
00243 else if (withStrVar) eqPreds[i]->printWithStrVar(out,domain);
00244 else eqPreds[i]->print(out,domain);
00245 out << ((i != eqPreds.size()-1 || !internalPreds.empty())?" v ":"");
00246 delete eqPreds[i];
00247 }
00248
00249 for (int i = 0; i < internalPreds.size(); i++)
00250 {
00251 if (asInt) internalPreds[i]->printAsInt(out);
00252 else if (withStrVar) internalPreds[i]->printWithStrVar(out,domain);
00253 else internalPreds[i]->print(out,domain);
00254 out << ((i!=internalPreds.size()-1)?" v ":"");
00255 delete internalPreds[i];
00256 }
00257
00258 return out;
00259 }
00260
00261
00262 ostream&
00263 GroundClause::printWithoutWt(ostream& out, const Domain* const & domain,
00264 const GroundPredicateHashArray* const & predHashArray) const
00265 { return print(out, domain, false, false, false, predHashArray); }
00266
00267 ostream&
00268 GroundClause::printWithoutWtWithStrVar(ostream& out,
00269 const Domain* const & domain,
00270 const GroundPredicateHashArray* const & predHashArray) const
00271 { return print(out, domain, false, false, true, predHashArray); }
00272
00273 ostream&
00274 GroundClause::printWithWtAndStrVar(ostream& out, const Domain* const& domain,
00275 const GroundPredicateHashArray* const & predHashArray) const
00276 { return print(out, domain, true, false, true, predHashArray); }
00277
00278 ostream&
00279 GroundClause::print(ostream& out, const Domain* const& domain,
00280 const GroundPredicateHashArray* const & predHashArray) const
00281 { return print(out, domain, true, false, false, predHashArray); }
00282
00283 ostream&
00284 GroundClause::printWithoutWtWithStrVarAndPeriod(ostream& out,
00285 const Domain* const& domain,
00286 const GroundPredicateHashArray* const & predHashArray) const
00287 {
00288 printWithoutWtWithStrVar(out, domain, predHashArray);
00289 if (isHardClause()) out << ".";
00290 return out;
00291 }
00292
00293
00297 double GroundClause::sizeKB()
00298 {
00299 double size = 0;
00300
00301 if (gndPredIndexes_)
00302 size += (gndPredIndexes_->size()*sizeof(int) / 1024.0);
00303
00304 size += (sizeof(double) / 1024.0);
00305
00306 if (foClauseFrequencies_)
00307 size += (foClauseFrequencies_->size()*
00308 (2*sizeof(int) + sizeof(bool)) / 1024.0);
00309
00310 return size;
00311 }
00312