libDAI
treeep.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4  *
5  * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6  */
7 
8 
13 
14 
15 #ifndef __defined_libdai_treeep_h
16 #define __defined_libdai_treeep_h
17 
18 
19 #include <vector>
20 #include <string>
21 #include <dai/daialg.h>
22 #include <dai/varset.h>
23 #include <dai/regiongraph.h>
24 #include <dai/factorgraph.h>
25 #include <dai/clustergraph.h>
26 #include <dai/weightedgraph.h>
27 #include <dai/jtree.h>
28 #include <dai/properties.h>
29 #include <dai/enum.h>
30 
31 
32 namespace dai {
33 
34 
36 class TreeEP : public JTree {
37  private:
41  size_t _iters;
42 
43  public:
45  struct Properties {
47 
53  DAI_ENUM(TypeType,ORG,ALT);
54 
56  size_t verbose;
57 
59  size_t maxiter;
60 
62  double maxtime;
63 
66 
68  TypeType type;
69  } props;
70 
71  private:
73 
77  class TreeEPSubTree {
78  private:
80  std::vector<Factor> _Qa;
82  std::vector<Factor> _Qb;
86  std::vector<size_t> _a;
88  std::vector<size_t> _b;
90  const Factor * _I;
97 
98  public:
100 
101 
102  TreeEPSubTree() : _Qa(), _Qb(), _RTree(), _a(), _b(), _I(NULL), _ns(), _nsrem(), _logZ(0.0) {}
103 
105  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) {}
106 
109  if( this != &x ) {
110  _Qa = x._Qa;
111  _Qb = x._Qb;
112  _RTree = x._RTree;
113  _a = x._a;
114  _b = x._b;
115  _I = x._I;
116  _ns = x._ns;
117  _nsrem = x._nsrem;
118  _logZ = x._logZ;
119  }
120  return *this;
121  }
122 
124  TreeEPSubTree( const RootedTree &subRTree, const RootedTree &jt_RTree, const std::vector<Factor> &jt_Qa, const std::vector<Factor> &jt_Qb, const Factor *I );
126 
128  void init();
129 
131  void InvertAndMultiply( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb );
132 
134  void HUGIN_with_I( std::vector<Factor> &Qa, std::vector<Factor> &Qb );
135 
137  Real logZ( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb ) const;
138 
140  const Factor *& I() { return _I; }
141  };
142 
144  std::map<size_t, TreeEPSubTree> _Q;
145 
146  public:
148  TreeEP() : JTree(), _maxdiff(0.0), _iters(0), props(), _Q() {}
149 
151  TreeEP( const TreeEP &x ) : JTree(x), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props), _Q(x._Q) {
152  for( size_t I = 0; I < nrFactors(); I++ )
153  if( offtree( I ) )
154  _Q[I].I() = &factor(I);
155  }
156 
158  TreeEP& operator=( const TreeEP &x ) {
159  if( this != &x ) {
160  JTree::operator=( x );
161  _maxdiff = x._maxdiff;
162  _iters = x._iters;
163  props = x.props;
164  _Q = x._Q;
165  for( size_t I = 0; I < nrFactors(); I++ )
166  if( offtree( I ) )
167  _Q[I].I() = &factor(I);
168  }
169  return *this;
170  }
171 
173 
176  TreeEP( const FactorGraph &fg, const PropertySet &opts );
177 
178 
180 
181  virtual TreeEP* clone() const { return new TreeEP(*this); }
182  virtual TreeEP* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new TreeEP( fg, opts ); }
183  virtual std::string name() const { return "TREEEP"; }
184  virtual Real logZ() const;
185  virtual void init();
186  virtual void init( const VarSet &/*ns*/ ) { init(); }
187  virtual Real run();
188  virtual Real maxDiff() const { return _maxdiff; }
189  virtual size_t Iterations() const { return _iters; }
190  virtual void setMaxIter( size_t maxiter ) { props.maxiter = maxiter; }
191  virtual void setProperties( const PropertySet &opts );
192  virtual PropertySet getProperties() const;
193  virtual std::string printProperties() const;
195 
196  private:
198  void construct( const FactorGraph& fg, const RootedTree& tree );
200  bool offtree( size_t I ) const { return (fac2OR(I) == -1U); }
201 };
202 
203 
204 } // end of namespace dai
205 
206 
207 #endif