libDAI
gibbs.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_gibbs_h
14 #define __defined_libdai_gibbs_h
15 
16 
17 #include <dai/dai_config.h>
18 #ifdef DAI_WITH_GIBBS
19 
20 
21 #include <dai/daialg.h>
22 #include <dai/factorgraph.h>
23 #include <dai/properties.h>
24 
25 
26 namespace dai {
27 
28 
30 
32 class Gibbs : public DAIAlgFG {
33  private:
35  typedef std::vector<size_t> _count_t;
37  typedef std::vector<size_t> _state_t;
39  size_t _sample_count;
41  std::vector<_count_t> _var_counts;
43  std::vector<_count_t> _factor_counts;
45  size_t _iters;
47  _state_t _state;
49  _state_t _max_state;
51  Real _max_score;
52 
53  public:
55  struct Properties {
57  size_t maxiter;
58 
60  double maxtime;
61 
63  size_t restart;
64 
66  size_t burnin;
67 
69  size_t verbose;
70  } props;
71 
72  public:
74  Gibbs() : DAIAlgFG(), _sample_count(0), _var_counts(), _factor_counts(), _iters(0), _state(), _max_state(), _max_score(-INFINITY) {}
75 
77 
80  Gibbs( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), _sample_count(0), _var_counts(), _factor_counts(), _iters(0), _state(), _max_state(), _max_score(-INFINITY) {
81  setProperties( opts );
82  construct();
83  }
84 
85 
87 
88  virtual Gibbs* clone() const { return new Gibbs(*this); }
89  virtual Gibbs* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new Gibbs( fg, opts ); }
90  virtual std::string name() const { return "GIBBS"; }
91  virtual Factor belief( const Var &v ) const { return beliefV( findVar( v ) ); }
92  virtual Factor belief( const VarSet &vs ) const;
93  virtual Factor beliefV( size_t i ) const;
94  virtual Factor beliefF( size_t I ) const;
95  virtual std::vector<Factor> beliefs() const;
96  virtual Real logZ() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
97  std::vector<size_t> findMaximum() const { return _max_state; }
98  virtual void init();
99  virtual void init( const VarSet &/*ns*/ ) { init(); }
100  virtual Real run();
101  virtual Real maxDiff() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
102  virtual size_t Iterations() const { return _iters; }
103  virtual void setMaxIter( size_t maxiter ) { props.maxiter = maxiter; }
104  virtual void setProperties( const PropertySet &opts );
105  virtual PropertySet getProperties() const;
106  virtual std::string printProperties() const;
108 
109 
111 
112  void randomizeState();
115  std::vector<size_t>& state() { return _state; }
117  const std::vector<size_t>& state() const { return _state; }
119 
120  private:
122  void construct();
124  void updateCounts();
126  Prob getVarDist( size_t i );
128  void resampleVar( size_t i );
130  size_t getFactorEntry( size_t I );
132  size_t getFactorEntryDiff( size_t I, size_t i );
133 };
134 
135 
137 
139 std::vector<size_t> getGibbsState( const FactorGraph &fg, size_t maxiter );
140 
141 
142 } // end of namespace dai
143 
144 
150 #endif
151 
152 
153 #endif
TFactor< Real > Factor
Represents a factor with values of type dai::Real.
Definition: factor.h:640
TProb< Real > Prob
Represents a vector with entries of type dai::Real.
Definition: prob.h:766
double Real
Real number (alias for double, which could be changed to long double if necessary) ...
Definition: util.h:98
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
Defines the Property and PropertySet classes, which are mainly used for managing parameters of infere...
DAIAlg< FactorGraph > DAIAlgFG
Base class for inference algorithms that operate on a FactorGraph.
Definition: daialg.h:263
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.