Updated copyright headers
[libdai.git] / include / dai / gibbs.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2008 Frederik Eaton [frederik at ofb dot net]
8 */
9
10
11 /// \file
12 /// \brief Defines class Gibbs
13 /// \todo Improve documentation
14
15
16 #ifndef __defined_libdai_gibbs_h
17 #define __defined_libdai_gibbs_h
18
19
20 #include <dai/daialg.h>
21 #include <dai/factorgraph.h>
22 #include <dai/properties.h>
23
24
25 namespace dai {
26
27
28 /// Approximate inference algorithm "Gibbs sampling"
29 class Gibbs : public DAIAlgFG {
30 private:
31 typedef std::vector<size_t> _count_t;
32 typedef std::vector<size_t> _state_t;
33
34 size_t _sample_count;
35 std::vector<_count_t> _var_counts;
36 std::vector<_count_t> _factor_counts;
37 _state_t _state;
38
39 public:
40 /// Parameters of this inference algorithm
41 struct Properties {
42 /// Number of iterations
43 size_t iters;
44
45 /// Verbosity
46 size_t verbose;
47 } props;
48
49 /// Name of this inference algorithm
50 static const char *Name;
51
52 public:
53 /// Default constructor
54 Gibbs() : DAIAlgFG(), _sample_count(0), _var_counts(), _factor_counts(), _state() {}
55
56 /// Construct from FactorGraph fg and PropertySet opts
57 Gibbs( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), _sample_count(0), _var_counts(), _factor_counts(), _state() {
58 setProperties( opts );
59 construct();
60 }
61
62
63 /// @name General InfAlg interface
64 //@{
65 virtual Gibbs* clone() const { return new Gibbs(*this); }
66 virtual std::string identify() const { return std::string(Name) + printProperties(); }
67 virtual Factor belief( const Var &n ) const;
68 virtual Factor belief( const VarSet &ns ) const;
69 virtual std::vector<Factor> beliefs() const;
70 virtual Real logZ() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
71 virtual void init();
72 virtual void init( const VarSet &/*ns*/ ) { init(); }
73 virtual double run();
74 virtual double maxDiff() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
75 virtual size_t Iterations() const { return props.iters; }
76 //@}
77
78
79 /// @name Additional interface specific for Gibbs
80 //@{
81 Factor beliefV( size_t i ) const;
82 Factor beliefF( size_t I ) const;
83 void randomizeState();
84 //@}
85
86 /// Return reference to current state vector
87 std::vector<size_t>& state() { return _state; }
88
89 /// Return const reference to current state vector
90 const std::vector<size_t>& state() const { return _state; }
91
92 private:
93 void updateCounts();
94 Prob getVarDist( size_t i );
95 void resampleVar( size_t i );
96 size_t getFactorEntry( size_t I );
97 size_t getFactorEntryDiff( size_t I, size_t i );
98
99 void construct();
100 /// 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
101 void setProperties( const PropertySet &opts );
102 PropertySet getProperties() const;
103 std::string printProperties() const;
104 };
105
106
107 } // end of namespace dai
108
109
110 #endif