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 
00214   size_t hashCode() { return hashCode_; }
00215   
00216   bool same(const GroundClause* const & gc)
00217   {
00218     if (this == gc) return true;
00219     if (gndPredIndexes_->size() != gc->getGndPredIndexes()->size())
00220     {
00221       return false;
00222     }
00223     return (memcmp(gndPredIndexes_->getItems(),
00224                    gc->getGndPredIndexes()->getItems(), 
00225                    (gndPredIndexes_->size())*sizeof(int)) == 0);
00226   }
00227 
00228   void printWithoutWt(ostream& out) const;
00229   void print(ostream& out) const;
00230 
00231   ostream& print(ostream& out, const Domain* const& domain, 
00232                  const bool& withWt, const bool& asInt, 
00233                  const bool& withStrVar,
00234                  const GroundPredicateHashArray* const & predHashArray) const;
00235 
00236   ostream& printWithoutWt(ostream& out, const Domain* const & domain,
00237                   const GroundPredicateHashArray* const & predHashArray) const;
00238 
00239   ostream& 
00240   printWithoutWtWithStrVar(ostream& out, const Domain* const & domain,
00241                   const GroundPredicateHashArray* const & predHashArray) const;
00242 
00243   ostream& printWithWtAndStrVar(ostream& out, const Domain* const& domain,
00244                   const GroundPredicateHashArray* const & predHashArray) const;
00245 
00246   ostream& print(ostream& out, const Domain* const& domain,
00247                  const GroundPredicateHashArray* const & predHashArray) const;
00248     
00249   ostream& printWithoutWtWithStrVarAndPeriod(ostream& out, 
00250                                              const Domain* const& domain,
00251                   const GroundPredicateHashArray* const & predHashArray) const;
00252 
00253   double sizeKB();
00254 
00255  private:
00256     // Hash code of this ground clause
00257   size_t hashCode_;
00258   Array<int>* gndPredIndexes_; // 4 + 4*n bytes (n is no. of preds)
00259 
00260     // overloaded to indicate whether this is a hard clause
00261     // if this is a hard clause, wt_ is set to HARD_GROUNDCLAUSE_WT
00262   double wt_; // 8 bytes
00263 
00264     // Number of first-order clauses this clause corresponds to
00265   IntPair* foClauseFrequencies_;
00266 
00267     // pointers to weights of first-order clauses that, after being grounded 
00268     // and having their ground predicates with known truth values removed, 
00269     // are equal to ground clause
00270   Array<const double*>* parentWtPtrs_;
00271 
00272 };
00273 
00274 
00276 
00277 
00278 class HashGroundClause
00279 {
00280  public: 
00281   size_t operator()(GroundClause* const & gc) const  { return gc->hashCode(); }
00282 };
00283 
00284 
00285 class EqualGroundClause
00286 {
00287  public:
00288   bool operator()(GroundClause* const & c1, GroundClause* const & c2) const
00289     { return c1->same(c2); }
00290 };
00291 
00292 
00294 
00295 typedef HashArray<GroundClause*, HashGroundClause, EqualGroundClause> 
00296   GroundClauseHashArray;
00297 
00298 typedef hash_set<GroundClause*, HashGroundClause, EqualGroundClause> 
00299   GroundClauseSet;
00300 
00301   
00302 #endif

Generated on Tue Jan 16 05:30:02 2007 for Alchemy by  doxygen 1.5.1