Merge branch 'joris'
[libdai.git] / include / dai / gibbs.h
1 /* Copyright (C) 2008 Frederik Eaton [frederik at ofb dot net],
2 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 /// \file
23 /// \brief Defines class Gibbs
24 /// \todo Improve documentation
25
26
27 #ifndef __defined_libdai_gibbs_h
28 #define __defined_libdai_gibbs_h
29
30
31 #include <dai/daialg.h>
32 #include <dai/factorgraph.h>
33 #include <dai/properties.h>
34
35
36 namespace dai {
37
38
39 /// Approximate inference algorithm "Gibbs sampling"
40 class Gibbs : public DAIAlgFG {
41 private:
42 typedef std::vector<size_t> _count_t;
43 typedef std::vector<size_t> _state_t;
44
45 size_t _sample_count;
46 std::vector<_count_t> _var_counts;
47 std::vector<_count_t> _factor_counts;
48 _state_t _state;
49
50 public:
51 /// Parameters of this inference algorithm
52 struct Properties {
53 /// Number of iterations
54 size_t iters;
55
56 /// Verbosity
57 size_t verbose;
58 } props;
59
60 /// Name of this inference algorithm
61 static const char *Name;
62
63 public:
64 /// Default constructor
65 Gibbs() : DAIAlgFG(), _sample_count(0), _var_counts(), _factor_counts(), _state() {}
66
67 /// Copy constructor
68 Gibbs(const Gibbs & x) : DAIAlgFG(x), _sample_count(x._sample_count), _var_counts(x._var_counts), _factor_counts(x._factor_counts), _state(x._state) {}
69
70 /// Assignment operator
71 Gibbs& operator=( const Gibbs &x ) {
72 if( this != &x ) {
73 DAIAlgFG::operator=( x );
74 _sample_count = x._sample_count;
75 _var_counts = x._var_counts;
76 _factor_counts = x._factor_counts;
77 _state = x._state;
78 }
79 return *this;
80 }
81
82 /// Construct from FactorGraph fg and PropertySet opts
83 Gibbs( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), _sample_count(0), _var_counts(), _factor_counts(), _state() {
84 setProperties( opts );
85 construct();
86 }
87
88
89 /// @name General InfAlg interface
90 //@{
91 virtual Gibbs* clone() const { return new Gibbs(*this); }
92 virtual Gibbs* create() const { return new Gibbs(); }
93 virtual std::string identify() const { return std::string(Name) + printProperties(); }
94 virtual Factor belief( const Var &n ) const;
95 virtual Factor belief( const VarSet &ns ) const;
96 virtual std::vector<Factor> beliefs() const;
97 virtual Real logZ() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
98 virtual void init();
99 virtual void init( const VarSet &/*ns*/ ) { init(); }
100 virtual double run();
101 virtual double maxDiff() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
102 virtual size_t Iterations() const { return props.iters; }
103 //@}
104
105
106 /// @name Additional interface specific for BP
107 //@{
108 Factor beliefV( size_t i ) const;
109 Factor beliefF( size_t I ) const;
110 void randomizeState();
111 //@}
112
113 private:
114 void updateCounts();
115 Prob getVarDist( size_t i );
116 void resampleVar( size_t i );
117 size_t getFactorEntry( size_t I );
118 size_t getFactorEntryDiff( size_t I, size_t i );
119
120 void construct();
121 /// Set Props according to the PropertySet opts, where the values can be stored as std::strings or as the type of the corresponding Props member
122 void setProperties( const PropertySet &opts );
123 PropertySet getProperties() const;
124 std::string printProperties() const;
125 };
126
127
128 } // end of namespace dai
129
130
131 #endif