libDAI
hak.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 
15 
16 
17 #include <dai/dai_config.h>
18 #ifdef DAI_WITH_HAK
19 
20 
21 #ifndef __defined_libdai_hak_h
22 #define __defined_libdai_hak_h
23 
24 
25 #include <string>
26 #include <dai/daialg.h>
27 #include <dai/regiongraph.h>
28 #include <dai/enum.h>
29 #include <dai/properties.h>
30 
31 
32 namespace dai {
33 
34 
36 class HAK : public DAIAlgRG {
37  private:
39  std::vector<Factor> _Qa;
41  std::vector<Factor> _Qb;
43  std::vector<std::vector<Factor> > _muab;
45  std::vector<std::vector<Factor> > _muba;
49  size_t _iters;
50 
51  public:
53  struct Properties {
55 
61  DAI_ENUM(ClustersType,MIN,BETHE,DELTA,LOOP);
62 
64  DAI_ENUM(InitType,UNIFORM,RANDOM);
65 
67  size_t verbose;
68 
70  size_t maxiter;
71 
73  double maxtime;
74 
77 
80 
82  ClustersType clusters;
83 
85  InitType init;
86 
88  bool doubleloop;
89 
91  size_t loopdepth;
92  } props;
93 
94  public:
96 
97  HAK() : DAIAlgRG(), _Qa(), _Qb(), _muab(), _muba(), _maxdiff(0.0), _iters(0U), props() {}
99 
101 
104  HAK( const FactorGraph &fg, const PropertySet &opts );
105 
107  HAK( const RegionGraph &rg, const PropertySet &opts );
109 
110 
112 
113  virtual HAK* clone() const { return new HAK(*this); }
114  virtual HAK* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new HAK( fg, opts ); }
115  virtual std::string name() const { return "HAK"; }
116  virtual Factor belief( const VarSet &vs ) const;
117  virtual std::vector<Factor> beliefs() const;
118  virtual Real logZ() const;
119  virtual void init();
120  virtual void init( const VarSet &vs );
121  virtual Real run();
122  virtual Real maxDiff() const { return _maxdiff; }
123  virtual size_t Iterations() const { return _iters; }
124  virtual void setMaxIter( size_t maxiter ) { props.maxiter = maxiter; }
125  virtual void setProperties( const PropertySet &opts );
126  virtual PropertySet getProperties() const;
127  virtual std::string printProperties() const;
129 
130 
132 
133  Factor & muab( size_t alpha, size_t _beta ) { return _muab[alpha][_beta]; }
136  Factor & muba( size_t alpha, size_t _beta ) { return _muba[alpha][_beta]; }
138  const Factor& Qa( size_t alpha ) const { return _Qa[alpha]; };
140  const Factor& Qb( size_t beta ) const { return _Qb[beta]; };
141 
143  Real doGBP();
145  Real doDoubleLoop();
147 
148  private:
150  void construct();
152 
160  void findLoopClusters( const FactorGraph &fg, std::set<VarSet> &allcl, VarSet newcl, const Var & root, size_t length, VarSet vars );
161 };
162 
163 
164 } // end of namespace dai
165 
166 
167 #endif
168 
169 
170 #endif
size_t loopdepth
Depth of loops (only relevant for clusters == ClustersType::LOOP)
Definition: hak.h:91
A RegionGraph combines a bipartite graph consisting of outer regions (type FRegion) and inner regions...
Definition: regiongraph.h:91
virtual size_t Iterations() const
Returns number of iterations done (one iteration passes over the complete factorgraph).
Definition: hak.h:123
void findLoopClusters(const FactorGraph &fg, std::set< VarSet > &allcl, VarSet newcl, const Var &root, size_t length, VarSet vars)
Recursive procedure for finding clusters of variables containing loops of length at most length...
Definition: hak.cpp:155
Real _maxdiff
Maximum difference encountered so far.
Definition: hak.h:47
virtual Real logZ() const
Returns the logarithm of the (approximated) partition sum (normalizing constant of the factor graph)...
Definition: hak.cpp:605
virtual HAK * construct(const FactorGraph &fg, const PropertySet &opts) const
Returns a pointer to a newly constructed inference algorithm.
Definition: hak.h:114
Represents a factor graph.
Definition: factorgraph.h:65
ClustersType clusters
How to choose the outer regions.
Definition: hak.h:82
const Factor & Qb(size_t beta) const
Returns belief of inner region beta.
Definition: hak.h:140
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
HAK()
Default constructor.
Definition: hak.h:98
std::vector< Factor > _Qa
Outer region beliefs.
Definition: hak.h:39
virtual void init()
Initializes all data structures of the approximate inference algorithm.
Definition: hak.cpp:301
virtual Real run()
Runs the approximate inference algorithm.
Definition: hak.cpp:568
virtual HAK * clone() const
Returns a pointer to a new, cloned copy of *this (i.e., virtual copy constructor) ...
Definition: hak.h:113
Real damping
Damping constant (0.0 means no damping, 1.0 is maximum damping)
Definition: hak.h:79
Defines the DAI_ENUM macro, which can be used to define an enum with additional functionality.
size_t _iters
Number of iterations needed.
Definition: hak.h:49
Represents a set of variables.
Definition: varset.h:94
Factor & muab(size_t alpha, size_t _beta)
Returns reference to message from outer region alpha to its _beta 'th neighboring inner region...
Definition: hak.h:134
DAI_ENUM(ClustersType, MIN, BETHE, DELTA, LOOP)
Enumeration of possible cluster choices.
virtual std::string printProperties() const
Returns parameters of this inference algorithm formatted as a string in the format "[key1=val1...
Definition: hak.cpp:95
Real doDoubleLoop()
Runs double-loop algorithm (as described in section 4.2 of [HAK03]), which always convergences...
Definition: hak.cpp:464
virtual Factor belief(const VarSet &vs) const
Returns the (approximate) marginal probability distribution of a set of variables.
Definition: hak.cpp:576
virtual void setProperties(const PropertySet &opts)
Set parameters of this inference algorithm.
Definition: hak.cpp:44
InitType init
How to initialize the messages.
Definition: hak.h:85
std::vector< Factor > _Qb
Inner region beliefs.
Definition: hak.h:41
bool doubleloop
Use single-loop (GBP) or double-loop (HAK)
Definition: hak.h:88
virtual std::string name() const
Returns the name of the algorithm.
Definition: hak.h:115
const Factor & Qa(size_t alpha) const
Returns belief of outer region alpha.
Definition: hak.h:138
Parameters for HAK.
Definition: hak.h:53
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...
virtual std::vector< Factor > beliefs() const
Returns all beliefs (approximate marginal probability distributions) calculated by the algorithm...
Definition: hak.cpp:595
Approximate inference algorithm: implementation of single-loop ("Generalized Belief Propagation") and...
Definition: hak.h:36
Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue) ...
Definition: properties.h:73
Real doGBP()
Runs single-loop algorithm (algorithm 1 in [HAK03])
Definition: hak.cpp:331
size_t verbose
Verbosity (amount of output sent to stderr)
Definition: hak.h:67
void construct()
Helper function for constructors.
Definition: hak.cpp:111
virtual void setMaxIter(size_t maxiter)
Sets maximum number of iterations (one iteration passes over the complete factorgraph).
Definition: hak.h:124
double maxtime
Maximum time (in seconds)
Definition: hak.h:73
Real tol
Tolerance for convergence test.
Definition: hak.h:76
size_t maxiter
Maximum number of iterations.
Definition: hak.h:70
std::vector< std::vector< Factor > > _muab
Messages from outer to inner regions.
Definition: hak.h:43
Represents a discrete random variable.
Definition: var.h:37
Namespace for libDAI.
Definition: alldai.cpp:16
virtual Real maxDiff() const
Returns maximum difference between single variable beliefs in the last iteration. ...
Definition: hak.h:122
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.
Factor & muba(size_t alpha, size_t _beta)
Returns reference to message the _beta 'th neighboring inner region of outer region alpha to that out...
Definition: hak.h:136
Combines the abstract base class InfAlg with a graphical model (e.g., a FactorGraph or RegionGraph)...
Definition: daialg.h:207
virtual PropertySet getProperties() const
Returns parameters of this inference algorithm converted into a PropertySet.
Definition: hak.cpp:80
std::vector< std::vector< Factor > > _muba
Messages from inner to outer regions.
Definition: hak.h:45