X-Git-Url: http://git.tuebingen.mpg.de/?p=libdai.git;a=blobdiff_plain;f=include%2Fdai%2Fgibbs.h;h=2e5ec68f314118382babab8654c6d851626ff21b;hp=fa9e2965b72febda7ed2588115d886cb466deb5f;hb=87c6827445f8fd67801efb6e818771e16229313b;hpb=9bbf3b86c3e9316ad786d5c5514a7f16a23b8259 diff --git a/include/dai/gibbs.h b/include/dai/gibbs.h index fa9e296..2e5ec68 100644 --- a/include/dai/gibbs.h +++ b/include/dai/gibbs.h @@ -1,4 +1,5 @@ -/* 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. @@ -18,6 +19,11 @@ */ +/// \file +/// \brief Defines class Gibbs +/// \todo Improve documentation + + #ifndef __defined_libdai_gibbs_h #define __defined_libdai_gibbs_h @@ -30,7 +36,17 @@ namespace dai { +/// Approximate inference algorithm "Gibbs sampling" class Gibbs : public DAIAlgFG { + private: + typedef std::vector _count_t; + typedef std::vector _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 { @@ -44,40 +60,19 @@ class Gibbs : public DAIAlgFG { /// Name of this inference algorithm static const char *Name; - protected: - typedef std::vector _count_t; - size_t _sample_count; - std::vector<_count_t> _var_counts; - std::vector<_count_t> _factor_counts; - - typedef std::vector _state_t; - void update_counts(_state_t &st); - void randomize_state(_state_t &st); - Prob get_var_dist(_state_t &st, size_t i); - void resample_var(_state_t &st, size_t i); - size_t get_factor_entry(const _state_t &st, int factor); - public: - // default constructor - Gibbs() : DAIAlgFG() {} - // copy constructor - Gibbs(const Gibbs & x) : DAIAlgFG(x), _sample_count(x._sample_count), _var_counts(x._var_counts), _factor_counts(x._factor_counts) {} - // 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; - } - 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(); } @@ -90,9 +85,28 @@ class Gibbs : public DAIAlgFG { 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& state() { return _state; } + + /// Return const reference to current state vector + const std::vector& 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