e62b7f670d42000feae0ec4be54db9cb0efe9c23
[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-2009 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.
14 /// \todo Improve documentation
15
16
17 #ifndef __defined_libdai_hak_h
18 #define __defined_libdai_hak_h
19
20
21 #include <string>
22 #include <dai/daialg.h>
23 #include <dai/regiongraph.h>
24 #include <dai/enum.h>
25 #include <dai/properties.h>
26
27
28 namespace dai {
29
30
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.
33 */
34 class HAK : public DAIAlgRG {
35 private:
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
41 double _maxdiff;
42 /// Number of iterations needed
43 size_t _iters;
44
45 public:
46 /// Parameters of this inference algorithm
47 struct Properties {
48 /// Enumeration of possible cluster choices
49 DAI_ENUM(ClustersType,MIN,DELTA,LOOP)
50
51 /// Verbosity
52 size_t verbose;
53
54 /// Maximum number of iterations
55 size_t maxiter;
56
57 /// Tolerance
58 double tol;
59
60 /// Damping constant
61 double damping;
62
63 /// How to choose the clusters
64 ClustersType clusters;
65
66 /// Use single-loop (GBP) or double-loop (HAK)
67 bool doubleloop;
68
69 /// Depth of loops (only relevant for clusters == ClustersType::LOOP)
70 size_t loopdepth;
71 } props;
72
73 /// Name of this inference algorithm
74 static const char *Name;
75
76 public:
77 /// Default constructor
78 HAK() : DAIAlgRG(), _Qa(), _Qb(), _muab(), _muba(), _maxdiff(0.0), _iters(0U), props() {}
79
80 /// Construct from FactorGraph fg and PropertySet opts
81 HAK( const FactorGraph &fg, const PropertySet &opts );
82
83 /// Construct from RegionGraph rg and PropertySet opts
84 HAK( const RegionGraph &rg, const PropertySet &opts );
85
86
87 /// @name General InfAlg interface
88 //@{
89 virtual HAK* clone() const { return new HAK(*this); }
90 virtual std::string identify() const;
91 virtual Factor belief( const Var &n ) const;
92 virtual Factor belief( const VarSet &ns ) const;
93 virtual std::vector<Factor> beliefs() const;
94 virtual Real logZ() const;
95 virtual void init();
96 virtual void init( const VarSet &ns );
97 virtual double run();
98 virtual double maxDiff() const { return _maxdiff; }
99 virtual size_t Iterations() const { return _iters; }
100 //@}
101
102
103 /// @name Additional interface specific for HAK
104 //@{
105 Factor & muab( size_t alpha, size_t _beta ) { return _muab[alpha][_beta]; }
106 Factor & muba( size_t alpha, size_t _beta ) { return _muba[alpha][_beta]; }
107 const Factor& Qa( size_t alpha ) const { return _Qa[alpha]; };
108 const Factor& Qb( size_t beta ) const { return _Qb[beta]; };
109
110 double doGBP();
111 double doDoubleLoop();
112 //@}
113
114 private:
115 void constructMessages();
116 void findLoopClusters( const FactorGraph &fg, std::set<VarSet> &allcl, VarSet newcl, const Var & root, size_t length, VarSet vars );
117
118 void setProperties( const PropertySet &opts );
119 PropertySet getProperties() const;
120 std::string printProperties() const;
121 };
122
123
124 } // end of namespace dai
125
126
127 #endif