Updated copyrights
[libdai.git] / include / dai / regiongraph.h
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #ifndef __defined_libdai_regiongraph_h
24 #define __defined_libdai_regiongraph_h
25
26
27 #include <iostream>
28 #include <dai/bipgraph.h>
29 #include <dai/factorgraph.h>
30 #include <dai/weightedgraph.h>
31
32
33 namespace dai {
34
35
36 /// A Region is a set of variables with a counting number
37 class Region : public VarSet {
38 private:
39 /// Counting number
40 double _c;
41
42 public:
43 /// Default constructor
44 Region() : VarSet(), _c(1.0) {}
45
46 /// Construct Region from a VarSet and a counting number
47 Region(const VarSet & x, double c) : VarSet(x), _c(c) {}
48
49 /// Copy constructor
50 Region(const Region & x) : VarSet(x), _c(x._c) {}
51
52 /// Assignment operator
53 Region & operator=(const Region & x) {
54 if( this != &x ) {
55 VarSet::operator=(x);
56 _c = x._c;
57 }
58 return *this;
59 }
60
61 /// Provide read access to counting number
62 const double & c() const { return _c; }
63 /// Provide full access to counting number
64 double & c() { return _c; }
65 };
66
67
68 /// A FRegion is a factor with a counting number
69 class FRegion : public Factor {
70 private:
71 /// Counting number
72 double _c;
73
74 public:
75 /// Default constructor
76 FRegion() : Factor(), _c(1.0) {}
77
78 /// Constructs FRegion from a Factor and a counting number
79 FRegion( const Factor & x, double c ) : Factor(x), _c(c) {}
80
81 /// Copy constructor
82 FRegion( const FRegion & x ) : Factor(x), _c(x._c) {}
83
84 /// Assignment operator
85 FRegion & operator=(const FRegion & x) {
86 if( this != &x ) {
87 Factor::operator=(x);
88 _c = x._c;
89 }
90 return *this;
91 }
92
93 /// Provide read access to counting number
94 const double & c() const { return _c; }
95 /// Provide full access to counting number
96 double & c() { return _c; }
97 };
98
99
100 /// A RegionGraph is a bipartite graph consisting of outer regions (type FRegion) and inner regions (type Region)
101 class RegionGraph : public FactorGraph {
102 public:
103 BipartiteGraph G;
104 std::vector<FRegion> ORs;
105 std::vector<Region> IRs;
106
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 /// Clone *this (virtual copy constructor)
140 virtual RegionGraph* clone() const { return new RegionGraph(*this); }
141
142 /// Create (virtual default constructor)
143 virtual RegionGraph* create() const { return new RegionGraph(); }
144
145 /// Set the content of the I'th factor and make a backup of its old content if backup == true
146 virtual void setFactor( size_t I, const Factor &newFactor, bool backup = false ) {
147 FactorGraph::setFactor( I, newFactor, backup );
148 RecomputeOR( I );
149 }
150
151 /// Set the contents of all factors as specified by facs and make a backup of the old contents if backup == true
152 virtual void setFactors( const std::map<size_t, Factor> & facs, bool backup = false ) {
153 FactorGraph::setFactors( facs, backup );
154 VarSet ns;
155 for( std::map<size_t, Factor>::const_iterator fac = facs.begin(); fac != facs.end(); fac++ )
156 ns |= fac->second.vars();
157 RecomputeORs( ns );
158 }
159
160
161 /// Provides read access to outer region
162 const FRegion & OR(size_t alpha) const { return ORs[alpha]; }
163 /// Provides access to outer region
164 FRegion & OR(size_t alpha) { return ORs[alpha]; }
165
166 /// Provides read access to inner region
167 const Region & IR(size_t beta) const { return IRs[beta]; }
168 /// Provides access to inner region
169 Region & IR(size_t beta) { return IRs[beta]; }
170
171 /// Returns number of outer regions
172 size_t nrORs() const { return ORs.size(); }
173 /// Returns number of inner regions
174 size_t nrIRs() const { return IRs.size(); }
175
176
177 /// Provides read access to neighbors of outer region
178 const Neighbors & nbOR( size_t alpha ) const { return G.nb1(alpha); }
179 /// Provides full access to neighbors of outer region
180 Neighbors & nbOR( size_t alpha ) { return G.nb1(alpha); }
181
182 /// Provides read access to neighbors of inner region
183 const Neighbors & nbIR( size_t beta ) const { return G.nb2(beta); }
184 /// Provides full access to neighbors of inner region
185 Neighbors & nbIR( size_t beta ) { return G.nb2(beta); }
186
187
188 /// Calculates counting numbers of inner regions based upon counting numbers of outer regions
189 void Calc_Counting_Numbers();
190 /// Check whether the counting numbers are valid
191 bool Check_Counting_Numbers();
192
193 /// Recompute all outer regions
194 void RecomputeORs();
195
196 /// Recompute all outer regions involving the variables in ns
197 void RecomputeORs( const VarSet & ns );
198
199 /// Recompute all outer regions involving factor I
200 void RecomputeOR( size_t I );
201
202 /// Send RegionGraph to output stream
203 friend std::ostream & operator << ( std::ostream & os, const RegionGraph & rg );
204 };
205
206
207 } // end of namespace dai
208
209
210 #endif