clausesampler.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 CLAUSESAMPLER_H_NOV_16_2005
00067 #define CLAUSESAMPLER_H_NOV_16_2005
00068 
00069 #include <cmath>
00070 #include "database.h"
00071 #include "truefalsegroundingsstore.h"
00072 
00073   // test for convergence once every this many samples
00074 const int NUM_SAMPLES_TEST_CONV = 200; 
00075 
00076 class Clause;
00077 class VarsGroundedType;
00078 
00079 
00080 class ClauseSampler
00081 {
00082  public:
00083   ClauseSampler(const double& delta, const double& epsilon,
00084                 const int& minSamples, const int& maxSamples) 
00085     : delta_(delta), epsilon_(epsilon), minSamples_(minSamples),
00086       maxSamples_(maxSamples) { random_.init(-1); }
00087   
00088   ~ClauseSampler() {}
00089   
00090   void setEpsilon(const double& epsilon) { epsilon_ = epsilon; }
00091   double getEpsilon() const { return epsilon_; }
00092 
00093   void setDelta(const double& delta) { delta_ = delta; }
00094   double getDelta() const { return delta_; }
00095 
00096   void setMinSamples(const int& min) { minSamples_ = min; }
00097   int getMinSamples() const { return minSamples_; }
00098 
00099   void setMaxSamples(const int& max) { maxSamples_ = max; }
00100   int getMaxSamples() const { return maxSamples_; }
00101 
00102 
00103   double computeNumSamples(const int& numPreds) const
00104   { 
00105     assert(numPreds > 1);
00106     double n =  9/2.0 * (numPreds-1) * 1/(epsilon_*epsilon_) * logl(2.0/delta_);
00107     if (minSamples_ >= 0 && n < minSamples_)     n = minSamples_;
00108     else if (maxSamples_ >= 0 && n > maxSamples_) n = maxSamples_;
00109     return n;
00110   }
00111 
00112     //clause must have more than one predicate and been canonicalized
00113   double estimateNumTrueGroundings(Clause* const& clause, 
00114                                    const Predicate* const & flippedGndPred,
00115                                    const Domain* const& domain,
00116                                    double numSamples=-1);
00117 
00118  private:
00119   void getNumGroundingWithEachPredGrounded(const Clause* const & clause,
00120                                           Array<double>& gndingsWithPredGnded,
00121                                         const Array<VarsGroundedType*>& vgtArr);
00122   
00123   void getProbInfo(const Clause* const & clause, const Database* const & db, 
00124                    const Predicate* const & flippedGndPred,
00125                    const Array<VarsGroundedType*>* const & vgtArr, 
00126                    TrueFalseGroundingsStore* const & tfGndingsStore,
00127                    Array<float>& corrProbLimit, double& sumTrueGnds, 
00128                    Array<double>& numTrueGndsPerPos);  
00129 
00130 
00131   int choosePredPos(const Array<float>& corrProbLimit)
00132   {
00133     float r = random_.random();    
00134     for (int predPos = 0; predPos < corrProbLimit.size(); predPos++)
00135       if (r < corrProbLimit[predPos]) return predPos;
00136     return -1;
00137   }
00138 
00139 
00140   Array<int>* chooseSample(const Clause* const & clause, 
00141                            const Array<VarsGroundedType*>* const & vgtArr,
00142                            const Domain* const & domain, const int& predPos,
00143                            const Predicate* const & flippedGndPred);
00144 
00145 
00146   void groundClause(const Array<VarsGroundedType*>& vgtArr, 
00147                     const Array<int>& samp);
00148 
00149   
00150   int testSampleMembership(Clause* const & clause, 
00151                            Array<VarsGroundedType*>* const & vgtArr,
00152                            const Database* const & db, 
00153                            const Array<int>& samp,
00154                            const double& sumTrueGnds);
00155 
00156 
00157   double getNumSamplesNeeded(const double& sigma, const double& gamma,
00158                              const double& epsilon)
00159   {
00160     double p = (gamma+1)/2.0;
00161     
00162       // inverse of normal function
00163     double a[] = {-3.969683028665376e+01,  2.209460984245205e+02,
00164                   -2.759285104469687e+02,  1.383577518672690e+02,
00165                   -3.066479806614716e+01,  2.506628277459239e+00};
00166     double b[] = {-5.447609879822406e+01,  1.615858368580409e+02,
00167                   -1.556989798598866e+02,  6.680131188771972e+01,
00168                   -1.328068155288572e+01 };
00169     
00170     double c[] = {-7.784894002430293e-03, -3.223964580411365e-01,
00171                   -2.400758277161838e+00, -2.549732539343734e+00,
00172                   4.374664141464968e+00,  2.938163982698783e+00};
00173     
00174     double d[] = {7.784695709041462e-03, 3.224671290700398e-01,
00175                   2.445134137142996e+00, 3.754408661907416e+00};
00176     
00177       // Define break-points.
00178     double plow  = 0.02425;
00179     double phigh = 1 - plow;
00180     double invNorm;
00181     
00182     if (p < plow)  // Rational approximation for lower region:
00183     {
00184       double q = sqrt(-2*log(p));
00185       invNorm = (((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
00186                  ((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1);
00187     }
00188     else
00189     if (phigh < p) // Rational approximation for upper region:
00190     {
00191       double q  = sqrt(-2*log(1-p));
00192       invNorm =  -(((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
00193                   ((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1);
00194     }
00195     else     // Rational approximation for central region:
00196     {
00197       double q = p - 0.5;
00198       double r = q*q;
00199       invNorm = (((((a[0]*r+a[1])*r+a[2])*r+a[3])*r+a[4])*r+a[5])*q /
00200                 (((((b[0]*r+b[1])*r+b[2])*r+b[3])*r+b[4])*r+1);
00201     }
00202       // compute number of samples needed
00203     double val = invNorm * sigma / epsilon;
00204     return val*val;
00205   }
00206 
00207 
00208   
00209  private:
00210   double delta_;
00211   double epsilon_;
00212 
00213   int minSamples_;
00214   int maxSamples_;
00215 
00216   Random random_;
00217 };
00218 
00219 
00220 #endif

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