112c8812ca0475e36cac3a33ebdd2cf33518a80d
[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 protected:
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
44 public:
45 struct Properties {
46 size_t verbose;
47 size_t maxiter;
48 double tol;
49 DAI_ENUM(ClustersType,MIN,DELTA,LOOP)
50 ClustersType clusters;
51 bool doubleloop;
52 size_t loopdepth;
53 } props;
54 double maxdiff;
55
56 public:
57 /// Default constructor
58 HAK() : DAIAlgRG(), _Qa(), _Qb(), _muab(), _muba(), props(), maxdiff() {}
59
60 /// Copy constructor
61 HAK(const HAK & x) : DAIAlgRG(x), _Qa(x._Qa), _Qb(x._Qb), _muab(x._muab), _muba(x._muba), props(x.props), maxdiff(x.maxdiff) {}
62
63 /// Clone function
64 HAK* clone() const { return new HAK(*this); }
65
66 /// Create (virtual constructor)
67 virtual HAK* create() const { return new HAK(); }
68
69 /// Construct from RegionGraph
70 HAK(const RegionGraph & rg, const PropertySet &opts);
71
72 /// Construct from RactorGraph using "clusters" option
73 HAK(const FactorGraph & fg, const PropertySet &opts);
74
75 /// Assignment operator
76 HAK & operator=(const HAK & x) {
77 if( this != &x ) {
78 DAIAlgRG::operator=(x);
79 _Qa = x._Qa;
80 _Qb = x._Qb;
81 _muab = x._muab;
82 _muba = x._muba;
83 props = x.props;
84 maxdiff = x.maxdiff;
85 }
86 return *this;
87 }
88
89 static const char *Name;
90
91 Factor & muab( size_t alpha, size_t _beta ) { return _muab[alpha][_beta]; }
92 Factor & muba( size_t alpha, size_t _beta ) { return _muba[alpha][_beta]; }
93 const Factor& Qa( size_t alpha ) const { return _Qa[alpha]; };
94 const Factor& Qb( size_t beta ) const { return _Qb[beta]; };
95
96 double doGBP();
97 double doDoubleLoop();
98 double run();
99 void init();
100 /// Clear messages and beliefs corresponding to the nodes in ns
101 virtual void init( const VarSet &ns );
102 std::string identify() const;
103 Factor belief( const Var &n ) const;
104 Factor belief( const VarSet &ns ) const;
105 std::vector<Factor> beliefs() const;
106 Real logZ () const;
107
108 void restoreFactors( const VarSet &ns ) { RegionGraph::restoreFactors( ns ); init( ns ); }
109 void setProperties( const PropertySet &opts );
110 PropertySet getProperties() const;
111 std::string printProperties() const;
112 double maxDiff() const { return maxdiff; }
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
119
120 } // end of namespace dai
121
122
123 #endif