1 /* This file is part of libDAI - http://www.libdai.org/
2 *
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]
9 */
12 /// \file
13 /// \brief Defines classes Region, FRegion and RegionGraph
14 /// \todo Improve documentation
17 #ifndef __defined_libdai_regiongraph_h
18 #define __defined_libdai_regiongraph_h
21 #include <iostream>
22 #include <dai/bipgraph.h>
23 #include <dai/factorgraph.h>
24 #include <dai/weightedgraph.h>
27 namespace dai {
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;
36 public:
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) {}
44 const double & c() const { return _c; }
46 double & c() { return _c; }
47 };
50 /// A FRegion is a factor with a counting number
51 class FRegion : public Factor {
52 private:
53 /// Counting number
54 double _c;
56 public:
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) {}
64 const double & c() const { return _c; }
66 double & c() { return _c; }
67 };
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;
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;
86 public:
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 );
105 RecomputeOR( I );
106 }
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 }
119 const FRegion & OR(size_t alpha) const { return ORs[alpha]; }
121 FRegion & OR(size_t alpha) { return ORs[alpha]; }
124 const Region & IR(size_t beta) const { return IRs[beta]; }
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(); }
135 const Neighbors & nbOR( size_t alpha ) const { return G.nb1(alpha); }
137 Neighbors & nbOR( size_t alpha ) { return G.nb1(alpha); }
140 const Neighbors & nbIR( size_t beta ) const { return G.nb2(beta); }
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
151 void RecomputeORs();
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 );
159 // Friends
160 friend std::ostream & operator << ( std::ostream & os, const RegionGraph & rg );
161 };
164 } // end of namespace dai
167 #endif