1 /* This file is part of libDAI - http://www.libdai.org/
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.
7 * Copyright (C) 2008 Frederik Eaton [frederik at ofb dot net]
12 /// \brief Defines class Gibbs
13 /// \todo Improve documentation
16 #ifndef __defined_libdai_gibbs_h
17 #define __defined_libdai_gibbs_h
20 #include <dai/daialg.h>
21 #include <dai/factorgraph.h>
22 #include <dai/properties.h>
28 /// Approximate inference algorithm "Gibbs sampling"
29 class Gibbs
: public DAIAlgFG
{
31 typedef std::vector
<size_t> _count_t
;
32 typedef std::vector
<size_t> _state_t
;
35 std::vector
<_count_t
> _var_counts
;
36 std::vector
<_count_t
> _factor_counts
;
40 /// Parameters of this inference algorithm
42 /// Number of iterations
49 /// Name of this inference algorithm
50 static const char *Name
;
53 /// Default constructor
54 Gibbs() : DAIAlgFG(), _sample_count(0), _var_counts(), _factor_counts(), _state() {}
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
);
63 /// @name General InfAlg interface
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 std::vector
<Factor
> beliefs() const;
70 virtual Real
logZ() const { DAI_THROW(NOT_IMPLEMENTED
); return 0.0; }
72 virtual void init( const VarSet
&/*ns*/ ) { init(); }
74 virtual double maxDiff() const { DAI_THROW(NOT_IMPLEMENTED
); return 0.0; }
75 virtual size_t Iterations() const { return props
.iters
; }
79 /// @name Additional interface specific for Gibbs
81 Factor
beliefV( size_t i
) const;
82 Factor
beliefF( size_t I
) const;
83 void randomizeState();
86 /// Return reference to current state vector
87 std::vector
<size_t>& state() { return _state
; }
89 /// Return const reference to current state vector
90 const std::vector
<size_t>& state() const { return _state
; }
94 Prob
getVarDist( size_t i
);
95 void resampleVar( size_t i
);
96 size_t getFactorEntry( size_t I
);
97 size_t getFactorEntryDiff( size_t I
, size_t i
);
100 /// 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
101 void setProperties( const PropertySet
&opts
);
102 PropertySet
getProperties() const;
103 std::string
printProperties() const;
107 } // end of namespace dai