Fixed tabs and trailing whitespaces
[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 /// \file
24 /// \brief Defines classes Region, FRegion and RegionGraph
25 /// \todo Improve documentation
26
27
28 #ifndef __defined_libdai_regiongraph_h
29 #define __defined_libdai_regiongraph_h
30
31
32 #include <iostream>
33 #include <dai/bipgraph.h>
34 #include <dai/factorgraph.h>
35 #include <dai/weightedgraph.h>
36
37
38 namespace dai {
39
40
41 /// A Region is a set of variables with a counting number
42 class Region : public VarSet {
43 private:
44 /// Counting number
45 double _c;
46
47 public:
48 /// Default constructor
49 Region() : VarSet(), _c(1.0) {}
50
51 /// Construct Region from a VarSet and a counting number
52 Region(const VarSet & x, double c) : VarSet(x), _c(c) {}
53
54 /// Provide read access to counting number
55 const double & c() const { return _c; }
56 /// Provide full access to counting number
57 double & c() { return _c; }
58 };
59
60
61 /// A FRegion is a factor with a counting number
62 class FRegion : public Factor {
63 private:
64 /// Counting number
65 double _c;
66
67 public:
68 /// Default constructor
69 FRegion() : Factor(), _c(1.0) {}
70
71 /// Constructs FRegion from a Factor and a counting number
72 FRegion( const Factor & x, double c ) : Factor(x), _c(c) {}
73
74 /// Provide read access to counting number
75 const double & c() const { return _c; }
76 /// Provide full access to counting number
77 double & c() { return _c; }
78 };
79
80
81 /// A RegionGraph is a bipartite graph consisting of outer regions (type FRegion) and inner regions (type Region)
82 class RegionGraph : public FactorGraph {
83 public:
84 /// Stores the neighborhood structure
85 BipartiteGraph G;
86
87 /// The outer regions (corresponding to nodes of type 1)
88 std::vector<FRegion> ORs;
89
90 /// The inner regions (corresponding to nodes of type 2)
91 std::vector<Region> IRs;
92
93 /// Stores for each factor index the index of the outer region it belongs to
94 std::vector<size_t> fac2OR;
95
96
97 public:
98 /// Default constructor
99 RegionGraph() : FactorGraph(), G(), ORs(), IRs(), fac2OR() {}
100
101 /// Constructs a RegionGraph from a FactorGraph
102 RegionGraph( const FactorGraph &fg ) : FactorGraph(fg), G(), ORs(), IRs(), fac2OR() {}
103
104 /// Constructs a RegionGraph from a FactorGraph, a vector of outer regions, a vector of inner regions and a vector of edges
105 RegionGraph( const FactorGraph &fg, const std::vector<Region> &ors, const std::vector<Region> &irs, const std::vector<std::pair<size_t,size_t> > &edges );
106
107 /// Constructs a RegionGraph from a FactorGraph and a vector of outer VarSets (CVM style)
108 RegionGraph( const FactorGraph &fg, const std::vector<VarSet> &cl );
109
110 /// Clone *this (virtual copy constructor)
111 virtual RegionGraph* clone() const { return new RegionGraph(*this); }
112
113 /// Set the content of the I'th factor and make a backup of its old content if backup == true
114 virtual void setFactor( size_t I, const Factor &newFactor, bool backup = false ) {
115 FactorGraph::setFactor( I, newFactor, backup );
116 RecomputeOR( I );
117 }
118
119 /// Set the contents of all factors as specified by facs and make a backup of the old contents if backup == true
120 virtual void setFactors( const std::map<size_t, Factor> & facs, bool backup = false ) {
121 FactorGraph::setFactors( facs, backup );
122 VarSet ns;
123 for( std::map<size_t, Factor>::const_iterator fac = facs.begin(); fac != facs.end(); fac++ )
124 ns |= fac->second.vars();
125 RecomputeORs( ns );
126 }
127
128
129 /// Provides read access to outer region
130 const FRegion & OR(size_t alpha) const { return ORs[alpha]; }
131 /// Provides access to outer region
132 FRegion & OR(size_t alpha) { return ORs[alpha]; }
133
134 /// Provides read access to inner region
135 const Region & IR(size_t beta) const { return IRs[beta]; }
136 /// Provides access to inner region
137 Region & IR(size_t beta) { return IRs[beta]; }
138
139 /// Returns number of outer regions
140 size_t nrORs() const { return ORs.size(); }
141 /// Returns number of inner regions
142 size_t nrIRs() const { return IRs.size(); }
143
144
145 /// Provides read access to neighbors of outer region
146 const Neighbors & nbOR( size_t alpha ) const { return G.nb1(alpha); }
147 /// Provides full access to neighbors of outer region
148 Neighbors & nbOR( size_t alpha ) { return G.nb1(alpha); }
149
150 /// Provides read access to neighbors of inner region
151 const Neighbors & nbIR( size_t beta ) const { return G.nb2(beta); }
152 /// Provides full access to neighbors of inner region
153 Neighbors & nbIR( size_t beta ) { return G.nb2(beta); }
154
155
156 /// Calculates counting numbers of inner regions based upon counting numbers of outer regions
157 void Calc_Counting_Numbers();
158 /// Check whether the counting numbers are valid
159 bool Check_Counting_Numbers();
160
161 /// Recompute all outer regions
162 void RecomputeORs();
163
164 /// Recompute all outer regions involving the variables in ns
165 void RecomputeORs( const VarSet & ns );
166
167 /// Recompute all outer regions involving factor I
168 void RecomputeOR( size_t I );
169
170 // Friends
171 friend std::ostream & operator << ( std::ostream & os, const RegionGraph & rg );
172 };
173
174
175 } // end of namespace dai
176
177
178 #endif