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 "intclause.h" 00067 #include "clause.h" 00068 #include "lazyinfo.h" 00069 00070 double LazyInfo::HARD_WT = 10.0; 00071 double LazyInfo::WSCALE = 1.0; 00072 00073 IntClause::IntClause(const Clause* const & c, PredicateHashArray* const & predHashArray) 00074 : wt_(c->getWt()) 00075 { 00076 int numPreds = c->getNumPredicates(); 00077 intPreds_ = new Array<int>; 00078 Array<unsigned int>* intArrRep = new Array<unsigned int>; 00079 00080 // for each predicate in clause c 00081 for (int i = 0; i < numPreds; i++) 00082 { 00083 bool deletePred = false; 00084 Predicate* pred = new Predicate(*(c->getPredicate(i))); 00085 int index = predHashArray->find(pred); 00086 if(index < 0 ) 00087 { 00088 index = predHashArray->append(pred) + 1; 00089 } 00090 else 00091 { 00092 deletePred = true; 00093 index++; 00094 } 00095 int wlit; 00096 if(pred->getSense()) 00097 { 00098 wlit = index; 00099 intArrRep->append(1); 00100 } 00101 else 00102 { 00103 wlit = -index; 00104 intArrRep->append((unsigned int)0); 00105 } 00106 intArrRep->append(index); 00107 intPreds_->append(wlit); 00108 if (deletePred) delete pred; 00109 } 00110 00111 hashCode_ = Hash::hash(*intArrRep); 00112 delete intArrRep; 00113 intPreds_->compress(); 00114 } 00115 00116 // Copy constructor 00117 IntClause::IntClause(const IntClause& ic) 00118 { 00119 wt_ = ic.getWt(); 00120 00121 intPreds_ = new Array<int>; 00122 intPreds_->growToSize(ic.intPreds_->size()); 00123 for (int i = 0; i < ic.intPreds_->size(); i++) 00124 (*intPreds_)[i] = (*ic.intPreds_)[i]; 00125 00126 hashCode_ = ic.hashCode_; 00127 } 00128 void IntClause::addWt(const double& wt) 00129 { if (wt_ == LazyInfo::HARD_WT) return; wt_ += wt; } 00130 00131 void IntClause::setWt(const double& wt) 00132 { if (wt_ == LazyInfo::HARD_WT) return; wt_ = wt; } 00133 00134 void IntClause::setWtToHardWt() { wt_ = LazyInfo::HARD_WT; } 00135 00136 bool IntClause::isHardClause() const { return (wt_ == LazyInfo::HARD_WT); } 00137 00138 /* 00139 * Checks if this intclause is satisfied given the reference predHashArray 00140 * and the assignment in db. 00141 */ 00142 bool IntClause::isSatisfied(const PredicateHashArray* const & predHashArray, 00143 const Database* const & db) const 00144 { 00145 for (int i = 0; i < intPreds_->size(); i++) 00146 { 00147 int index = (*intPreds_)[i]; 00148 Predicate* pred = new Predicate((*(*predHashArray)[abs(index)-1])); 00149 assert(pred); 00150 if (index < 0) pred->setSense(false); 00151 else pred->setSense(true); 00152 TruthValue tv = db->getValue(pred); 00153 if (db->sameTruthValueAndSense(tv, pred->getSense())) 00154 { 00155 delete pred; 00156 return true; 00157 } 00158 delete pred; 00159 } 00160 return false; 00161 }