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