Adopted contributions by Christian.
[libdai.git] / treeep.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 __TREEEP_H__
23 #define __TREEEP_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 "jtree.h"
34 #include "enum.h"
35
36
37 namespace dai {
38
39
40 using namespace std;
41
42
43 class TreeEPSubTree {
44 protected:
45 vector<Factor> _Qa;
46 vector<Factor> _Qb;
47 DEdgeVec _RTree;
48 vector<size_t> _a; // _Qa[alpha] <-> superTree._Qa[_a[alpha]]
49 vector<size_t> _b; // _Qb[beta] <-> superTree._Qb[_b[beta]]
50 // _Qb[beta] <-> _RTree[beta]
51 const Factor * _I;
52 VarSet _ns;
53 VarSet _nsrem;
54 double _logZ;
55
56
57 public:
58 TreeEPSubTree() : _Qa(), _Qb(), _RTree(), _a(), _b(), _I(NULL), _ns(), _nsrem(), _logZ(0.0) {}
59 TreeEPSubTree( const TreeEPSubTree &x) : _Qa(x._Qa), _Qb(x._Qb), _RTree(x._RTree), _a(x._a), _b(x._b), _I(x._I), _ns(x._ns), _nsrem(x._nsrem), _logZ(x._logZ) {}
60 TreeEPSubTree & operator=( const TreeEPSubTree& x ) {
61 if( this != &x ) {
62 _Qa = x._Qa;
63 _Qb = x._Qb;
64 _RTree = x._RTree;
65 _a = x._a;
66 _b = x._b;
67 _I = x._I;
68 _ns = x._ns;
69 _nsrem = x._nsrem;
70 _logZ = x._logZ;
71 }
72 return *this;
73 }
74
75 TreeEPSubTree( const DEdgeVec &subRTree, const DEdgeVec &jt_RTree, const vector<Factor> &jt_Qa, const vector<Factor> &jt_Qb, const Factor *I );
76 void init();
77 void InvertAndMultiply( const vector<Factor> &Qa, const vector<Factor> &Qb );
78 void HUGIN_with_I( vector<Factor> &Qa, vector<Factor> &Qb );
79 double logZ( const vector<Factor> &Qa, const vector<Factor> &Qb ) const;
80 const Factor *& I() { return _I; }
81 };
82
83
84 class TreeEP : public JTree {
85 protected:
86 map<size_t, TreeEPSubTree> _Q;
87
88 public:
89 ENUM2(TypeType,ORG,ALT)
90 TypeType Type() const { return GetPropertyAs<TypeType>("type"); }
91
92 TreeEP() : JTree(), _Q() {};
93 TreeEP( const TreeEP& x ) : JTree(x), _Q(x._Q) {
94 for( size_t I = 0; I < nrFactors(); I++ )
95 if( offtree( I ) )
96 _Q[I].I() = &factor(I);
97 }
98 TreeEP* clone() const { return new TreeEP(*this); }
99 TreeEP & operator=( const TreeEP& x ) {
100 if( this != &x ) {
101 JTree::operator=(x);
102 _Q = x._Q;
103 for( size_t I = 0; I < nrFactors(); I++ )
104 if( offtree( I ) )
105 _Q[I].I() = &factor(I);
106 }
107 return *this;
108 }
109 TreeEP( const FactorGraph &fg, const Properties &opts );
110 void ConstructRG( const DEdgeVec &tree );
111
112 static const char *Name;
113 string identify() const;
114 void init();
115 double run();
116 Complex logZ() const;
117
118 bool offtree(size_t I) const { return !_fac2OR.count(I); }
119
120 void init( const VarSet &ns ) { init(); }
121 void undoProbs( const VarSet &ns ) { RegionGraph::undoProbs( ns ); init( ns ); }
122 bool checkProperties();
123 };
124
125
126 }
127
128
129 #endif