Oops, correct previous partial commit.
[libdai.git] / include / dai / hak.h
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 /// \file
24 /// \brief Defines class HAK.
25 /// \todo Improve documentation
26
27
28 #ifndef __defined_libdai_hak_h
29 #define __defined_libdai_hak_h
30
31
32 #include <string>
33 #include <dai/daialg.h>
34 #include <dai/regiongraph.h>
35 #include <dai/enum.h>
36 #include <dai/properties.h>
37
38
39 namespace dai {
40
41
42 /// Approximate inference algorithm: implementation of single-loop ("Generalized Belief Propagation") and double-loop algorithms by Heskes, Albers and Kappen
43 class HAK : public DAIAlgRG {
44 private:
45 std::vector<Factor> _Qa;
46 std::vector<Factor> _Qb;
47 std::vector<std::vector<Factor> > _muab;
48 std::vector<std::vector<Factor> > _muba;
49 /// Maximum difference encountered so far
50 double _maxdiff;
51 /// Number of iterations needed
52 size_t _iters;
53
54 public:
55 /// Parameters of this inference algorithm
56 struct Properties {
57 /// Enumeration of possible cluster choices
58 DAI_ENUM(ClustersType,MIN,DELTA,LOOP)
59
60 /// Verbosity
61 size_t verbose;
62
63 /// Maximum number of iterations
64 size_t maxiter;
65
66 /// Tolerance
67 double tol;
68
69 /// Damping constant
70 double damping;
71
72 /// How to choose the clusters
73 ClustersType clusters;
74
75 /// Use single-loop (GBP) or double-loop (HAK)
76 bool doubleloop;
77
78 /// Depth of loops (only relevant for clusters == ClustersType::LOOP)
79 size_t loopdepth;
80 } props;
81
82 /// Name of this inference algorithm
83 static const char *Name;
84
85 public:
86 /// Default constructor
87 HAK() : DAIAlgRG(), _Qa(), _Qb(), _muab(), _muba(), _maxdiff(0.0), _iters(0U), props() {}
88
89 /// Copy constructor
90 HAK( const HAK &x ) : DAIAlgRG(x), _Qa(x._Qa), _Qb(x._Qb), _muab(x._muab), _muba(x._muba), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props) {}
91
92 /// Assignment operator
93 HAK& operator=( const HAK &x ) {
94 if( this != &x ) {
95 DAIAlgRG::operator=( x );
96 _Qa = x._Qa;
97 _Qb = x._Qb;
98 _muab = x._muab;
99 _muba = x._muba;
100 _maxdiff = x._maxdiff;
101 _iters = x._iters;
102 props = x.props;
103 }
104 return *this;
105 }
106
107 /// Construct from FactorGraph fg and PropertySet opts
108 HAK( const FactorGraph &fg, const PropertySet &opts );
109
110 /// Construct from RegionGraph rg and PropertySet opts
111 HAK( const RegionGraph &rg, const PropertySet &opts );
112
113
114 /// @name General InfAlg interface
115 //@{
116 virtual HAK* clone() const { return new HAK(*this); }
117 virtual HAK* create() const { return new HAK(); }
118 virtual std::string identify() const;
119 virtual Factor belief( const Var &n ) const;
120 virtual Factor belief( const VarSet &ns ) const;
121 virtual std::vector<Factor> beliefs() const;
122 virtual Real logZ() const;
123 virtual void init();
124 virtual void init( const VarSet &ns );
125 virtual double run();
126 virtual double maxDiff() const { return _maxdiff; }
127 virtual size_t Iterations() const { return _iters; }
128 //@}
129
130
131 /// @name Additional interface specific for HAK
132 //@{
133 Factor & muab( size_t alpha, size_t _beta ) { return _muab[alpha][_beta]; }
134 Factor & muba( size_t alpha, size_t _beta ) { return _muba[alpha][_beta]; }
135 const Factor& Qa( size_t alpha ) const { return _Qa[alpha]; };
136 const Factor& Qb( size_t beta ) const { return _Qb[beta]; };
137
138 double doGBP();
139 double doDoubleLoop();
140 //@}
141
142 private:
143 void constructMessages();
144 void findLoopClusters( const FactorGraph &fg, std::set<VarSet> &allcl, VarSet newcl, const Var & root, size_t length, VarSet vars );
145
146 void setProperties( const PropertySet &opts );
147 PropertySet getProperties() const;
148 std::string printProperties() const;
149 };
150
151
152 } // end of namespace dai
153
154
155 #endif