Cleaned up error handling by introducing the DAI_THROWE macro.
[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 std::string identify() const { return std::string(Name) + printProperties(); }
78 virtual Factor belief( const Var &n ) const;
79 virtual Factor belief( const VarSet &ns ) const;
80 virtual std::vector<Factor> beliefs() const;
81 virtual Real logZ() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
82 virtual void init();
83 virtual void init( const VarSet &/*ns*/ ) { init(); }
84 virtual double run();
85 virtual double maxDiff() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
86 virtual size_t Iterations() const { return props.iters; }
87 //@}
88
89
90 /// @name Additional interface specific for Gibbs
91 //@{
92 Factor beliefV( size_t i ) const;
93 Factor beliefF( size_t I ) const;
94 void randomizeState();
95 //@}
96
97 /// Return reference to current state vector
98 std::vector<size_t>& state() { return _state; }
99
100 /// Return const reference to current state vector
101 const std::vector<size_t>& state() const { return _state; }
102
103 private:
104 void updateCounts();
105 Prob getVarDist( size_t i );
106 void resampleVar( size_t i );
107 size_t getFactorEntry( size_t I );
108 size_t getFactorEntryDiff( size_t I, size_t i );
109
110 void construct();
111 /// 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
112 void setProperties( const PropertySet &opts );
113 PropertySet getProperties() const;
114 std::string printProperties() const;
115 };
116
117
118 } // end of namespace dai
119
120
121 #endif