1 /* This file is part of libDAI - http://www.libdai.org/
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
13 /// \brief Defines class TreeEP
14 /// \todo Improve documentation
17 #ifndef __defined_libdai_treeep_h
18 #define __defined_libdai_treeep_h
23 #include <dai/daialg.h>
24 #include <dai/varset.h>
25 #include <dai/regiongraph.h>
26 #include <dai/factorgraph.h>
27 #include <dai/clustergraph.h>
28 #include <dai/weightedgraph.h>
29 #include <dai/jtree.h>
30 #include <dai/properties.h>
37 /// Approximate inference algorithm "TreeEP" by Minka and Qi
38 class TreeEP
: public JTree
{
40 /// Maximum difference encountered so far
42 /// Number of iterations needed
46 /// Parameters of this inference algorithm
48 /// Enumeration of possible choices for the tree
49 DAI_ENUM(TypeType
,ORG
,ALT
)
54 /// Maximum number of iterations
60 /// How to choose the tree
62 } props
; // FIXME: should be props2 because of conflict with JTree::props?
64 /// Name of this inference method
65 static const char *Name
;
70 std::vector
<Factor
> _Qa
;
71 std::vector
<Factor
> _Qb
;
73 std::vector
<size_t> _a
; // _Qa[alpha] <-> superTree.Qa[_a[alpha]]
74 std::vector
<size_t> _b
; // _Qb[beta] <-> superTree.Qb[_b[beta]]
75 // _Qb[beta] <-> _RTree[beta]
83 TreeEPSubTree() : _Qa(), _Qb(), _RTree(), _a(), _b(), _I(NULL
), _ns(), _nsrem(), _logZ(0.0) {}
84 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
) {}
85 TreeEPSubTree
& operator=( const TreeEPSubTree
& x
) {
100 TreeEPSubTree( const DEdgeVec
&subRTree
, const DEdgeVec
&jt_RTree
, const std::vector
<Factor
> &jt_Qa
, const std::vector
<Factor
> &jt_Qb
, const Factor
*I
);
102 void InvertAndMultiply( const std::vector
<Factor
> &Qa
, const std::vector
<Factor
> &Qb
);
103 void HUGIN_with_I( std::vector
<Factor
> &Qa
, std::vector
<Factor
> &Qb
);
104 double logZ( const std::vector
<Factor
> &Qa
, const std::vector
<Factor
> &Qb
) const;
105 const Factor
*& I() { return _I
; }
108 std::map
<size_t, TreeEPSubTree
> _Q
;
111 /// Default constructor
112 TreeEP() : JTree(), _maxdiff(0.0), _iters(0), props(), _Q() {}
115 TreeEP( const TreeEP
&x
) : JTree(x
), _maxdiff(x
._maxdiff
), _iters(x
._iters
), props(x
.props
), _Q(x
._Q
) {
116 for( size_t I
= 0; I
< nrFactors(); I
++ )
118 _Q
[I
].I() = &factor(I
);
121 /// Assignment operator
122 TreeEP
& operator=( const TreeEP
&x
) {
124 JTree::operator=( x
);
125 _maxdiff
= x
._maxdiff
;
129 for( size_t I
= 0; I
< nrFactors(); I
++ )
131 _Q
[I
].I() = &factor(I
);
136 /// Construct from FactorGraph fg and PropertySet opts
137 TreeEP( const FactorGraph
&fg
, const PropertySet
&opts
);
140 /// @name General InfAlg interface
142 virtual TreeEP
* clone() const { return new TreeEP(*this); }
143 virtual std::string
identify() const;
144 virtual Real
logZ() const;
146 virtual void init( const VarSet
&/*ns*/ ) { init(); }
147 virtual double run();
148 virtual double maxDiff() const { return _maxdiff
; }
149 virtual size_t Iterations() const { return _iters
; }
153 /// @name Additional interface specific for TreeEP
158 void ConstructRG( const DEdgeVec
&tree
);
159 bool offtree( size_t I
) const { return (fac2OR
[I
] == -1U); }
161 void setProperties( const PropertySet
&opts
);
162 PropertySet
getProperties() const;
163 std::string
printProperties() const;
167 } // end of namespace dai