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