libDAI version 0.3.2
[libdai.git] / include / dai / hak.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 /// \file
10 /// \brief Defines class HAK, which implements a variant of Generalized Belief Propagation.
11 /// \idea Implement more general region graphs and corresponding Generalized Belief Propagation updates as described in [\ref YFW05].
12 /// \todo Use ClusterGraph instead of a vector<VarSet> for speed.
13 /// \todo Optimize this code for large factor graphs.
14 /// \todo Implement GBP parent-child algorithm.
15
16
17 #include <dai/dai_config.h>
18 #ifdef DAI_WITH_HAK
19
20
21 #ifndef __defined_libdai_hak_h
22 #define __defined_libdai_hak_h
23
24
25 #include <string>
26 #include <dai/daialg.h>
27 #include <dai/regiongraph.h>
28 #include <dai/enum.h>
29 #include <dai/properties.h>
30
31
32 namespace dai {
33
34
35 /// Approximate inference algorithm: implementation of single-loop ("Generalized Belief Propagation") and double-loop algorithms by Heskes, Albers and Kappen [\ref HAK03]
36 class HAK : public DAIAlgRG {
37 private:
38 /// Outer region beliefs
39 std::vector<Factor> _Qa;
40 /// Inner region beliefs
41 std::vector<Factor> _Qb;
42 /// Messages from outer to inner regions
43 std::vector<std::vector<Factor> > _muab;
44 /// Messages from inner to outer regions
45 std::vector<std::vector<Factor> > _muba;
46 /// Maximum difference encountered so far
47 Real _maxdiff;
48 /// Number of iterations needed
49 size_t _iters;
50
51 public:
52 /// Parameters for HAK
53 struct Properties {
54 /// Enumeration of possible cluster choices
55 /** The following cluster choices are defined:
56 * - MIN minimal clusters, i.e., one outer region for each maximal factor
57 * - DELTA one outer region for each variable and its Markov blanket
58 * - LOOP one cluster for each loop of length at most \a Properties::loopdepth, and in addition one cluster for each maximal factor
59 * - BETHE Bethe approximation (one outer region for each maximal factor, inner regions are single variables)
60 */
61 DAI_ENUM(ClustersType,MIN,BETHE,DELTA,LOOP);
62
63 /// Enumeration of possible message initializations
64 DAI_ENUM(InitType,UNIFORM,RANDOM);
65
66 /// Verbosity (amount of output sent to stderr)
67 size_t verbose;
68
69 /// Maximum number of iterations
70 size_t maxiter;
71
72 /// Maximum time (in seconds)
73 double maxtime;
74
75 /// Tolerance for convergence test
76 Real tol;
77
78 /// Damping constant (0.0 means no damping, 1.0 is maximum damping)
79 Real damping;
80
81 /// How to choose the outer regions
82 ClustersType clusters;
83
84 /// How to initialize the messages
85 InitType init;
86
87 /// Use single-loop (GBP) or double-loop (HAK)
88 bool doubleloop;
89
90 /// Depth of loops (only relevant for \a clusters == \c ClustersType::LOOP)
91 size_t loopdepth;
92 } props;
93
94 public:
95 /// \name Constructors/destructors
96 //@{
97 /// Default constructor
98 HAK() : DAIAlgRG(), _Qa(), _Qb(), _muab(), _muba(), _maxdiff(0.0), _iters(0U), props() {}
99
100 /// Construct from FactorGraph \a fg and PropertySet \a opts
101 /** \param fg Factor graph.
102 * \param opts Parameters @see Properties
103 */
104 HAK( const FactorGraph &fg, const PropertySet &opts );
105
106 /// Construct from RegionGraph \a rg and PropertySet \a opts
107 HAK( const RegionGraph &rg, const PropertySet &opts );
108 //@}
109
110
111 /// \name General InfAlg interface
112 //@{
113 virtual HAK* clone() const { return new HAK(*this); }
114 virtual HAK* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new HAK( fg, opts ); }
115 virtual std::string name() const { return "HAK"; }
116 virtual Factor belief( const VarSet &vs ) const;
117 virtual std::vector<Factor> beliefs() const;
118 virtual Real logZ() const;
119 virtual void init();
120 virtual void init( const VarSet &vs );
121 virtual Real run();
122 virtual Real maxDiff() const { return _maxdiff; }
123 virtual size_t Iterations() const { return _iters; }
124 virtual void setMaxIter( size_t maxiter ) { props.maxiter = maxiter; }
125 virtual void setProperties( const PropertySet &opts );
126 virtual PropertySet getProperties() const;
127 virtual std::string printProperties() const;
128 //@}
129
130
131 /// \name Additional interface specific for HAK
132 //@{
133 /// Returns reference to message from outer region \a alpha to its \a _beta 'th neighboring inner region
134 Factor & muab( size_t alpha, size_t _beta ) { return _muab[alpha][_beta]; }
135 /// Returns reference to message the \a _beta 'th neighboring inner region of outer region \a alpha to that outer region
136 Factor & muba( size_t alpha, size_t _beta ) { return _muba[alpha][_beta]; }
137 /// Returns belief of outer region \a alpha
138 const Factor& Qa( size_t alpha ) const { return _Qa[alpha]; };
139 /// Returns belief of inner region \a beta
140 const Factor& Qb( size_t beta ) const { return _Qb[beta]; };
141
142 /// Runs single-loop algorithm (algorithm 1 in [\ref HAK03])
143 Real doGBP();
144 /// Runs double-loop algorithm (as described in section 4.2 of [\ref HAK03]), which always convergences
145 Real doDoubleLoop();
146 //@}
147
148 private:
149 /// Helper function for constructors
150 void construct();
151 /// Recursive procedure for finding clusters of variables containing loops of length at most \a length
152 /** \param fg the factor graph
153 * \param allcl the clusters found so far
154 * \param newcl partial candidate cluster
155 * \param root start (and end) point of the loop
156 * \param length number of variables that may be added to \a newcl
157 * \param vars neighboring variables of \a newcl
158 * \return allcl all clusters of variables with loops of length at most \a length passing through root
159 */
160 void findLoopClusters( const FactorGraph &fg, std::set<VarSet> &allcl, VarSet newcl, const Var & root, size_t length, VarSet vars );
161 };
162
163
164 } // end of namespace dai
165
166
167 #endif
168
169
170 #endif