b62a8f87a04c84806b6181c7b3ec5b630d1a4f30
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 #ifndef __defined_libdai_hak_h
18 #define __defined_libdai_hak_h
22 #include <dai/daialg.h>
23 #include <dai/regiongraph.h>
25 #include <dai/properties.h>
31 /// Approximate inference algorithm: implementation of single-loop ("Generalized Belief Propagation") and double-loop algorithms by Heskes, Albers and Kappen [\ref HAK03]
32 class HAK
: public DAIAlgRG
{
34 /// Outer region beliefs
35 std::vector
<Factor
> _Qa
;
36 /// Inner region beliefs
37 std::vector
<Factor
> _Qb
;
38 /// Messages from outer to inner regions
39 std::vector
<std::vector
<Factor
> > _muab
;
40 /// Messages from inner to outer regions
41 std::vector
<std::vector
<Factor
> > _muba
;
42 /// Maximum difference encountered so far
44 /// Number of iterations needed
48 /// Parameters for HAK
50 /// Enumeration of possible cluster choices
51 /** The following cluster choices are defined:
52 * - MIN minimal clusters, i.e., one outer region for each maximal factor
53 * - DELTA one outer region for each variable and its Markov blanket
54 * - LOOP one cluster for each loop of length at most \a Properties::loopdepth, and in addition one cluster for each maximal factor
55 * - BETHE Bethe approximation (one outer region for each maximal factor, inner regions are single variables)
57 DAI_ENUM(ClustersType
,MIN
,BETHE
,DELTA
,LOOP
);
59 /// Enumeration of possible message initializations
60 DAI_ENUM(InitType
,UNIFORM
,RANDOM
);
62 /// Verbosity (amount of output sent to stderr)
65 /// Maximum number of iterations
68 /// Maximum time (in seconds)
71 /// Tolerance for convergence test
74 /// Damping constant (0.0 means no damping, 1.0 is maximum damping)
77 /// How to choose the outer regions
78 ClustersType clusters
;
80 /// How to initialize the messages
83 /// Use single-loop (GBP) or double-loop (HAK)
86 /// Depth of loops (only relevant for \a clusters == \c ClustersType::LOOP)
91 /// \name Constructors/destructors
93 /// Default constructor
94 HAK() : DAIAlgRG(), _Qa(), _Qb(), _muab(), _muba(), _maxdiff(0.0), _iters(0U), props() {}
96 /// Construct from FactorGraph \a fg and PropertySet \a opts
97 /** \param fg Factor graph.
98 * \param opts Parameters @see Properties
100 HAK( const FactorGraph
&fg
, const PropertySet
&opts
);
102 /// Construct from RegionGraph \a rg and PropertySet \a opts
103 HAK( const RegionGraph
&rg
, const PropertySet
&opts
);
107 /// \name General InfAlg interface
109 virtual HAK
* clone() const { return new HAK(*this); }
110 virtual HAK
* construct( const FactorGraph
&fg
, const PropertySet
&opts
) const { return new HAK( fg
, opts
); }
111 virtual std::string
name() const { return "HAK"; }
112 virtual Factor
belief( const VarSet
&vs
) const;
113 virtual std::vector
<Factor
> beliefs() const;
114 virtual Real
logZ() const;
116 virtual void init( const VarSet
&vs
);
118 virtual Real
maxDiff() const { return _maxdiff
; }
119 virtual size_t Iterations() const { return _iters
; }
120 virtual void setMaxIter( size_t maxiter
) { props
.maxiter
= maxiter
; }
121 virtual void setProperties( const PropertySet
&opts
);
122 virtual PropertySet
getProperties() const;
123 virtual std::string
printProperties() const;
127 /// \name Additional interface specific for HAK
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
]; };
138 /// Runs single-loop algorithm (algorithm 1 in [\ref HAK03])
140 /// Runs double-loop algorithm (as described in section 4.2 of [\ref HAK03]), which always convergences
145 /// Helper function for constructors
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
156 void findLoopClusters( const FactorGraph
&fg
, std::set
<VarSet
> &allcl
, VarSet newcl
, const Var
& root
, size_t length
, VarSet vars
);
160 } // end of namespace dai