groundclause.h

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 and Hoifung Poon.
00006  * 
00007  * Copyright [2004-07] Stanley Kok, Parag Singla, Matthew
00008  * Richardson, Pedro Domingos, Marc Sumner and Hoifung
00009  * Poon. 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 and Hoifung
00032  * Poon 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 #ifndef GROUNDCLAUSE_H_JUN_26_2005
00067 #define GROUNDCLAUSE_H_JUN_26_2005
00068 
00069 #include <cfloat>
00070 #include <ext/hash_set>
00071 using namespace __gnu_cxx;
00072 #include "hasharray.h"
00073 #include "array.h"
00074 #include "hash.h"
00075 #include <map>
00076 
00077 using namespace std;
00078 
00079 // containers    /////////////////////////////////////////////
00080 typedef map<int,int> IntPair;
00081 typedef IntPair::iterator IntPairItr;  
00083 
00084 // Constants
00085 const double HARD_GROUNDCLAUSE_WT = DBL_MAX;
00086 
00087 // Forward declarations
00088 class Domain;
00089 class Clause;
00090 class GroundPredicate;
00091 class HashGroundPredicate;
00092 class EqualGroundPredicate;
00093 
00094 // Typedefs
00095 typedef HashArray<GroundPredicate*, HashGroundPredicate, EqualGroundPredicate> 
00096  GroundPredicateHashArray;
00097 
00101 class GroundClause
00102 {
00103  public:
00104   //GroundClause(const Clause* const & c);
00105   GroundClause(const Clause* const & c, 
00106                GroundPredicateHashArray* const & gndPredHashArray);
00107 
00108   ~GroundClause() 
00109   { 
00110     if (gndPredIndexes_) delete gndPredIndexes_;
00111     if (parentWtPtrs_) delete parentWtPtrs_;
00112     if (foClauseFrequencies_) delete foClauseFrequencies_;
00113   }
00114 
00115   void deleteParentWtPtrs()
00116   { 
00117     if (parentWtPtrs_) 
00118     {
00119       for (int i = 0; i < parentWtPtrs_->size(); i++)
00120         delete (*parentWtPtrs_)[i];
00121       delete parentWtPtrs_;
00122     }
00123     parentWtPtrs_ = NULL;
00124   }
00125 
00126   void deleteFoClauseFrequencies()
00127   { 
00128     if (foClauseFrequencies_) delete foClauseFrequencies_;
00129     foClauseFrequencies_ = NULL;
00130   }
00131 
00132   void addWt(const double& wt) 
00133   { if (wt_ == HARD_GROUNDCLAUSE_WT) return; wt_ += wt; }
00134 
00135   void setWt(const double& wt) 
00136   { if (wt_ == HARD_GROUNDCLAUSE_WT) return; wt_ = wt; }
00137 
00138   double getWt() const { return wt_; }
00139 
00140   void setWtToHardWt() { wt_ = HARD_GROUNDCLAUSE_WT; }
00141   bool isHardClause() const { return (wt_ == HARD_GROUNDCLAUSE_WT); }
00142 
00143   int getNumGroundPredicates() const { return gndPredIndexes_->size(); }
00144 
00145   const GroundPredicate* getGroundPredicate(const int& i,
00146                       GroundPredicateHashArray* const & gndPredHashArray) const 
00147   { 
00148     return (*gndPredHashArray)[abs((*gndPredIndexes_)[i]) - 1];
00149   }
00150 
00157   void appendToGndPreds(GroundPredicateHashArray* const & gndPredHashArray);
00158 
00159   bool getGroundPredicateSense(const int& i) const 
00160   { return ((*gndPredIndexes_)[i] > 0); }
00161 
00162   void setGroundPredicateIndex(const int& i, const int& gndPredIdx) 
00163   { (*gndPredIndexes_)[i] = gndPredIdx; }
00164 
00165   int getGroundPredicateIndex(const int& i) const 
00166   { return (*gndPredIndexes_)[i]; }
00167 
00168   const Array<int>* getGndPredIndexes() const
00169   {
00170     return gndPredIndexes_;
00171   }
00172 
00173   void appendParentWtPtr(const double* const & wtPtr) 
00174   {
00175     if (parentWtPtrs_ == NULL)
00176       parentWtPtrs_ = new Array<const double*>;
00177     parentWtPtrs_->append(wtPtr);
00178   }
00179 
00180   void setWtToSumOfParentWts()
00181   {
00182     assert(parentWtPtrs_);
00183     wt_ = 0;
00184     for (int i = 0; i < parentWtPtrs_->size(); i++)
00185       wt_ += *((*parentWtPtrs_)[i]);
00186   }
00187   
00188   IntPair *getClauseFrequencies()
00189   {
00190     return foClauseFrequencies_;
00191   }
00192 
00193   int getClauseFrequency(int clauseno)
00194   {
00195         if (!foClauseFrequencies_) return 0;
00196         IntPairItr itr = foClauseFrequencies_->find(clauseno);
00197         if (itr == foClauseFrequencies_->end()) 
00198           return 0;
00199         else
00200           return itr->second;
00201   }
00202   
00203   void incrementClauseFrequency(int clauseno, int increment)
00204   {
00205         if (!foClauseFrequencies_)
00206       foClauseFrequencies_ = new IntPair;
00207         IntPairItr itr = foClauseFrequencies_->find(clauseno);
00208         if (itr == foClauseFrequencies_->end()) 
00209           foClauseFrequencies_->insert(IntPair::value_type(clauseno, increment));
00210         else
00211           itr->second += increment;
00212   }
00213 
00220   void removeGndPred(const int& gndPred)
00221   {
00222     for (int i = 0; i < gndPredIndexes_->size(); i++)
00223     {
00224       if (gndPred == (*gndPredIndexes_)[i])
00225       {
00226         gndPredIndexes_->removeItem(i);
00227         gndPredIndexes_->compress();
00228         rehash();
00229         break;
00230       }
00231     }
00232   }
00233 
00241   void changeGndPredIndex(const int& oldIdx, const int& newIdx)
00242   {
00243     for (int i = 0; i < gndPredIndexes_->size(); i++)
00244     {
00245       if (oldIdx == (*gndPredIndexes_)[i])
00246       {
00247         (*gndPredIndexes_)[i] = newIdx;
00248         rehash();
00249         break;
00250       }
00251     }    
00252   }
00253 
00254   size_t hashCode() { return hashCode_; }
00255   
00256   bool same(const GroundClause* const & gc)
00257   {
00258     if (this == gc) return true;
00259     if (gndPredIndexes_->size() != gc->getGndPredIndexes()->size())
00260     {
00261       return false;
00262     }
00263     return (memcmp(gndPredIndexes_->getItems(),
00264                    gc->getGndPredIndexes()->getItems(), 
00265                    (gndPredIndexes_->size())*sizeof(int)) == 0);
00266   }
00267 
00268   void printWithoutWt(ostream& out) const;
00269   void print(ostream& out) const;
00270 
00271   ostream& print(ostream& out, const Domain* const& domain, 
00272                  const bool& withWt, const bool& asInt, 
00273                  const bool& withStrVar,
00274                  const GroundPredicateHashArray* const & predHashArray) const;
00275 
00276   ostream& printWithoutWt(ostream& out, const Domain* const & domain,
00277                   const GroundPredicateHashArray* const & predHashArray) const;
00278 
00279   ostream& 
00280   printWithoutWtWithStrVar(ostream& out, const Domain* const & domain,
00281                   const GroundPredicateHashArray* const & predHashArray) const;
00282 
00283   ostream& printWithWtAndStrVar(ostream& out, const Domain* const& domain,
00284                   const GroundPredicateHashArray* const & predHashArray) const;
00285 
00286   ostream& print(ostream& out, const Domain* const& domain,
00287                  const GroundPredicateHashArray* const & predHashArray) const;
00288     
00289   ostream& printWithoutWtWithStrVarAndPeriod(ostream& out, 
00290                                              const Domain* const& domain,
00291                   const GroundPredicateHashArray* const & predHashArray) const;
00292 
00293   double sizeKB();
00294 
00295  private:
00296   
00300   void rehash()
00301   {
00302     Array<unsigned int>* intArrRep = new Array<unsigned int>;
00303 
00304       // For each predicate
00305     for (int i = 0; i < gndPredIndexes_->size(); i++)
00306     {
00307         // For each pred 1 (if pos.) or 0 (if neg.) is appended to intArrRep
00308       if ((*gndPredIndexes_)[i] > 0)
00309         intArrRep->append(1);
00310       else
00311         intArrRep->append((unsigned int)0);
00312       intArrRep->append(abs((*gndPredIndexes_)[i]));
00313     }
00314   
00315     hashCode_ = Hash::hash(*intArrRep);
00316     delete intArrRep;    
00317   }
00318 
00319  private:
00320     // Hash code of this ground clause
00321   size_t hashCode_;
00322   Array<int>* gndPredIndexes_; // 4 + 4*n bytes (n is no. of preds)
00323 
00324     // overloaded to indicate whether this is a hard clause
00325     // if this is a hard clause, wt_ is set to HARD_GROUNDCLAUSE_WT
00326   double wt_; // 8 bytes
00327 
00328     // Number of first-order clauses this clause corresponds to
00329   IntPair* foClauseFrequencies_;
00330 
00331     // pointers to weights of first-order clauses that, after being grounded 
00332     // and having their ground predicates with known truth values removed, 
00333     // are equal to ground clause
00334   Array<const double*>* parentWtPtrs_;
00335 
00336 };
00337 
00338 
00340 
00341 
00342 class HashGroundClause
00343 {
00344  public: 
00345   size_t operator()(GroundClause* const & gc) const  { return gc->hashCode(); }
00346 };
00347 
00348 
00349 class EqualGroundClause
00350 {
00351  public:
00352   bool operator()(GroundClause* const & c1, GroundClause* const & c2) const
00353     { return c1->same(c2); }
00354 };
00355 
00356 
00358 
00359 typedef HashArray<GroundClause*, HashGroundClause, EqualGroundClause> 
00360   GroundClauseHashArray;
00361 
00362 typedef hash_set<GroundClause*, HashGroundClause, EqualGroundClause> 
00363   GroundClauseSet;
00364 
00365   
00366 #endif

Generated on Wed Feb 14 15:15:17 2007 for Alchemy by  doxygen 1.5.1