Fixed long-standing bug in TreeEP (now, within-loop propagation optimization works)
[libdai.git] / include / dai / treeep.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 /// \file
13 /// \brief Defines class TreeEP, which implements Tree Expectation Propagation
14 /// \todo Clean up the TreeEP code (by making JTree more powerful, e.g., by
15 /// adding Pearl's cutset algorithm and propagation with an arbitrary root)
16
17
18 #ifndef __defined_libdai_treeep_h
19 #define __defined_libdai_treeep_h
20
21
22 #include <vector>
23 #include <string>
24 #include <dai/daialg.h>
25 #include <dai/varset.h>
26 #include <dai/regiongraph.h>
27 #include <dai/factorgraph.h>
28 #include <dai/clustergraph.h>
29 #include <dai/weightedgraph.h>
30 #include <dai/jtree.h>
31 #include <dai/properties.h>
32 #include <dai/enum.h>
33
34
35 namespace dai {
36
37
38 /// Approximate inference algorithm "Tree Expectation Propagation" [\ref MiQ04]
39 class TreeEP : public JTree {
40 private:
41 /// Maximum difference encountered so far
42 Real _maxdiff;
43 /// Number of iterations needed
44 size_t _iters;
45
46 public:
47 /// Parameters for TreeEP
48 struct Properties {
49 /// Enumeration of possible choices for the tree
50 /** The two possibilities are:
51 * - \c ORG: take the maximum spanning tree where the weights are crude
52 * estimates of the mutual information between the nodes;
53 * - \c ALT: take the maximum spanning tree where the weights are upper
54 * bounds on the effective interaction strengths between pairs of nodes.
55 */
56 DAI_ENUM(TypeType,ORG,ALT);
57
58 /// Verbosity (amount of output sent to stderr)
59 size_t verbose;
60
61 /// Maximum number of iterations
62 size_t maxiter;
63
64 /// Tolerance for convergence test
65 Real tol;
66
67 /// How to choose the tree
68 TypeType type;
69 } props;
70
71 /// Name of this inference method
72 static const char *Name;
73
74 private:
75 /// Stores the data structures needed to efficiently update the approximation of an off-tree factor
76 class TreeEPSubTree {
77 private:
78 /// Outer region pseudomarginals (corresponding with the \f$\tilde f_i(x_j,x_k)\f$ in [\ref MiQ04])
79 std::vector<Factor> _Qa;
80 /// Inner region pseudomarginals (corresponding with the \f$\tilde f_i(x_s)\f$ in [\ref MiQ04])
81 std::vector<Factor> _Qb;
82 /// The junction tree (stored as a rooted tree)
83 RootedTree _RTree;
84 /// Index conversion table for outer region indices (_Qa[alpha] corresponds with Qa[_a[alpha]] of the supertree)
85 std::vector<size_t> _a;
86 /// Index conversion table for inner region indices (_Qb[beta] corresponds with Qb[_b[beta]] of the supertree)
87 std::vector<size_t> _b;
88 /// Pointer to off-tree factor
89 const Factor * _I;
90 /// Variables in off-tree factor
91 VarSet _ns;
92 /// Variables in off-tree factor which are not in the root of this subtree
93 VarSet _nsrem;
94 /// Used for calculating the free energy
95 Real _logZ;
96
97 public:
98 /// \name Constructors/destructors
99 //@{
100 /// Default constructor
101 TreeEPSubTree() : _Qa(), _Qb(), _RTree(), _a(), _b(), _I(NULL), _ns(), _nsrem(), _logZ(0.0) {}
102
103 /// Copy constructor
104 TreeEPSubTree( const TreeEPSubTree &x ) : _Qa(x._Qa), _Qb(x._Qb), _RTree(x._RTree), _a(x._a), _b(x._b), _I(x._I), _ns(x._ns), _nsrem(x._nsrem), _logZ(x._logZ) {}
105
106 /// Assignment operator
107 TreeEPSubTree & operator=( const TreeEPSubTree& x ) {
108 if( this != &x ) {
109 _Qa = x._Qa;
110 _Qb = x._Qb;
111 _RTree = x._RTree;
112 _a = x._a;
113 _b = x._b;
114 _I = x._I;
115 _ns = x._ns;
116 _nsrem = x._nsrem;
117 _logZ = x._logZ;
118 }
119 return *this;
120 }
121
122 /// Construct from super tree
123 TreeEPSubTree( const RootedTree &subRTree, const RootedTree &jt_RTree, const std::vector<Factor> &jt_Qa, const std::vector<Factor> &jt_Qb, const Factor *I );
124 //@}
125
126 /// Initializes beliefs of this subtree
127 void init();
128
129 /// Inverts this approximation and multiplies it by the (super) junction tree marginals \a Qa and \a Qb
130 void InvertAndMultiply( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb );
131
132 /// Runs junction tree algorithm (including off-tree factor I) storing the results in the (super) junction tree \a Qa and \a Qb
133 void HUGIN_with_I( std::vector<Factor> &Qa, std::vector<Factor> &Qb );
134
135 /// Returns energy (?) of this subtree
136 Real logZ( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb ) const;
137
138 /// Returns constant reference to the pointer to the off-tree factor
139 const Factor *& I() { return _I; }
140 };
141
142 /// Stores a TreeEPSubTree object for each off-tree factor
143 std::map<size_t, TreeEPSubTree> _Q;
144
145 public:
146 /// Default constructor
147 TreeEP() : JTree(), _maxdiff(0.0), _iters(0), props(), _Q() {}
148
149 /// Copy constructor
150 TreeEP( const TreeEP &x ) : JTree(x), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props), _Q(x._Q) {
151 for( size_t I = 0; I < nrFactors(); I++ )
152 if( offtree( I ) )
153 _Q[I].I() = &factor(I);
154 }
155
156 /// Assignment operator
157 TreeEP& operator=( const TreeEP &x ) {
158 if( this != &x ) {
159 JTree::operator=( x );
160 _maxdiff = x._maxdiff;
161 _iters = x._iters;
162 props = x.props;
163 _Q = x._Q;
164 for( size_t I = 0; I < nrFactors(); I++ )
165 if( offtree( I ) )
166 _Q[I].I() = &factor(I);
167 }
168 return *this;
169 }
170
171 /// Construct from FactorGraph \a fg and PropertySet \a opts
172 /** \param opts Parameters @see Properties
173 * \note The factor graph has to be connected.
174 * \throw FACTORGRAPH_NOT_CONNECTED if \a fg is not connected
175 */
176 TreeEP( const FactorGraph &fg, const PropertySet &opts );
177
178
179 /// \name General InfAlg interface
180 //@{
181 virtual TreeEP* clone() const { return new TreeEP(*this); }
182 virtual std::string identify() const;
183 virtual Real logZ() const;
184 virtual void init();
185 virtual void init( const VarSet &/*ns*/ ) { init(); }
186 virtual Real run();
187 virtual Real maxDiff() const { return _maxdiff; }
188 virtual size_t Iterations() const { return _iters; }
189 virtual void setProperties( const PropertySet &opts );
190 virtual PropertySet getProperties() const;
191 virtual std::string printProperties() const;
192 //@}
193
194
195 private:
196 /// Helper function for constructors
197 void construct( const RootedTree &tree );
198 /// Returns \c true if factor \a I is not part of the tree
199 bool offtree( size_t I ) const { return (fac2OR[I] == -1U); }
200 };
201
202
203 } // end of namespace dai
204
205
206 #endif