factor.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 _FACTOR_H_Jan_2008
00068 #define _FACTOR_H_Jan_2008
00069 
00070 #include <math.h>
00071 #include "util.h"
00072 #include "mrf.h"
00073 #include "array.h"
00074 #include "link.h"
00075 #include "node.h"
00076 #include "superclause.h"
00077 
00078 using namespace std;
00079 using namespace __gnu_cxx;
00080 
00084 class Factor
00085 {
00086  public:
00087 
00091   Factor(Clause * const & clause, SuperClause * const & superClause, 
00092          Array<int> * const & constants, Domain * const & domain,
00093          double outputWt)
00094   {
00095     clause_ = clause;
00096     superClause_ = superClause;
00097     constants_ = constants;
00098     domain_ = domain;
00099 
00100     msgsArr_ = new Array<double *>;
00101     nextMsgsArr_ = new Array<double *>;
00102     links_ = new Array<Link *>();
00103     outputWt_ = outputWt;
00104     initFactorMesssages();
00105   }
00106   
00110   Factor()
00111   {
00112     links_ = new Array<Link *>();
00113     msgsArr_ = new Array<double *>;
00114   }
00115                   
00119   ~Factor()
00120   {
00121     for (int i = 0; i < msgsArr_->size(); i++)
00122     {
00123       delete (*msgsArr_)[i];
00124       delete (*nextMsgsArr_)[i];
00125     }
00126     delete links_;
00127     delete msgsArr_;
00128     delete nextMsgsArr_;
00129   }
00130 
00135   void initFactorMesssages();
00136   
00140   int getSuperClauseId()
00141   {
00142     if (superClause_) 
00143       return superClause_->getSuperClauseId();
00144     else
00145       return -1;
00146   }
00147                   
00151   int getParentSuperClauseId()
00152   {
00153     if( superClause_) 
00154       return superClause_->getParentSuperClauseId();
00155     else
00156       return -1;
00157   }
00158 
00162   SuperClause* getSuperClause() {return superClause_;}
00163 
00167   Clause * getClause() {return clause_;}
00168 
00172   Domain *getDomain() {return domain_;}
00173 
00177   Array<int> * getConstants() {return constants_;}
00178 
00183   int getNumLinks() {return links_->size();}
00184                   
00192   void getMessage(int index, double msgs[])
00193   { 
00194     msgs[0] = (*msgsArr_)[index][0];
00195     msgs[1] = (*msgsArr_)[index][1];
00196   }
00197 
00206   void addLink(Link *link, double inpMsgs[2])
00207   {
00208     links_->append(link);
00209     double *msgs;
00210     msgs = new double[2];
00211                            
00212     if (inpMsgs)
00213     {
00214       msgs[0] = inpMsgs[0];
00215       msgs[1] = inpMsgs[1];
00216     }
00217     else
00218     {
00219       msgs[0] = msgs[1] = 0;
00220     }
00221     msgsArr_->append(msgs);
00222     msgs = new double[2];
00223     nextMsgsArr_->append(msgs);
00224   }
00225 
00229   void receiveMessage(double* inpMsgs, Link *link)
00230   {
00231     double *nextMsgs;
00232     int reverseNodeIndex = link->getReverseNodeIndex();
00233     nextMsgs = (*nextMsgsArr_)[reverseNodeIndex];
00234     nextMsgs[0] = inpMsgs[0];
00235     nextMsgs[1] = inpMsgs[1];
00236   }
00237 
00241   double* multiplyMessagesAndSumOut(int predIndex);
00242 
00246   void sendMessage();
00247    
00251   void moveToNextStep();
00252 
00253   ostream& print(ostream& out);
00254 
00255   ostream& printWts(ostream& out);
00256 
00257  protected:
00258   Clause * clause_;
00259 
00260     //only one of the two below is used - superClause in case of lifted
00261     //inference and constants in case of ground inference
00262   SuperClause * superClause_;
00263   Array<int> * constants_;
00264   Domain * domain_;
00265   Array<Link *> *links_;
00266     // Incoming messages from nodes
00267   Array<double *> *msgsArr_;
00268     // Incoming messages from nodes for next step
00269   Array<double *> *nextMsgsArr_;
00270     // Outgoing messages
00271   double *factorMsgs_;
00272   double outputWt_;
00273 };
00274 
00275 #endif
00276 

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