unitpropagation.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-07] 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 UNITPROPAGATION_H_
00067 #define UNITPROPAGATION_H_
00068 
00069 #include "inference.h"
00070 
00071 const int updebug = false;
00072 
00073 class UnitPropagation : public Inference
00074 {
00075  public:
00076   
00080   UnitPropagation(VariableState* state, long int seed,
00081                   const bool& trackClauseTrueCnts)
00082     : Inference(state, seed, trackClauseTrueCnts) {}
00083 
00084   ~UnitPropagation() {}
00085   
00089   void init() { return; }
00090   
00094   void infer()
00095   {
00096     if (updebug) cout << "Entering UnitPropagation::infer" << endl;
00097     bool done = false;
00098       // We are done when no more propagation occurs
00099     while (!done)
00100     {
00101       if (updebug) cout << endl << endl;
00102       done = true;
00103       for (int i = 0; i < state_->getNumClauses(); i++)
00104       {
00105           // Don't look at dead clauses
00106         if (state_->isDeadClause(i)) continue;
00107           // Simplify clause i using the atoms already fixed
00108         Array<int>* atoms = state_->simplifyClauseFromFixedAtoms(i);
00109         if (atoms->size() == 0)
00110         {
00111           delete atoms;
00112           continue;
00113         }
00114         if (updebug) cout << "Clause " << i << " ";
00115           // If unit clause or negative weight, then fix the atom(s)
00116         long double cost = state_->getClauseCost(i);
00117         if (atoms->size() == 1 || cost < 0)
00118         {
00119           if (updebug) cout << "unit or negative" << endl;
00120             // We are fixing atoms, so we have possible propagation
00121           done = false;
00122             // Fix each atom in the clause
00123           for (int j = 0; j < atoms->size(); j++)
00124           {
00125             int lit = (*atoms)[j];
00126               // Set value to true if pos. lit. and pos. clause
00127               // or neg. lit. and neg. clause
00128             bool value = ((lit > 0 && cost > 0) || (lit < 0 && cost < 0))
00129               ? true : false;
00130             state_->fixAtom(abs(lit), value);
00131           }
00132         }
00133         else
00134         {
00135           if (updebug) cout << endl;
00136         }
00137         delete atoms;
00138       }
00139     }
00140       // Done with UP, save this state
00141     state_->saveLowState();
00142     if (updebug) cout << "Leaving UnitPropagation::infer" << endl;
00143   }
00144   
00148   void printProbabilities(ostream& out)
00149   {
00150     for (int i = 0; i < state_->getNumAtoms(); i++)
00151     {
00152       state_->printGndPred(i, out);
00153       out << " " << state_->getValueOfLowAtom(i + 1) << endl;
00154     }
00155   }
00156 
00170   void getChangedPreds(vector<string>& changedPreds, vector<float>& probs,
00171                        vector<float>& oldProbs, const float& probDelta)
00172   {
00173     changedPreds.clear();
00174     probs.clear();
00175     int numAtoms = state_->getNumAtoms();
00176       // Atoms may have been added to the state, previous tv was 0
00177     oldProbs.resize(numAtoms, 0);
00178     for (int i = 0; i < numAtoms; i++)
00179     {
00180       int tv = state_->getValueOfLowAtom(i + 1);
00181       if (tv != oldProbs[i])
00182       {
00183           // Truth value has changed: Store new value in oldProbs and add to
00184           // two return vectors
00185         oldProbs[i] = tv;
00186         ostringstream oss(ostringstream::out);
00187         state_->printGndPred(i, oss);
00188         changedPreds.push_back(oss.str());
00189         probs.push_back(tv);
00190       }
00191     }
00192   }
00193 
00197   double getProbability(GroundPredicate* const& gndPred)
00198   {
00199     int idx = state_->getGndPredIndex(gndPred);
00200     int truthValue = 0;
00201     if (idx >= 0) truthValue = state_->getValueOfLowAtom(idx + 1);
00202     return truthValue;
00203   }
00204 
00208   void printTruePreds(ostream& out)
00209   {
00210     for (int i = 0; i < state_->getNumAtoms(); i++)
00211     {
00212       if (state_->getValueOfLowAtom(i + 1))
00213       {
00214         state_->printGndPred(i, out);
00215         out << endl;
00216       }
00217     }
00218   }
00219   
00220  private:
00221  
00222    
00223 };
00224 
00225 
00226 #endif /*UNITPROPAGATION_H_*/

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