bpfactor.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, and Daniel Lowd.
00006  * 
00007  * Copyright [2004-08] Stanley Kok, Parag Singla, Matthew
00008  * Richardson, Pedro Domingos, Marc Sumner, Hoifung
00009  * Poon, and Daniel Lowd. 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, and Daniel Lowd 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 _BPFACTOR_H_Jan_2008
00067 #define _BPFACTOR_H_Jan_2008
00068 
00069 #include <math.h>
00070 #include "util.h"
00071 #include "mrf.h"
00072 #include "array.h"
00073 #include "bplink.h"
00074 #include "bpnode.h"
00075 #include "superclause.h"
00076 
00077 using namespace std;
00078 using namespace __gnu_cxx;
00079 
00083 class BPFactor
00084 {
00085  public:
00086   BPFactor(Clause * const & clause, SuperClause * const & superClause, 
00087            Array<int> * const & constants, Domain * const & domain,
00088            double outputWt)
00089   {
00090     clause_ = clause;
00091     superClause_ = superClause;
00092     constants_ = constants;
00093     domain_ = domain;
00094 
00095     msgsArr_ = new Array<double *>;
00096     nextMsgsArr_ = new Array<double *>;
00097     links_ = new Array<BPLink *>();
00098     outputWt_ = outputWt;
00099     initFactorMesssages();
00100   }
00101                   
00102   ~BPFactor()
00103   {
00104     for (int i = 0; i < msgsArr_->size(); i++)
00105     {
00106       delete (*msgsArr_)[i];
00107       delete (*nextMsgsArr_)[i];
00108     }
00109     delete links_;
00110     delete msgsArr_;
00111     delete nextMsgsArr_;
00112   }
00113 
00114   void initFactorMesssages();
00115   
00116   int getSuperClauseId()
00117   {
00118     if (superClause_) 
00119       return superClause_->getSuperClauseId();
00120     else
00121       return -1;
00122   }
00123                   
00124   int getParentSuperClauseId()
00125   {
00126     if( superClause_) 
00127       return superClause_->getParentSuperClauseId();
00128     else
00129       return -1;
00130   }
00131 
00132   SuperClause* getSuperClause() {return superClause_;}
00133   Clause * getClause() {return clause_;}
00134   Domain *getDomain() {return domain_;}
00135   Array<int> * getConstants() {return constants_;}
00136   int getNumLinks() {return links_->size();}
00137                   
00138   void getMessage(int index, double msgs[])
00139   { 
00140     msgs[0] = (*msgsArr_)[index][0];
00141     msgs[1] = (*msgsArr_)[index][1];
00142   }
00143 
00144   void addLink(BPLink *link, double inpMsgs[2])
00145   {
00146     links_->append(link);
00147     double *msgs;
00148     msgs = new double[2];
00149                            
00150     if (inpMsgs)
00151     {
00152       msgs[0] = inpMsgs[0];
00153       msgs[1] = inpMsgs[1];
00154     }
00155     else
00156     {
00157       msgs[0] = msgs[1] = 0;
00158     }
00159     msgsArr_->append(msgs);
00160     msgs = new double[2];
00161     nextMsgsArr_->append(msgs);
00162   }
00163 
00167   void receiveMessage(double* inpMsgs, BPLink *link)
00168   {
00169     double *nextMsgs;
00170     int reverseNodeIndex = link->getReverseNodeIndex();
00171     nextMsgs = (*nextMsgsArr_)[reverseNodeIndex];
00172     nextMsgs[0] = inpMsgs[0];
00173     nextMsgs[1] = inpMsgs[1];
00174   }
00175                   
00176     //find the outgoing message for the given predIndex - helper for sendMessage
00177   double* multiplyMessagesAndSumOut(int predIndex);
00178 
00179     //send Message on all the links
00180   void sendMessage();
00181          
00182     //update the stored msgs and update the msgProduct
00183   void moveToNextStep();
00184 
00185   ostream& print(ostream& out);
00186 
00187   ostream& printWts(ostream& out);
00188 
00189  private:
00190   Clause * clause_;
00191 
00192     //only one of the two below is used - superClause in case of lifted
00193     //inference and constants in case of ground inference
00194   SuperClause * superClause_;
00195   Array<int> * constants_;
00196   Domain * domain_;
00197   Array<BPLink *> *links_;
00198   Array<double *> *msgsArr_;
00199   Array<double *> *nextMsgsArr_;
00200   double *factorMsgs_;
00201   double outputWt_;
00202 };
00203 
00204 #endif
00205 

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