1 /* This file is part of libDAI - http://www.libdai.org/
2 *
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
9 */
12 /// \file
13 /// \brief Defines class JTree, which implements the junction tree algorithm
16 #ifndef __defined_libdai_jtree_h
17 #define __defined_libdai_jtree_h
20 #include <vector>
21 #include <string>
22 #include <dai/daialg.h>
23 #include <dai/varset.h>
24 #include <dai/regiongraph.h>
25 #include <dai/factorgraph.h>
26 #include <dai/clustergraph.h>
27 #include <dai/weightedgraph.h>
28 #include <dai/enum.h>
29 #include <dai/properties.h>
32 namespace dai {
35 /// Exact inference algorithm using junction tree
36 /** The junction tree algorithm uses message passing on a junction tree to calculate
37 * exact marginal probability distributions ("beliefs") for specified cliques
38 * (outer regions) and separators (intersections of pairs of cliques).
39 *
40 * There are two variants, the sum-product algorithm (corresponding to
41 * finite temperature) and the max-product algorithm (corresponding to
42 * zero temperature).
43 */
44 class JTree : public DAIAlgRG {
45 private:
46 /// Stores the messages
47 std::vector<std::vector<Factor> > _mes;
49 /// Stores the logarithm of the partition sum
50 Real _logZ;
52 public:
53 /// The junction tree (stored as a rooted tree)
54 RootedTree RTree;
56 /// Outer region beliefs
57 std::vector<Factor> Qa;
59 /// Inner region beliefs
60 std::vector<Factor> Qb;
62 /// Parameters for JTree
63 struct Properties {
64 /// Enumeration of possible JTree updates
65 /** There are two types of updates:
66 * - HUGIN similar to those in HUGIN
67 * - SHSH Shafer-Shenoy type
68 */
69 DAI_ENUM(UpdateType,HUGIN,SHSH);
71 /// Enumeration of inference variants
72 /** There are two inference variants:
73 * - SUMPROD Sum-Product
74 * - MAXPROD Max-Product (equivalent to Min-Sum)
75 */
76 DAI_ENUM(InfType,SUMPROD,MAXPROD);
78 /// Verbosity (amount of output sent to stderr)
79 size_t verbose;
84 /// Type of inference
85 InfType inference;
86 } props;
88 /// Name of this inference algorithm
89 static const char *Name;
91 public:
92 /// \name Constructors/destructors
93 //@{
94 /// Default constructor
95 JTree() : DAIAlgRG(), _mes(), _logZ(), RTree(), Qa(), Qb(), props() {}
97 /// Construct from FactorGraph \a fg and PropertySet \a opts
98 /** \param fg factor graph (which has to be connected);
99 ** \param opts Parameters @see Properties
100 * \param automatic if \c true, construct the junction tree automatically, using the MinFill heuristic.
101 * \throw FACTORGRAPH_NOT_CONNECTED if \a fg is not connected
102 */
103 JTree( const FactorGraph &fg, const PropertySet &opts, bool automatic=true );
104 //@}
107 /// \name General InfAlg interface
108 //@{
109 virtual JTree* clone() const { return new JTree(*this); }
110 virtual std::string identify() const;
111 virtual Factor belief( const VarSet &vs ) const;
112 virtual std::vector<Factor> beliefs() const;
113 virtual Real logZ() const;
114 virtual void init() {}
115 virtual void init( const VarSet &/*ns*/ ) {}
116 virtual Real run();
117 virtual Real maxDiff() const { return 0.0; }
118 virtual size_t Iterations() const { return 1UL; }
119 virtual void setProperties( const PropertySet &opts );
120 virtual PropertySet getProperties() const;
121 virtual std::string printProperties() const;
122 //@}
125 /// \name Additional interface specific for JTree
126 //@{
127 /// Constructs a junction tree based on the cliques \a cl (corresponding to some elimination sequence).
128 /** First, constructs a weighted graph, where the nodes are the elements of \a cl, and
129 * each edge is weighted with the cardinality of the intersection of the state spaces of the nodes.
130 * Then, a maximal spanning tree for this weighted graph is calculated.
131 * Subsequently, a corresponding region graph is built:
132 * - the outer regions correspond with the cliques and have counting number 1;
133 * - the inner regions correspond with the seperators, i.e., the intersections of two
134 * cliques that are neighbors in the spanning tree, and have counting number -1;
135 * - inner and outer regions are connected by an edge if the inner region is a
136 * seperator for the outer region.
137 * Finally, Beliefs are constructed.
138 * If \a verify == \c true, checks whether each factor is subsumed by a clique.
139 */
140 void construct( const std::vector<VarSet> &cl, bool verify=false );
142 /// Constructs a junction tree based on the cliques \a cl (corresponding to some elimination sequence).
143 /** Invokes construct() and then constructs messages.
144 * \see construct()
145 */
146 void GenerateJT( const std::vector<VarSet> &cl );
148 /// Returns constant reference to the message from outer region \a alpha to its \a _beta 'th neighboring inner region
149 const Factor & message( size_t alpha, size_t _beta ) const { return _mes[alpha][_beta]; }
150 /// Returns reference to the message from outer region \a alpha to its \a _beta 'th neighboring inner region
151 Factor & message( size_t alpha, size_t _beta ) { return _mes[alpha][_beta]; }
153 /// Runs junction tree algorithm using HUGIN updates
154 /** \note The initial messages may be arbitrary.
155 */
156 void runHUGIN();
158 /// Runs junction tree algorithm using Shafer-Shenoy updates
159 /** \note The initial messages may be arbitrary.
160 */
161 void runShaferShenoy();
163 /// Finds an efficient subtree for calculating the marginal of the variables in \a vs
164 /** First, the current junction tree is reordered such that it gets as root the clique
165 * that has maximal state space overlap with \a vs. Then, the minimal subtree
166 * (starting from the root) is identified that contains all the variables in \a vs
167 * and also the outer region with index \a PreviousRoot (if specified). Finally,
168 * the current junction tree is reordered such that this minimal subtree comes
169 * before the other edges, and the size of the minimal subtree is returned.
170 */
171 size_t findEfficientTree( const VarSet& vs, RootedTree &Tree, size_t PreviousRoot=(size_t)-1 ) const;
173 /// Calculates the marginal of a set of variables (using cutset conditioning, if necessary)
174 /** \pre assumes that run() has been called already
175 */
176 Factor calcMarginal( const VarSet& vs );
178 /// Calculates the joint state of all variables that has maximum probability
179 /** \pre Assumes that run() has been called and that \a props.inference == \c MAXPROD
180 */
181 std::vector<std::size_t> findMaximum() const;
182 //@}
183 };
186 /// Calculates upper bound to the treewidth of a FactorGraph, using the MinFill heuristic
187 /** \relates JTree
188 * \return a pair (number of variables in largest clique, number of states in largest clique)
189 */
190 std::pair<size_t,size_t> boundTreewidth( const FactorGraph & fg );
193 } // end of namespace dai
196 #endif