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