e6bf12ffa45570267056045c299d37ac0537d1bf
[libdai.git] / include / dai / jtree.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_jtree_h
23 #define __defined_libdai_jtree_h
24
25
26 #include <vector>
27 #include <string>
28 #include <dai/daialg.h>
29 #include <dai/varset.h>
30 #include <dai/regiongraph.h>
31 #include <dai/factorgraph.h>
32 #include <dai/clustergraph.h>
33 #include <dai/weightedgraph.h>
34 #include <dai/enum.h>
35
36
37 namespace dai {
38
39
40 class JTree : public DAIAlgRG {
41 protected:
42 DEdgeVec _RTree; // rooted tree
43 std::vector<Factor> _Qa;
44 std::vector<Factor> _Qb;
45 std::vector<std::vector<Factor> > _mes;
46 double _logZ;
47
48
49 public:
50 ENUM2(UpdateType,HUGIN,SHSH)
51 UpdateType Updates() const { return GetPropertyAs<UpdateType>("updates"); }
52
53 JTree() : DAIAlgRG(), _RTree(), _Qa(), _Qb(), _mes(), _logZ() {};
54 JTree( const JTree& x ) : DAIAlgRG(x), _RTree(x._RTree), _Qa(x._Qa), _Qb(x._Qb), _mes(x._mes), _logZ(x._logZ) {};
55 JTree* clone() const { return new JTree(*this); }
56 JTree & operator=( const JTree& x ) {
57 if( this != &x ) {
58 DAIAlgRG::operator=(x);
59 _RTree = x._RTree;
60 _Qa = x._Qa;
61 _Qb = x._Qb;
62 _mes = x._mes;
63 _logZ = x._logZ;
64 }
65 return *this;
66 }
67 JTree( const FactorGraph &fg, const Properties &opts, bool automatic=true );
68 void GenerateJT( const std::vector<VarSet> &Cliques );
69
70 Factor & message( size_t alpha, size_t _beta ) { return _mes[alpha][_beta]; }
71 const Factor & message( size_t alpha, size_t _beta ) const { return _mes[alpha][_beta]; }
72
73 static const char *Name;
74 std::string identify() const;
75 void init() { assert( checkProperties() ); }
76 void runHUGIN();
77 void runShaferShenoy();
78 double run();
79 Factor belief( const Var &n ) const;
80 Factor belief( const VarSet &ns ) const;
81 std::vector<Factor> beliefs() const;
82 Complex logZ() const;
83
84 void init( const VarSet &/*ns*/ ) {}
85 void undoProbs( const VarSet &ns ) { RegionGraph::undoProbs( ns ); init( ns ); }
86
87 size_t findEfficientTree( const VarSet& ns, DEdgeVec &Tree, size_t PreviousRoot=(size_t)-1 ) const;
88 Factor calcMarginal( const VarSet& ns );
89 bool checkProperties();
90 };
91
92
93 } // end of namespace dai
94
95
96 #endif