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() {}
68 Gibbs(const Gibbs
& x
) : DAIAlgFG(x
), _sample_count(x
._sample_count
), _var_counts(x
._var_counts
), _factor_counts(x
._factor_counts
), _state(x
._state
) {}
70 /// Assignment operator
71 Gibbs
& operator=( const Gibbs
&x
) {
73 DAIAlgFG::operator=( x
);
74 _sample_count
= x
._sample_count
;
75 _var_counts
= x
._var_counts
;
76 _factor_counts
= x
._factor_counts
;
82 /// Construct from FactorGraph fg and PropertySet opts
83 Gibbs( const FactorGraph
&fg
, const PropertySet
&opts
) : DAIAlgFG(fg
), _sample_count(0), _var_counts(), _factor_counts(), _state() {
84 setProperties( opts
);
89 /// @name General InfAlg interface
91 virtual Gibbs
* clone() const { return new Gibbs(*this); }
92 virtual Gibbs
* create() const { return new Gibbs(); }
93 virtual std::string
identify() const { return std::string(Name
) + printProperties(); }
94 virtual Factor
belief( const Var
&n
) const;
95 virtual Factor
belief( const VarSet
&ns
) const;
96 virtual std::vector
<Factor
> beliefs() const;
97 virtual Real
logZ() const { DAI_THROW(NOT_IMPLEMENTED
); return 0.0; }
99 virtual void init( const VarSet
&/*ns*/ ) { init(); }
100 virtual double run();
101 virtual double maxDiff() const { DAI_THROW(NOT_IMPLEMENTED
); return 0.0; }
102 virtual size_t Iterations() const { return props
.iters
; }
106 /// @name Additional interface specific for BP
108 Factor
beliefV( size_t i
) const;
109 Factor
beliefF( size_t I
) const;
110 void randomizeState();
115 Prob
getVarDist( size_t i
);
116 void resampleVar( size_t i
);
117 size_t getFactorEntry( size_t I
);
118 size_t getFactorEntryDiff( size_t I
, size_t i
);
121 /// 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
122 void setProperties( const PropertySet
&opts
);
123 PropertySet
getProperties() const;
124 std::string
printProperties() const;
128 } // end of namespace dai