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 double maxtime;
00066
00068 Real tol;
00069
00071 TypeType type;
00072 } props;
00073
00075 static const char *Name;
00076
00077 private:
00079
00083 class TreeEPSubTree {
00084 private:
00086 std::vector<Factor> _Qa;
00088 std::vector<Factor> _Qb;
00090 RootedTree _RTree;
00092 std::vector<size_t> _a;
00094 std::vector<size_t> _b;
00096 const Factor * _I;
00098 VarSet _ns;
00100 VarSet _nsrem;
00102 Real _logZ;
00103
00104 public:
00106
00107
00108 TreeEPSubTree() : _Qa(), _Qb(), _RTree(), _a(), _b(), _I(NULL), _ns(), _nsrem(), _logZ(0.0) {}
00109
00111 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) {}
00112
00114 TreeEPSubTree & operator=( const TreeEPSubTree& x ) {
00115 if( this != &x ) {
00116 _Qa = x._Qa;
00117 _Qb = x._Qb;
00118 _RTree = x._RTree;
00119 _a = x._a;
00120 _b = x._b;
00121 _I = x._I;
00122 _ns = x._ns;
00123 _nsrem = x._nsrem;
00124 _logZ = x._logZ;
00125 }
00126 return *this;
00127 }
00128
00130 TreeEPSubTree( const RootedTree &subRTree, const RootedTree &jt_RTree, const std::vector<Factor> &jt_Qa, const std::vector<Factor> &jt_Qb, const Factor *I );
00132
00134 void init();
00135
00137 void InvertAndMultiply( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb );
00138
00140 void HUGIN_with_I( std::vector<Factor> &Qa, std::vector<Factor> &Qb );
00141
00143 Real logZ( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb ) const;
00144
00146 const Factor *& I() { return _I; }
00147 };
00148
00150 std::map<size_t, TreeEPSubTree> _Q;
00151
00152 public:
00154 TreeEP() : JTree(), _maxdiff(0.0), _iters(0), props(), _Q() {}
00155
00157 TreeEP( const TreeEP &x ) : JTree(x), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props), _Q(x._Q) {
00158 for( size_t I = 0; I < nrFactors(); I++ )
00159 if( offtree( I ) )
00160 _Q[I].I() = &factor(I);
00161 }
00162
00164 TreeEP& operator=( const TreeEP &x ) {
00165 if( this != &x ) {
00166 JTree::operator=( x );
00167 _maxdiff = x._maxdiff;
00168 _iters = x._iters;
00169 props = x.props;
00170 _Q = x._Q;
00171 for( size_t I = 0; I < nrFactors(); I++ )
00172 if( offtree( I ) )
00173 _Q[I].I() = &factor(I);
00174 }
00175 return *this;
00176 }
00177
00179
00182 TreeEP( const FactorGraph &fg, const PropertySet &opts );
00183
00184
00186
00187 virtual TreeEP* clone() const { return new TreeEP(*this); }
00188 virtual std::string identify() const;
00189 virtual Real logZ() const;
00190 virtual void init();
00191 virtual void init( const VarSet & ) { init(); }
00192 virtual Real run();
00193 virtual Real maxDiff() const { return _maxdiff; }
00194 virtual size_t Iterations() const { return _iters; }
00195 virtual void setMaxIter( size_t maxiter ) { props.maxiter = maxiter; }
00196 virtual void setProperties( const PropertySet &opts );
00197 virtual PropertySet getProperties() const;
00198 virtual std::string printProperties() const;
00200
00201 private:
00203 void construct( const FactorGraph& fg, const RootedTree& tree );
00205 bool offtree( size_t I ) const { return (fac2OR(I) == -1U); }
00206 };
00207
00208
00209 }
00210
00211
00212 #endif