2e5ec68f314118382babab8654c6d851626ff21b
1 /* Copyright (C) 2008 Frederik Eaton [frederik at ofb dot net],
2 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
4 This file is part of libDAI.
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.
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.
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
23 /// \brief Defines class Gibbs
24 /// \todo Improve documentation
27 #ifndef __defined_libdai_gibbs_h
28 #define __defined_libdai_gibbs_h
31 #include <dai/daialg.h>
32 #include <dai/factorgraph.h>
33 #include <dai/properties.h>
39 /// Approximate inference algorithm "Gibbs sampling"
40 class Gibbs
: public DAIAlgFG
{
42 typedef std::vector
<size_t> _count_t
;
43 typedef std::vector
<size_t> _state_t
;
46 std::vector
<_count_t
> _var_counts
;
47 std::vector
<_count_t
> _factor_counts
;
51 /// Parameters of this inference algorithm
53 /// Number of iterations
60 /// Name of this inference algorithm
61 static const char *Name
;
64 /// Default constructor
65 Gibbs() : DAIAlgFG(), _sample_count(0), _var_counts(), _factor_counts(), _state() {}
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
);
74 /// @name General InfAlg interface
76 virtual Gibbs
* clone() const { return new Gibbs(*this); }
77 virtual Gibbs
* create() const { return new Gibbs(); }
78 virtual std::string
identify() const { return std::string(Name
) + printProperties(); }
79 virtual Factor
belief( const Var
&n
) const;
80 virtual Factor
belief( const VarSet
&ns
) const;
81 virtual std::vector
<Factor
> beliefs() const;
82 virtual Real
logZ() const { DAI_THROW(NOT_IMPLEMENTED
); return 0.0; }
84 virtual void init( const VarSet
&/*ns*/ ) { init(); }
86 virtual double maxDiff() const { DAI_THROW(NOT_IMPLEMENTED
); return 0.0; }
87 virtual size_t Iterations() const { return props
.iters
; }
91 /// @name Additional interface specific for Gibbs
93 Factor
beliefV( size_t i
) const;
94 Factor
beliefF( size_t I
) const;
95 void randomizeState();
98 /// Return reference to current state vector
99 std::vector
<size_t>& state() { return _state
; }
101 /// Return const reference to current state vector
102 const std::vector
<size_t>& state() const { return _state
; }
106 Prob
getVarDist( size_t i
);
107 void resampleVar( size_t i
);
108 size_t getFactorEntry( size_t I
);
109 size_t getFactorEntryDiff( size_t I
, size_t i
);
112 /// 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
113 void setProperties( const PropertySet
&opts
);
114 PropertySet
getProperties() const;
115 std::string
printProperties() const;
119 } // end of namespace dai