libDAI
fbp.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_fbp_h
14 #define __defined_libdai_fbp_h
15 
16 
17 #include <dai/dai_config.h>
18 #ifdef DAI_WITH_FBP
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 
59 class FBP : public BP {
60  protected:
62  std::vector<Real> _weight;
63 
64  public:
66 
67  FBP() : BP(), _weight() {}
69 
71 
74  FBP( const FactorGraph &fg, const PropertySet &opts ) : BP(fg, opts), _weight() {
75  setProperties( opts );
76  construct();
77  }
79 
81 
82  virtual FBP* clone() const { return new FBP(*this); }
83  virtual FBP* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new FBP( fg, opts ); }
84  virtual std::string name() const { return "FBP"; }
85  virtual Real logZ() const;
87 
89 
90  Real Weight( size_t I ) const { return _weight[I]; }
92 
94  const std::vector<Real>& Weights() const { return _weight; }
95 
97  void setWeight( size_t I, Real c ) { _weight[I] = c; }
98 
100 
102  void setWeights( const std::vector<Real> &c ) { _weight = c; }
103 
104  protected:
106 
109  virtual Prob calcIncomingMessageProduct( size_t I, bool without_i, size_t i ) const;
110 
111  // Calculate the updated message from the \a _I 'th neighbor of variable \a i to variable \a i
112  virtual void calcNewMessage( size_t i, size_t _I );
113 
114  // Calculates unnormalized belief of factor \a I
115  virtual void calcBeliefF( size_t I, Prob &p ) const {
116  p = calcIncomingMessageProduct( I, false, 0 );
117  }
118 
119  // Helper function for constructors
120  virtual void construct();
121 };
122 
123 
124 } // end of namespace dai
125 
126 
127 #endif
128 
129 
130 #endif
FBP(const FactorGraph &fg, const PropertySet &opts)
Construct from FactorGraph fg and PropertySet opts.
Definition: fbp.h:74
virtual std::string name() const
Returns the name of the algorithm.
Definition: fbp.h:84
Defines class BP, which implements (Loopy) Belief Propagation.
virtual void construct()
Helper function for constructors.
Definition: fbp.cpp:154
void setWeights(const std::vector< Real > &c)
Sets the weights of all factors simultaenously.
Definition: fbp.h:102
Approximate inference algorithm "Fractional Belief Propagation" [WiH03].
Definition: fbp.h:59
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
virtual Real logZ() const
Returns the logarithm of the (approximated) partition sum (normalizing constant of the factor graph)...
Definition: fbp.cpp:26
Defines the DAI_ENUM macro, which can be used to define an enum with additional functionality.
const std::vector< Real > & Weights() const
Returns constant reference to vector of all factor weights.
Definition: fbp.h:94
virtual void calcNewMessage(size_t i, size_t _I)
Calculate the updated message from the _I 'th neighbor of variable i to variable i.
Definition: fbp.cpp:101
virtual void setProperties(const PropertySet &opts)
Definition: bp.cpp:33
virtual Prob calcIncomingMessageProduct(size_t I, bool without_i, size_t i) const
Calculate the product of factor I and the incoming messages.
Definition: fbp.cpp:44
void setWeight(size_t I, Real c)
Sets the weight of the I 'th factor to c.
Definition: fbp.h:97
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
Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue) ...
Definition: properties.h:73
FBP()
Default constructor.
Definition: fbp.h:68
virtual void calcBeliefF(size_t I, Prob &p) const
Calculates unnormalized belief of factor I.
Definition: fbp.h:115
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.
std::vector< Real > _weight
Factor weights (indexed by factor ID)
Definition: fbp.h:62
virtual FBP * construct(const FactorGraph &fg, const PropertySet &opts) const
Returns a pointer to a newly constructed inference algorithm.
Definition: fbp.h:83
virtual FBP * clone() const
Returns a pointer to a new, cloned copy of *this (i.e., virtual copy constructor) ...
Definition: fbp.h:82
Real Weight(size_t I) const
Returns weight of the I 'th factor.
Definition: fbp.h:91