00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00016
00017
00018 #ifndef __defined_libdai_treeep_h
00019 #define __defined_libdai_treeep_h
00020
00021
00022 #include <vector>
00023 #include <string>
00024 #include <dai/daialg.h>
00025 #include <dai/varset.h>
00026 #include <dai/regiongraph.h>
00027 #include <dai/factorgraph.h>
00028 #include <dai/clustergraph.h>
00029 #include <dai/weightedgraph.h>
00030 #include <dai/jtree.h>
00031 #include <dai/properties.h>
00032 #include <dai/enum.h>
00033
00034
00035 namespace dai {
00036
00037
00039 class TreeEP : public JTree {
00040 private:
00042 Real _maxdiff;
00044 size_t _iters;
00045
00046 public:
00048 struct Properties {
00050
00056 DAI_ENUM(TypeType,ORG,ALT);
00057
00059 size_t verbose;
00060
00062 size_t maxiter;
00063
00065 Real tol;
00066
00068 TypeType type;
00069 } props;
00070
00072 static const char *Name;
00073
00074 private:
00076
00080 class TreeEPSubTree {
00081 private:
00083 std::vector<Factor> _Qa;
00085 std::vector<Factor> _Qb;
00087 RootedTree _RTree;
00089 std::vector<size_t> _a;
00091 std::vector<size_t> _b;
00093 const Factor * _I;
00095 VarSet _ns;
00097 VarSet _nsrem;
00099 Real _logZ;
00100
00101 public:
00103
00104
00105 TreeEPSubTree() : _Qa(), _Qb(), _RTree(), _a(), _b(), _I(NULL), _ns(), _nsrem(), _logZ(0.0) {}
00106
00108 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) {}
00109
00111 TreeEPSubTree & operator=( const TreeEPSubTree& x ) {
00112 if( this != &x ) {
00113 _Qa = x._Qa;
00114 _Qb = x._Qb;
00115 _RTree = x._RTree;
00116 _a = x._a;
00117 _b = x._b;
00118 _I = x._I;
00119 _ns = x._ns;
00120 _nsrem = x._nsrem;
00121 _logZ = x._logZ;
00122 }
00123 return *this;
00124 }
00125
00127 TreeEPSubTree( const RootedTree &subRTree, const RootedTree &jt_RTree, const std::vector<Factor> &jt_Qa, const std::vector<Factor> &jt_Qb, const Factor *I );
00129
00131 void init();
00132
00134 void InvertAndMultiply( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb );
00135
00137 void HUGIN_with_I( std::vector<Factor> &Qa, std::vector<Factor> &Qb );
00138
00140 Real logZ( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb ) const;
00141
00143 const Factor *& I() { return _I; }
00144 };
00145
00147 std::map<size_t, TreeEPSubTree> _Q;
00148
00149 public:
00151 TreeEP() : JTree(), _maxdiff(0.0), _iters(0), props(), _Q() {}
00152
00154 TreeEP( const TreeEP &x ) : JTree(x), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props), _Q(x._Q) {
00155 for( size_t I = 0; I < nrFactors(); I++ )
00156 if( offtree( I ) )
00157 _Q[I].I() = &factor(I);
00158 }
00159
00161 TreeEP& operator=( const TreeEP &x ) {
00162 if( this != &x ) {
00163 JTree::operator=( x );
00164 _maxdiff = x._maxdiff;
00165 _iters = x._iters;
00166 props = x.props;
00167 _Q = x._Q;
00168 for( size_t I = 0; I < nrFactors(); I++ )
00169 if( offtree( I ) )
00170 _Q[I].I() = &factor(I);
00171 }
00172 return *this;
00173 }
00174
00176
00180 TreeEP( const FactorGraph &fg, const PropertySet &opts );
00181
00182
00184
00185 virtual TreeEP* clone() const { return new TreeEP(*this); }
00186 virtual std::string identify() const;
00187 virtual Real logZ() const;
00188 virtual void init();
00189 virtual void init( const VarSet & ) { init(); }
00190 virtual Real run();
00191 virtual Real maxDiff() const { return _maxdiff; }
00192 virtual size_t Iterations() const { return _iters; }
00193 virtual void setProperties( const PropertySet &opts );
00194 virtual PropertySet getProperties() const;
00195 virtual std::string printProperties() const;
00197
00198 private:
00200 void construct( const RootedTree &tree );
00202 bool offtree( size_t I ) const { return (fac2OR[I] == -1U); }
00203 };
00204
00205
00206 }
00207
00208
00209 #endif