groundclause.cpp

00001 /*
00002  * All of the documentation and software included in the
00003  * Alchemy Software is copyrighted by Stanley Kok, Parag
00004  * Singla, Matthew Richardson, Pedro Domingos, Marc
00005  * Sumner, Hoifung Poon, and Daniel Lowd.
00006  * 
00007  * Copyright [2004-07] Stanley Kok, Parag Singla, Matthew
00008  * Richardson, Pedro Domingos, Marc Sumner, Hoifung
00009  * Poon, and Daniel Lowd. All rights reserved.
00010  * 
00011  * Contact: Pedro Domingos, University of Washington
00012  * (pedrod@cs.washington.edu).
00013  * 
00014  * Redistribution and use in source and binary forms, with
00015  * or without modification, are permitted provided that
00016  * the following conditions are met:
00017  * 
00018  * 1. Redistributions of source code must retain the above
00019  * copyright notice, this list of conditions and the
00020  * following disclaimer.
00021  * 
00022  * 2. Redistributions in binary form must reproduce the
00023  * above copyright notice, this list of conditions and the
00024  * following disclaimer in the documentation and/or other
00025  * materials provided with the distribution.
00026  * 
00027  * 3. All advertising materials mentioning features or use
00028  * of this software must display the following
00029  * acknowledgment: "This product includes software
00030  * developed by Stanley Kok, Parag Singla, Matthew
00031  * Richardson, Pedro Domingos, Marc Sumner, Hoifung
00032  * Poon, and Daniel Lowd in the Department of Computer Science and
00033  * Engineering at the University of Washington".
00034  * 
00035  * 4. Your publications acknowledge the use or
00036  * contribution made by the Software to your research
00037  * using the following citation(s): 
00038  * Stanley Kok, Parag Singla, Matthew Richardson and
00039  * Pedro Domingos (2005). "The Alchemy System for
00040  * Statistical Relational AI", Technical Report,
00041  * Department of Computer Science and Engineering,
00042  * University of Washington, Seattle, WA.
00043  * http://www.cs.washington.edu/ai/alchemy.
00044  * 
00045  * 5. Neither the name of the University of Washington nor
00046  * the names of its contributors may be used to endorse or
00047  * promote products derived from this software without
00048  * specific prior written permission.
00049  * 
00050  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF WASHINGTON
00051  * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00052  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00053  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00054  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY
00055  * OF WASHINGTON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00056  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00057  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00058  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00059  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00060  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00061  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00062  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00063  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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     // For each predicate in clause c
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     { // Predicate not seen before: add it to hash array
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     { // Predicate seen before: must be deleted later
00120       if (gcdebug) cout << "Pred " << i << " seen before" << endl;
00121       deletePred = true;
00122       index++;
00123     }
00124       // index is the index in hash array + 1    
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     // For each ground pred in this clause
00157   for (int i = 0; i < gndPredIndexes_->size(); i++)
00158   {
00159     bool sense = ((*gndPredIndexes_)[i] > 0);
00160     int index = abs((*gndPredIndexes_)[i]) - 1;
00161       // Tell the ground pred that it occurs in this ground clause
00162     (*gndPredHashArray)[index]->appendGndClause(this, sense);
00163     //assert(ok); ok = true;  // avoid compilation warning
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     //Predicate* pred = 
00219       //new Predicate((*(*predHashArray)[abs(index)-1]));
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     // gndPredIndexes_
00301   if (gndPredIndexes_)
00302     size += (gndPredIndexes_->size()*sizeof(int) / 1024.0);
00303     // wt_
00304   size += (sizeof(double) / 1024.0);
00305     // foClauseFrequencies_
00306   if (foClauseFrequencies_)
00307     size += (foClauseFrequencies_->size()*
00308              (2*sizeof(int) + sizeof(bool)) / 1024.0);
00309   
00310   return size;
00311 }
00312 

Generated on Sun Jun 7 11:55:11 2009 for Alchemy by  doxygen 1.5.1