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 (virtual copy constructor)
42 virtual InfAlg
* clone() const = 0;
44 /// Virtual desctructor
45 // (this is needed because this class contains virtual functions)
48 /// Identifies itself for logging purposes
49 virtual std::string
identify() const = 0;
51 /// Get single node belief
52 virtual Factor
belief( const Var
&n
) const = 0;
54 /// Get general belief
55 virtual Factor
belief( const VarSet
&n
) const = 0;
58 virtual std::vector
<Factor
> beliefs() const = 0;
60 /// Get log partition sum
61 virtual Real
logZ() const = 0;
63 /// Clear messages and beliefs
64 virtual void init() = 0;
66 /// The actual approximate inference algorithm
67 virtual double run() = 0;
70 virtual void backupFactor( size_t I
) = 0;
71 /// Save Factors involving ns
72 virtual void backupFactors( const VarSet
&ns
) = 0;
75 virtual void restoreFactor( size_t I
) = 0;
76 /// Restore Factors involving ns
77 virtual void restoreFactors( const VarSet
&ns
) = 0;
79 /// Clamp variable n to value i (i.e. multiply with a Kronecker delta \f$\delta_{x_n, i}\f$)
80 virtual void clamp( const Var
& n
, size_t i
) = 0;
82 /// Set all factors interacting with var(i) to 1
83 virtual void makeCavity( size_t i
) = 0;
85 /// Get reference to underlying FactorGraph
86 virtual FactorGraph
&fg() = 0;
88 /// Get const reference to underlying FactorGraph
89 virtual const FactorGraph
&fg() const = 0;
91 /// Return maximum difference between beliefs in the last pass
92 virtual double maxDiff() const = 0;
97 class DAIAlg
: public InfAlg
, public T
{
99 /// Default constructor
100 DAIAlg() : InfAlg(), T() {}
103 DAIAlg( const T
&t
) : InfAlg(), T(t
) {}
106 DAIAlg( const DAIAlg
& x
) : InfAlg(x
), T(x
) {}
108 /// Save factor I (using T::backupFactor)
109 void backupFactor( size_t I
) { T::backupFactor( I
); }
110 /// Save Factors involving ns (using T::backupFactors)
111 void backupFactors( const VarSet
&ns
) { T::backupFactors( ns
); }
113 /// Restore factor I (using T::restoreFactor)
114 void restoreFactor( size_t I
) { T::restoreFactor( I
); }
115 /// Restore Factors involving ns (using T::restoreFactors)
116 void restoreFactors( const VarSet
&ns
) { T::restoreFactors( ns
); }
118 /// Clamp variable n to value i (i.e. multiply with a Kronecker delta \f$\delta_{x_n, i}\f$) (using T::clamp)
119 void clamp( const Var
& n
, size_t i
) { T::clamp( n
, i
); }
121 /// Set all factors interacting with var(i) to 1 (using T::makeCavity)
122 void makeCavity( size_t i
) { T::makeCavity( i
); }
124 /// Get reference to underlying FactorGraph
125 FactorGraph
&fg() { return (FactorGraph
&)(*this); }
127 /// Get const reference to underlying FactorGraph
128 const FactorGraph
&fg() const { return (const FactorGraph
&)(*this); }
132 typedef DAIAlg
<FactorGraph
> DAIAlgFG
;
133 typedef DAIAlg
<RegionGraph
> DAIAlgRG
;
136 /// Calculate the marginal of obj on ns by clamping
137 /// all variables in ns and calculating logZ for each joined state
138 Factor
calcMarginal( const InfAlg
& obj
, const VarSet
& ns
, bool reInit
);
141 /// Calculate beliefs of all pairs in ns (by clamping
142 /// nodes in ns and calculating logZ and the beliefs for each state)
143 std::vector
<Factor
> calcPairBeliefs( const InfAlg
& obj
, const VarSet
& ns
, bool reInit
);
146 /// Calculate beliefs of all pairs in ns (by clamping
147 /// pairs in ns and calculating logZ for each joined state)
148 std::vector
<Factor
> calcPairBeliefsNew( const InfAlg
& obj
, const VarSet
& ns
, bool reInit
);
151 /// Calculate 2nd order interactions of the marginal of obj on ns
152 Factor
calcMarginal2ndO( const InfAlg
& obj
, const VarSet
& ns
, bool reInit
);
155 } // end of namespace dai