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