1147b2220aefc3530b8972f13dbc575c802a2c29
[libdai.git] / include / dai / daialg.h
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
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.
10
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.
15
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
19 */
20
21
22 #ifndef __defined_libdai_daialg_h
23 #define __defined_libdai_daialg_h
24
25
26 #include <string>
27 #include <iostream>
28 #include <vector>
29 #include <dai/factorgraph.h>
30 #include <dai/regiongraph.h>
31
32
33 namespace dai {
34
35
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.
39 class InfAlg {
40 public:
41 /// Clone *this (virtual copy constructor)
42 virtual InfAlg* clone() const = 0;
43
44 /// Create (virtual default constructor)
45 virtual InfAlg* create() const = 0;
46
47 /// Virtual desctructor (needed because this class contains virtual functions)
48 virtual ~InfAlg() {}
49
50 /// Identifies itself for logging purposes
51 virtual std::string identify() const = 0;
52
53 /// Get single node belief
54 virtual Factor belief( const Var &n ) const = 0;
55
56 /// Get general belief
57 virtual Factor belief( const VarSet &n ) const = 0;
58
59 /// Get all beliefs
60 virtual std::vector<Factor> beliefs() const = 0;
61
62 /// Get log partition sum
63 virtual Real logZ() const = 0;
64
65 /// Clear messages and beliefs
66 virtual void init() = 0;
67
68 /// Clear messages and beliefs corresponding to the nodes in ns
69 virtual void init( const VarSet &ns ) = 0;
70
71 /// The actual approximate inference algorithm
72 virtual double run() = 0;
73
74 /// Save factor I
75 virtual void backupFactor( size_t I ) = 0;
76 /// Save Factors involving ns
77 virtual void backupFactors( const VarSet &ns ) = 0;
78
79 /// Restore factor I
80 virtual void restoreFactor( size_t I ) = 0;
81 /// Restore Factors involving ns
82 virtual void restoreFactors( const VarSet &ns ) = 0;
83
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;
86
87 /// Set all factors interacting with var(i) to 1
88 virtual void makeCavity( size_t i, bool backup = false ) = 0;
89
90 /// Get reference to underlying FactorGraph
91 virtual FactorGraph &fg() = 0;
92
93 /// Get const reference to underlying FactorGraph
94 virtual const FactorGraph &fg() const = 0;
95
96 /// Return maximum difference between single node beliefs in the last pass
97 virtual double maxDiff() const = 0;
98
99 /// Return number of passes over the factorgraph
100 virtual size_t Iterations() const = 0;
101 };
102
103
104 template <class T>
105 class DAIAlg : public InfAlg, public T {
106 public:
107 /// Default constructor
108 DAIAlg() : InfAlg(), T() {}
109
110 /// Construct from T
111 DAIAlg( const T &t ) : InfAlg(), T(t) {}
112
113 /// Copy constructor
114 DAIAlg( const DAIAlg & x ) : InfAlg(x), T(x) {}
115
116 /// Assignment operator
117 DAIAlg & operator=( const DAIAlg &x ) {
118 if( this != &x ) {
119 InfAlg::operator=(x);
120 T::operator=(x);
121 }
122 return *this;
123 }
124
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 ); }
129
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 ); }
134
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 ); }
137
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 ); }
140
141 /// Get reference to underlying FactorGraph
142 FactorGraph &fg() { return (FactorGraph &)(*this); }
143
144 /// Get const reference to underlying FactorGraph
145 const FactorGraph &fg() const { return (const FactorGraph &)(*this); }
146 };
147
148
149 typedef DAIAlg<FactorGraph> DAIAlgFG;
150 typedef DAIAlg<RegionGraph> DAIAlgRG;
151
152
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 );
156
157
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 );
161
162
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 );
166
167
168 /// Calculate 2nd order interactions of the marginal of obj on ns
169 Factor calcMarginal2ndO( const InfAlg & obj, const VarSet& ns, bool reInit );
170
171
172 } // end of namespace dai
173
174
175 #endif