1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
4 This file is part of libDAI.
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.
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.
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
22 #ifndef __defined_libdai_jtree_h
23 #define __defined_libdai_jtree_h
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>
35 #include <dai/properties.h>
41 class JTree
: public DAIAlgRG
{
43 std::vector
<std::vector
<Factor
> > _mes
;
47 DEdgeVec RTree
; // rooted tree
48 std::vector
<Factor
> Qa
;
49 std::vector
<Factor
> Qb
;
52 DAI_ENUM(UpdateType
,HUGIN
,SHSH
)
55 /// Name of this inference method
56 static const char *Name
;
59 /// Default constructor
60 JTree() : DAIAlgRG(), _mes(), _logZ(), RTree(), Qa(), Qb(), props() {}
62 /// Construct from FactorGraph fg and PropertySet opts
63 JTree( const FactorGraph
&fg
, const PropertySet
&opts
, bool automatic
=true );
66 JTree( const JTree
&x
) : DAIAlgRG(x
), _mes(x
._mes
), _logZ(x
._logZ
), RTree(x
.RTree
), Qa(x
.Qa
), Qb(x
.Qb
), props(x
.props
) {}
68 /// Clone *this (virtual copy constructor)
69 virtual JTree
* clone() const { return new JTree(*this); }
71 /// Create (virtual default constructor)
72 virtual JTree
* create() const { return new JTree(); }
74 /// Assignment operator
75 JTree
& operator=( const JTree
&x
) {
77 DAIAlgRG::operator=( x
);
88 /// Identifies itself for logging purposes
89 virtual std::string
identify() const;
91 /// Get single node belief
92 virtual Factor
belief( const Var
&n
) const;
94 /// Get general belief
95 virtual Factor
belief( const VarSet
&ns
) const;
98 virtual std::vector
<Factor
> beliefs() const;
100 /// Get log partition sum
101 virtual Real
logZ() const;
103 /// Clear messages and beliefs
104 virtual void init() {}
106 /// Clear messages and beliefs corresponding to the nodes in ns
107 virtual void init( const VarSet
&/*ns*/ ) {}
109 /// The actual approximate inference algorithm
110 virtual double run();
112 /// Return maximum difference between single node beliefs in the last pass
113 virtual double maxDiff() const { return 0.0; }
115 /// Return number of passes over the factorgraph
116 virtual size_t Iterations() const { return 1UL; }
119 void GenerateJT( const std::vector
<VarSet
> &Cliques
);
121 Factor
& message( size_t alpha
, size_t _beta
) { return _mes
[alpha
][_beta
]; }
122 const Factor
& message( size_t alpha
, size_t _beta
) const { return _mes
[alpha
][_beta
]; }
125 void runShaferShenoy();
126 size_t findEfficientTree( const VarSet
& ns
, DEdgeVec
&Tree
, size_t PreviousRoot
=(size_t)-1 ) const;
127 Factor
calcMarginal( const VarSet
& ns
);
129 void setProperties( const PropertySet
&opts
);
130 PropertySet
getProperties() const;
131 std::string
printProperties() const;
135 std::pair
<size_t,size_t> treewidth( const FactorGraph
& fg
);
138 } // end of namespace dai