Partial adoption of contributions by Giuseppe:
[libdai.git] / include / dai / regiongraph.h
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 #ifndef __defined_libdai_regiongraph_h
23 #define __defined_libdai_regiongraph_h
24
25
26 #include <iostream>
27 #include <dai/bipgraph.h>
28 #include <dai/factorgraph.h>
29 #include <dai/weightedgraph.h>
30
31
32 namespace dai {
33
34
35 /// A Region is a set of variables with a counting number
36 class Region : public VarSet {
37 protected:
38 /// Counting number
39 double _c;
40
41 public:
42 /// Default constructor
43 Region() : VarSet(), _c(1.0) {}
44
45 /// Construct Region from a VarSet and a counting number
46 Region(const VarSet & x, double c) : VarSet(x), _c(c) {}
47
48 /// Copy constructor
49 Region(const Region & x) : VarSet(x), _c(x._c) {}
50
51 /// Assignment operator
52 Region & operator=(const Region & x) {
53 if( this != &x ) {
54 VarSet::operator=(x);
55 _c = x._c;
56 }
57 return *this;
58 }
59
60 /// Provide read access to counting number
61 const double & c() const { return _c; }
62 /// Provide full access to counting number
63 double & c() { return _c; }
64 };
65
66
67 /// A FRegion is a factor with a counting number
68 class FRegion : public Factor {
69 protected:
70 /// Counting number
71 double _c;
72
73 public:
74 /// Default constructor
75 FRegion() : Factor(), _c(1.0) {}
76
77 /// Constructs FRegion from a Factor and a counting number
78 FRegion( const Factor & x, double c ) : Factor(x), _c(c) {}
79
80 /// Copy constructor
81 FRegion( const FRegion & x ) : Factor(x), _c(x._c) {}
82
83 /// Assignment operator
84 FRegion & operator=(const FRegion & x) {
85 if( this != &x ) {
86 Factor::operator=(x);
87 _c = x._c;
88 }
89 return *this;
90 }
91
92 /// Provide read access to counting number
93 const double & c() const { return _c; }
94 /// Provide full access to counting number
95 double & c() { return _c; }
96 };
97
98
99 typedef BipartiteGraph<FRegion,Region> BipRegGraph;
100
101
102 /// A RegionGraph is a bipartite graph consisting of outer regions (type FRegion) and inner regions (type Region)
103 class RegionGraph : public FactorGraph, BipRegGraph {
104 public:
105 typedef BipRegGraph::_nb_t R_nb_t;
106 typedef R_nb_t::const_iterator R_nb_cit;
107 typedef BipRegGraph::_edge_t R_edge_t;
108
109
110 protected:
111 /// Give back the OR index that corresponds to a factor index
112 typedef std::map<size_t,size_t>::const_iterator fac2OR_cit;
113 std::map<size_t,size_t> _fac2OR;
114
115
116 public:
117 /// Default constructor
118 RegionGraph() : FactorGraph(), BipRegGraph(), _fac2OR() {}
119
120 /// Constructs a RegionGraph from a FactorGraph
121 RegionGraph(const FactorGraph & fg) : FactorGraph(fg), BipRegGraph(), _fac2OR() {}
122
123 /// Constructs a RegionGraph from a FactorGraph, a vector of outer regions, a vector of inner regions and a vector of edges
124 RegionGraph(const FactorGraph & fg, const std::vector<Region> & ors, const std::vector<Region> & irs, const std::vector<R_edge_t> & edges);
125
126 /// Constructs a RegionGraph from a FactorGraph and a vector of outer VarSets (CVM style)
127 RegionGraph(const FactorGraph & fg, const std::vector<VarSet> & cl);
128
129 /// Copy constructor
130 RegionGraph(const RegionGraph & x) : FactorGraph(x), BipRegGraph(x), _fac2OR(x._fac2OR) {}
131
132 /// Assignment operator
133 RegionGraph & operator=(const RegionGraph & x) {
134 if( this != &x ) {
135 FactorGraph::operator=(x);
136 BipRegGraph::operator=(x);
137 _fac2OR = x._fac2OR;
138 }
139 return *this;
140 }
141
142 /// Provides read access to outer region
143 const FRegion & OR(long alpha) const { return BipRegGraph::V1(alpha); }
144 /// Provides access to outer region
145 FRegion & OR(long alpha) { return BipRegGraph::V1(alpha); }
146 /// Provides read access to all outer regions
147 const std::vector<FRegion> & ORs() const { return BipRegGraph::V1s(); }
148 /// Provides access to all outer regions
149 std::vector<FRegion> &ORs() { return BipRegGraph::V1s(); }
150 /// Returns number of outer regions
151 size_t nr_ORs() const { return BipRegGraph::V1s().size(); }
152
153 /// Provides read access to inner region
154 const Region & IR(long beta) const { return BipRegGraph::V2(beta); }
155 /// Provides access to inner region
156 Region & IR(long beta) { return BipRegGraph::V2(beta); }
157 /// Provides read access to all inner regions
158 const std::vector<Region> & IRs() const { return BipRegGraph::V2s(); }
159 /// Provides access to all inner regions
160 std::vector<Region> & IRs() { return BipRegGraph::V2s(); }
161 /// Returns number of inner regions
162 size_t nr_IRs() const { return BipRegGraph::V2s().size(); }
163
164 /// Provides read access to edge
165 const R_edge_t & Redge(size_t ind) const { return BipRegGraph::edge(ind); }
166 /// Provides full access to edge
167 R_edge_t & Redge(size_t ind) { return BipRegGraph::edge(ind); }
168 /// Provides read access to all edges
169 const std::vector<R_edge_t> & Redges() const { return BipRegGraph::edges(); }
170 /// Provides full access to all edges
171 std::vector<R_edge_t> & Redges() { return BipRegGraph::edges(); }
172 /// Returns number of edges
173 size_t nr_Redges() const { return BipRegGraph::edges().size(); }
174
175 /// Provides read access to neighbours of outer region
176 const R_nb_t & nbOR( size_t i1 ) const { return BipRegGraph::nb1(i1); }
177 /// Provides full access to neighbours of outer region
178 R_nb_t & nbOR( size_t i1 ) { return BipRegGraph::nb1(i1); }
179
180 /// Provides read access to neighbours of inner region
181 const R_nb_t & nbIR( size_t i2 ) const { return BipRegGraph::nb2(i2); }
182 /// Provides full access to neighbours of inner region
183 R_nb_t & nbIR( size_t i2 ) { return BipRegGraph::nb2(i2); }
184
185 /// Converts the pair of outer/inner region indices (i1,i2) to the corresponding edge index
186 size_t ORIR2E( const size_t i1, const size_t i2 ) const { return BipRegGraph::VV2E(i1, i2); }
187
188 void Regenerate() { BipRegGraph::Regenerate(); }
189
190
191 /// Calculates counting numbers of inner regions based upon counting numbers of outer regions
192 void Calc_Counting_Numbers();
193 /// Check whether the counting numbers are valid
194 bool Check_Counting_Numbers();
195
196 /// Recompute all outer regions
197 void RecomputeORs();
198
199 /// Recompute all outer regions involving the variables in ns
200 void RecomputeORs( const VarSet & ns );
201
202 /// Recompute all outer regions involving factor I
203 void RecomputeOR( size_t I );
204
205 /// We have to overload FactorGraph::clamp because the corresponding outer regions have to be recomputed
206 void clamp( const Var &n, size_t i ) { FactorGraph::clamp( n, i ); RecomputeORs( n ); }
207
208 /// We have to overload FactorGraph::makeCavity because the corresponding outer regions have to be recomputed
209 void makeCavity( const Var &n ) { FactorGraph::makeCavity( n ); RecomputeORs( n ); }
210
211 /// We have to overload FactorGraph::makeFactorCavity because the corresponding outer regions have to be recomputed
212 void makeFactorCavity( size_t I ) { FactorGraph::makeFactorCavity( I ); RecomputeOR( I ); }
213
214 /// We have to overload FactorGraph::undoProbs because the corresponding outer regions have to be recomputed
215 void undoProbs( const VarSet &ns ) { FactorGraph::undoProbs( ns ); RecomputeORs( ns ); }
216
217 /// We have to overload FactorGraph::undoProb because the corresponding outer regions have to be recomputed
218 void undoProb( size_t I ) { FactorGraph::undoProb( I ); RecomputeOR( I ); }
219
220 /// If updateFactor is called, we know that factor I has been changed and we should recompute the outer regions involving I
221 void updatedFactor( size_t I ) { RecomputeOR( I ); }
222
223 /// Send RegionGraph to output stream
224 friend std::ostream & operator << ( std::ostream & os, const RegionGraph & rg );
225 };
226
227
228 } // end of namespace dai
229
230
231 #endif