X-Git-Url: http://git.tuebingen.mpg.de/?p=libdai.git;a=blobdiff_plain;f=include%2Fdai%2Fjtree.h;h=aaff0e21adacb144366e8f2fb302b195a3018917;hp=e6bf12ffa45570267056045c299d37ac0537d1bf;hb=87c6827445f8fd67801efb6e818771e16229313b;hpb=7e77161d381c0062fb7159276d09633a0cce6a63 diff --git a/include/dai/jtree.h b/include/dai/jtree.h index e6bf12f..aaff0e2 100644 --- a/include/dai/jtree.h +++ b/include/dai/jtree.h @@ -1,6 +1,7 @@ -/* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl] - Radboud University Nijmegen, The Netherlands - +/* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de] + Radboud University Nijmegen, The Netherlands / + Max Planck Institute for Biological Cybernetics, Germany + This file is part of libDAI. libDAI is free software; you can redistribute it and/or modify @@ -19,6 +20,11 @@ */ +/// \file +/// \brief Defines class JTree +/// \todo Improve documentation + + #ifndef __defined_libdai_jtree_h #define __defined_libdai_jtree_h @@ -32,64 +38,104 @@ #include #include #include +#include namespace dai { +/// Exact inference algorithm using junction tree class JTree : public DAIAlgRG { - protected: - DEdgeVec _RTree; // rooted tree - std::vector _Qa; - std::vector _Qb; + private: std::vector > _mes; double _logZ; + public: + /// Rooted tree + DEdgeVec RTree; + + /// Outer region beliefs + std::vector Qa; + + /// Inner region beliefs + std::vector Qb; + + /// Parameters of this inference algorithm + struct Properties { + /// Enumeration of possible JTree updates + DAI_ENUM(UpdateType,HUGIN,SHSH) + + /// Verbosity + size_t verbose; + + /// Type of updates + UpdateType updates; + } props; + + /// Name of this inference algorithm + static const char *Name; public: - ENUM2(UpdateType,HUGIN,SHSH) - UpdateType Updates() const { return GetPropertyAs("updates"); } - - JTree() : DAIAlgRG(), _RTree(), _Qa(), _Qb(), _mes(), _logZ() {}; - JTree( const JTree& x ) : DAIAlgRG(x), _RTree(x._RTree), _Qa(x._Qa), _Qb(x._Qb), _mes(x._mes), _logZ(x._logZ) {}; - JTree* clone() const { return new JTree(*this); } - JTree & operator=( const JTree& x ) { - if( this != &x ) { - DAIAlgRG::operator=(x); - _RTree = x._RTree; - _Qa = x._Qa; - _Qb = x._Qb; - _mes = x._mes; - _logZ = x._logZ; - } - return *this; - } - JTree( const FactorGraph &fg, const Properties &opts, bool automatic=true ); + /// Default constructor + JTree() : DAIAlgRG(), _mes(), _logZ(), RTree(), Qa(), Qb(), props() {} + + /// Construct from FactorGraph fg and PropertySet opts + JTree( const FactorGraph &fg, const PropertySet &opts, bool automatic=true ); + + + /// @name General InfAlg interface + //@{ + virtual JTree* clone() const { return new JTree(*this); } + virtual JTree* create() const { return new JTree(); } + virtual std::string identify() const; + virtual Factor belief( const Var &n ) const; + virtual Factor belief( const VarSet &ns ) const; + virtual std::vector beliefs() const; + virtual Real logZ() const; + virtual void init() {} + virtual void init( const VarSet &/*ns*/ ) {} + virtual double run(); + virtual double maxDiff() const { return 0.0; } + virtual size_t Iterations() const { return 1UL; } + //@} + + + /// @name Additional interface specific for JTree + //@{ void GenerateJT( const std::vector &Cliques ); + /// Returns reference the message from outer region alpha to its _beta'th neighboring inner region Factor & message( size_t alpha, size_t _beta ) { return _mes[alpha][_beta]; } + /// Returns const reference to the message from outer region alpha to its _beta'th neighboring inner region const Factor & message( size_t alpha, size_t _beta ) const { return _mes[alpha][_beta]; } - static const char *Name; - std::string identify() const; - void init() { assert( checkProperties() ); } + /// Runs junction-tree with HUGIN updates void runHUGIN(); - void runShaferShenoy(); - double run(); - Factor belief( const Var &n ) const; - Factor belief( const VarSet &ns ) const; - std::vector beliefs() const; - Complex logZ() const; - void init( const VarSet &/*ns*/ ) {} - void undoProbs( const VarSet &ns ) { RegionGraph::undoProbs( ns ); init( ns ); } + /// Runs junction-tree with Shafer-Shenoy updates + void runShaferShenoy(); + /// Finds an efficient tree for calculating the marginal of some variables size_t findEfficientTree( const VarSet& ns, DEdgeVec &Tree, size_t PreviousRoot=(size_t)-1 ) const; + + /// Calculates the marginal of a set of variables Factor calcMarginal( const VarSet& ns ); - bool checkProperties(); + //@} + + private: + void setProperties( const PropertySet &opts ); + PropertySet getProperties() const; + std::string printProperties() const; }; +/// Calculates upper bound to the treewidth of a FactorGraph +/** \relates JTree + * \return a pair (number of variables in largest clique, number of states in largest clique) + */ +std::pair treewidth( const FactorGraph & fg ); + + } // end of namespace dai