Improved documentation of include/dai/hak.h
[libdai.git] / include / dai / hak.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 HAK, which implements a variant of Generalized Belief Propagation.
14
15
16 #ifndef __defined_libdai_hak_h
17 #define __defined_libdai_hak_h
18
19
20 #include <string>
21 #include <dai/daialg.h>
22 #include <dai/regiongraph.h>
23 #include <dai/enum.h>
24 #include <dai/properties.h>
25
26
27 namespace dai {
28
29
30 /// Approximate inference algorithm: implementation of single-loop ("Generalized Belief Propagation") and double-loop algorithms by Heskes, Albers and Kappen [\ref HAK03]
31 /** \todo Optimize HAK with precalculated indices, similarly to BP.
32 */
33 class HAK : public DAIAlgRG {
34 private:
35 /// Outer region beliefs
36 std::vector<Factor> _Qa;
37 /// Inner region beliefs
38 std::vector<Factor> _Qb;
39 /// Messages from outer to inner regions
40 std::vector<std::vector<Factor> > _muab;
41 /// Messages from inner to outer regions
42 std::vector<std::vector<Factor> > _muba;
43 /// Maximum difference encountered so far
44 Real _maxdiff;
45 /// Number of iterations needed
46 size_t _iters;
47
48 public:
49 /// Parameters of this inference algorithm
50 struct Properties {
51 /// Enumeration of possible cluster choices
52 DAI_ENUM(ClustersType,MIN,DELTA,LOOP);
53
54 /// Enumeration of possible message initializations
55 DAI_ENUM(InitType,UNIFORM,RANDOM);
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 /// Damping constant
67 Real damping;
68
69 /// How to choose the outer regions
70 ClustersType clusters;
71
72 /// How to initialize the messages
73 InitType init;
74
75 /// Use single-loop (GBP) or double-loop (HAK)
76 bool doubleloop;
77
78 /// Depth of loops (only relevant for \a clusters == \c ClustersType::LOOP)
79 size_t loopdepth;
80 } props;
81
82 /// Name of this inference algorithm
83 static const char *Name;
84
85 public:
86 /// \name Constructors/destructors
87 //@{
88 /// Default constructor
89 HAK() : DAIAlgRG(), _Qa(), _Qb(), _muab(), _muba(), _maxdiff(0.0), _iters(0U), props() {}
90
91 /// Construct from FactorGraph \a fg and PropertySet \a opts
92 /** The clusters are chosen according to \a opts["clusters"]:
93 * - MIN minimal clusters, i.e., one outer region for each maximal factor
94 * - DELTA one outer region for each variable and its Markov blanket
95 * - LOOP one cluster for each loop of length at most \a opts["loopdepth"], and in addition one cluster for each maximal factor of \a fg
96 */
97 HAK( const FactorGraph &fg, const PropertySet &opts );
98
99 /// Construct from RegionGraph \a rg and PropertySet \a opts
100 HAK( const RegionGraph &rg, const PropertySet &opts );
101 //@}
102
103
104 /// \name General InfAlg interface
105 //@{
106 virtual HAK* clone() const { return new HAK(*this); }
107 virtual std::string identify() const;
108 virtual Factor belief( const Var &v ) const;
109 virtual Factor belief( const VarSet &vs ) const;
110 virtual std::vector<Factor> beliefs() const;
111 virtual Real logZ() const;
112 virtual void init();
113 virtual void init( const VarSet &vs );
114 virtual Real run();
115 virtual Real maxDiff() const { return _maxdiff; }
116 virtual size_t Iterations() const { return _iters; }
117 virtual void setProperties( const PropertySet &opts );
118 virtual PropertySet getProperties() const;
119 virtual std::string printProperties() const;
120 //@}
121
122
123 /// \name Additional interface specific for HAK
124 //@{
125 /// Returns reference to message from outer region \a alpha to its \a _beta 'th neighboring inner region
126 Factor & muab( size_t alpha, size_t _beta ) { return _muab[alpha][_beta]; }
127 /// Returns reference to message the \a _beta 'th neighboring inner region of outer region \a alpha to that outer region
128 Factor & muba( size_t alpha, size_t _beta ) { return _muba[alpha][_beta]; }
129 /// Returns belief of outer region \a alpha
130 const Factor& Qa( size_t alpha ) const { return _Qa[alpha]; };
131 /// Returns belief of inner region \a beta
132 const Factor& Qb( size_t beta ) const { return _Qb[beta]; };
133
134 /// Runs single-loop algorithm (algorithm 1 in [\ref HAK03])
135 Real doGBP();
136 /// Runs double-loop algorithm (as described in section 4.2 of [\ref HAK03]), which always convergences
137 Real doDoubleLoop();
138 //@}
139
140 private:
141 /// Helper function for constructors
142 void construct();
143 /// Recursive procedure for finding clusters of variables containing loops of length at most \a length
144 /** \param fg the factor graph
145 * \param allcl the clusters found so far
146 * \param newcl partial candidate cluster
147 * \param root start (and end) point of the loop
148 * \param length number of variables that may be added to \a newcl
149 * \param vars neighboring variables of \a newcl
150 * \return allcl all clusters of variables with loops of length at most \a length passing through root
151 */
152 void findLoopClusters( const FactorGraph &fg, std::set<VarSet> &allcl, VarSet newcl, const Var & root, size_t length, VarSet vars );
153 };
154
155
156 } // end of namespace dai
157
158
159 #endif