61ee8dc19638438425b04d498f083cb85483590c
[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 (virtual copy constructor)
42 virtual InfAlg* clone() const = 0;
43
44 /// Virtual desctructor
45 // (this is needed because this class contains virtual functions)
46 virtual ~InfAlg() {}
47
48 /// Identifies itself for logging purposes
49 virtual std::string identify() const = 0;
50
51 /// Get single node belief
52 virtual Factor belief( const Var &n ) const = 0;
53
54 /// Get general belief
55 virtual Factor belief( const VarSet &n ) const = 0;
56
57 /// Get all beliefs
58 virtual std::vector<Factor> beliefs() const = 0;
59
60 /// Get log partition sum
61 virtual Real logZ() const = 0;
62
63 /// Clear messages and beliefs
64 virtual void init() = 0;
65
66 /// The actual approximate inference algorithm
67 virtual double run() = 0;
68
69 /// Save factor I
70 virtual void saveProb( size_t I ) = 0;
71 /// Save Factors involving ns
72 virtual void saveProbs( const VarSet &ns ) = 0;
73
74 /// Restore factor I
75 virtual void undoProb( size_t I ) = 0;
76 /// Restore Factors involving ns
77 virtual void undoProbs( const VarSet &ns ) = 0;
78
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;
81
82 /// Set all factors interacting with var(i) to 1
83 virtual void makeCavity( size_t i ) = 0;
84
85 /// Get reference to underlying FactorGraph
86 virtual FactorGraph &fg() = 0;
87
88 /// Get const reference to underlying FactorGraph
89 virtual const FactorGraph &fg() const = 0;
90
91 /// Return maximum difference between beliefs in the last pass
92 virtual double maxDiff() const = 0;
93 };
94
95
96 template <class T>
97 class DAIAlg : public InfAlg, public T {
98 public:
99 /// Default constructor
100 DAIAlg() : InfAlg(), T() {}
101
102 /// Construct from T
103 DAIAlg( const T &t ) : InfAlg(), T(t) {}
104
105 /// Copy constructor
106 DAIAlg( const DAIAlg & x ) : InfAlg(x), T(x) {}
107
108 /// Save factor I (using T::saveProb)
109 void saveProb( size_t I ) { T::saveProb( I ); }
110 /// Save Factors involving ns (using T::saveProbs)
111 void saveProbs( const VarSet &ns ) { T::saveProbs( ns ); }
112
113 /// Restore factor I (using T::undoProb)
114 void undoProb( size_t I ) { T::undoProb( I ); }
115 /// Restore Factors involving ns (using T::undoProbs)
116 void undoProbs( const VarSet &ns ) { T::undoProbs( ns ); }
117
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 ); }
120
121 /// Set all factors interacting with var(i) to 1 (using T::makeCavity)
122 void makeCavity( size_t i ) { T::makeCavity( i ); }
123
124 /// Get reference to underlying FactorGraph
125 FactorGraph &fg() { return (FactorGraph &)(*this); }
126
127 /// Get const reference to underlying FactorGraph
128 const FactorGraph &fg() const { return (const FactorGraph &)(*this); }
129 };
130
131
132 typedef DAIAlg<FactorGraph> DAIAlgFG;
133 typedef DAIAlg<RegionGraph> DAIAlgRG;
134
135
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 );
139
140
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 );
144
145
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 );
149
150
151 /// Calculate 2nd order interactions of the marginal of obj on ns
152 Factor calcMarginal2ndO( const InfAlg & obj, const VarSet& ns, bool reInit );
153
154
155 } // end of namespace dai
156
157
158 #endif