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 <dai/dai_config.h>
20 #ifdef DAI_WITH_TREEEP
21 
22 
23 #include <vector>
24 #include <string>
25 #include <dai/daialg.h>
26 #include <dai/varset.h>
27 #include <dai/regiongraph.h>
28 #include <dai/factorgraph.h>
29 #include <dai/clustergraph.h>
30 #include <dai/weightedgraph.h>
31 #include <dai/jtree.h>
32 #include <dai/properties.h>
33 #include <dai/enum.h>
34 
35 
36 namespace dai {
37 
38 
40 class TreeEP : public JTree {
41  private:
45  size_t _iters;
46 
47  public:
49  struct Properties {
51 
57  DAI_ENUM(TypeType,ORG,ALT);
58 
60  size_t verbose;
61 
63  size_t maxiter;
64 
66  double maxtime;
67 
70 
72  TypeType type;
73  } props;
74 
75  private:
77 
81  class TreeEPSubTree {
82  private:
84  std::vector<Factor> _Qa;
86  std::vector<Factor> _Qb;
90  std::vector<size_t> _a;
92  std::vector<size_t> _b;
94  const Factor * _I;
101 
102  public:
104 
105  TreeEPSubTree() : _Qa(), _Qb(), _RTree(), _a(), _b(), _I(NULL), _ns(), _nsrem(), _logZ(0.0) {}
107 
109  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) {}
110 
113  if( this != &x ) {
114  _Qa = x._Qa;
115  _Qb = x._Qb;
116  _RTree = x._RTree;
117  _a = x._a;
118  _b = x._b;
119  _I = x._I;
120  _ns = x._ns;
121  _nsrem = x._nsrem;
122  _logZ = x._logZ;
123  }
124  return *this;
125  }
126 
128  TreeEPSubTree( const RootedTree &subRTree, const RootedTree &jt_RTree, const std::vector<Factor> &jt_Qa, const std::vector<Factor> &jt_Qb, const Factor *I );
130 
132  void init();
133 
135  void InvertAndMultiply( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb );
136 
138  void HUGIN_with_I( std::vector<Factor> &Qa, std::vector<Factor> &Qb );
139 
141  Real logZ( const std::vector<Factor> &Qa, const std::vector<Factor> &Qb ) const;
142 
144  const Factor *& I() { return _I; }
145  };
146 
148  std::map<size_t, TreeEPSubTree> _Q;
149 
150  public:
152  TreeEP() : JTree(), _maxdiff(0.0), _iters(0), props(), _Q() {}
153 
155  TreeEP( const TreeEP &x ) : JTree(x), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props), _Q(x._Q) {
156  for( size_t I = 0; I < nrFactors(); I++ )
157  if( offtree( I ) )
158  _Q[I].I() = &factor(I);
159  }
160 
162  TreeEP& operator=( const TreeEP &x ) {
163  if( this != &x ) {
164  JTree::operator=( x );
165  _maxdiff = x._maxdiff;
166  _iters = x._iters;
167  props = x.props;
168  _Q = x._Q;
169  for( size_t I = 0; I < nrFactors(); I++ )
170  if( offtree( I ) )
171  _Q[I].I() = &factor(I);
172  }
173  return *this;
174  }
175 
177 
180  TreeEP( const FactorGraph &fg, const PropertySet &opts );
181 
182 
184 
185  virtual TreeEP* clone() const { return new TreeEP(*this); }
186  virtual TreeEP* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new TreeEP( fg, opts ); }
187  virtual std::string name() const { return "TREEEP"; }
188  virtual Real logZ() const;
189  virtual void init();
190  virtual void init( const VarSet &/*ns*/ ) { init(); }
191  virtual Real run();
192  virtual Real maxDiff() const { return _maxdiff; }
193  virtual size_t Iterations() const { return _iters; }
194  virtual void setMaxIter( size_t maxiter ) { props.maxiter = maxiter; }
195  virtual void setProperties( const PropertySet &opts );
196  virtual PropertySet getProperties() const;
197  virtual std::string printProperties() const;
199 
200  private:
202  void construct( const FactorGraph& fg, const RootedTree& tree );
204  bool offtree( size_t I ) const { return (fac2OR(I) == -1U); }
205 };
206 
207 
208 } // end of namespace dai
209 
210 
211 #endif
212 
213 
214 #endif
virtual std::string name() const
Returns the name of the algorithm.
Definition: treeep.h:187
VarSet _nsrem
Variables in off-tree factor which are not in the root of this subtree.
Definition: treeep.h:98
virtual Real run()
Runs the approximate inference algorithm.
Definition: treeep.cpp:190
virtual void setProperties(const PropertySet &opts)
Set parameters of this inference algorithm.
Definition: treeep.cpp:27
virtual std::string printProperties() const
Returns parameters of this inference algorithm formatted as a string in the format "[key1=val1...
Definition: treeep.cpp:59
virtual Real maxDiff() const
Returns maximum difference between single variable beliefs in the last iteration. ...
Definition: treeep.h:192
Exact inference algorithm using junction tree.
Definition: jtree.h:45
Real logZ(const std::vector< Factor > &Qa, const std::vector< Factor > &Qb) const
Returns energy (?) of this subtree.
Definition: treeep.cpp:376
Approximate inference algorithm "Tree Expectation Propagation" [MiQ04].
Definition: treeep.h:40
size_t maxiter
Maximum number of iterations.
Definition: treeep.h:63
Defines some utility functions for (weighted) undirected graphs, trees and rooted trees...
Represents a factor graph.
Definition: factorgraph.h:65
Defines the VarSet class, which represents a set of random variables.
std::vector< Factor > Qa
Outer region beliefs.
Definition: jtree.h:58
virtual void init(const VarSet &)
Initializes all data structures corresponding to some set of variables.
Definition: treeep.h:190
FactorGraph & fg()
Returns reference to underlying FactorGraph.
Definition: daialg.h:221
double Real
Real number (alias for double, which could be changed to long double if necessary) ...
Definition: util.h:98
TreeEPSubTree(const TreeEPSubTree &x)
Copy constructor.
Definition: treeep.h:109
void HUGIN_with_I(std::vector< Factor > &Qa, std::vector< Factor > &Qb)
Runs junction tree algorithm (including off-tree factor I) storing the results in the (super) junctio...
Definition: treeep.cpp:318
bool offtree(size_t I) const
Returns true if factor I is not part of the tree.
Definition: treeep.h:204
TreeEP(const TreeEP &x)
Copy constructor.
Definition: treeep.h:155
virtual TreeEP * construct(const FactorGraph &fg, const PropertySet &opts) const
Returns a pointer to a newly constructed inference algorithm.
Definition: treeep.h:186
Real _logZ
Used for calculating the free energy.
Definition: treeep.h:100
virtual void init()
Initializes all data structures of the approximate inference algorithm.
Definition: treeep.cpp:180
const Factor *& I()
Returns constant reference to the pointer to the off-tree factor.
Definition: treeep.h:144
DAI_ENUM(TypeType, ORG, ALT)
Enumeration of possible choices for the tree.
virtual size_t Iterations() const
Returns number of iterations done (one iteration passes over the complete factorgraph).
Definition: treeep.h:193
TreeEPSubTree & operator=(const TreeEPSubTree &x)
Assignment operator.
Definition: treeep.h:112
Defines the DAI_ENUM macro, which can be used to define an enum with additional functionality.
TreeEPSubTree()
Default constructor.
Definition: treeep.h:106
std::vector< Factor > _Qb
Inner region pseudomarginals (corresponding with the in [MiQ04])
Definition: treeep.h:86
Represents a set of variables.
Definition: varset.h:94
std::vector< Factor > _Qa
Outer region pseudomarginals (corresponding with the in [MiQ04])
Definition: treeep.h:84
std::vector< size_t > _b
Index conversion table for inner region indices (_Qb[beta] corresponds with Qb[_b[beta]] of the super...
Definition: treeep.h:92
virtual void setMaxIter(size_t maxiter)
Sets maximum number of iterations (one iteration passes over the complete factorgraph).
Definition: treeep.h:194
Defines class JTree, which implements the junction tree algorithm.
RootedTree _RTree
The junction tree (stored as a rooted tree)
Definition: treeep.h:88
const Factor * _I
Pointer to off-tree factor.
Definition: treeep.h:94
VarSet _ns
Variables in off-tree factor.
Definition: treeep.h:96
std::vector< Factor > Qb
Inner region beliefs.
Definition: jtree.h:61
Defines class ClusterGraph, which is used by JTree, TreeEP and HAK.
Defines the Property and PropertySet classes, which are mainly used for managing parameters of infere...
Defines classes Region, FRegion and RegionGraph, which implement a particular subclass of region grap...
TypeType type
How to choose the tree.
Definition: treeep.h:72
std::map< size_t, TreeEPSubTree > _Q
Stores a TreeEPSubTree object for each off-tree factor.
Definition: treeep.h:148
Represents a rooted tree, implemented as a vector of directed edges.
Definition: weightedgraph.h:157
double maxtime
Maximum time (in seconds)
Definition: treeep.h:66
Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue) ...
Definition: properties.h:73
Parameters for TreeEP.
Definition: treeep.h:49
std::vector< size_t > _a
Index conversion table for outer region indices (_Qa[alpha] corresponds with Qa[_a[alpha]] of the sup...
Definition: treeep.h:90
void InvertAndMultiply(const std::vector< Factor > &Qa, const std::vector< Factor > &Qb)
Inverts this approximation and multiplies it by the (super) junction tree marginals Qa and Qb...
Definition: treeep.cpp:309
virtual Real logZ() const
Returns the logarithm of the (approximated) partition sum (normalizing constant of the factor graph)...
Definition: treeep.cpp:241
Namespace for libDAI.
Definition: alldai.cpp:16
Defines the FactorGraph class, which represents factor graphs (e.g., Bayesian networks or Markov rand...
Defines the general interface for inference methods in libDAI (classes InfAlg, DaiAlg<>, DaiAlgFG and DaiAlgRG).
Allows the user to specify which algorithms will be built into libDAI.
TreeEP & operator=(const TreeEP &x)
Assignment operator.
Definition: treeep.h:162
Real _maxdiff
Maximum difference encountered so far.
Definition: treeep.h:43
virtual PropertySet getProperties() const
Returns parameters of this inference algorithm converted into a PropertySet.
Definition: treeep.cpp:48
virtual TreeEP * clone() const
Returns a pointer to a new, cloned copy of *this (i.e., virtual copy constructor) ...
Definition: treeep.h:185
Stores the data structures needed to efficiently update the approximation of an off-tree factor...
Definition: treeep.h:81
void init()
Initializes beliefs of this subtree.
Definition: treeep.cpp:301
Real tol
Tolerance for convergence test.
Definition: treeep.h:69
TreeEP()
Default constructor.
Definition: treeep.h:152
size_t _iters
Number of iterations needed.
Definition: treeep.h:45
size_t verbose
Verbosity (amount of output sent to stderr)
Definition: treeep.h:60