Extended InfAlg interface with setProperties(), getProperties() and printProperties()
[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 Factor beliefV( size_t i ) const;
70 virtual Factor beliefF( size_t I ) const;
71 virtual std::vector<Factor> beliefs() const;
72 virtual Real logZ() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
73 virtual void init();
74 virtual void init( const VarSet &/*ns*/ ) { init(); }
75 virtual Real run();
76 virtual Real maxDiff() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
77 virtual size_t Iterations() const { return props.iters; }
78 virtual void setProperties( const PropertySet &opts );
79 virtual PropertySet getProperties() const;
80 virtual std::string printProperties() const;
81 //@}
82
83
84 /// \name Additional interface specific for Gibbs
85 //@{
86 void randomizeState();
87 /// Return reference to current state vector
88 std::vector<size_t>& state() { return _state; }
89
90 /// Return const reference to current state vector
91 const std::vector<size_t>& state() const { return _state; }
92 //@}
93
94 private:
95 void updateCounts();
96 Prob getVarDist( size_t i );
97 void resampleVar( size_t i );
98 size_t getFactorEntry( size_t I );
99 size_t getFactorEntryDiff( size_t I, size_t i );
100
101 void construct();
102 };
103
104
105 } // end of namespace dai
106
107
108 #endif