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