1 /* This file is part of libDAI - http://www.libdai.org/
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE 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.
17 #include <dai/dai_config.h>
21 #ifndef __defined_libdai_hak_h
22 #define __defined_libdai_hak_h
26 #include <dai/daialg.h>
27 #include <dai/regiongraph.h>
29 #include <dai/properties.h>
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
{
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
48 /// Number of iterations needed
52 /// Parameters for HAK
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)
61 DAI_ENUM(ClustersType
,MIN
,BETHE
,DELTA
,LOOP
);
63 /// Enumeration of possible message initializations
64 DAI_ENUM(InitType
,UNIFORM
,RANDOM
);
66 /// Verbosity (amount of output sent to stderr)
69 /// Maximum number of iterations
72 /// Maximum time (in seconds)
75 /// Tolerance for convergence test
78 /// Damping constant (0.0 means no damping, 1.0 is maximum damping)
81 /// How to choose the outer regions
82 ClustersType clusters
;
84 /// How to initialize the messages
87 /// Use single-loop (GBP) or double-loop (HAK)
90 /// Depth of loops (only relevant for \a clusters == \c ClustersType::LOOP)
95 /// \name Constructors/destructors
97 /// Default constructor
98 HAK() : DAIAlgRG(), _Qa(), _Qb(), _muab(), _muba(), _maxdiff(0.0), _iters(0U), props() {}
100 /// Construct from FactorGraph \a fg and PropertySet \a opts
101 /** \param fg Factor graph.
102 * \param opts Parameters @see Properties
104 HAK( const FactorGraph
&fg
, const PropertySet
&opts
);
106 /// Construct from RegionGraph \a rg and PropertySet \a opts
107 HAK( const RegionGraph
&rg
, const PropertySet
&opts
);
111 /// \name General InfAlg interface
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;
120 virtual void init( const VarSet
&vs
);
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;
131 /// \name Additional interface specific for HAK
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
]; };
142 /// Runs single-loop algorithm (algorithm 1 in [\ref HAK03])
144 /// Runs double-loop algorithm (as described in section 4.2 of [\ref HAK03]), which always convergences
149 /// Helper function for constructors
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
160 void findLoopClusters( const FactorGraph
&fg
, std::set
<VarSet
> &allcl
, VarSet newcl
, const Var
& root
, size_t length
, VarSet vars
);
164 } // end of namespace dai