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, Hoifung Poon, Daniel Lowd, and Jue Wang.
00006  * 
00007  * Copyright [2004-09] Stanley Kok, Parag Singla, Matthew
00008  * Richardson, Pedro Domingos, Marc Sumner, Hoifung
00009  * Poon, Daniel Lowd, and Jue Wang. 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, Hoifung
00032  * Poon, Daniel Lowd, and Jue Wang in the Department of
00033  * Computer Science and Engineering at the University of
00034  * Washington".
00035  * 
00036  * 4. Your publications acknowledge the use or
00037  * contribution made by the Software to your research
00038  * using the following citation(s): 
00039  * Stanley Kok, Parag Singla, Matthew Richardson and
00040  * Pedro Domingos (2005). "The Alchemy System for
00041  * Statistical Relational AI", Technical Report,
00042  * Department of Computer Science and Engineering,
00043  * University of Washington, Seattle, WA.
00044  * http://alchemy.cs.washington.edu.
00045  * 
00046  * 5. Neither the name of the University of Washington nor
00047  * the names of its contributors may be used to endorse or
00048  * promote products derived from this software without
00049  * specific prior written permission.
00050  * 
00051  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF WASHINGTON
00052  * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00053  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00054  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00055  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY
00056  * OF WASHINGTON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00057  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00058  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00059  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00060  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00061  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00062  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00063  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00064  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00065  * 
00066  */
00067 #ifndef MLNHELPER_H_NOV_4_2005
00068 #define MLNHELPER_H_NOV_4_2005
00069 
00070 //auxiliary data structures used by MLN
00071 
00072 
00073   //index is the index of an IndexClause object in an array.
00074 struct IndexClause 
00075 {
00076   IndexClause(const int& iindex, Clause* const & cclause)
00077     : index(iindex), clause(cclause) {}
00078   ~IndexClause() {}
00079   int index; 
00080   Clause* clause; 
00081 };
00082 
00083 class HashIndexClause
00084 {
00085  public:
00086   size_t operator()(IndexClause* const& ic) const 
00087   { return ic->clause->hashCode(); }
00088 };
00089 
00090 class EqualIndexClause
00091 {
00092  public:
00093   bool operator()(IndexClause* const & ic1, IndexClause* const & ic2) const
00094   { return ic1->clause->same(ic2->clause); }
00095 };
00096 
00097 typedef HashArray<IndexClause*, HashIndexClause, EqualIndexClause> 
00098   IndexClauseHashArray;
00099 
00101 
00102 
00103   //Used to quickly access a clause in predIdToClausesMap_. predId is an index
00104   //of predIdToClausesMap_, while clauseIndex is an index of the clause array 
00105   //predIdToClausesMap_[predId];
00106 struct PredIdClauseIndex 
00107 {
00108   PredIdClauseIndex(const int& ppredId, int* const & cclauseIndex)
00109     : predId(ppredId), clauseIndex(cclauseIndex) {}
00110   ~PredIdClauseIndex() {}
00111   int predId; 
00112   int* clauseIndex; 
00113 };
00114 
00115   //Used to quickly access a clause in formAndClauses_. formulaIndex an index of
00116   //formAndClauses_ and clauseIndex is an index of forAndClauses_[formulaIndex].
00117 struct FormulaClauseIndexes 
00118 { 
00119   FormulaClauseIndexes(int* const & fformulaIndex, int* const & cclauseIndex)
00120     : formulaIndex(fformulaIndex), clauseIndex(cclauseIndex) {}
00121   ~FormulaClauseIndexes() {}
00122   int* formulaIndex; 
00123   int* clauseIndex; 
00124 };
00125 
00126   //Each MLNClauseInfo corresponds to a clause. index is the index of 
00127   //MLNClauseInfo in clauseInfos_. It should be the same as the index of its 
00128   //corresponding clause in clauses_. 
00129   //predIdsClauseIndexes and formulaClauseIndexes help one to quickly find the 
00130   //occurrences of the clause in predIdToClausesMap_, and formAndClauses_.
00131 struct MLNClauseInfo
00132 {
00133   MLNClauseInfo(const int& iindex, const double& ppriorMean) 
00134     : index(iindex), priorMean(ppriorMean) {}
00135   MLNClauseInfo(const int& iindex) : index(iindex), priorMean(0) {}
00136 
00137   ~MLNClauseInfo() { predIdsClauseIndexes.deleteItemsAndClear();
00138                      formulaClauseIndexes.deleteItemsAndClear(); }
00139   void compress() { predIdsClauseIndexes.compress(); 
00140                     formulaClauseIndexes.compress(); }
00141   int index;
00142   Array<PredIdClauseIndex*> predIdsClauseIndexes;
00143   Array<FormulaClauseIndexes*> formulaClauseIndexes;
00144   double priorMean;
00145 };
00146 
00148 
00149 struct FormulaAndClauses 
00150 { 
00151   FormulaAndClauses(const string& fformula, const int& iindex, 
00152                     const bool& hhasExist, const bool& ttiedClauses)
00153     : formula(fformula), indexClauses(new IndexClauseHashArray), index(iindex), 
00154       hasExist(hhasExist), tiedClauses(ttiedClauses), numPreds(-1),
00155       isHard(false), priorMean(0), wt(0), isExistUnique(false) {}
00156   ~FormulaAndClauses() { indexClauses->deleteItemsAndClear();
00157                          delete indexClauses; }
00158   string formula; 
00159   IndexClauseHashArray* indexClauses; 
00160   int index; 
00161   bool hasExist;
00162   bool tiedClauses;
00163   int numPreds;
00164   bool isHard;
00165   double priorMean;
00166   double wt;
00167   bool isExistUnique; //contains existentially and uniquely quant. vars
00168 };
00169 
00170 class HashFormulaAndClauses
00171 {
00172  public:
00173   size_t operator()(const FormulaAndClauses* const& f) const
00174   { return hash<char const *>()(f->formula.c_str()); }
00175 };
00176 
00177 class EqualHashFormulaAndClauses
00178 {
00179  public:
00180   bool operator()(const FormulaAndClauses* const & f1, 
00181                   const FormulaAndClauses* const & f2) const
00182   { return (f1->formula.compare(f2->formula)==0); }
00183 };
00184 
00185 typedef HashArray<FormulaAndClauses*, HashFormulaAndClauses, 
00186   EqualHashFormulaAndClauses> FormulaAndClausesArray;
00187 
00188 
00189 #endif

Generated on Sun Jun 7 11:55:17 2009 for Alchemy by  doxygen 1.5.1