libDAI
daialg.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_daialg_h
14 #define __defined_libdai_daialg_h
15 
16 
17 #include <string>
18 #include <iostream>
19 #include <vector>
20 #include <dai/factorgraph.h>
21 #include <dai/regiongraph.h>
22 #include <dai/cobwebgraph.h>
23 #include <dai/properties.h>
24 
25 
26 namespace dai {
27 
28 
30 
35 class InfAlg {
36  public:
38 
39  virtual ~InfAlg() {}
41 
43  virtual InfAlg* clone() const = 0;
44 
46 
49  virtual InfAlg* construct( const FactorGraph &fg, const PropertySet &opts ) const = 0;
51 
53 
54  virtual std::string name() const = 0;
56 
58  virtual std::string identify() const {
59  return name() + printProperties();
60  }
61 
63  virtual FactorGraph &fg() = 0;
64 
66  virtual const FactorGraph &fg() const = 0;
68 
70 
71 
74  virtual void init() = 0;
75 
77 
81  virtual void init( const VarSet &vs ) = 0;
82 
84 
86  virtual Real run() = 0;
87 
89 
91  virtual Factor belief( const Var &v ) const { return belief( VarSet(v) ); }
92 
94 
98  virtual Factor belief( const VarSet &vs ) const = 0;
99 
101 
104  virtual Factor beliefV( size_t i ) const { return belief( fg().var(i) ); }
105 
107 
110  virtual Factor beliefF( size_t I ) const { return belief( fg().factor(I).vars() ); }
111 
113 
115  virtual std::vector<Factor> beliefs() const = 0;
116 
118 
121  virtual Real logZ() const = 0;
122 
124 
127  virtual std::vector<size_t> findMaximum() const { DAI_THROW(NOT_IMPLEMENTED); }
128 
130 
132  virtual Real maxDiff() const { DAI_THROW(NOT_IMPLEMENTED); };
133 
135 
137  virtual size_t Iterations() const { DAI_THROW(NOT_IMPLEMENTED); };
138 
140 
142  virtual void setMaxIter( size_t /*maxiter*/ ) { DAI_THROW(NOT_IMPLEMENTED); }
144 
146 
147 
150  virtual void clamp( size_t i, size_t x, bool backup = false ) = 0;
151 
153 
155  virtual void makeCavity( size_t i, bool backup = false ) = 0;
156 
158 
160  virtual void makeRegionCavity( std::vector<size_t> facInds, bool backup = false ) = 0;
162 
164 
165 
168  virtual void backupFactor( size_t I ) = 0;
170 
172  virtual void backupFactors( const VarSet &vs ) = 0;
173 
175  virtual void restoreFactor( size_t I ) = 0;
177  virtual void restoreFactors( const VarSet &vs ) = 0;
179 
181 
182 
186  virtual void setProperties( const PropertySet &opts ) = 0;
188  virtual PropertySet getProperties() const = 0;
190  virtual std::string printProperties() const = 0;
192 };
193 
194 
196 
206 template <class GRM>
207 class DAIAlg : public InfAlg, public GRM {
208  public:
210 
211  DAIAlg() : InfAlg(), GRM() {}
213 
215  DAIAlg( const GRM &grm ) : InfAlg(), GRM(grm) {}
217 
219 
220  FactorGraph &fg() { return (FactorGraph &)(*this); }
222 
224  const FactorGraph &fg() const { return (const FactorGraph &)(*this); }
226 
228 
229 
232  void clamp( size_t i, size_t x, bool backup = false ) { GRM::clamp( i, x, backup ); }
233 
235 
237  void makeCavity( size_t i, bool backup = false ) { GRM::makeCavity( i, backup ); }
238 
240 
242  void makeRegionCavity( std::vector<size_t> facInds, bool backup ){ GRM::makeRegionCavity( facInds, backup ); }
244 
246 
247  void backupFactor( size_t I ) { GRM::backupFactor( I ); }
250  void backupFactors( const VarSet &vs ) { GRM::backupFactors( vs ); }
251 
253  void restoreFactor( size_t I ) { GRM::restoreFactor( I ); }
255  void restoreFactors( const VarSet &vs ) { GRM::restoreFactors( vs ); }
257  void restoreFactors() { GRM::restoreFactors(); }
259 };
260 
261 
264 
267 
270 
271 
273 
279 Factor calcMarginal( const InfAlg& obj, const VarSet& vs, bool reInit );
280 
281 
283 
294 std::vector<Factor> calcPairBeliefs( const InfAlg& obj, const VarSet& vs, bool reInit, bool accurate=false );
295 
296 
298 
300 std::vector<size_t> findMaximum( const InfAlg& obj );
301 
302 
303 } // end of namespace dai
304 
305 
306 #endif
void restoreFactors()
Restore all factors from their backup copies.
Definition: daialg.h:257
virtual void backupFactors(const VarSet &vs)=0
Make backup copies of all factors involving the variables in vs.
vector< Factor > calcPairBeliefs(const InfAlg &obj, const VarSet &vs, bool reInit, bool accurate)
Calculates beliefs for all pairs of variables in vs using inference algorithm obj.
Definition: daialg.cpp:76
virtual ~InfAlg()
Virtual destructor (needed because this class contains virtual functions)
Definition: daialg.h:40
virtual Factor belief(const Var &v) const
Returns the (approximate) marginal probability distribution of a variable.
Definition: daialg.h:91
Represents a factor graph.
Definition: factorgraph.h:65
void backupFactor(size_t I)
Make a backup copy of factor I.
Definition: daialg.h:248
virtual std::string printProperties() const =0
Returns parameters of this inference algorithm formatted as a string in the format "[key1=val1...
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 void setProperties(const PropertySet &opts)=0
Set parameters of this inference algorithm.
virtual void init()=0
Initializes all data structures of the approximate inference algorithm.
virtual std::string identify() const
Identifies itself for logging purposes.
Definition: daialg.h:58
virtual void restoreFactors(const VarSet &vs)=0
Restore the factors involving the variables in vs from their backup copies.
virtual InfAlg * clone() const =0
Returns a pointer to a new, cloned copy of *this (i.e., virtual copy constructor) ...
virtual void restoreFactor(size_t I)=0
Restore factor I from its backup copy.
virtual Factor beliefV(size_t i) const
Returns the (approximate) marginal probability distribution of the variable with index i...
Definition: daialg.h:104
Represents a set of variables.
Definition: varset.h:94
Defines class CobwebGraph, which implements a type of region graph used by GLC.
void makeRegionCavity(std::vector< size_t > facInds, bool backup)
Sets all factors indicated by facInds to one.
Definition: daialg.h:242
virtual std::vector< Factor > beliefs() const =0
Returns all beliefs (approximate marginal probability distributions) calculated by the algorithm...
void backupFactors(const VarSet &vs)
Make backup copies of all factors involving the variables in vs.
Definition: daialg.h:250
void restoreFactors(const VarSet &vs)
Restore the factors involving the variables in vs from their backup copies.
Definition: daialg.h:255
DAIAlg< CobwebGraph > DAIAlgCG
Base class for GLC that operates on CobwebGraph.
Definition: daialg.h:269
std::vector< size_t > findMaximum(const InfAlg &obj)
Calculates the joint state of all variables that has maximum probability, according to the inference ...
Definition: daialg.cpp:211
virtual PropertySet getProperties() const =0
Returns parameters of this inference algorithm converted into a PropertySet.
virtual Real maxDiff() const
Returns maximum difference between single variable beliefs in the last iteration. ...
Definition: daialg.h:132
const FactorGraph & fg() const
Returns constant reference to underlying FactorGraph.
Definition: daialg.h:224
virtual Real logZ() const =0
Returns the logarithm of the (approximated) partition sum (normalizing constant of the factor graph)...
virtual void setMaxIter(size_t)
Sets maximum number of iterations (one iteration passes over the complete factorgraph).
Definition: daialg.h:142
Defines the Property and PropertySet classes, which are mainly used for managing parameters of infere...
virtual std::string name() const =0
Returns the name of the algorithm.
Defines classes Region, FRegion and RegionGraph, which implement a particular subclass of region grap...
DAIAlg< RegionGraph > DAIAlgRG
Base class for inference algorithms that operate on a RegionGraph.
Definition: daialg.h:266
InfAlg is an abstract base class, defining the common interface of all inference algorithms in libDAI...
Definition: daialg.h:35
Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue) ...
Definition: properties.h:73
DAIAlg< FactorGraph > DAIAlgFG
Base class for inference algorithms that operate on a FactorGraph.
Definition: daialg.h:263
virtual InfAlg * construct(const FactorGraph &fg, const PropertySet &opts) const =0
Returns a pointer to a newly constructed inference algorithm.
Represents a discrete random variable.
Definition: var.h:37
Namespace for libDAI.
Definition: alldai.cpp:16
Defines the FactorGraph class, which represents factor graphs (e.g., Bayesian networks or Markov rand...
Factor calcMarginal(const InfAlg &obj, const VarSet &vs, bool reInit)
Calculates the marginal probability distribution for vs using inference algorithm obj...
Definition: daialg.cpp:20
DAIAlg(const GRM &grm)
Construct from GRM.
Definition: daialg.h:215
void restoreFactor(size_t I)
Restore factor I from its backup copy.
Definition: daialg.h:253
virtual void makeRegionCavity(std::vector< size_t > facInds, bool backup=false)=0
Sets all factors indicated by facInds to one.
virtual Factor beliefF(size_t I) const
Returns the (approximate) marginal probability distribution of the variables on which factor I depend...
Definition: daialg.h:110
virtual Real run()=0
Runs the approximate inference algorithm.
DAIAlg()
Default constructor.
Definition: daialg.h:212
Combines the abstract base class InfAlg with a graphical model (e.g., a FactorGraph or RegionGraph)...
Definition: daialg.h:207
void clamp(size_t i, size_t x, bool backup=false)
Clamp variable with index i to value x (i.e. multiply with a Kronecker delta )
Definition: daialg.h:232
virtual void clamp(size_t i, size_t x, bool backup=false)=0
Clamp variable with index i to value x (i.e. multiply with a Kronecker delta )
virtual void makeCavity(size_t i, bool backup=false)=0
Sets all factors interacting with variable with index i to one.
virtual void backupFactor(size_t I)=0
Make a backup copy of factor I.
virtual FactorGraph & fg()=0
Returns reference to underlying FactorGraph.
void makeCavity(size_t i, bool backup=false)
Sets all factors interacting with variable with index i to one.
Definition: daialg.h:237
virtual std::vector< size_t > findMaximum() const
Calculates the joint state of all variables that has maximum probability.
Definition: daialg.h:127
virtual size_t Iterations() const
Returns number of iterations done (one iteration passes over the complete factorgraph).
Definition: daialg.h:137