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 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 #include "groundclause.h"
00067 #include "groundpredicate.h"
00068 #include "clause.h"
00069 
00070 GroundClause::GroundClause(const Clause* const & c, 
00071                            GroundPredicateHashArray* const & gndPredHashArray) 
00072   : wt_(c->getWt()), foClauseFrequencies_(NULL), parentWtPtrs_(NULL)
00073 {
00074   int numPreds = c->getNumPredicates();
00075   gndPredIndexes_ = new Array<int>;
00076   //gndPredIndexes_->growToSize(numPreds);
00077   Array<unsigned int>* intArrRep = new Array<unsigned int>;
00078 
00079     // For each predicate in clause c
00080   for (int i = 0; i < numPreds; i++)
00081   {
00082     bool deletePred = false;
00083     Predicate* pred = c->getPredicate(i);
00084     GroundPredicate* gndPred = new GroundPredicate(pred);
00085     int index = gndPredHashArray->find(gndPred);
00086     if (index < 0 )
00087     { // Predicate not seen before: add it to hash array
00088       index = gndPredHashArray->append(gndPred) + 1;
00089     }
00090     else
00091     { // Predicate seen before: must be deleted later
00092       deletePred = true;
00093       index++;
00094     }
00095       // index is the index in hash array + 1    
00096     int wlit;
00097     if (pred->getSense())
00098     {
00099       wlit = index;
00100       intArrRep->append(1);
00101     }
00102     else
00103     {
00104       wlit = -index;
00105       intArrRep->append((unsigned int)0);
00106     }
00107     intArrRep->append(index);
00108     gndPredIndexes_->append(wlit);
00109 
00110     if (deletePred) delete gndPred;
00111   }
00112   
00113   hashCode_ = Hash::hash(*intArrRep);
00114   delete intArrRep;
00115   gndPredIndexes_->compress();
00116 }
00117 
00124 void GroundClause::appendToGndPreds(
00125                       GroundPredicateHashArray* const & gndPredHashArray)
00126 { 
00127     // For each ground pred in this clause
00128   for (int i = 0; i < gndPredIndexes_->size(); i++)
00129   {
00130     bool sense = ((*gndPredIndexes_)[i] > 0);
00131     int index = abs((*gndPredIndexes_)[i]) - 1;
00132       // Tell the ground pred that it occurs in this ground clause
00133     bool ok = (*gndPredHashArray)[index]->appendGndClause(this, sense);
00134     assert(ok); ok = true;  // avoid compilation warning
00135   }
00136 }
00137 
00138 void GroundClause::printWithoutWt(ostream& out) const
00139 {
00140   for (int i = 0; i < gndPredIndexes_->size(); i++)
00141   {
00142     out << (*gndPredIndexes_)[i];
00143     if (i < gndPredIndexes_->size() - 1) out << " v ";
00144   }
00145 }
00146 
00147 void GroundClause::print(ostream& out) const
00148 { out << wt_ << " "; printWithoutWt(out); }
00149 
00150 ostream&
00151 GroundClause::print(ostream& out, const Domain* const& domain, 
00152                     const bool& withWt, const bool& asInt, 
00153                     const bool& withStrVar,
00154                    const GroundPredicateHashArray* const & predHashArray) const
00155 {
00156   if (withWt) out << wt_ << " ";
00157 
00158   Array<Predicate*> eqPreds;
00159   Array<Predicate*> internalPreds;
00160   for (int i = 0; i < gndPredIndexes_->size(); i++)
00161   {
00162     int index = (*gndPredIndexes_)[i];
00163     Predicate* pred =
00164       (*predHashArray)[abs(index)-1]->createEquivalentPredicate(domain);
00165     //Predicate* pred = 
00166       //new Predicate((*(*predHashArray)[abs(index)-1]));
00167     assert(pred);
00168     if (index < 0) pred->setSense(false);
00169     else pred->setSense(true);
00170 
00171     if (pred->isEqualPred()) 
00172       eqPreds.append(pred);
00173     else
00174     if (pred->isInternalPred()) 
00175       internalPreds.append(pred);
00176     else
00177     {
00178       if (asInt)           pred->printAsInt(out);
00179       else if (withStrVar) pred->printWithStrVar(out, domain);
00180       else                 pred->print(out,domain);
00181       if (i < gndPredIndexes_->size() - 1 || !eqPreds.empty() ||
00182           !internalPreds.empty()) out << " v ";
00183       delete pred;
00184     }
00185   }
00186 
00187   for (int i = 0; i < eqPreds.size(); i++)
00188   {
00189     if (asInt)           eqPreds[i]->printAsInt(out);
00190     else if (withStrVar) eqPreds[i]->printWithStrVar(out,domain);
00191     else                 eqPreds[i]->print(out,domain);
00192     out << ((i != eqPreds.size()-1 || !internalPreds.empty())?" v ":"");      
00193     delete eqPreds[i];
00194   }
00195 
00196   for (int i = 0; i < internalPreds.size(); i++)
00197   {
00198     if (asInt)           internalPreds[i]->printAsInt(out);
00199     else if (withStrVar) internalPreds[i]->printWithStrVar(out,domain);
00200     else                 internalPreds[i]->print(out,domain);
00201     out << ((i!=internalPreds.size()-1)?" v ":"");      
00202     delete internalPreds[i];
00203   }
00204 
00205   return out;
00206 }
00207 
00208 
00209 ostream&
00210 GroundClause::printWithoutWt(ostream& out, const Domain* const & domain,
00211                    const GroundPredicateHashArray* const & predHashArray) const
00212 { return print(out, domain, false, false, false, predHashArray); }
00213 
00214 ostream& 
00215 GroundClause::printWithoutWtWithStrVar(ostream& out,
00216                                        const Domain* const & domain,
00217                    const GroundPredicateHashArray* const & predHashArray) const
00218 { return print(out, domain, false, false, true, predHashArray); }
00219 
00220 ostream&
00221 GroundClause::printWithWtAndStrVar(ostream& out, const Domain* const& domain,
00222                    const GroundPredicateHashArray* const & predHashArray) const
00223 { return print(out, domain, true, false, true, predHashArray); }
00224 
00225 ostream&
00226 GroundClause::print(ostream& out, const Domain* const& domain,
00227                    const GroundPredicateHashArray* const & predHashArray) const
00228 { return print(out, domain, true, false, false, predHashArray); }
00229     
00230 ostream&
00231 GroundClause::printWithoutWtWithStrVarAndPeriod(ostream& out, 
00232                                                 const Domain* const& domain,
00233                    const GroundPredicateHashArray* const & predHashArray) const
00234 {
00235   printWithoutWtWithStrVar(out, domain, predHashArray);
00236   if (isHardClause()) out << ".";
00237   return out;
00238 }
00239 
00240 
00244 double GroundClause::sizeKB()
00245 {
00246   double size = 0;
00247     // gndPredIndexes_
00248   if (gndPredIndexes_)
00249     size += (gndPredIndexes_->size()*sizeof(int) / 1024.0);
00250     // wt_
00251   size += (sizeof(double) / 1024.0);
00252     // parentWtPtrs_
00253   if (parentWtPtrs_)
00254     size += (parentWtPtrs_->size()*sizeof(const double*) / 1024.0);
00255     // foClauseFrequencies_
00256   if (foClauseFrequencies_)
00257     size += (foClauseFrequencies_->size()*2*sizeof(int) / 1024.0);
00258   
00259   return size;
00260 }
00261 

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