Small changes
[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/properties.h>
36 #include <dai/enum.h>
37
38
39 namespace dai {
40
41
42 class TreeEPSubTree {
43 protected:
44 std::vector<Factor> _Qa;
45 std::vector<Factor> _Qb;
46 DEdgeVec _RTree;
47 std::vector<size_t> _a; // _Qa[alpha] <-> superTree._Qa[_a[alpha]]
48 std::vector<size_t> _b; // _Qb[beta] <-> superTree._Qb[_b[beta]]
49 // _Qb[beta] <-> _RTree[beta]
50 const Factor * _I;
51 VarSet _ns;
52 VarSet _nsrem;
53 double _logZ;
54
55
56 public:
57 TreeEPSubTree() : _Qa(), _Qb(), _RTree(), _a(), _b(), _I(NULL), _ns(), _nsrem(), _logZ(0.0) {}
58 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) {}
59 TreeEPSubTree & operator=( const TreeEPSubTree& x ) {
60 if( this != &x ) {
61 _Qa = x._Qa;
62 _Qb = x._Qb;
63 _RTree = x._RTree;
64 _a = x._a;
65 _b = x._b;
66 _I = x._I;
67 _ns = x._ns;
68 _nsrem = x._nsrem;
69 _logZ = x._logZ;
70 }
71 return *this;
72 }
73
74 TreeEPSubTree( const DEdgeVec &subRTree, const DEdgeVec &jt_RTree, const std::vector<Factor> &jt_Qa, const std::vector<Factor> &jt_Qb, const Factor *I );
75 void init();
76 void InvertAndMultiply( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb );
77 void HUGIN_with_I( std::vector<Factor> &Qa, std::vector<Factor> &Qb );
78 double logZ( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb ) const;
79 const Factor *& I() { return _I; }
80 };
81
82
83 class TreeEP : public JTree {
84 protected:
85 std::map<size_t, TreeEPSubTree> _Q;
86
87 public:
88 struct Properties {
89 size_t verbose;
90 size_t maxiter;
91 double tol;
92 DAI_ENUM(TypeType,ORG,ALT)
93 TypeType type;
94 } props;
95 double maxdiff;
96
97 public:
98 /// Default constructor
99 TreeEP() : JTree(), _Q(), props(), maxdiff() {};
100 /// Copy constructor
101 TreeEP( const TreeEP& x ) : JTree(x), _Q(x._Q), props(x.props), maxdiff(x.maxdiff) {
102 for( size_t I = 0; I < nrFactors(); I++ )
103 if( offtree( I ) )
104 _Q[I].I() = &factor(I);
105 }
106 TreeEP* clone() const { return new TreeEP(*this); }
107 TreeEP & operator=( const TreeEP& x ) {
108 if( this != &x ) {
109 JTree::operator=(x);
110 _Q = x._Q;
111 for( size_t I = 0; I < nrFactors(); I++ )
112 if( offtree( I ) )
113 _Q[I].I() = &factor(I);
114 props = x.props;
115 maxdiff = x.maxdiff;
116 }
117 return *this;
118 }
119 TreeEP( const FactorGraph &fg, const PropertySet &opts );
120 void ConstructRG( const DEdgeVec &tree );
121
122 static const char *Name;
123 std::string identify() const;
124 void init();
125 double run();
126 Real logZ() const;
127
128 bool offtree( size_t I ) const { return (fac2OR[I] == -1U); }
129
130 void init( const VarSet &/*ns*/ ) { init(); }
131 void undoProbs( const VarSet &ns ) { RegionGraph::undoProbs( ns ); init( ns ); }
132
133 void setProperties( const PropertySet &opts );
134 PropertySet getProperties() const;
135 double maxDiff() const { return maxdiff; }
136 };
137
138
139 } // end of namespace dai
140
141
142 #endif