Cleaned up BBP and improved documentation of include/dai/bbp.h
[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
15
16
17 #ifndef __defined_libdai_treeep_h
18 #define __defined_libdai_treeep_h
19
20
21 #include <vector>
22 #include <string>
23 #include <dai/daialg.h>
24 #include <dai/varset.h>
25 #include <dai/regiongraph.h>
26 #include <dai/factorgraph.h>
27 #include <dai/clustergraph.h>
28 #include <dai/weightedgraph.h>
29 #include <dai/jtree.h>
30 #include <dai/properties.h>
31 #include <dai/enum.h>
32
33
34 namespace dai {
35
36
37 /// Approximate inference algorithm "Tree Expectation Propagation" [\ref MiQ04]
38 class TreeEP : public JTree {
39 private:
40 /// Maximum difference encountered so far
41 Real _maxdiff;
42 /// Number of iterations needed
43 size_t _iters;
44
45 public:
46 /// Parameters of this inference algorithm
47 struct Properties {
48 /// Enumeration of possible choices for the tree
49 /** The two possibilities are:
50 * - \c ORG: take the maximum spanning tree where the weights are crude
51 * estimates of the mutual information between the nodes;
52 * - \c ALT: take the maximum spanning tree where the weights are upper
53 * bounds on the effective interaction strengths between pairs of nodes.
54 */
55 DAI_ENUM(TypeType,ORG,ALT);
56
57 /// Verbosity
58 size_t verbose;
59
60 /// Maximum number of iterations
61 size_t maxiter;
62
63 /// Tolerance
64 Real tol;
65
66 /// How to choose the tree
67 TypeType type;
68 } props;
69
70 /// Name of this inference method
71 static const char *Name;
72
73 private:
74 /// Stores the data structures needed to efficiently update the approximation of an off-tree factor
75 class TreeEPSubTree {
76 private:
77 std::vector<Factor> _Qa;
78 std::vector<Factor> _Qb;
79 RootedTree _RTree;
80 std::vector<size_t> _a; // _Qa[alpha] <-> superTree.Qa[_a[alpha]]
81 std::vector<size_t> _b; // _Qb[beta] <-> superTree.Qb[_b[beta]]
82 // _Qb[beta] <-> _RTree[beta]
83 const Factor * _I;
84 VarSet _ns;
85 VarSet _nsrem;
86 Real _logZ;
87
88
89 public:
90 TreeEPSubTree() : _Qa(), _Qb(), _RTree(), _a(), _b(), _I(NULL), _ns(), _nsrem(), _logZ(0.0) {}
91 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) {}
92 TreeEPSubTree & operator=( const TreeEPSubTree& x ) {
93 if( this != &x ) {
94 _Qa = x._Qa;
95 _Qb = x._Qb;
96 _RTree = x._RTree;
97 _a = x._a;
98 _b = x._b;
99 _I = x._I;
100 _ns = x._ns;
101 _nsrem = x._nsrem;
102 _logZ = x._logZ;
103 }
104 return *this;
105 }
106
107 TreeEPSubTree( const RootedTree &subRTree, const RootedTree &jt_RTree, const std::vector<Factor> &jt_Qa, const std::vector<Factor> &jt_Qb, const Factor *I );
108 void init();
109 void InvertAndMultiply( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb );
110 void HUGIN_with_I( std::vector<Factor> &Qa, std::vector<Factor> &Qb );
111 Real logZ( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb ) const;
112 const Factor *& I() { return _I; }
113 };
114
115 /// Stores a TreeEPSubTree object for each off-tree factor
116 std::map<size_t, TreeEPSubTree> _Q;
117
118 public:
119 /// Default constructor
120 TreeEP() : JTree(), _maxdiff(0.0), _iters(0), props(), _Q() {}
121
122 /// Copy constructor
123 TreeEP( const TreeEP &x ) : JTree(x), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props), _Q(x._Q) {
124 for( size_t I = 0; I < nrFactors(); I++ )
125 if( offtree( I ) )
126 _Q[I].I() = &factor(I);
127 }
128
129 /// Assignment operator
130 TreeEP& operator=( const TreeEP &x ) {
131 if( this != &x ) {
132 JTree::operator=( x );
133 _maxdiff = x._maxdiff;
134 _iters = x._iters;
135 props = x.props;
136 _Q = x._Q;
137 for( size_t I = 0; I < nrFactors(); I++ )
138 if( offtree( I ) )
139 _Q[I].I() = &factor(I);
140 }
141 return *this;
142 }
143
144 /// Construct from FactorGraph \a fg and PropertySet \a opts
145 TreeEP( const FactorGraph &fg, const PropertySet &opts );
146
147
148 /// \name General InfAlg interface
149 //@{
150 virtual TreeEP* clone() const { return new TreeEP(*this); }
151 virtual std::string identify() const;
152 virtual Real logZ() const;
153 virtual void init();
154 virtual void init( const VarSet &/*ns*/ ) { init(); }
155 virtual Real run();
156 virtual Real maxDiff() const { return _maxdiff; }
157 virtual size_t Iterations() const { return _iters; }
158 virtual void setProperties( const PropertySet &opts );
159 virtual PropertySet getProperties() const;
160 virtual std::string printProperties() const;
161 //@}
162
163
164 private:
165 /// Helper function for constructors
166 void construct( const RootedTree &tree );
167 /// Returns \c true if factor \a I is not part of the tree
168 bool offtree( size_t I ) const { return (fac2OR[I] == -1U); }
169 };
170
171
172 } // end of namespace dai
173
174
175 #endif