Improved TreeEP (added 'maxtime' property)
[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-2010 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 (exploiting that a large part of the code
15 /// is just a special case of JTree).
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 /// Maximum time (in seconds)
65 double maxtime;
66
67 /// Tolerance for convergence test
68 Real tol;
69
70 /// How to choose the tree
71 TypeType type;
72 } props;
73
74 /// Name of this inference method
75 static const char *Name;
76
77 private:
78 /// Stores the data structures needed to efficiently update the approximation of an off-tree factor.
79 /** The TreeEP object stores a TreeEPSubTree object for each off-tree factor.
80 * It stores the approximation of that off-tree factor, which is represented
81 * as a distribution on a subtree of the main tree.
82 */
83 class TreeEPSubTree {
84 private:
85 /// Outer region pseudomarginals (corresponding with the \f$\tilde f_i(x_j,x_k)\f$ in [\ref MiQ04])
86 std::vector<Factor> _Qa;
87 /// Inner region pseudomarginals (corresponding with the \f$\tilde f_i(x_s)\f$ in [\ref MiQ04])
88 std::vector<Factor> _Qb;
89 /// The junction tree (stored as a rooted tree)
90 RootedTree _RTree;
91 /// Index conversion table for outer region indices (_Qa[alpha] corresponds with Qa[_a[alpha]] of the supertree)
92 std::vector<size_t> _a;
93 /// Index conversion table for inner region indices (_Qb[beta] corresponds with Qb[_b[beta]] of the supertree)
94 std::vector<size_t> _b;
95 /// Pointer to off-tree factor
96 const Factor * _I;
97 /// Variables in off-tree factor
98 VarSet _ns;
99 /// Variables in off-tree factor which are not in the root of this subtree
100 VarSet _nsrem;
101 /// Used for calculating the free energy
102 Real _logZ;
103
104 public:
105 /// \name Constructors/destructors
106 //@{
107 /// Default constructor
108 TreeEPSubTree() : _Qa(), _Qb(), _RTree(), _a(), _b(), _I(NULL), _ns(), _nsrem(), _logZ(0.0) {}
109
110 /// Copy constructor
111 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) {}
112
113 /// Assignment operator
114 TreeEPSubTree & operator=( const TreeEPSubTree& x ) {
115 if( this != &x ) {
116 _Qa = x._Qa;
117 _Qb = x._Qb;
118 _RTree = x._RTree;
119 _a = x._a;
120 _b = x._b;
121 _I = x._I;
122 _ns = x._ns;
123 _nsrem = x._nsrem;
124 _logZ = x._logZ;
125 }
126 return *this;
127 }
128
129 /// Construct from \a subRTree, which is a subtree of the main tree \a jt_RTree, with distribution represented by \a jt_Qa and \a jt_Qb, for off-tree factor \a I
130 TreeEPSubTree( const RootedTree &subRTree, const RootedTree &jt_RTree, const std::vector<Factor> &jt_Qa, const std::vector<Factor> &jt_Qb, const Factor *I );
131 //@}
132
133 /// Initializes beliefs of this subtree
134 void init();
135
136 /// Inverts this approximation and multiplies it by the (super) junction tree marginals \a Qa and \a Qb
137 void InvertAndMultiply( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb );
138
139 /// Runs junction tree algorithm (including off-tree factor I) storing the results in the (super) junction tree \a Qa and \a Qb
140 void HUGIN_with_I( std::vector<Factor> &Qa, std::vector<Factor> &Qb );
141
142 /// Returns energy (?) of this subtree
143 Real logZ( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb ) const;
144
145 /// Returns constant reference to the pointer to the off-tree factor
146 const Factor *& I() { return _I; }
147 };
148
149 /// Stores a TreeEPSubTree object for each off-tree factor
150 std::map<size_t, TreeEPSubTree> _Q;
151
152 public:
153 /// Default constructor
154 TreeEP() : JTree(), _maxdiff(0.0), _iters(0), props(), _Q() {}
155
156 /// Copy constructor
157 TreeEP( const TreeEP &x ) : JTree(x), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props), _Q(x._Q) {
158 for( size_t I = 0; I < nrFactors(); I++ )
159 if( offtree( I ) )
160 _Q[I].I() = &factor(I);
161 }
162
163 /// Assignment operator
164 TreeEP& operator=( const TreeEP &x ) {
165 if( this != &x ) {
166 JTree::operator=( x );
167 _maxdiff = x._maxdiff;
168 _iters = x._iters;
169 props = x.props;
170 _Q = x._Q;
171 for( size_t I = 0; I < nrFactors(); I++ )
172 if( offtree( I ) )
173 _Q[I].I() = &factor(I);
174 }
175 return *this;
176 }
177
178 /// Construct from FactorGraph \a fg and PropertySet \a opts
179 /** \param fg Factor graph.
180 * \param opts Parameters @see Properties
181 */
182 TreeEP( const FactorGraph &fg, const PropertySet &opts );
183
184
185 /// \name General InfAlg interface
186 //@{
187 virtual TreeEP* clone() const { return new TreeEP(*this); }
188 virtual std::string identify() const;
189 virtual Real logZ() const;
190 virtual void init();
191 virtual void init( const VarSet &/*ns*/ ) { init(); }
192 virtual Real run();
193 virtual Real maxDiff() const { return _maxdiff; }
194 virtual size_t Iterations() const { return _iters; }
195 virtual void setProperties( const PropertySet &opts );
196 virtual PropertySet getProperties() const;
197 virtual std::string printProperties() const;
198 //@}
199
200 private:
201 /// Helper function for constructors
202 void construct( const FactorGraph& fg, const RootedTree& tree );
203 /// Returns \c true if factor \a I is not part of the tree
204 bool offtree( size_t I ) const { return (fac2OR(I) == -1U); }
205 };
206
207
208 } // end of namespace dai
209
210
211 #endif