1 /* This file is part of libDAI - http://www.libdai.org/
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.
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
13 /// \brief Defines classes Region, FRegion and RegionGraph
14 /// \todo Improve documentation
17 #ifndef __defined_libdai_regiongraph_h
18 #define __defined_libdai_regiongraph_h
22 #include <dai/bipgraph.h>
23 #include <dai/factorgraph.h>
24 #include <dai/weightedgraph.h>
30 /// A Region is a set of variables with a counting number
31 class Region
: public VarSet
{
37 /// Default constructor
38 Region() : VarSet(), _c(1.0) {}
40 /// Construct Region from a VarSet and a counting number
41 Region(const VarSet
& x
, double c
) : VarSet(x
), _c(c
) {}
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
; }
50 /// A FRegion is a factor with a counting number
51 class FRegion
: public Factor
{
57 /// Default constructor
58 FRegion() : Factor(), _c(1.0) {}
60 /// Constructs FRegion from a Factor and a counting number
61 FRegion( const Factor
& x
, double c
) : Factor(x
), _c(c
) {}
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
; }
70 /// A RegionGraph is a bipartite graph consisting of outer regions (type FRegion) and inner regions (type Region)
71 class RegionGraph
: public FactorGraph
{
73 /// Stores the neighborhood structure
76 /// The outer regions (corresponding to nodes of type 1)
77 std::vector
<FRegion
> ORs
;
79 /// The inner regions (corresponding to nodes of type 2)
80 std::vector
<Region
> IRs
;
82 /// Stores for each factor index the index of the outer region it belongs to
83 std::vector
<size_t> fac2OR
;
87 /// Default constructor
88 RegionGraph() : FactorGraph(), G(), ORs(), IRs(), fac2OR() {}
90 /// Constructs a RegionGraph from a FactorGraph
91 RegionGraph( const FactorGraph
&fg
) : FactorGraph(fg
), G(), ORs(), IRs(), fac2OR() {}
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
);
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
);
99 /// Clone *this (virtual copy constructor)
100 virtual RegionGraph
* clone() const { return new RegionGraph(*this); }
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
);
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
);
112 for( std::map
<size_t, Factor
>::const_iterator fac
= facs
.begin(); fac
!= facs
.end(); fac
++ )
113 ns
|= fac
->second
.vars();
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
]; }
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
]; }
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(); }
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
); }
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
); }
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();
150 /// Recompute all outer regions
153 /// Recompute all outer regions involving the variables in ns
154 void RecomputeORs( const VarSet
& ns
);
156 /// Recompute all outer regions involving factor I
157 void RecomputeOR( size_t I
);
160 friend std::ostream
& operator << ( std::ostream
& os
, const RegionGraph
& rg
);
164 } // end of namespace dai