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 
110  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 
152  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 
202 
207  bool checkCountingNumbers() const;
209 
211 
212 
213  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 
231 
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 
250  virtual void printDot( std::ostream& /*os*/ ) const {
251  DAI_THROW(NOT_IMPLEMENTED);
252  }
254 
255  protected:
257  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 );
258 
260  void constructCVM( const FactorGraph& fg, const std::vector<VarSet>& cl, size_t verbose=0 );
261 
263 
265  void recomputeORs();
266 
268 
270  void recomputeORs( const VarSet& vs );
271 
273 
275  void recomputeOR( size_t I );
276 
278 
284  void calcCVMCountingNumbers();
285 
286 };
287 
288 
289 } // end of namespace dai
290 
291 
292 #endif