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