-/* Copyright (C) 2008 Frederik Eaton [frederik at ofb dot net]
+/* Copyright (C) 2008 Frederik Eaton [frederik at ofb dot net],
+ Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
This file is part of libDAI.
*/
+/// \file
+/// \brief Defines class Gibbs
+/// \todo Improve documentation
+
+
#ifndef __defined_libdai_gibbs_h
#define __defined_libdai_gibbs_h
namespace dai {
+/// Approximate inference algorithm "Gibbs sampling"
class Gibbs : public DAIAlgFG {
+ private:
+ typedef std::vector<size_t> _count_t;
+ typedef std::vector<size_t> _state_t;
+
+ size_t _sample_count;
+ std::vector<_count_t> _var_counts;
+ std::vector<_count_t> _factor_counts;
+ _state_t _state;
+
public:
/// Parameters of this inference algorithm
struct Properties {
/// Name of this inference algorithm
static const char *Name;
- protected:
- typedef std::vector<size_t> _count_t;
- typedef std::vector<size_t> _state_t;
-
- size_t _sample_count;
- std::vector<_count_t> _var_counts;
- std::vector<_count_t> _factor_counts;
- std::vector<size_t> _factor_entries;
- _state_t _state;
-
- void update_counts();
- void randomize_state();
- Prob get_var_dist( size_t i );
- void resample_var( size_t i );
- size_t get_factor_entry( size_t I );
- size_t get_factor_entry_interval( size_t I, size_t i );
- void calc_factor_entries();
- void update_factor_entries( size_t i );
-
public:
- // default constructor
- Gibbs() : DAIAlgFG(), _sample_count(0), _var_counts(), _factor_counts(), _factor_entries(), _state() {}
- // copy constructor
- Gibbs(const Gibbs & x) : DAIAlgFG(x), _sample_count(x._sample_count), _var_counts(x._var_counts), _factor_counts(x._factor_counts), _factor_entries(x._factor_entries), _state(x._state) {}
- // construct Gibbs object from FactorGraph
- Gibbs( const FactorGraph & fg, const PropertySet &opts ) : DAIAlgFG(fg) {
+ /// Default constructor
+ Gibbs() : DAIAlgFG(), _sample_count(0), _var_counts(), _factor_counts(), _state() {}
+
+ /// Construct from FactorGraph fg and PropertySet opts
+ Gibbs( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), _sample_count(0), _var_counts(), _factor_counts(), _state() {
setProperties( opts );
construct();
}
- // assignment operator
- Gibbs & operator=(const Gibbs & x) {
- if(this!=&x) {
- DAIAlgFG::operator=(x);
- _sample_count = x._sample_count;
- _var_counts = x._var_counts;
- _factor_counts = x._factor_counts;
- _factor_entries = x._factor_entries;
- _state = x._state;
- }
- return *this;
- }
-
+
+
+ /// @name General InfAlg interface
+ //@{
virtual Gibbs* clone() const { return new Gibbs(*this); }
virtual Gibbs* create() const { return new Gibbs(); }
virtual std::string identify() const { return std::string(Name) + printProperties(); }
virtual double run();
virtual double maxDiff() const { DAI_THROW(NOT_IMPLEMENTED); return 0.0; }
virtual size_t Iterations() const { return props.iters; }
+ //@}
+
+ /// @name Additional interface specific for Gibbs
+ //@{
Factor beliefV( size_t i ) const;
Factor beliefF( size_t I ) const;
+ void randomizeState();
+ //@}
+
+ /// Return reference to current state vector
+ std::vector<size_t>& state() { return _state; }
+
+ /// Return const reference to current state vector
+ const std::vector<size_t>& state() const { return _state; }
+
+ private:
+ void updateCounts();
+ Prob getVarDist( size_t i );
+ void resampleVar( size_t i );
+ size_t getFactorEntry( size_t I );
+ size_t getFactorEntryDiff( size_t I, size_t i );
void construct();
/// 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