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
;
49 typedef std::vector
<size_t> _state_t
;
52 std::vector
<_count_t
> _var_counts
;
53 std::vector
<_count_t
> _factor_counts
;
57 void randomizeState();
58 Prob
getVarDist( size_t i
);
59 void resampleVar( size_t i
);
60 size_t getFactorEntry( size_t I
);
61 size_t getFactorEntryDiff( size_t I
, size_t i
);
64 // default constructor
65 Gibbs() : DAIAlgFG(), _sample_count(0), _var_counts(), _factor_counts(), _state() {}
67 Gibbs(const Gibbs
& x
) : DAIAlgFG(x
), _sample_count(x
._sample_count
), _var_counts(x
._var_counts
), _factor_counts(x
._factor_counts
), _state(x
._state
) {}
68 // construct Gibbs object from FactorGraph
69 Gibbs( const FactorGraph
& fg
, const PropertySet
&opts
) : DAIAlgFG(fg
) {
70 setProperties( opts
);
73 // assignment operator
74 Gibbs
& operator=(const Gibbs
& x
) {
76 DAIAlgFG::operator=(x
);
77 _sample_count
= x
._sample_count
;
78 _var_counts
= x
._var_counts
;
79 _factor_counts
= x
._factor_counts
;
85 virtual Gibbs
* clone() const { return new Gibbs(*this); }
86 virtual Gibbs
* create() const { return new Gibbs(); }
87 virtual std::string
identify() const { return std::string(Name
) + printProperties(); }
88 virtual Factor
belief( const Var
&n
) const;
89 virtual Factor
belief( const VarSet
&ns
) const;
90 virtual std::vector
<Factor
> beliefs() const;
91 virtual Real
logZ() const { DAI_THROW(NOT_IMPLEMENTED
); return 0.0; }
93 virtual void init( const VarSet
&/*ns*/ ) { init(); }
95 virtual double maxDiff() const { DAI_THROW(NOT_IMPLEMENTED
); return 0.0; }
96 virtual size_t Iterations() const { return props
.iters
; }
98 Factor
beliefV( size_t i
) const;
99 Factor
beliefF( size_t I
) const;
102 /// 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
103 void setProperties( const PropertySet
&opts
);
104 PropertySet
getProperties() const;
105 std::string
printProperties() const;
109 } // end of namespace dai