1 /* Copyright (C) 2008 Frederik Eaton [frederik at ofb dot net]
3 This file is part of libDAI.
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.
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.
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
21 #ifndef __defined_libdai_gibbs_h
22 #define __defined_libdai_gibbs_h
25 #include <dai/daialg.h>
26 #include <dai/factorgraph.h>
27 #include <dai/properties.h>
33 class Gibbs
: public DAIAlgFG
{
35 /// Parameters of this inference algorithm
37 /// Number of iterations
44 /// Name of this inference algorithm
45 static const char *Name
;
48 typedef std::vector
<size_t> _count_t
;
50 std::vector
<_count_t
> _var_counts
;
51 std::vector
<_count_t
> _factor_counts
;
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
);
61 // default constructor
62 Gibbs() : DAIAlgFG() {}
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
);
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
;
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; }
89 virtual void init( const VarSet
&/*ns*/ ) { init(); }
91 virtual double maxDiff() const { DAI_THROW(NOT_IMPLEMENTED
); return 0.0; }
92 virtual size_t Iterations() const { return props
.iters
; }
94 Factor
beliefV( size_t i
) const;
95 Factor
beliefF( size_t I
) const;
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;
105 } // end of namespace dai