libDAI
jtree.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4  *
5  * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6  */
7 
8 
11 
12 
13 #ifndef __defined_libdai_jtree_h
14 #define __defined_libdai_jtree_h
15 
16 
17 #include <dai/dai_config.h>
18 #ifdef DAI_WITH_JTREE
19 
20 
21 #include <vector>
22 #include <string>
23 #include <dai/daialg.h>
24 #include <dai/varset.h>
25 #include <dai/regiongraph.h>
26 #include <dai/factorgraph.h>
27 #include <dai/clustergraph.h>
28 #include <dai/weightedgraph.h>
29 #include <dai/enum.h>
30 #include <dai/properties.h>
31 
32 
33 namespace dai {
34 
35 
37 
45 class JTree : public DAIAlgRG {
46  private:
48  std::vector<std::vector<Factor> > _mes;
49 
52 
53  public:
56 
58  std::vector<Factor> Qa;
59 
61  std::vector<Factor> Qb;
62 
64  struct Properties {
66 
70  DAI_ENUM(UpdateType,HUGIN,SHSH);
71 
73 
77  DAI_ENUM(InfType,SUMPROD,MAXPROD);
78 
80 
89  DAI_ENUM(HeuristicType,MINNEIGHBORS,MINWEIGHT,MINFILL,WEIGHTEDMINFILL);
90 
92  size_t verbose;
93 
95  UpdateType updates;
96 
98  InfType inference;
99 
101  HeuristicType heuristic;
102 
104  size_t maxmem;
105  } props;
106 
107  public:
109 
110  JTree() : DAIAlgRG(), _mes(), _logZ(), RTree(), Qa(), Qb(), props() {}
112 
114 
118  JTree( const FactorGraph &fg, const PropertySet &opts, bool automatic=true );
120 
121 
123 
124  virtual JTree* clone() const { return new JTree(*this); }
125  virtual JTree* construct( const FactorGraph &fg, const PropertySet &opts ) const { return new JTree( fg, opts ); }
126  virtual std::string name() const { return "JTREE"; }
127  virtual Factor belief( const VarSet &vs ) const;
128  virtual std::vector<Factor> beliefs() const;
129  virtual Real logZ() const;
132  std::vector<size_t> findMaximum() const;
133  virtual void init() {}
134  virtual void init( const VarSet &/*ns*/ ) {}
135  virtual Real run();
136  virtual Real maxDiff() const { return 0.0; }
137  virtual size_t Iterations() const { return 1UL; }
138  virtual void setProperties( const PropertySet &opts );
139  virtual PropertySet getProperties() const;
140  virtual std::string printProperties() const;
142 
143 
145 
146 
160  void construct( const FactorGraph &fg, const std::vector<VarSet> &cl, bool verify=false );
161 
163 
166  void GenerateJT( const FactorGraph &fg, const std::vector<VarSet> &cl );
167 
169  const Factor & message( size_t alpha, size_t _beta ) const { return _mes[alpha][_beta]; }
171  Factor & message( size_t alpha, size_t _beta ) { return _mes[alpha][_beta]; }
172 
174 
176  void runHUGIN();
177 
179 
181  void runShaferShenoy();
182 
184 
191  size_t findEfficientTree( const VarSet& vs, RootedTree &Tree, size_t PreviousRoot=(size_t)-1 ) const;
192 
194 
196  Factor calcMarginal( const VarSet& vs );
198 };
199 
200 
202 
209 std::pair<size_t,BigInt> boundTreewidth( const FactorGraph &fg, greedyVariableElimination::eliminationCostFunction fn, size_t maxStates=0 );
210 
211 
212 } // end of namespace dai
213 
214 
215 #endif
216 
217 
218 #endif
virtual std::string name() const
Returns the name of the algorithm.
Definition: jtree.h:126
Parameters for JTree.
Definition: jtree.h:64
Factor & message(size_t alpha, size_t _beta)
Returns reference to the message from outer region alpha to its _beta 'th neighboring inner region...
Definition: jtree.h:171
Exact inference algorithm using junction tree.
Definition: jtree.h:45
Factor calcMarginal(const VarSet &vs)
Calculates the marginal of a set of variables (using cutset conditioning, if necessary) ...
Definition: jtree.cpp:466
virtual PropertySet getProperties() const
Returns parameters of this inference algorithm converted into a PropertySet.
Definition: jtree.cpp:47
std::vector< size_t > findMaximum() const
Definition: jtree.cpp:585
Defines some utility functions for (weighted) undirected graphs, trees and rooted trees...
Represents a factor graph.
Definition: factorgraph.h:65
Defines the VarSet class, which represents a set of random variables.
std::vector< Factor > Qa
Outer region beliefs.
Definition: jtree.h:58
virtual size_t Iterations() const
Returns number of iterations done (one iteration passes over the complete factorgraph).
Definition: jtree.h:137
FactorGraph & fg()
Returns reference to underlying FactorGraph.
Definition: daialg.h:221
double Real
Real number (alias for double, which could be changed to long double if necessary) ...
Definition: util.h:98
UpdateType updates
Type of updates.
Definition: jtree.h:95
void runHUGIN()
Runs junction tree algorithm using HUGIN (message-free) updates.
Definition: jtree.cpp:265
JTree()
Default constructor.
Definition: jtree.h:111
virtual std::string printProperties() const
Returns parameters of this inference algorithm formatted as a string in the format "[key1=val1...
Definition: jtree.cpp:58
virtual void setProperties(const PropertySet &opts)
Set parameters of this inference algorithm.
Definition: jtree.cpp:24
virtual Real logZ() const
Returns the logarithm of the (approximated) partition sum (normalizing constant of the factor graph)...
Definition: jtree.cpp:384
size_t(* eliminationCostFunction)(const ClusterGraph &, size_t)
Type of cost functions to be used for greedy variable elimination.
Definition: clustergraph.h:289
void runShaferShenoy()
Runs junction tree algorithm using Shafer-Shenoy updates.
Definition: jtree.cpp:312
size_t maxmem
Maximum memory to use in bytes (0 means unlimited)
Definition: jtree.h:104
size_t verbose
Verbosity (amount of output sent to stderr)
Definition: jtree.h:92
virtual std::vector< Factor > beliefs() const
Returns all beliefs (approximate marginal probability distributions) calculated by the algorithm...
Definition: jtree.cpp:255
Defines the DAI_ENUM macro, which can be used to define an enum with additional functionality.
Represents a set of variables.
Definition: varset.h:94
HeuristicType heuristic
Heuristic to use for constructing the junction tree.
Definition: jtree.h:101
InfType inference
Type of inference.
Definition: jtree.h:98
virtual void init(const VarSet &)
Initializes all data structures corresponding to some set of variables.
Definition: jtree.h:134
std::vector< Factor > Qb
Inner region beliefs.
Definition: jtree.h:61
const Factor & message(size_t alpha, size_t _beta) const
Returns constant reference to the message from outer region alpha to its _beta 'th neighboring inner ...
Definition: jtree.h:169
Real _logZ
Stores the logarithm of the partition sum.
Definition: jtree.h:51
Defines class ClusterGraph, which is used by JTree, TreeEP and HAK.
DAI_ENUM(UpdateType, HUGIN, SHSH)
Enumeration of possible JTree updates.
virtual Real run()
Runs the approximate inference algorithm.
Definition: jtree.cpp:375
Defines the Property and PropertySet classes, which are mainly used for managing parameters of infere...
Defines classes Region, FRegion and RegionGraph, which implement a particular subclass of region grap...
void GenerateJT(const FactorGraph &fg, const std::vector< VarSet > &cl)
Constructs a junction tree based on the cliques cl (corresponding to some elimination sequence)...
Definition: jtree.cpp:209
Represents a rooted tree, implemented as a vector of directed edges.
Definition: weightedgraph.h:157
virtual void init()
Initializes all data structures of the approximate inference algorithm.
Definition: jtree.h:133
Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue) ...
Definition: properties.h:73
Namespace for libDAI.
Definition: alldai.cpp:16
Defines the FactorGraph class, which represents factor graphs (e.g., Bayesian networks or Markov rand...
std::vector< std::vector< Factor > > _mes
Stores the messages.
Definition: jtree.h:48
Defines the general interface for inference methods in libDAI (classes InfAlg, DaiAlg<>, DaiAlgFG and DaiAlgRG).
virtual Factor belief(const VarSet &vs) const
Returns the (approximate) marginal probability distribution of a set of variables.
Definition: jtree.cpp:227
Allows the user to specify which algorithms will be built into libDAI.
virtual JTree * clone() const
Returns a pointer to a new, cloned copy of *this (i.e., virtual copy constructor) ...
Definition: jtree.h:124
Combines the abstract base class InfAlg with a graphical model (e.g., a FactorGraph or RegionGraph)...
Definition: daialg.h:207
size_t findEfficientTree(const VarSet &vs, RootedTree &Tree, size_t PreviousRoot=(size_t)-1) const
Finds an efficient subtree for calculating the marginal of the variables in vs.
Definition: jtree.cpp:398
RootedTree RTree
The junction tree (stored as a rooted tree)
Definition: jtree.h:55
virtual Real maxDiff() const
Returns maximum difference between single variable beliefs in the last iteration. ...
Definition: jtree.h:136
virtual JTree * construct(const FactorGraph &fg, const PropertySet &opts) const
Returns a pointer to a newly constructed inference algorithm.
Definition: jtree.h:125