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