Replaced complex numbers by real numbers
[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
31
32 namespace dai {
33
34
35 /// HAK provides an implementation of the single and double-loop algorithms by Heskes, Albers and Kappen
36 class HAK : public DAIAlgRG {
37 protected:
38 std::vector<Factor> _Qa;
39 std::vector<Factor> _Qb;
40 std::vector<std::vector<Factor> > _muab;
41 std::vector<std::vector<Factor> > _muba;
42
43 public:
44 /// Default constructor
45 HAK() : DAIAlgRG() {};
46
47 /// Copy constructor
48 HAK(const HAK & x) : DAIAlgRG(x), _Qa(x._Qa), _Qb(x._Qb), _muab(x._muab), _muba(x._muba) {};
49
50 /// Clone function
51 HAK* clone() const { return new HAK(*this); }
52
53 /// Construct from RegionGraph
54 HAK(const RegionGraph & rg, const Properties &opts);
55
56 /// Construct from RactorGraph using "clusters" option
57 HAK(const FactorGraph & fg, const Properties &opts);
58
59 /// Assignment operator
60 HAK & operator=(const HAK & x) {
61 if( this != &x ) {
62 DAIAlgRG::operator=(x);
63 _Qa = x._Qa;
64 _Qb = x._Qb;
65 _muab = x._muab;
66 _muba = x._muba;
67 }
68 return *this;
69 }
70
71 static const char *Name;
72
73 ENUM3(ClustersType,MIN,DELTA,LOOP)
74 ClustersType Clusters() const { return GetPropertyAs<ClustersType>("clusters"); }
75 bool DoubleLoop() { return GetPropertyAs<bool>("doubleloop"); }
76 size_t LoopDepth() { return GetPropertyAs<size_t>("loopdepth"); }
77
78 Factor & muab( size_t alpha, size_t _beta ) { return _muab[alpha][_beta]; }
79 Factor & muba( size_t alpha, size_t _beta ) { return _muba[alpha][_beta]; }
80 const Factor& Qa( size_t alpha ) const { return _Qa[alpha]; };
81 const Factor& Qb( size_t beta ) const { return _Qb[beta]; };
82
83 double doGBP();
84 double doDoubleLoop();
85 double run();
86 void init();
87 std::string identify() const;
88 Factor belief( const Var &n ) const;
89 Factor belief( const VarSet &ns ) const;
90 std::vector<Factor> beliefs() const;
91 Real logZ () const;
92
93 void init( const VarSet &ns );
94 void undoProbs( const VarSet &ns ) { RegionGraph::undoProbs( ns ); init( ns ); }
95 bool checkProperties();
96
97 private:
98 void constructMessages();
99 void findLoopClusters( const FactorGraph &fg, std::set<VarSet> &allcl, VarSet newcl, const Var & root, size_t length, VarSet vars );
100 };
101
102
103 } // end of namespace dai
104
105
106 #endif