Merge branch 'no-edges2'
[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 /// A RegionGraph is a bipartite graph consisting of outer regions (type FRegion) and inner regions (type Region)
100 class RegionGraph : public FactorGraph {
101 public:
102 BipartiteGraph G;
103 std::vector<FRegion> ORs;
104 std::vector<Region> IRs;
105
106 protected:
107 /// Give back the OR index that corresponds to a factor index
108 std::vector<size_t> fac2OR;
109
110
111 public:
112 /// Default constructor
113 RegionGraph() : FactorGraph(), G(), ORs(), IRs(), fac2OR() {}
114
115 /// Constructs a RegionGraph from a FactorGraph
116 RegionGraph( const FactorGraph &fg ) : FactorGraph(fg), G(), ORs(), IRs(), fac2OR() {}
117
118 /// Constructs a RegionGraph from a FactorGraph, a vector of outer regions, a vector of inner regions and a vector of edges
119 RegionGraph( const FactorGraph &fg, const std::vector<Region> &ors, const std::vector<Region> &irs, const std::vector<std::pair<size_t,size_t> > &edges );
120
121 /// Constructs a RegionGraph from a FactorGraph and a vector of outer VarSets (CVM style)
122 RegionGraph( const FactorGraph &fg, const std::vector<VarSet> &cl );
123
124 /// Copy constructor
125 RegionGraph( const RegionGraph &x ) : FactorGraph(x), G(x.G), ORs(x.ORs), IRs(x.IRs), fac2OR(x.fac2OR) {}
126
127 /// Assignment operator
128 RegionGraph & operator=( const RegionGraph &x ) {
129 if( this != &x ) {
130 FactorGraph::operator=( x );
131 G = x.G;
132 ORs = x.ORs;
133 IRs = x.IRs;
134 fac2OR = x.fac2OR;
135 }
136 return *this;
137 }
138
139 /// Provides read access to outer region
140 const FRegion & OR(size_t alpha) const { return ORs[alpha]; }
141 /// Provides access to outer region
142 FRegion & OR(size_t alpha) { return ORs[alpha]; }
143
144 /// Provides read access to inner region
145 const Region & IR(size_t beta) const { return IRs[beta]; }
146 /// Provides access to inner region
147 Region & IR(size_t beta) { return IRs[beta]; }
148
149 /// Returns number of outer regions
150 size_t nrORs() const { return ORs.size(); }
151 /// Returns number of inner regions
152 size_t nrIRs() const { return IRs.size(); }
153
154
155 /// Provides read access to neighbors of outer region
156 const Neighbors & nbOR( size_t alpha ) const { return G.nb1(alpha); }
157 /// Provides full access to neighbors of outer region
158 Neighbors & nbOR( size_t alpha ) { return G.nb1(alpha); }
159
160 /// Provides read access to neighbors of inner region
161 const Neighbors & nbIR( size_t beta ) const { return G.nb2(beta); }
162 /// Provides full access to neighbors of inner region
163 Neighbors & nbIR( size_t beta ) { return G.nb2(beta); }
164
165
166 /// Calculates counting numbers of inner regions based upon counting numbers of outer regions
167 void Calc_Counting_Numbers();
168 /// Check whether the counting numbers are valid
169 bool Check_Counting_Numbers();
170
171 /// Recompute all outer regions
172 void RecomputeORs();
173
174 /// Recompute all outer regions involving the variables in ns
175 void RecomputeORs( const VarSet & ns );
176
177 /// Recompute all outer regions involving factor I
178 void RecomputeOR( size_t I );
179
180 /// We have to overload FactorGraph::clamp because the corresponding outer regions have to be recomputed
181 void clamp( const Var &n, size_t i ) { FactorGraph::clamp( n, i ); RecomputeORs( n ); }
182
183 /// We have to overload FactorGraph::makeCavity because the corresponding outer regions have to be recomputed
184 void makeCavity( size_t i ) { FactorGraph::makeCavity( i ); RecomputeORs( var(i) ); }
185
186 /// We have to overload FactorGraph::undoProbs because the corresponding outer regions have to be recomputed
187 void undoProbs( const VarSet &ns ) { FactorGraph::undoProbs( ns ); RecomputeORs( ns ); }
188
189 /// We have to overload FactorGraph::undoProb because the corresponding outer regions have to be recomputed
190 void undoProb( size_t I ) { FactorGraph::undoProb( I ); RecomputeOR( I ); }
191
192 /// If updateFactor is called, we know that factor I has been changed and we should recompute the outer regions involving I
193 void updatedFactor( size_t I ) { RecomputeOR( I ); }
194
195 /// Send RegionGraph to output stream
196 friend std::ostream & operator << ( std::ostream & os, const RegionGraph & rg );
197 };
198
199
200 } // end of namespace dai
201
202
203 #endif