-/* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
- Radboud University Nijmegen, The Netherlands
+/* This file is part of libDAI - http://www.libdai.org/
+ *
+ * libDAI is licensed under the terms of the GNU General Public License version
+ * 2, or (at your option) any later version. libDAI is distributed without any
+ * warranty. See the file COPYING for more details.
+ *
+ * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
+ * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
+ */
- This file is part of libDAI.
- libDAI is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- libDAI is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with libDAI; if not, write to the Free Software
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-*/
+/// \file
+/// \brief Defines class HAK.
+/// \todo Improve documentation
#ifndef __defined_libdai_hak_h
namespace dai {
-/// HAK provides an implementation of the single and double-loop algorithms by Heskes, Albers and Kappen
+/// Approximate inference algorithm: implementation of single-loop ("Generalized Belief Propagation") and double-loop algorithms by Heskes, Albers and Kappen
+/** \todo Optimize HAK with precalculated indices, similarly to BP.
+ */
class HAK : public DAIAlgRG {
private:
std::vector<Factor> _Qa;
size_t _iters;
public:
+ /// Parameters of this inference algorithm
struct Properties {
+ /// Enumeration of possible cluster choices
+ DAI_ENUM(ClustersType,MIN,DELTA,LOOP)
+
+ /// Verbosity
size_t verbose;
+
+ /// Maximum number of iterations
size_t maxiter;
+
+ /// Tolerance
double tol;
+
+ /// Damping constant
double damping;
- DAI_ENUM(ClustersType,MIN,DELTA,LOOP)
+
+ /// How to choose the clusters
ClustersType clusters;
+
+ /// Use single-loop (GBP) or double-loop (HAK)
bool doubleloop;
+
+ /// Depth of loops (only relevant for clusters == ClustersType::LOOP)
size_t loopdepth;
} props;
- /// Name of this inference method
+
+ /// Name of this inference algorithm
static const char *Name;
public:
/// Construct from RegionGraph rg and PropertySet opts
HAK( const RegionGraph &rg, const PropertySet &opts );
- /// Copy constructor
- HAK( const HAK &x ) : DAIAlgRG(x), _Qa(x._Qa), _Qb(x._Qb), _muab(x._muab), _muba(x._muba), _maxdiff(x._maxdiff), _iters(x._iters), props(x.props) {}
- /// Clone *this (virtual copy constructor)
+ /// @name General InfAlg interface
+ //@{
virtual HAK* clone() const { return new HAK(*this); }
-
- /// Create (virtual default constructor)
- virtual HAK* create() const { return new HAK(); }
-
- /// Assignment operator
- HAK& operator=( const HAK &x ) {
- if( this != &x ) {
- DAIAlgRG::operator=( x );
- _Qa = x._Qa;
- _Qb = x._Qb;
- _muab = x._muab;
- _muba = x._muba;
- _maxdiff = x._maxdiff;
- _iters = x._iters;
- props = x.props;
- }
- return *this;
- }
-
- /// Identifies itself for logging purposes
virtual std::string identify() const;
-
- /// Get single node belief
virtual Factor belief( const Var &n ) const;
-
- /// Get general belief
virtual Factor belief( const VarSet &ns ) const;
-
- /// Get all beliefs
virtual std::vector<Factor> beliefs() const;
-
- /// Get log partition sum
virtual Real logZ() const;
-
- /// Clear messages and beliefs
virtual void init();
-
- /// Clear messages and beliefs corresponding to the nodes in ns
virtual void init( const VarSet &ns );
-
- /// The actual approximate inference algorithm
virtual double run();
-
- /// Return maximum difference between single node beliefs in the last pass
virtual double maxDiff() const { return _maxdiff; }
-
- /// Return number of passes over the factorgraph
virtual size_t Iterations() const { return _iters; }
+ //@}
+ /// @name Additional interface specific for HAK
+ //@{
Factor & muab( size_t alpha, size_t _beta ) { return _muab[alpha][_beta]; }
Factor & muba( size_t alpha, size_t _beta ) { return _muba[alpha][_beta]; }
const Factor& Qa( size_t alpha ) const { return _Qa[alpha]; };
double doGBP();
double doDoubleLoop();
-
- void setProperties( const PropertySet &opts );
- PropertySet getProperties() const;
- std::string printProperties() const;
+ //@}
private:
void constructMessages();
void findLoopClusters( const FactorGraph &fg, std::set<VarSet> &allcl, VarSet newcl, const Var & root, size_t length, VarSet vars );
+
+ void setProperties( const PropertySet &opts );
+ PropertySet getProperties() const;
+ std::string printProperties() const;
};