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