1 /* This file is part of libDAI - http://www.libdai.org/
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.
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
13 /// \brief Defines class HAK.
14 /// \todo Improve documentation
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
32 /** \todo Optimize HAK with precalculated indices, similarly to BP.
34 class HAK
: public DAIAlgRG
{
36 std::vector
<Factor
> _Qa
;
37 std::vector
<Factor
> _Qb
;
38 std::vector
<std::vector
<Factor
> > _muab
;
39 std::vector
<std::vector
<Factor
> > _muba
;
40 /// Maximum difference encountered so far
42 /// Number of iterations needed
46 /// Parameters of this inference algorithm
48 /// Enumeration of possible cluster choices
49 DAI_ENUM(ClustersType
,MIN
,DELTA
,LOOP
);
51 /// Enumeration of possible message initializations
52 DAI_ENUM(InitType
,UNIFORM
,RANDOM
);
57 /// Maximum number of iterations
66 /// How to choose the clusters
67 ClustersType clusters
;
69 /// How to initialize the messages
72 /// Use single-loop (GBP) or double-loop (HAK)
75 /// Depth of loops (only relevant for clusters == ClustersType::LOOP)
79 /// Name of this inference algorithm
80 static const char *Name
;
83 /// Default constructor
84 HAK() : DAIAlgRG(), _Qa(), _Qb(), _muab(), _muba(), _maxdiff(0.0), _iters(0U), props() {}
86 /// Construct from FactorGraph fg and PropertySet opts
87 HAK( const FactorGraph
&fg
, const PropertySet
&opts
);
89 /// Construct from RegionGraph rg and PropertySet opts
90 HAK( const RegionGraph
&rg
, const PropertySet
&opts
);
93 /// @name General InfAlg interface
95 virtual HAK
* clone() const { return new HAK(*this); }
96 virtual std::string
identify() const;
97 virtual Factor
belief( const Var
&n
) const;
98 virtual Factor
belief( const VarSet
&ns
) const;
99 virtual std::vector
<Factor
> beliefs() const;
100 virtual Real
logZ() const;
102 virtual void init( const VarSet
&ns
);
104 virtual Real
maxDiff() const { return _maxdiff
; }
105 virtual size_t Iterations() const { return _iters
; }
109 /// @name Additional interface specific for HAK
111 Factor
& muab( size_t alpha
, size_t _beta
) { return _muab
[alpha
][_beta
]; }
112 Factor
& muba( size_t alpha
, size_t _beta
) { return _muba
[alpha
][_beta
]; }
113 const Factor
& Qa( size_t alpha
) const { return _Qa
[alpha
]; };
114 const Factor
& Qb( size_t beta
) const { return _Qb
[beta
]; };
121 void constructMessages();
122 void findLoopClusters( const FactorGraph
&fg
, std::set
<VarSet
> &allcl
, VarSet newcl
, const Var
& root
, size_t length
, VarSet vars
);
124 void setProperties( const PropertySet
&opts
);
125 PropertySet
getProperties() const;
126 std::string
printProperties() const;
130 } // end of namespace dai