libDAI
regiongraph.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4  *
5  * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6  */
7 
8 
11 
12 
13 #ifndef __defined_libdai_regiongraph_h
14 #define __defined_libdai_regiongraph_h
15 
16 
17 #include <iostream>
18 #include <dai/bipgraph.h>
19 #include <dai/factorgraph.h>
20 #include <dai/weightedgraph.h>
21 
22 
23 namespace dai {
24 
25 
27 class Region : public VarSet {
28  private:
31 
32  public:
34  Region() : VarSet(), _c(1.0) {}
35 
37  Region( const VarSet& x, Real c ) : VarSet(x), _c(c) {}
38 
40  const Real& c() const { return _c; }
41 
43  Real& c() { return _c; }
44 };
45 
46 
48 class FRegion : public Factor {
49  private:
52 
53  public:
55  FRegion() : Factor(), _c(1.0) {}
56 
58  FRegion( const Factor& x, Real c ) : Factor(x), _c(c) {}
59 
61  const Real& c() const { return _c; }
62 
64  Real& c() { return _c; }
65 };
66 
67 
69 
91 class RegionGraph : public FactorGraph {
92  protected:
95 
97  std::vector<FRegion> _ORs;
98 
100  std::vector<Region> _IRs;
101 
103  std::vector<size_t> _fac2OR;
104 
105 
106  public:
108 
109  RegionGraph() : FactorGraph(), _G(), _ORs(), _IRs(), _fac2OR() {}
111 
113 
115  RegionGraph( const FactorGraph& fg, const std::vector<VarSet>& ors, const std::vector<Region>& irs, const std::vector<std::pair<size_t,size_t> >& edges ) : FactorGraph(), _G(), _ORs(), _IRs(), _fac2OR() {
116  construct( fg, ors, irs, edges );
117 
118  // Check counting numbers
119 #ifdef DAI_DEBUG
121 #endif
122  }
123 
125 
136  RegionGraph( const FactorGraph& fg, const std::vector<VarSet>& cl ) : FactorGraph(), _G(), _ORs(), _IRs(), _fac2OR() {
137  constructCVM( fg, cl );
138 
139  // Check counting numbers
140 #ifdef DAI_DEBUG
142 #endif
143  }
144 
146  virtual RegionGraph* clone() const { return new RegionGraph(*this); }
148 
150 
151  size_t nrORs() const { return _ORs.size(); }
154  size_t nrIRs() const { return _IRs.size(); }
155 
157  const FRegion& OR( size_t alpha ) const {
158  DAI_DEBASSERT( alpha < nrORs() );
159  return _ORs[alpha];
160  }
162  FRegion& OR( size_t alpha ) {
163  DAI_DEBASSERT( alpha < nrORs() );
164  return _ORs[alpha];
165  }
166 
168  const Region& IR( size_t beta ) const {
169  DAI_DEBASSERT( beta < nrIRs() );
170  return _IRs[beta];
171  }
173  Region& IR( size_t beta ) {
174  DAI_DEBASSERT( beta < nrIRs() );
175  return _IRs[beta];
176  }
177 
179  size_t fac2OR( size_t I ) const {
180  DAI_DEBASSERT( I < nrFactors() );
181  DAI_DEBASSERT( I < _fac2OR.size() );
182  return _fac2OR[I];
183  }
184 
186  const Neighbors& nbOR( size_t alpha ) const { return _G.nb1(alpha); }
187 
189  const Neighbors& nbIR( size_t beta ) const { return _G.nb2(beta); }
190 
192 
196  const BipartiteGraph& DAG() const { return _G; }
198 
200 
201 
207  bool checkCountingNumbers() const;
209 
211 
212  virtual void setFactor( size_t I, const Factor& newFactor, bool backup = false ) {
214  FactorGraph::setFactor( I, newFactor, backup );
215  recomputeOR( I );
216  }
217 
219  virtual void setFactors( const std::map<size_t, Factor>& facs, bool backup = false ) {
220  FactorGraph::setFactors( facs, backup );
221  VarSet ns;
222  for( std::map<size_t, Factor>::const_iterator fac = facs.begin(); fac != facs.end(); fac++ )
223  ns |= fac->second.vars();
224  recomputeORs( ns );
225  }
227 
229 
230 
233  virtual void ReadFromFile( const char* /*filename*/ ) {
234  DAI_THROW(NOT_IMPLEMENTED);
235  }
236 
238 
240  virtual void WriteToFile( const char* /*filename*/, size_t /*precision*/=15 ) const {
241  DAI_THROW(NOT_IMPLEMENTED);
242  }
243 
245  friend std::ostream& operator<< ( std::ostream& os, const RegionGraph& rg );
246 
248  std::string toString() const {
249  std::stringstream ss;
250  ss << *this;
251  return ss.str();
252  }
253 
255 
257  virtual void printDot( std::ostream& /*os*/ ) const {
258  DAI_THROW(NOT_IMPLEMENTED);
259  }
261 
262  protected:
264  void construct( const FactorGraph& fg, const std::vector<VarSet>& ors, const std::vector<Region>& irs, const std::vector<std::pair<size_t,size_t> >& edges );
265 
267  void constructCVM( const FactorGraph& fg, const std::vector<VarSet>& cl, size_t verbose=0 );
268 
270 
272  void recomputeORs();
273 
275 
277  void recomputeORs( const VarSet& vs );
278 
280 
282  void recomputeOR( size_t I );
283 
285 
291  void calcCVMCountingNumbers();
292 
293 };
294 
295 
296 } // end of namespace dai
297 
298 
299 #endif
A RegionGraph combines a bipartite graph consisting of outer regions (type FRegion) and inner regions...
Definition: regiongraph.h:91
std::vector< Neighbor > Neighbors
Describes the set of neighbors of some node in a graph.
Definition: graph.h:92
std::vector< size_t > _fac2OR
Stores for each factor index the index of the outer region it belongs to.
Definition: regiongraph.h:103
const Neighbors & nbIR(size_t beta) const
Returns constant reference to the neighbors of inner region beta.
Definition: regiongraph.h:189
A Region is a set of variables with a counting number.
Definition: regiongraph.h:27
const Real & c() const
Returns constant reference to counting number.
Definition: regiongraph.h:40
const Region & IR(size_t beta) const
Returns constant reference to inner region beta.
Definition: regiongraph.h:168
virtual void ReadFromFile(const char *)
Reads a region graph from a file.
Definition: regiongraph.h:233
friend std::ostream & operator<<(std::ostream &os, const RegionGraph &rg)
Writes a region graph to an output stream.
Definition: regiongraph.cpp:218
const Neighbor & nb1(size_t i1, size_t _i2) const
Returns constant reference to the _i2 'th neighbor of node i1 of type 1.
Definition: bipgraph.h:87
Defines some utility functions for (weighted) undirected graphs, trees and rooted trees...
Represents a factor graph.
Definition: factorgraph.h:65
Region & IR(size_t beta)
Returns reference to inner region beta.
Definition: regiongraph.h:173
std::vector< FRegion > _ORs
The outer regions (corresponding to nodes of type 1)
Definition: regiongraph.h:97
double Real
Real number (alias for double, which could be changed to long double if necessary) ...
Definition: util.h:98
virtual RegionGraph * clone() const
Clone *this (virtual copy constructor)
Definition: regiongraph.h:146
RegionGraph(const FactorGraph &fg, const std::vector< VarSet > &ors, const std::vector< Region > &irs, const std::vector< std::pair< size_t, size_t > > &edges)
Constructs a region graph from a factor graph, a vector of outer regions, a vector of inner regions a...
Definition: regiongraph.h:115
const BipartiteGraph & DAG() const
Returns DAG structure of the region graph.
Definition: regiongraph.h:196
Represents the neighborhood structure of nodes in an undirected, bipartite graph. ...
Definition: bipgraph.h:45
size_t fac2OR(size_t I) const
Returns the index of the outer region to which the I 'th factor corresponds.
Definition: regiongraph.h:179
FRegion(const Factor &x, Real c)
Constructs from a factor and a counting number.
Definition: regiongraph.h:58
void constructCVM(const FactorGraph &fg, const std::vector< VarSet > &cl, size_t verbose=0)
Helper function for constructors (CVM style)
Definition: regiongraph.cpp:56
FRegion()
Default constructor.
Definition: regiongraph.h:55
Real & c()
Returns reference to counting number.
Definition: regiongraph.h:43
size_t nrORs() const
Returns number of outer regions.
Definition: regiongraph.h:152
RegionGraph()
Default constructor.
Definition: regiongraph.h:110
RegionGraph(const FactorGraph &fg, const std::vector< VarSet > &cl)
Constructs a region graph from a factor graph and a vector of outer clusters (CVM style) ...
Definition: regiongraph.h:136
const Neighbor & nb2(size_t i2, size_t _i1) const
Returns constant reference to the _i1 'th neighbor of node i2 of type 2.
Definition: bipgraph.h:100
Represents a set of variables.
Definition: varset.h:94
Region()
Default constructor.
Definition: regiongraph.h:34
std::vector< Region > _IRs
The inner regions (corresponding to nodes of type 2)
Definition: regiongraph.h:100
std::string toString() const
Formats a region graph as a string.
Definition: regiongraph.h:248
virtual void setFactors(const std::map< size_t, Factor > &facs, bool backup=false)
Set the contents of all factors as specified by facs and make a backup of the old contents if backup ...
Definition: regiongraph.h:219
void recomputeOR(size_t I)
Recompute all outer regions involving factor I.
Definition: regiongraph.cpp:205
Real _c
Counting number.
Definition: regiongraph.h:30
virtual void setFactor(size_t I, const Factor &newFactor, bool backup=false)
Set the content of the I 'th factor and make a backup of its old content if backup == true...
Definition: regiongraph.h:213
Real _c
Counting number.
Definition: regiongraph.h:51
size_t nrIRs() const
Returns number of inner regions.
Definition: regiongraph.h:154
const FRegion & OR(size_t alpha) const
Returns constant reference to outer region alpha.
Definition: regiongraph.h:157
Region(const VarSet &x, Real c)
Construct from a set of variables and a counting number.
Definition: regiongraph.h:37
FRegion & OR(size_t alpha)
Returns reference to outer region alpha.
Definition: regiongraph.h:162
void calcCVMCountingNumbers()
Calculates counting numbers of inner regions based upon counting numbers of outer regions...
Definition: regiongraph.cpp:126
Namespace for libDAI.
Definition: alldai.cpp:16
Defines the FactorGraph class, which represents factor graphs (e.g., Bayesian networks or Markov rand...
#define DAI_DEBASSERT(x)
Assertion mechanism similar to DAI_ASSERT which is only active if DAI_DEBUG is defined.
Definition: exceptions.h:65
virtual void setFactors(const std::map< size_t, Factor > &facs, bool backup=false)
Set the contents of all factors as specified by facs and make a backup of the old contents if backup ...
Definition: factorgraph.h:261
An FRegion is a factor with a counting number.
Definition: regiongraph.h:48
Real & c()
Returns reference to counting number.
Definition: regiongraph.h:64
Defines the BipartiteGraph class, which represents a bipartite graph.
virtual void setFactor(size_t I, const Factor &newFactor, bool backup=false)
Set the content of the I 'th factor and make a backup of its old content if backup == true...
Definition: factorgraph.h:253
size_t nrFactors() const
Returns number of factors.
Definition: factorgraph.h:136
BipartiteGraph _G
Stores the neighborhood structure.
Definition: regiongraph.h:94
virtual void WriteToFile(const char *, size_t=15) const
Writes a region graph to a file.
Definition: regiongraph.h:240
void recomputeORs()
Recompute all outer regions.
Definition: regiongraph.cpp:185
const Neighbors & nbOR(size_t alpha) const
Returns constant reference to the neighbors of outer region alpha.
Definition: regiongraph.h:186
void construct(const FactorGraph &fg, const std::vector< VarSet > &ors, const std::vector< Region > &irs, const std::vector< std::pair< size_t, size_t > > &edges)
Helper function for constructors.
Definition: regiongraph.cpp:23
virtual void printDot(std::ostream &) const
Writes a region graph to a GraphViz .dot file.
Definition: regiongraph.h:257
bool checkCountingNumbers() const
Check whether the counting numbers are valid.
Definition: regiongraph.cpp:163
const Real & c() const
Returns constant reference to counting number.
Definition: regiongraph.h:61