Updated copyrights
[libdai.git] / include / dai / daialg.h
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #ifndef __defined_libdai_daialg_h
24 #define __defined_libdai_daialg_h
25
26
27 #include <string>
28 #include <iostream>
29 #include <vector>
30 #include <dai/factorgraph.h>
31 #include <dai/regiongraph.h>
32
33
34 namespace dai {
35
36
37 /// The InfAlg class is the common denominator of the various approximate inference algorithms.
38 /// A InfAlg object represents a discrete factorized probability distribution over multiple variables
39 /// together with an inference algorithm.
40 class InfAlg {
41 public:
42 /// Clone *this (virtual copy constructor)
43 virtual InfAlg* clone() const = 0;
44
45 /// Create (virtual default constructor)
46 virtual InfAlg* create() const = 0;
47
48 /// Virtual desctructor (needed because this class contains virtual functions)
49 virtual ~InfAlg() {}
50
51 /// Identifies itself for logging purposes
52 virtual std::string identify() const = 0;
53
54 /// Get single node belief
55 virtual Factor belief( const Var &n ) const = 0;
56
57 /// Get general belief
58 virtual Factor belief( const VarSet &n ) const = 0;
59
60 /// Get all beliefs
61 virtual std::vector<Factor> beliefs() const = 0;
62
63 /// Get log partition sum
64 virtual Real logZ() const = 0;
65
66 /// Clear messages and beliefs
67 virtual void init() = 0;
68
69 /// Clear messages and beliefs corresponding to the nodes in ns
70 virtual void init( const VarSet &ns ) = 0;
71
72 /// The actual approximate inference algorithm
73 virtual double run() = 0;
74
75 /// Save factor I
76 virtual void backupFactor( size_t I ) = 0;
77 /// Save Factors involving ns
78 virtual void backupFactors( const VarSet &ns ) = 0;
79
80 /// Restore factor I
81 virtual void restoreFactor( size_t I ) = 0;
82 /// Restore Factors involving ns
83 virtual void restoreFactors( const VarSet &ns ) = 0;
84
85 /// Clamp variable n to value i (i.e. multiply with a Kronecker delta \f$\delta_{x_n, i}\f$)
86 virtual void clamp( const Var & n, size_t i, bool backup = false ) = 0;
87
88 /// Set all factors interacting with var(i) to 1
89 virtual void makeCavity( size_t i, bool backup = false ) = 0;
90
91 /// Get reference to underlying FactorGraph
92 virtual FactorGraph &fg() = 0;
93
94 /// Get const reference to underlying FactorGraph
95 virtual const FactorGraph &fg() const = 0;
96
97 /// Return maximum difference between single node beliefs in the last pass
98 virtual double maxDiff() const = 0;
99
100 /// Return number of passes over the factorgraph
101 virtual size_t Iterations() const = 0;
102 };
103
104
105 template <class T>
106 class DAIAlg : public InfAlg, public T {
107 public:
108 /// Default constructor
109 DAIAlg() : InfAlg(), T() {}
110
111 /// Construct from T
112 DAIAlg( const T &t ) : InfAlg(), T(t) {}
113
114 /// Copy constructor
115 DAIAlg( const DAIAlg & x ) : InfAlg(x), T(x) {}
116
117 /// Assignment operator
118 DAIAlg & operator=( const DAIAlg &x ) {
119 if( this != &x ) {
120 InfAlg::operator=(x);
121 T::operator=(x);
122 }
123 return *this;
124 }
125
126 /// Save factor I (using T::backupFactor)
127 void backupFactor( size_t I ) { T::backupFactor( I ); }
128 /// Save Factors involving ns (using T::backupFactors)
129 void backupFactors( const VarSet &ns ) { T::backupFactors( ns ); }
130
131 /// Restore factor I (using T::restoreFactor)
132 void restoreFactor( size_t I ) { T::restoreFactor( I ); }
133 /// Restore Factors involving ns (using T::restoreFactors)
134 void restoreFactors( const VarSet &ns ) { T::restoreFactors( ns ); }
135
136 /// Clamp variable n to value i (i.e. multiply with a Kronecker delta \f$\delta_{x_n, i}\f$) (using T::clamp)
137 void clamp( const Var & n, size_t i, bool backup = false ) { T::clamp( n, i, backup ); }
138
139 /// Set all factors interacting with var(i) to 1 (using T::makeCavity)
140 void makeCavity( size_t i, bool backup = false ) { T::makeCavity( i, backup ); }
141
142 /// Get reference to underlying FactorGraph
143 FactorGraph &fg() { return (FactorGraph &)(*this); }
144
145 /// Get const reference to underlying FactorGraph
146 const FactorGraph &fg() const { return (const FactorGraph &)(*this); }
147 };
148
149
150 typedef DAIAlg<FactorGraph> DAIAlgFG;
151 typedef DAIAlg<RegionGraph> DAIAlgRG;
152
153
154 /// Calculate the marginal of obj on ns by clamping
155 /// all variables in ns and calculating logZ for each joined state
156 Factor calcMarginal( const InfAlg & obj, const VarSet & ns, bool reInit );
157
158
159 /// Calculate beliefs of all pairs in ns (by clamping
160 /// nodes in ns and calculating logZ and the beliefs for each state)
161 std::vector<Factor> calcPairBeliefs( const InfAlg & obj, const VarSet& ns, bool reInit );
162
163
164 /// Calculate beliefs of all pairs in ns (by clamping
165 /// pairs in ns and calculating logZ for each joined state)
166 std::vector<Factor> calcPairBeliefsNew( const InfAlg & obj, const VarSet& ns, bool reInit );
167
168
169 /// Calculate 2nd order interactions of the marginal of obj on ns
170 Factor calcMarginal2ndO( const InfAlg & obj, const VarSet& ns, bool reInit );
171
172
173 } // end of namespace dai
174
175
176 #endif