libDAI
trwbp.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 
11 
12 
13 #ifndef __defined_libdai_trwbp_h
14 #define __defined_libdai_trwbp_h
15 
16 
17 #include <dai/dai_config.h>
18 #ifdef DAI_WITH_TRWBP
19 
20 
21 #include <string>
22 #include <dai/daialg.h>
23 #include <dai/factorgraph.h>
24 #include <dai/properties.h>
25 #include <dai/enum.h>
26 #include <dai/bp.h>
27 
28 
29 namespace dai {
30 
31 
33 
52 class TRWBP : public BP {
53  protected:
55 
60  std::vector<Real> _weight;
61 
62  public:
64 
68  size_t nrtrees;
69 
70  public:
72 
73  TRWBP() : BP(), _weight() {}
75 
77 
82  TRWBP( const FactorGraph &fg, const PropertySet &opts ) : BP(fg, opts), _weight() {
83  setProperties( opts );
84  construct();
85  }
87 
89 
90  virtual TRWBP* clone() const { return new TRWBP(*this); }
91  virtual TRWBP* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new TRWBP( fg, opts ); }
92  virtual std::string name() const { return "TRWBP"; }
93  virtual Real logZ() const;
94  virtual void setProperties( const PropertySet &opts );
95  virtual PropertySet getProperties() const;
96  virtual std::string printProperties() const;
98 
100 
101  Real Weight( size_t I ) const { return _weight[I]; }
103 
105  const std::vector<Real>& Weights() const { return _weight; }
106 
108  void setWeight( size_t I, Real c ) { _weight[I] = c; }
109 
111 
113  void setWeights( const std::vector<Real> &c ) { _weight = c; }
114 
116  void addTreeToWeights( const RootedTree &tree );
117 
119  void sampleWeights( size_t nrTrees );
120 
121  protected:
123 
126  virtual Prob calcIncomingMessageProduct( size_t I, bool without_i, size_t i ) const;
127 
129  virtual void calcBeliefV( size_t i, Prob &p ) const;
130 
131  // Calculates unnormalized belief of factor \a I
132  virtual void calcBeliefF( size_t I, Prob &p ) const {
133  p = calcIncomingMessageProduct( I, false, 0 );
134  }
135 
136  // Helper function for constructors
137  virtual void construct();
138 };
139 
140 
141 } // end of namespace dai
142 
143 
144 #endif
145 
146 
147 #endif
Approximate inference algorithm "Tree-Reweighted Belief Propagation" [WJW03].
Definition: trwbp.h:52
void addTreeToWeights(const RootedTree &tree)
Increases weights corresponding to pairwise factors in tree with 1.
Definition: trwbp.cpp:151
virtual TRWBP * clone() const
Returns a pointer to a new, cloned copy of *this (i.e., virtual copy constructor) ...
Definition: trwbp.h:90
virtual Prob calcIncomingMessageProduct(size_t I, bool without_i, size_t i) const
Calculate the product of factor I and the incoming messages.
Definition: trwbp.cpp:71
Defines class BP, which implements (Loopy) Belief Propagation.
std::vector< Real > _weight
"Edge weights" (indexed by factor ID)
Definition: trwbp.h:60
void sampleWeights(size_t nrTrees)
Samples weights from a sample of nrTrees random spanning trees.
Definition: trwbp.cpp:160
virtual TRWBP * construct(const FactorGraph &fg, const PropertySet &opts) const
Returns a pointer to a newly constructed inference algorithm.
Definition: trwbp.h:91
virtual std::string name() const
Returns the name of the algorithm.
Definition: trwbp.h:92
Represents a factor graph.
Definition: factorgraph.h:65
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
TRWBP(const FactorGraph &fg, const PropertySet &opts)
Construct from FactorGraph fg and PropertySet opts.
Definition: trwbp.h:82
virtual void setProperties(const PropertySet &opts)
Definition: trwbp.cpp:25
virtual void calcBeliefV(size_t i, Prob &p) const
Calculates unnormalized belief of variable i.
Definition: trwbp.cpp:130
Defines the DAI_ENUM macro, which can be used to define an enum with additional functionality.
virtual Real logZ() const
Returns the logarithm of the (approximated) partition sum (normalizing constant of the factor graph)...
Definition: trwbp.cpp:53
size_t nrtrees
Size of sample of trees used to set the weights.
Definition: trwbp.h:68
void setWeights(const std::vector< Real > &c)
Sets the weights of all factors simultaenously.
Definition: trwbp.h:113
Defines the Property and PropertySet classes, which are mainly used for managing parameters of infere...
Approximate inference algorithm "(Loopy) Belief Propagation".
Definition: bp.h:63
void setWeight(size_t I, Real c)
Sets the weight of the I 'th factor to c.
Definition: trwbp.h:108
Represents a rooted tree, implemented as a vector of directed edges.
Definition: weightedgraph.h:157
Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue) ...
Definition: properties.h:73
TRWBP()
Default constructor.
Definition: trwbp.h:74
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.
virtual std::string printProperties() const
Returns parameters of this inference algorithm formatted as a string in the format "[key1=val1...
Definition: trwbp.cpp:42
Real Weight(size_t I) const
Returns weight corresponding to the I 'th factor.
Definition: trwbp.h:102
virtual void construct()
Helper function for constructors.
Definition: trwbp.cpp:142
const std::vector< Real > & Weights() const
Returns constant reference to vector of all weights.
Definition: trwbp.h:105
virtual void calcBeliefF(size_t I, Prob &p) const
Calculates unnormalized belief of factor I.
Definition: trwbp.h:132
virtual PropertySet getProperties() const
Returns parameters of this inference algorithm converted into a PropertySet.
Definition: trwbp.cpp:35