Cleanup of BBP code
[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 /// Construct from FactorGraph fg and PropertySet opts
68 Gibbs( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), _sample_count(0), _var_counts(), _factor_counts(), _state() {
69 setProperties( opts );
70 construct();
71 }
72
73
74 /// @name General InfAlg interface
75 //@{
76 virtual Gibbs* clone() const { return new Gibbs(*this); }
77 virtual Gibbs* create() const { return new Gibbs(); }
78 virtual std::string identify() const { return std::string(Name) + printProperties(); }
79 virtual Factor belief( const Var &n ) const;
80 virtual Factor belief( const VarSet &ns ) const;
81 virtual std::vector<Factor> beliefs() const;
82 virtual Real logZ() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
83 virtual void init();
84 virtual void init( const VarSet &/*ns*/ ) { init(); }
85 virtual double run();
86 virtual double maxDiff() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
87 virtual size_t Iterations() const { return props.iters; }
88 //@}
89
90
91 /// @name Additional interface specific for Gibbs
92 //@{
93 Factor beliefV( size_t i ) const;
94 Factor beliefF( size_t I ) const;
95 void randomizeState();
96 //@}
97
98 /// Return reference to current state vector
99 std::vector<size_t>& state() { return _state; }
100
101 /// Return const reference to current state vector
102 const std::vector<size_t>& state() const { return _state; }
103
104 private:
105 void updateCounts();
106 Prob getVarDist( size_t i );
107 void resampleVar( size_t i );
108 size_t getFactorEntry( size_t I );
109 size_t getFactorEntryDiff( size_t I, size_t i );
110
111 void construct();
112 /// 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
113 void setProperties( const PropertySet &opts );
114 PropertySet getProperties() const;
115 std::string printProperties() const;
116 };
117
118
119 } // end of namespace dai
120
121
122 #endif