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