1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
4 This file is part of libDAI.
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef __defined_libdai_daialg_h
23 #define __defined_libdai_daialg_h
29 #include <dai/factorgraph.h>
30 #include <dai/regiongraph.h>
36 /// The InfAlg class is the common denominator of the various approximate inference algorithms.
37 /// A InfAlg object represents a discrete factorized probability distribution over multiple variables
38 /// together with an inference algorithm.
41 /// Clone *this (virtual copy constructor)
42 virtual InfAlg
* clone() const = 0;
44 /// Create (virtual default constructor)
45 virtual InfAlg
* create() const = 0;
47 /// Virtual desctructor (needed because this class contains virtual functions)
50 /// Identifies itself for logging purposes
51 virtual std::string
identify() const = 0;
53 /// Get single node belief
54 virtual Factor
belief( const Var
&n
) const = 0;
56 /// Get general belief
57 virtual Factor
belief( const VarSet
&n
) const = 0;
60 virtual std::vector
<Factor
> beliefs() const = 0;
62 /// Get log partition sum
63 virtual Real
logZ() const = 0;
65 /// Clear messages and beliefs
66 virtual void init() = 0;
68 /// Clear messages and beliefs corresponding to the nodes in ns
69 virtual void init( const VarSet
&ns
) = 0;
71 /// The actual approximate inference algorithm
72 virtual double run() = 0;
75 virtual void backupFactor( size_t I
) = 0;
76 /// Save Factors involving ns
77 virtual void backupFactors( const VarSet
&ns
) = 0;
80 virtual void restoreFactor( size_t I
) = 0;
81 /// Restore Factors involving ns
82 virtual void restoreFactors( const VarSet
&ns
) = 0;
84 /// Clamp variable n to value i (i.e. multiply with a Kronecker delta \f$\delta_{x_n, i}\f$)
85 virtual void clamp( const Var
& n
, size_t i
, bool backup
= false ) = 0;
87 /// Set all factors interacting with var(i) to 1
88 virtual void makeCavity( size_t i
, bool backup
= false ) = 0;
90 /// Get reference to underlying FactorGraph
91 virtual FactorGraph
&fg() = 0;
93 /// Get const reference to underlying FactorGraph
94 virtual const FactorGraph
&fg() const = 0;
96 /// Return maximum difference between single node beliefs in the last pass
97 virtual double maxDiff() const = 0;
99 /// Return number of passes over the factorgraph
100 virtual size_t Iterations() const = 0;
105 class DAIAlg
: public InfAlg
, public T
{
107 /// Default constructor
108 DAIAlg() : InfAlg(), T() {}
111 DAIAlg( const T
&t
) : InfAlg(), T(t
) {}
114 DAIAlg( const DAIAlg
& x
) : InfAlg(x
), T(x
) {}
116 /// Assignment operator
117 DAIAlg
& operator=( const DAIAlg
&x
) {
119 InfAlg::operator=(x
);
125 /// Save factor I (using T::backupFactor)
126 void backupFactor( size_t I
) { T::backupFactor( I
); }
127 /// Save Factors involving ns (using T::backupFactors)
128 void backupFactors( const VarSet
&ns
) { T::backupFactors( ns
); }
130 /// Restore factor I (using T::restoreFactor)
131 void restoreFactor( size_t I
) { T::restoreFactor( I
); }
132 /// Restore Factors involving ns (using T::restoreFactors)
133 void restoreFactors( const VarSet
&ns
) { T::restoreFactors( ns
); }
135 /// Clamp variable n to value i (i.e. multiply with a Kronecker delta \f$\delta_{x_n, i}\f$) (using T::clamp)
136 void clamp( const Var
& n
, size_t i
, bool backup
= false ) { T::clamp( n
, i
, backup
); }
138 /// Set all factors interacting with var(i) to 1 (using T::makeCavity)
139 void makeCavity( size_t i
, bool backup
= false ) { T::makeCavity( i
, backup
); }
141 /// Get reference to underlying FactorGraph
142 FactorGraph
&fg() { return (FactorGraph
&)(*this); }
144 /// Get const reference to underlying FactorGraph
145 const FactorGraph
&fg() const { return (const FactorGraph
&)(*this); }
149 typedef DAIAlg
<FactorGraph
> DAIAlgFG
;
150 typedef DAIAlg
<RegionGraph
> DAIAlgRG
;
153 /// Calculate the marginal of obj on ns by clamping
154 /// all variables in ns and calculating logZ for each joined state
155 Factor
calcMarginal( const InfAlg
& obj
, const VarSet
& ns
, bool reInit
);
158 /// Calculate beliefs of all pairs in ns (by clamping
159 /// nodes in ns and calculating logZ and the beliefs for each state)
160 std::vector
<Factor
> calcPairBeliefs( const InfAlg
& obj
, const VarSet
& ns
, bool reInit
);
163 /// Calculate beliefs of all pairs in ns (by clamping
164 /// pairs in ns and calculating logZ for each joined state)
165 std::vector
<Factor
> calcPairBeliefsNew( const InfAlg
& obj
, const VarSet
& ns
, bool reInit
);
168 /// Calculate 2nd order interactions of the marginal of obj on ns
169 Factor
calcMarginal2ndO( const InfAlg
& obj
, const VarSet
& ns
, bool reInit
);
172 } // end of namespace dai