Fixed some bugs in the previous commit
[libdai.git] / include / dai / regiongraph.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
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]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 /// \file
13 /// \brief Defines classes Region, FRegion and RegionGraph, which implement a particular subclass of region graphs.
14
15
16 #ifndef __defined_libdai_regiongraph_h
17 #define __defined_libdai_regiongraph_h
18
19
20 #include <iostream>
21 #include <dai/bipgraph.h>
22 #include <dai/factorgraph.h>
23 #include <dai/weightedgraph.h>
24
25
26 namespace dai {
27
28
29 /// A Region is a set of variables with a counting number
30 class Region : public VarSet {
31 private:
32 /// Counting number
33 Real _c;
34
35 public:
36 /// Default constructor
37 Region() : VarSet(), _c(1.0) {}
38
39 /// Construct from a set of variables and a counting number
40 Region( const VarSet &x, Real c ) : VarSet(x), _c(c) {}
41
42 /// Returns constant reference to counting number
43 const Real & c() const { return _c; }
44
45 /// Returns reference to counting number
46 Real & c() { return _c; }
47 };
48
49
50 /// An FRegion is a factor with a counting number
51 class FRegion : public Factor {
52 private:
53 /// Counting number
54 Real _c;
55
56 public:
57 /// Default constructor
58 FRegion() : Factor(), _c(1.0) {}
59
60 /// Constructs from a factor and a counting number
61 FRegion( const Factor & x, Real c ) : Factor(x), _c(c) {}
62
63 /// Returns constant reference to counting number
64 const Real & c() const { return _c; }
65
66 /// Returns reference to counting number
67 Real & c() { return _c; }
68 };
69
70
71 /// A RegionGraph combines a bipartite graph consisting of outer regions (type FRegion) and inner regions (type Region) with a FactorGraph
72 /** A RegionGraph inherits from a FactorGraph and adds additional structure in the form of a "region graph". Our definition of region graph
73 * is inspired by [\ref HAK03], which is less general than the definition given in [\ref YFW05].
74 *
75 * The extra structure described by a RegionGraph compared with that described by a FactorGraph is:
76 * - a set of outer regions (indexed by \f$\alpha\f$), where each outer region consists of
77 * - a factor defined on a subset of variables
78 * - a counting number
79 * - a set of inner regions (indexed by \f$\beta\f$), where each inner region consists of
80 * - a subset of variables
81 * - a counting number
82 * - edges between inner and outer regions
83 *
84 * Each factor in the factor graph belongs to an outer region; normally, the factor contents
85 * of an outer region would be the product of all the factors that belong to that region.
86 */
87 class RegionGraph : public FactorGraph {
88 protected:
89 /// Stores the neighborhood structure
90 BipartiteGraph _G;
91
92 /// The outer regions (corresponding to nodes of type 1)
93 std::vector<FRegion> _ORs;
94
95 /// The inner regions (corresponding to nodes of type 2)
96 std::vector<Region> _IRs;
97
98 /// Stores for each factor index the index of the outer region it belongs to
99 std::vector<size_t> _fac2OR;
100
101
102 public:
103 /// \name Constructors and destructors
104 //@{
105 /// Default constructor
106 RegionGraph() : FactorGraph(), _G(), _ORs(), _IRs(), _fac2OR() {}
107
108 /// Constructs a region graph from a factor graph, a vector of outer regions, a vector of inner regions and a vector of edges
109 /** The counting numbers for the outer regions are set to 1.
110 */
111 RegionGraph( const FactorGraph& fg, const std::vector<VarSet>& ors, const std::vector<Region>& irs, const std::vector<std::pair<size_t,size_t> >& edges ) : FactorGraph(), _G(), _ORs(), _IRs(), _fac2OR() {
112 construct( fg, ors, irs, edges );
113
114 // Check counting numbers
115 #ifdef DAI_DEBUG
116 checkCountingNumbers();
117 #endif
118 }
119
120 /// Constructs a region graph from a factor graph and a vector of outer clusters (CVM style)
121 /** The region graph is constructed as in the Cluster Variation Method.
122 *
123 * The outer regions have as variable subsets the clusters specified in \a cl.
124 * Each factor in the factor graph \a fg is assigned to one of the outer regions.
125 * Each outer region gets counting number 1.
126 *
127 * The inner regions are (repeated) intersections of outer regions.
128 * An inner and an outer region are connected if the variables in the inner region form a
129 * subset of the variables in the outer region. The counting numbers for the inner
130 * regions are calculated by calcCountingNumbers() and satisfy the Moebius formula.
131 */
132 RegionGraph( const FactorGraph& fg, const std::vector<VarSet>& cl ) : FactorGraph(), _G(), _ORs(), _IRs(), _fac2OR() {
133 constructCVM( fg, cl );
134
135 // Check counting numbers
136 #ifdef DAI_DEBUG
137 checkCountingNumbers();
138 #endif
139 }
140
141 /// Clone \c *this (virtual copy constructor)
142 virtual RegionGraph* clone() const { return new RegionGraph(*this); }
143 //@}
144
145 /// \name Queries
146 //@{
147 /// Returns number of outer regions
148 size_t nrORs() const { return _ORs.size(); }
149 /// Returns number of inner regions
150 size_t nrIRs() const { return _IRs.size(); }
151
152 /// Returns constant reference to outer region \a alpha
153 const FRegion& OR( size_t alpha ) const {
154 DAI_DEBASSERT( alpha < nrORs() );
155 return _ORs[alpha];
156 }
157 /// Returns reference to outer region \a alpha
158 FRegion& OR( size_t alpha ) {
159 DAI_DEBASSERT( alpha < nrORs() );
160 return _ORs[alpha];
161 }
162
163 /// Returns constant reference to inner region \a beta
164 const Region& IR( size_t beta ) const {
165 DAI_DEBASSERT( beta < nrIRs() );
166 return _IRs[beta];
167 }
168 /// Returns reference to inner region \a beta
169 Region& IR( size_t beta ) {
170 DAI_DEBASSERT( beta < nrIRs() );
171 return _IRs[beta];
172 }
173
174 /// Returns the index of the outer region to which the \a I 'th factor corresponds
175 size_t fac2OR( size_t I ) const {
176 DAI_DEBASSERT( I < nrFactors() );
177 DAI_DEBASSERT( I < _fac2OR.size() );
178 return _fac2OR[I];
179 }
180
181 /// Returns constant reference to the neighbors of outer region \a alpha
182 const Neighbors& nbOR( size_t alpha ) const { return _G.nb1(alpha); }
183 /// Returns constant reference to the neighbors of inner region \a beta
184 const Neighbors& nbIR( size_t beta ) const { return _G.nb2(beta); }
185
186 /// Check whether the counting numbers are valid
187 /** Counting numbers are said to be (variable) valid if for each variable \f$x\f$,
188 * \f[\sum_{\alpha \ni x} c_\alpha + \sum_{\beta \ni x} c_\beta = 1\f]
189 * or in words, if the sum of the counting numbers of the regions
190 * that contain the variable equals one.
191 */
192 bool checkCountingNumbers() const;
193 //@}
194
195 /// \name Operations
196 //@{
197 /// Set the content of the \a I 'th factor and make a backup of its old content if \a backup == \c true
198 virtual void setFactor( size_t I, const Factor& newFactor, bool backup = false ) {
199 FactorGraph::setFactor( I, newFactor, backup );
200 RecomputeOR( I );
201 }
202
203 /// Set the contents of all factors as specified by \a facs and make a backup of the old contents if \a backup == \c true
204 virtual void setFactors( const std::map<size_t, Factor>& facs, bool backup = false ) {
205 FactorGraph::setFactors( facs, backup );
206 VarSet ns;
207 for( std::map<size_t, Factor>::const_iterator fac = facs.begin(); fac != facs.end(); fac++ )
208 ns |= fac->second.vars();
209 RecomputeORs( ns );
210 }
211
212 /// Recompute all outer regions
213 /** The factor contents of each outer region is set to the product of the factors belonging to that region.
214 */
215 void RecomputeORs();
216
217 /// Recompute all outer regions involving the variables in \a vs
218 /** The factor contents of each outer region involving at least one of the variables in \a vs is set to the product of the factors belonging to that region.
219 */
220 void RecomputeORs( const VarSet& vs );
221
222 /// Recompute all outer regions involving factor \a I
223 /** The factor contents of each outer region involving the \a I 'th factor is set to the product of the factors belonging to that region.
224 */
225 void RecomputeOR( size_t I );
226
227 /// Calculates counting numbers of inner regions based upon counting numbers of outer regions
228 /** The counting numbers of the inner regions are set using the Moebius inversion formula:
229 * \f[ c_\beta := 1 - \sum_{\gamma \in \mathrm{an}(\beta)} c_\gamma \f]
230 * where \f$\mathrm{an}(\beta)\f$ are the ancestors of inner region \f$\beta\f$ according to
231 * the partial ordering induced by the subset relation (i.e., a region is a child of another
232 * region if its variables are a subset of the variables of its parent region).
233 */
234 void calcCountingNumbers();
235 //@}
236
237 /// \name Input/output
238 //@{
239 /// Writes a RegionGraph to an output stream
240 friend std::ostream& operator << ( std::ostream& os, const RegionGraph& rg );
241 //@}
242
243 protected:
244 /// Helper function for constructors
245 void construct( const FactorGraph& fg, const std::vector<VarSet>& ors, const std::vector<Region>& irs, const std::vector<std::pair<size_t,size_t> >& edges );
246
247 /// Helper function for constructors (CVM style)
248 void constructCVM( const FactorGraph& fg, const std::vector<VarSet>& cl );
249 };
250
251
252 } // end of namespace dai
253
254
255 #endif