fa9e2965b72febda7ed2588115d886cb466deb5f
[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 size_t _sample_count;
50 std::vector<_count_t> _var_counts;
51 std::vector<_count_t> _factor_counts;
52
53 typedef std::vector<size_t> _state_t;
54 void update_counts(_state_t &st);
55 void randomize_state(_state_t &st);
56 Prob get_var_dist(_state_t &st, size_t i);
57 void resample_var(_state_t &st, size_t i);
58 size_t get_factor_entry(const _state_t &st, int factor);
59
60 public:
61 // default constructor
62 Gibbs() : DAIAlgFG() {}
63 // copy constructor
64 Gibbs(const Gibbs & x) : DAIAlgFG(x), _sample_count(x._sample_count), _var_counts(x._var_counts), _factor_counts(x._factor_counts) {}
65 // construct Gibbs object from FactorGraph
66 Gibbs( const FactorGraph & fg, const PropertySet &opts ) : DAIAlgFG(fg) {
67 setProperties( opts );
68 construct();
69 }
70 // assignment operator
71 Gibbs & operator=(const Gibbs & x) {
72 if(this!=&x) {
73 DAIAlgFG::operator=(x);
74 _sample_count = x._sample_count;
75 _var_counts = x._var_counts;
76 _factor_counts = x._factor_counts;
77 }
78 return *this;
79 }
80
81 virtual Gibbs* clone() const { return new Gibbs(*this); }
82 virtual Gibbs* create() const { return new Gibbs(); }
83 virtual std::string identify() const { return std::string(Name) + printProperties(); }
84 virtual Factor belief( const Var &n ) const;
85 virtual Factor belief( const VarSet &ns ) const;
86 virtual std::vector<Factor> beliefs() const;
87 virtual Real logZ() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
88 virtual void init();
89 virtual void init( const VarSet &/*ns*/ ) { init(); }
90 virtual double run();
91 virtual double maxDiff() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
92 virtual size_t Iterations() const { return props.iters; }
93
94 Factor beliefV( size_t i ) const;
95 Factor beliefF( size_t I ) const;
96
97 void construct();
98 /// 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
99 void setProperties( const PropertySet &opts );
100 PropertySet getProperties() const;
101 std::string printProperties() const;
102 };
103
104
105 } // end of namespace dai
106
107
108 #endif