20c9dd57c38f8786c053d74bb04c8df9e60955f3
[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 #include <dai/properties.h>
36
37
38 namespace dai {
39
40
41 class JTree : public DAIAlgRG {
42 protected:
43 DEdgeVec _RTree; // rooted tree
44 std::vector<Factor> _Qa;
45 std::vector<Factor> _Qb;
46 std::vector<std::vector<Factor> > _mes;
47 double _logZ;
48
49 public:
50 struct Properties {
51 size_t verbose;
52 DAI_ENUM(UpdateType,HUGIN,SHSH)
53 UpdateType updates;
54 } props;
55
56 public:
57 JTree() : DAIAlgRG(), _RTree(), _Qa(), _Qb(), _mes(), _logZ(), props() {}
58 JTree( const JTree& x ) : DAIAlgRG(x), _RTree(x._RTree), _Qa(x._Qa), _Qb(x._Qb), _mes(x._mes), _logZ(x._logZ), props(x.props) {}
59 JTree* clone() const { return new JTree(*this); }
60 /// Create (virtual constructor)
61 virtual JTree* create() const { return new JTree(); }
62 JTree & operator=( const JTree& x ) {
63 if( this != &x ) {
64 DAIAlgRG::operator=(x);
65 _RTree = x._RTree;
66 _Qa = x._Qa;
67 _Qb = x._Qb;
68 _mes = x._mes;
69 _logZ = x._logZ;
70 props = x.props;
71 }
72 return *this;
73 }
74 JTree( const FactorGraph &fg, const PropertySet &opts, bool automatic=true );
75 void GenerateJT( const std::vector<VarSet> &Cliques );
76
77 Factor & message( size_t alpha, size_t _beta ) { return _mes[alpha][_beta]; }
78 const Factor & message( size_t alpha, size_t _beta ) const { return _mes[alpha][_beta]; }
79
80 static const char *Name;
81 std::string identify() const;
82 void init() {}
83 /// Clear messages and beliefs corresponding to the nodes in ns
84 virtual void init( const VarSet &/*ns*/ ) {}
85 void runHUGIN();
86 void runShaferShenoy();
87 double run();
88 Factor belief( const Var &n ) const;
89 Factor belief( const VarSet &ns ) const;
90 std::vector<Factor> beliefs() const;
91 Real logZ() const;
92
93 void restoreFactors( const VarSet &ns ) { RegionGraph::restoreFactors( ns ); init( ns ); }
94
95 size_t findEfficientTree( const VarSet& ns, DEdgeVec &Tree, size_t PreviousRoot=(size_t)-1 ) const;
96 Factor calcMarginal( const VarSet& ns );
97 void setProperties( const PropertySet &opts );
98 PropertySet getProperties() const;
99 std::string printProperties() const;
100 double maxDiff() const { return 0.0; }
101 };
102
103
104 } // end of namespace dai
105
106
107 #endif