mlnhelper.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 MLNHELPER_H_NOV_4_2005
00067 #define MLNHELPER_H_NOV_4_2005
00068 
00069 //auxiliary data structures used by MLN
00070 
00071 
00072   //index is the index of an IndexClause object in an array.
00073 struct IndexClause 
00074 {
00075   IndexClause(const int& iindex, Clause* const & cclause)
00076     : index(iindex), clause(cclause) {}
00077   ~IndexClause() {}
00078   int index; 
00079   Clause* clause; 
00080 };
00081 
00082 class HashIndexClause
00083 {
00084  public:
00085   size_t operator()(IndexClause* const& ic) const 
00086   { return ic->clause->hashCode(); }
00087 };
00088 
00089 class EqualIndexClause
00090 {
00091  public:
00092   bool operator()(IndexClause* const & ic1, IndexClause* const & ic2) const
00093   { return ic1->clause->same(ic2->clause); }
00094 };
00095 
00096 typedef HashArray<IndexClause*, HashIndexClause, EqualIndexClause> 
00097   IndexClauseHashArray;
00098 
00100 
00101 
00102   //Used to quickly access a clause in predIdToClausesMap_. predId is an index
00103   //of predIdToClausesMap_, while clauseIndex is an index of the clause array 
00104   //predIdToClausesMap_[predId];
00105 struct PredIdClauseIndex 
00106 {
00107   PredIdClauseIndex(const int& ppredId, int* const & cclauseIndex)
00108     : predId(ppredId), clauseIndex(cclauseIndex) {}
00109   ~PredIdClauseIndex() {}
00110   int predId; 
00111   int* clauseIndex; 
00112 };
00113 
00114   //Used to quickly access a clause in formAndClauses_. formulaIndex an index of
00115   //formAndClauses_ and clauseIndex is an index of forAndClauses_[formulaIndex].
00116 struct FormulaClauseIndexes 
00117 { 
00118   FormulaClauseIndexes(int* const & fformulaIndex, int* const & cclauseIndex)
00119     : formulaIndex(fformulaIndex), clauseIndex(cclauseIndex) {}
00120   ~FormulaClauseIndexes() {}
00121   int* formulaIndex; 
00122   int* clauseIndex; 
00123 };
00124 
00125   //Each MLNClauseInfo corresponds to a clause. index is the index of 
00126   //MLNClauseInfo in clauseInfos_. It should be the same as the index of its 
00127   //corresponding clause in clauses_. 
00128   //predIdsClauseIndexes and formulaClauseIndexes help one to quickly find the 
00129   //occurrences of the clause in predIdToClausesMap_, and formAndClauses_.
00130 struct MLNClauseInfo
00131 {
00132   MLNClauseInfo(const int& iindex, const double& ppriorMean) 
00133     : index(iindex), priorMean(ppriorMean) {}
00134   MLNClauseInfo(const int& iindex) : index(iindex), priorMean(0) {}
00135 
00136   ~MLNClauseInfo() { predIdsClauseIndexes.deleteItemsAndClear();
00137                      formulaClauseIndexes.deleteItemsAndClear(); }
00138   void compress() { predIdsClauseIndexes.compress(); 
00139                     formulaClauseIndexes.compress(); }
00140   int index;
00141   Array<PredIdClauseIndex*> predIdsClauseIndexes;
00142   Array<FormulaClauseIndexes*> formulaClauseIndexes;
00143   double priorMean;
00144 };
00145 
00147 
00148 struct FormulaAndClauses 
00149 { 
00150   FormulaAndClauses(const string& fformula, const int& iindex, 
00151                     const bool& hhasExist)
00152     : formula(fformula), indexClauses(new IndexClauseHashArray), index(iindex), 
00153       hasExist(hhasExist), numPreds(-1), isHard(false), priorMean(0), wt(0),
00154       isExistUnique(false) {}
00155   ~FormulaAndClauses() { indexClauses->deleteItemsAndClear();
00156                          delete indexClauses; }
00157   string formula; 
00158   IndexClauseHashArray* indexClauses; 
00159   int index; 
00160   bool hasExist;
00161   int numPreds;
00162   bool isHard;
00163   double priorMean;
00164   double wt;
00165   bool isExistUnique; //contains existentially and uniquely quant. vars
00166 };
00167 
00168 class HashFormulaAndClauses
00169 {
00170  public:
00171   size_t operator()(const FormulaAndClauses* const& f) const
00172   { return hash<char const *>()(f->formula.c_str()); }
00173 };
00174 
00175 class EqualHashFormulaAndClauses
00176 {
00177  public:
00178   bool operator()(const FormulaAndClauses* const & f1, 
00179                   const FormulaAndClauses* const & f2) const
00180   { return (f1->formula.compare(f2->formula)==0); }
00181 };
00182 
00183 typedef HashArray<FormulaAndClauses*, HashFormulaAndClauses, 
00184   EqualHashFormulaAndClauses> FormulaAndClausesArray;
00185 
00186 
00187 #endif

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